Class: Formatron::Chef::Berkshelf

Inherits:
Object
  • Object
show all
Defined in:
lib/formatron/chef/berkshelf.rb

Overview

Wrapper for the berkshelf cli

Constant Summary collapse

CONFIG_FILE_CONTENTS =
<<-EOH.gsub(/^ {8}/, '')
  {
    "chef": {
      "chef_server_url": "%{server_url}",
      "node_name": "%{user}",
      "client_key": "%{key_file}"
    },
    "ssl": {
      "verify": %{ssl_verify}
    }
  }
EOH
CONFIG_FILE =
'berkshelf.json'

Instance Method Summary collapse

Constructor Details

#initialize(directory:, keys:, chef_server_url:, username:, ssl_verify:) ⇒ Berkshelf

rubocop:disable Metrics/MethodLength



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/formatron/chef/berkshelf.rb', line 23

def initialize(
  directory:,
  keys:,
  chef_server_url:,
  username:,
  ssl_verify:
)
  @config_file = File.join directory, CONFIG_FILE
  @keys = keys
  @chef_server_url = chef_server_url
  @username = username
  @ssl_verify = ssl_verify
end

Instance Method Details

#initObject

rubocop:enable Metrics/MethodLength



38
39
40
41
42
43
44
45
# File 'lib/formatron/chef/berkshelf.rb', line 38

def init
  File.write(@config_file, CONFIG_FILE_CONTENTS % {
    server_url: @chef_server_url,
    user: @username,
    key_file: @keys.user_key,
    ssl_verify: @ssl_verify
  })
end

#upload(cookbook:, environment:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/formatron/chef/berkshelf.rb', line 47

def upload(cookbook:, environment:)
  # rubocop:disable Metrics/LineLength
  command = "berks install -b #{File.join(cookbook, 'Berksfile')}"
  fail "failed to download cookbooks for opscode environment: #{environment}" unless Util::Shell.exec command
  command = "berks upload -c #{@config_file} -b #{File.join(cookbook, 'Berksfile')}"
  fail "failed to upload cookbooks for opscode environment: #{environment}" unless Util::Shell.exec command
  command = "berks apply #{environment} -c #{@config_file} -b #{File.join(cookbook, 'Berksfile.lock')}"
  fail "failed to apply cookbooks to opscode environment: #{environment}" unless Util::Shell.exec command
  # rubocop:enable Metrics/LineLength
end