Class: ImageBuilder::Provisioners::Chef::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/image_builder/provisioners/chef_base.rb

Overview

Generic class doc comment

Direct Known Subclasses

Client, Solo

Constant Summary

Constants included from ImageBuilder

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



24
25
26
27
28
# File 'lib/image_builder/provisioners/chef_base.rb', line 24

def initialize
  @prevent_sudo = false
  @skip_install = false
  @encrypted_data_bag_secret_path = ENV['ENCRYPTED_DATA_BAG_SECRET']
end

Instance Attribute Details

#chef_versionObject

Returns the value of attribute chef_version.



10
11
12
# File 'lib/image_builder/provisioners/chef_base.rb', line 10

def chef_version
  @chef_version
end

#config_templateObject

Returns the value of attribute config_template.



12
13
14
# File 'lib/image_builder/provisioners/chef_base.rb', line 12

def config_template
  @config_template
end

#cookbook_pathsObject

Returns the value of attribute cookbook_paths.



11
12
13
# File 'lib/image_builder/provisioners/chef_base.rb', line 11

def cookbook_paths
  @cookbook_paths
end

#encrypted_data_bag_secret_pathObject

Returns the value of attribute encrypted_data_bag_secret_path.



13
14
15
# File 'lib/image_builder/provisioners/chef_base.rb', line 13

def encrypted_data_bag_secret_path
  @encrypted_data_bag_secret_path
end

#execute_commandObject

Returns the value of attribute execute_command.



14
15
16
# File 'lib/image_builder/provisioners/chef_base.rb', line 14

def execute_command
  @execute_command
end

#install_commandObject

Returns the value of attribute install_command.



15
16
17
# File 'lib/image_builder/provisioners/chef_base.rb', line 15

def install_command
  @install_command
end

#jsonObject

Returns the value of attribute json.



16
17
18
# File 'lib/image_builder/provisioners/chef_base.rb', line 16

def json
  @json
end

#prevent_sudoObject

Returns the value of attribute prevent_sudo.



17
18
19
# File 'lib/image_builder/provisioners/chef_base.rb', line 17

def prevent_sudo
  @prevent_sudo
end

#run_listObject

Returns the value of attribute run_list.



18
19
20
# File 'lib/image_builder/provisioners/chef_base.rb', line 18

def run_list
  @run_list
end

#skip_installObject

Returns the value of attribute skip_install.



19
20
21
# File 'lib/image_builder/provisioners/chef_base.rb', line 19

def skip_install
  @skip_install
end

#staging_directoryObject

Returns the value of attribute staging_directory.



20
21
22
# File 'lib/image_builder/provisioners/chef_base.rb', line 20

def staging_directory
  @staging_directory
end

#typeObject (readonly)

Returns the value of attribute type.



22
23
24
# File 'lib/image_builder/provisioners/chef_base.rb', line 22

def type
  @type
end

Class Method Details

.from_file(path) ⇒ Object

set attributes from something that looks like a knife.rb file



31
32
33
34
35
36
37
38
39
40
# File 'lib/image_builder/provisioners/chef_base.rb', line 31

def self.from_file(path)
  inst = new
  @knife_file = ::File.expand_path(path)

  # For knife hash params in knife.rb
  knife = {} # rubocop:disable Lint/UselessAssignment
  inst.instance_eval(IO.read(@knife_file))

  inst
end

Instance Method Details

#cookbook_path(path) ⇒ Object

While not used explicitly in the chef-client provisioner, this will give us consistent behavior across chef provisioners if we need to do a local cookbook lookup



53
54
55
56
57
58
# File 'lib/image_builder/provisioners/chef_base.rb', line 53

def cookbook_path(path)
  @cookbook_paths = [ENV['COOKBOOK_PATH']] << path
  @cookbook_paths.flatten!
  @cookbook_paths.compact!
  @cookbook_paths.uniq!
end

#encrypted_data_bag_secret(sec) ⇒ Object

Map knife.rb config options to their corresponding object attributes the :encrypted_data_bag_secret_path attribute is specific to the chef-solo provisioner, but we’ll need it for all chef provisioners, since we need to upload it to the node before provisioning via the chef-client provisioner



46
47
48
# File 'lib/image_builder/provisioners/chef_base.rb', line 46

def encrypted_data_bag_secret(sec)
  @encrypted_data_bag_secret_path = sec
end

#packer_hashObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/image_builder/provisioners/chef_base.rb', line 60

def packer_hash
  hash = { type: @type }

  unless @chef_version.nil? && @install_command.nil?
    @install_command = 'curl -L https://www.opscode.com/chef/install.sh | '
    @install_command += 'sudo ' unless @prevent_sudo
    @install_command += 'bash -s -- -v ' + @chef_version
  end

  attr_to_hash(hash, :config_template)
  attr_to_hash(hash, :execute_command)
  attr_to_hash(hash, :install_command)
  attr_to_hash(hash, :json)
  attr_to_hash(hash, :prevent_sudo)
  attr_to_hash(hash, :run_list)
  attr_to_hash(hash, :skip_install)
  attr_to_hash(hash, :staging_directory)

  hash
end