Class: ImageBuilder::Backends::Packer
- Defined in:
- lib/image_builder/backends/packer.rb
Overview
Generic class doc comment
Constant Summary collapse
- @@default_path =
['/bin', '/usr/bin', '/sbin', '/usr/sbin', '/usr/local/bin']
- @@packer_exe =
'packer'
Constants included from ImageBuilder
Instance Attribute Summary collapse
-
#builders ⇒ Object
Returns the value of attribute builders.
-
#description ⇒ Object
Returns the value of attribute description.
-
#min_packer_version ⇒ Object
Returns the value of attribute min_packer_version.
-
#post_processors ⇒ Object
Returns the value of attribute post_processors.
-
#provisioners ⇒ Object
Returns the value of attribute provisioners.
-
#variables ⇒ Object
Returns the value of attribute variables.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_builder(builder_obj) ⇒ Object
rubocop:disable Metrics/LineLength.
- #add_post_processor(post_obj) ⇒ Object
- #add_provisioner(provisioner_obj) ⇒ Object
-
#add_variable(name, value = '') ⇒ Object
rubocop:enable Metrics/LineLength.
- #build(args = []) ⇒ Object
-
#initialize ⇒ Packer
constructor
A new instance of Packer.
- #to_json ⇒ Object
- #validate(args = []) ⇒ Object
Constructor Details
#initialize ⇒ Packer
Returns a new instance of Packer.
24 25 26 27 28 29 |
# File 'lib/image_builder/backends/packer.rb', line 24 def initialize @builders = [] @post_processors = [] @provisioners = [] @variables = {} end |
Instance Attribute Details
#builders ⇒ Object
Returns the value of attribute builders.
14 15 16 |
# File 'lib/image_builder/backends/packer.rb', line 14 def builders @builders end |
#description ⇒ Object
Returns the value of attribute description.
12 13 14 |
# File 'lib/image_builder/backends/packer.rb', line 12 def description @description end |
#min_packer_version ⇒ Object
Returns the value of attribute min_packer_version.
13 14 15 |
# File 'lib/image_builder/backends/packer.rb', line 13 def min_packer_version @min_packer_version end |
#post_processors ⇒ Object
Returns the value of attribute post_processors.
15 16 17 |
# File 'lib/image_builder/backends/packer.rb', line 15 def post_processors @post_processors end |
#provisioners ⇒ Object
Returns the value of attribute provisioners.
16 17 18 |
# File 'lib/image_builder/backends/packer.rb', line 16 def provisioners @provisioners end |
#variables ⇒ Object
Returns the value of attribute variables.
17 18 19 |
# File 'lib/image_builder/backends/packer.rb', line 17 def variables @variables end |
Class Method Details
.packer_path(path) ⇒ Object
31 32 33 34 |
# File 'lib/image_builder/backends/packer.rb', line 31 def self.packer_path(path) @@packer_exe = ::File.(path) @@default_path.unshift ::File.dirname(::File.(@@packer_exe)) end |
.packer_path=(path) ⇒ Object
36 37 38 |
# File 'lib/image_builder/backends/packer.rb', line 36 def self.packer_path=(path) packer_path(path) end |
Instance Method Details
#add_builder(builder_obj) ⇒ Object
rubocop:disable Metrics/LineLength
41 42 43 44 |
# File 'lib/image_builder/backends/packer.rb', line 41 def add_builder(builder_obj) fail ArgumentError, 'Invalid builder object, does not respond to packer_hash()' unless builder_obj.respond_to?(:packer_hash) @builders << builder_obj.packer_hash end |
#add_post_processor(post_obj) ⇒ Object
46 47 48 49 |
# File 'lib/image_builder/backends/packer.rb', line 46 def add_post_processor(post_obj) fail ArgumentError, 'Invalid post-processor object, does not respond to packer_hash()' unless post_obj.respond_to?(:packer_hash) @post_processors << post_obj.packer_hash end |
#add_provisioner(provisioner_obj) ⇒ Object
51 52 53 54 |
# File 'lib/image_builder/backends/packer.rb', line 51 def add_provisioner(provisioner_obj) fail ArgumentError, 'Invalid provisioner object, does not respond to packer_hash()' unless provisioner_obj.respond_to?(:packer_hash) @provisioners << provisioner_obj.packer_hash end |
#add_variable(name, value = '') ⇒ Object
rubocop:enable Metrics/LineLength
57 58 59 |
# File 'lib/image_builder/backends/packer.rb', line 57 def add_variable(name, value = '') @variables[name] = value end |
#build(args = []) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/image_builder/backends/packer.rb', line 61 def build(args = []) validate cmd = [@@packer_exe, 'build'].concat(args) cmd << '-' if args.include?('-debug') json_obj = JSON.parse(to_json) puts "DEBUG: PACKER_EXE = #{@@packer_exe}" puts "DEBUG: PATH = #{@@default_path}" puts 'DEBUG: PACKER_JSON =' puts JSON.pretty_generate(json_obj) end # Some provisioners may take a long time to run without giving output (applying OS updates via Chef # to CentOS6, and AMI copying, comes to mind), so extend the timeout to 1.5 hours. shellout = Mixlib::ShellOut.new(cmd, env: { 'PATH' => @@default_path.join(':') }, input: to_json, timeout: 5400) shellout.live_stdout = STDOUT shellout.live_stderr = STDERR shellout.run_command shellout.error! true end |
#to_json ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/image_builder/backends/packer.rb', line 101 def to_json config = {} attr_to_hash(config, :description) attr_to_hash(config, :min_packer_version) attr_to_hash(config, :variables) attr_to_hash(config, :builders, true) attr_to_hash(config, :provisioners) # Special handling for post-processors, since the packer config name uses '-', and ruby doesn't # like those (you know....subtraction operator, and all that) config['post-processors'] = @post_processors unless @post_processors.nil? || @post_processors.empty? config.to_json end |
#validate(args = []) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/image_builder/backends/packer.rb', line 87 def validate(args = []) cmd = [@@packer_exe, 'validate'].concat(args) cmd << '-' shellout = Mixlib::ShellOut.new(cmd, env: { 'PATH' => @@default_path.join(':') }, input: to_json) shellout.inspect shellout.run_command shellout.error! shellout.stdout true end |