Class: Packer::Config
- Inherits:
-
DataObject
- Object
- DataObject
- Packer::Config
- Defined in:
- lib/packer-config.rb
Defined Under Namespace
Classes: DumpError, PackerBuildError, UndefinedVariableError
Constant Summary collapse
- PACKER_VERSION =
'0.8.5'
Instance Attribute Summary collapse
-
#builders ⇒ Object
Returns the value of attribute builders.
-
#envvar ⇒ Object
readonly
Returns the value of attribute envvar.
-
#macro ⇒ Object
readonly
Returns the value of attribute macro.
-
#output_file ⇒ Object
readonly
Returns the value of attribute output_file.
-
#packer ⇒ Object
Returns the value of attribute packer.
-
#packer_options ⇒ Object
Returns the value of attribute packer_options.
-
#postprocessors ⇒ Object
Returns the value of attribute postprocessors.
-
#provisioners ⇒ Object
Returns the value of attribute provisioners.
Attributes inherited from DataObject
#data, #key_dependencies, #required
Instance Method Summary collapse
- #add_builder(type) ⇒ Object
- #add_postprocessor(type) ⇒ Object
- #add_provisioner(type) ⇒ Object
- #add_variable(name, value) ⇒ Object
- #build(quiet: false) ⇒ Object
- #delete ⇒ Object
- #description(description) ⇒ Object
- #dump(format = 'json', pretty = false) ⇒ Object
-
#initialize(file) ⇒ Config
constructor
A new instance of Config.
- #validate ⇒ Object
- #variable(name) ⇒ Object
- #variables ⇒ Object
- #write(format = 'json') ⇒ Object
Methods inherited from DataObject
#__add_array_of_array_of_strings, #__add_array_of_hashes, #__add_array_of_ints, #__add_array_of_strings, #__add_boolean, #__add_hash, #__add_integer, #__add_json, #__add_string, #__exclusive_key_error, #add_key_dependencies, #add_required, #deep_copy, #validate_key_dependencies, #validate_required
Constructor Details
#initialize(file) ⇒ Config
Returns a new instance of Config.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/packer-config.rb', line 26 def initialize(file) super() self.data['variables'] = {} self.output_file = file self.builders = [] self.provisioners = [] self.postprocessors = [] self.packer = 'packer' self. = [] self.macro = Macro.new self.envvar = EnvVar.new end |
Instance Attribute Details
#builders ⇒ Object
Returns the value of attribute builders.
15 16 17 |
# File 'lib/packer-config.rb', line 15 def builders @builders end |
#envvar ⇒ Object
Returns the value of attribute envvar.
21 22 23 |
# File 'lib/packer-config.rb', line 21 def envvar @envvar end |
#macro ⇒ Object
Returns the value of attribute macro.
20 21 22 |
# File 'lib/packer-config.rb', line 20 def macro @macro end |
#output_file ⇒ Object
Returns the value of attribute output_file.
22 23 24 |
# File 'lib/packer-config.rb', line 22 def output_file @output_file end |
#packer ⇒ Object
Returns the value of attribute packer.
18 19 20 |
# File 'lib/packer-config.rb', line 18 def packer @packer end |
#packer_options ⇒ Object
Returns the value of attribute packer_options.
19 20 21 |
# File 'lib/packer-config.rb', line 19 def @packer_options end |
#postprocessors ⇒ Object
Returns the value of attribute postprocessors.
16 17 18 |
# File 'lib/packer-config.rb', line 16 def postprocessors @postprocessors end |
#provisioners ⇒ Object
Returns the value of attribute provisioners.
17 18 19 |
# File 'lib/packer-config.rb', line 17 def provisioners @provisioners end |
Instance Method Details
#add_builder(type) ⇒ Object
128 129 130 131 132 |
# File 'lib/packer-config.rb', line 128 def add_builder(type) builder = Packer::Builder.get_builder(type) self.builders.push(builder) builder end |
#add_postprocessor(type) ⇒ Object
140 141 142 143 144 |
# File 'lib/packer-config.rb', line 140 def add_postprocessor(type) postprocessor = Packer::PostProcessor.get_postprocessor(type) self.postprocessors.push(postprocessor) postprocessor end |
#add_provisioner(type) ⇒ Object
134 135 136 137 138 |
# File 'lib/packer-config.rb', line 134 def add_provisioner(type) provisioner = Packer::Provisioner.get_provisioner(type) self.provisioners.push(provisioner) provisioner end |
#add_variable(name, value) ⇒ Object
146 147 148 149 150 |
# File 'lib/packer-config.rb', line 146 def add_variable(name, value) variables_copy = Marshal.load(Marshal.dump(self.variables)) variables_copy[name.to_s] = value.to_s self.__add_hash('variables', variables_copy) end |
#build(quiet: false) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/packer-config.rb', line 105 def build(quiet: false) self.validate self.write stdout = nil Dir.chdir(File.dirname(self.output_file)) do begin stdout = Packer::Runner.run! self.packer, 'build', self., File.basename(self.output_file), quiet: quiet rescue Packer::Runner::CommandExecutionError => e raise PackerBuildError.new(e.to_s) end end self.delete stdout end |
#delete ⇒ Object
98 99 100 |
# File 'lib/packer-config.rb', line 98 def delete File.delete(self.output_file) end |
#description(description) ⇒ Object
120 121 122 |
# File 'lib/packer-config.rb', line 120 def description(description) self.__add_string('description', description) end |
#dump(format = 'json', pretty = false) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/packer-config.rb', line 64 def dump(format='json', pretty=false) data_copy = self.deep_copy data_copy['builders'] = [] self.builders.each do |thing| data_copy['builders'].push(thing.deep_copy) end if self.provisioners.length > 0 data_copy['provisioners'] = [] self.provisioners.each do |thing| data_copy['provisioners'].push(thing.deep_copy) end end if self.postprocessors.length > 0 data_copy['post-processors'] = [] self.postprocessors.each do |thing| data_copy['post-processors'].push(thing.deep_copy) end end case format when 'json' if pretty JSON.pretty_generate(data_copy) else data_copy.to_json end else raise DumpError.new("Unrecognized format #{format} use one of ['json']") end end |
#validate ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/packer-config.rb', line 39 def validate super verify_packer_version if self.builders.length == 0 raise DataValidationError.new("At least one builder is required") end self.builders.each(&:validate) self.provisioners.each(&:validate) self.postprocessors.each(&:validate) self.write stdout = nil Dir.chdir(File.dirname(self.output_file)) do begin stdout = Packer::Runner.run! self.packer, 'validate', File.basename(self.output_file), quiet: true rescue Packer::Runner::CommandExecutionError => e raise PackerBuildError.new(e.to_s) end end self.delete stdout end |
#variable(name) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/packer-config.rb', line 155 def variable(name) unless self.variables.has_key? name raise UndefinedVariableError.new("No variable named #{name} has been defined -- did you forget to call add_variable?") end "{{user `#{name}`}}" end |
#variables ⇒ Object
124 125 126 |
# File 'lib/packer-config.rb', line 124 def variables self.data['variables'] end |
#write(format = 'json') ⇒ Object
94 95 96 |
# File 'lib/packer-config.rb', line 94 def write(format='json') File.write(self.output_file, self.dump(format)) end |