Class: Packer::Config
Defined Under Namespace
Classes: DumpError, PackerBuildError, UndefinedVariableError
Constant Summary
collapse
- VERSION =
'0.0.2'
Instance Attribute Summary collapse
Attributes inherited from DataObject
#data, #required
Instance Method Summary
collapse
Methods inherited from DataObject
#__add_array_of_array_of_strings, #__add_array_of_hashes, #__add_array_of_strings, #__add_boolean, #__add_hash, #__add_integer, #__add_string, #__exclusive_key_error, #add_required, #deep_copy
Constructor Details
#initialize(file) ⇒ Config
Returns a new instance of Config.
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/packer-config.rb', line 38
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
#builders ⇒ Object
Returns the value of attribute builders.
29
30
31
|
# File 'lib/packer-config.rb', line 29
def builders
@builders
end
|
#envvar ⇒ Object
Returns the value of attribute envvar.
35
36
37
|
# File 'lib/packer-config.rb', line 35
def envvar
@envvar
end
|
#macro ⇒ Object
Returns the value of attribute macro.
34
35
36
|
# File 'lib/packer-config.rb', line 34
def macro
@macro
end
|
#output_file ⇒ Object
Returns the value of attribute output_file.
36
37
38
|
# File 'lib/packer-config.rb', line 36
def output_file
@output_file
end
|
#packer ⇒ Object
Returns the value of attribute packer.
32
33
34
|
# File 'lib/packer-config.rb', line 32
def packer
@packer
end
|
#packer_options ⇒ Object
Returns the value of attribute packer_options.
33
34
35
|
# File 'lib/packer-config.rb', line 33
def packer_options
@packer_options
end
|
#postprocessors ⇒ Object
Returns the value of attribute postprocessors.
30
31
32
|
# File 'lib/packer-config.rb', line 30
def postprocessors
@postprocessors
end
|
#provisioners ⇒ Object
Returns the value of attribute provisioners.
31
32
33
|
# File 'lib/packer-config.rb', line 31
def provisioners
@provisioners
end
|
Instance Method Details
#add_builder(type) ⇒ Object
141
142
143
144
145
|
# File 'lib/packer-config.rb', line 141
def add_builder(type)
builder = Packer::Builder.get_builder(type)
self.builders.push(builder)
builder
end
|
#add_postprocessor(type) ⇒ Object
153
154
155
156
157
|
# File 'lib/packer-config.rb', line 153
def add_postprocessor(type)
postprocessor = Packer::PostProcessor.get_postprocessor(type)
self.postprocessors.push(postprocessor)
postprocessor
end
|
#add_provisioner(type) ⇒ Object
147
148
149
150
151
|
# File 'lib/packer-config.rb', line 147
def add_provisioner(type)
provisioner = Packer::Provisioner.get_provisioner(type)
self.provisioners.push(provisioner)
provisioner
end
|
#add_variable(name, value) ⇒ Object
159
160
161
162
163
|
# File 'lib/packer-config.rb', line 159
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 ⇒ Object
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/packer-config.rb', line 118
def build
self.validate
self.write
Dir.chdir(File.dirname(self.output_file)) do
cmd = [self.packer, 'build', self.packer_options, File.basename(self.output_file)].join(' ')
stdout, stderr, status = Open3.capture3(cmd)
raise PackerBuildError.new(stderr) unless status == 0
end
self.delete
end
|
#delete ⇒ Object
111
112
113
|
# File 'lib/packer-config.rb', line 111
def delete
File.delete(self.output_file)
end
|
#description(description) ⇒ Object
129
130
131
|
# File 'lib/packer-config.rb', line 129
def description(description)
self.__add_string('description', description)
end
|
#dump(format = 'json', pretty = false) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/packer-config.rb', line 77
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
|
#min_packer_version(version) ⇒ Object
133
134
135
|
# File 'lib/packer-config.rb', line 133
def min_packer_version(version)
self.__add_string('min_packer_version', version)
end
|
#validate ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/packer-config.rb', line 51
def validate
super
if self.builders.length == 0
raise DataValidationError.new("At least one builder is required")
end
self.builders.each do |thing|
thing.validate
end
self.provisioners.each do |thing|
thing.validate
end
self.postprocessors.each do |thing|
thing.validate
end
self.write
Dir.chdir(File.dirname(self.output_file)) do
cmd = [self.packer, 'validate', File.basename(self.output_file)].join(' ')
stdout, stderr, status = Open3.capture3(cmd)
raise PackerBuildError.new(stderr) unless status == 0
end
self.delete
end
|
#variable(name) ⇒ Object
168
169
170
171
172
173
|
# File 'lib/packer-config.rb', line 168
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
137
138
139
|
# File 'lib/packer-config.rb', line 137
def variables
self.data['variables']
end
|
#write(format = 'json') ⇒ Object
107
108
109
|
# File 'lib/packer-config.rb', line 107
def write(format='json')
File.write(self.output_file, self.dump(format))
end
|