Class: Packer::Config

Inherits:
DataObject show all
Defined in:
lib/packer-config.rb

Defined Under Namespace

Classes: DumpError, PackerBuildError, UndefinedVariableError

Constant Summary collapse

PACKER_VERSION =
'1.0.0'

Instance Attribute Summary collapse

Attributes inherited from DataObject

#data, #key_dependencies, #required

Instance Method Summary collapse

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.packer_options = []
  self.macro = Macro.new
  self.envvar = EnvVar.new
end

Instance Attribute Details

#buildersObject

Returns the value of attribute builders


15
16
17
# File 'lib/packer-config.rb', line 15

def builders
  @builders
end

#envvarObject

Returns the value of attribute envvar


21
22
23
# File 'lib/packer-config.rb', line 21

def envvar
  @envvar
end

#macroObject

Returns the value of attribute macro


20
21
22
# File 'lib/packer-config.rb', line 20

def macro
  @macro
end

#output_fileObject

Returns the value of attribute output_file


22
23
24
# File 'lib/packer-config.rb', line 22

def output_file
  @output_file
end

#packerObject

Returns the value of attribute packer


18
19
20
# File 'lib/packer-config.rb', line 18

def packer
  @packer
end

#packer_optionsObject

Returns the value of attribute packer_options


19
20
21
# File 'lib/packer-config.rb', line 19

def packer_options
  @packer_options
end

#postprocessorsObject

Returns the value of attribute postprocessors


16
17
18
# File 'lib/packer-config.rb', line 16

def postprocessors
  @postprocessors
end

#provisionersObject

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


129
130
131
132
133
# File 'lib/packer-config.rb', line 129

def add_builder(type)
  builder = Packer::Builder.get_builder(type)
  self.builders.push(builder)
  builder
end

#add_postprocessor(type) ⇒ Object


141
142
143
144
145
# File 'lib/packer-config.rb', line 141

def add_postprocessor(type)
  postprocessor = Packer::PostProcessor.get_postprocessor(type)
  self.postprocessors.push(postprocessor)
  postprocessor
end

#add_provisioner(type) ⇒ Object


135
136
137
138
139
# File 'lib/packer-config.rb', line 135

def add_provisioner(type)
  provisioner = Packer::Provisioner.get_provisioner(type)
  self.provisioners.push(provisioner)
  provisioner
end

#add_variable(name, value) ⇒ Object


147
148
149
150
151
# File 'lib/packer-config.rb', line 147

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


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/packer-config.rb', line 106

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.packer_options, File.basename(self.output_file), quiet: quiet
    rescue Packer::Runner::CommandExecutionError => e
      raise PackerBuildError.new(e.to_s)
    end
  end
  self.delete
  stdout
end

#deleteObject


99
100
101
# File 'lib/packer-config.rb', line 99

def delete
  File.delete(self.output_file)
end

#description(description) ⇒ Object


121
122
123
# File 'lib/packer-config.rb', line 121

def description(description)
  self.__add_string('description', description)
end

#dump(format = 'json', pretty = false) ⇒ Object


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
93
# File 'lib/packer-config.rb', line 65

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
  unless self.provisioners.empty?
    data_copy['provisioners'] = []
    self.provisioners.each do |thing|
      data_copy['provisioners'].push(thing.deep_copy)
    end
  end
  unless self.postprocessors.empty?
    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

#min_packer_version(version) ⇒ Object


172
173
174
# File 'lib/packer-config.rb', line 172

def min_packer_version(version)
  self.__add_string('min_packer_version', version)
end

#validate(verbose: false) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/packer-config.rb', line 39

def validate(verbose: false)
  super()
  verify_packer_version
  if self.builders.empty?
    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: !verbose
    rescue Packer::Runner::CommandExecutionError => e
      raise PackerBuildError.new(e.to_s)
    end
  end
  self.delete
  stdout
end

#variable(name) ⇒ Object


156
157
158
159
160
161
162
# File 'lib/packer-config.rb', line 156

def variable(name)
  unless self.variables.key? name
    raise UndefinedVariableError.new("No variable named #{name} has been defined -- did you forget to call add_variable?")
  end

  "{{user `#{name}`}}"
end

#variablesObject


125
126
127
# File 'lib/packer-config.rb', line 125

def variables
  self.data['variables']
end

#verify_packer_versionObject


164
165
166
167
168
169
170
# File 'lib/packer-config.rb', line 164

def verify_packer_version
  min_packer_version PACKER_VERSION
  version = /Packer v(\d+\.\d+\.\d+)/.match(
    Packer::Runner.run!(packer, 'version', quiet: true)
  )[1]
  Gem::Dependency.new('', ">= #{PACKER_VERSION}").match?('', version) || raise("Packer version #{version} is not new enough (requires >= #{PACKER_VERSION})")
end

#write(format = 'json') ⇒ Object


95
96
97
# File 'lib/packer-config.rb', line 95

def write(format='json')
  File.write(self.output_file, self.dump(format))
end