Class: MinimalPipeline::Packer
- Inherits:
-
Object
- Object
- MinimalPipeline::Packer
- Defined in:
- lib/minimal_pipeline/packer.rb
Overview
Here is an example of how to use this class to build AMIs from Packer YAML
“‘ # Pass in the path to the Packer JSON config file packer = MinimalPipeline::Packer.new(’path/to/packer.json’)
variables =
'foo' => 'bar',
# Build the AMI and get the new AMI ID new_ami_id = packer.build_ami(variables) “‘
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#build_ami(variables = {}) ⇒ String
Build and execute a packer build command.
-
#get_ami_id(output) ⇒ String
Parse the newly built AMI from a given packer command output.
-
#initialize(packer_config) ⇒ Packer
constructor
Instaniate a Packer object.
Constructor Details
#initialize(packer_config) ⇒ Packer
Instaniate a Packer object
25 26 27 |
# File 'lib/minimal_pipeline/packer.rb', line 25 def initialize(packer_config) @config = packer_config end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
20 21 22 |
# File 'lib/minimal_pipeline/packer.rb', line 20 def config @config end |
Instance Method Details
#build_ami(variables = {}) ⇒ String
Build and execute a packer build command
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/minimal_pipeline/packer.rb', line 42 def build_ami(variables = {}) variable_string = '' variables.each_pair do |key, value| variable_string += "-var '#{key}=#{value}' " end command = 'packer -machine-readable build ' command += variable_string unless variable_string.empty? command += @config puts command if ENV['DEBUG'] output = `#{command}` puts output if ENV['DEBUG'] get_ami_id(output) end |
#get_ami_id(output) ⇒ String
Parse the newly built AMI from a given packer command output
33 34 35 36 |
# File 'lib/minimal_pipeline/packer.rb', line 33 def get_ami_id(output) return if output.nil? || output.empty? output.match(/AMIs were created:.+ (ami-.{17})/)[1] end |