Class: Packer::Client
- Inherits:
-
Object
- Object
- Packer::Client
- Defined in:
- lib/packer/client.rb
Overview
Ruby client for HashiCorp Packer.
Instance Attribute Summary collapse
-
#executable_path ⇒ String
Gets the path to the Packer executable.
-
#execution_timeout ⇒ Fixnum
Gets the maximum amount of time Packer may execute for before timing out.
Instance Method Summary collapse
-
#build(template, options = {}) ⇒ Packer::Output::Build
Executes packer build.
- #command(args, stream = nil) ⇒ Object private
-
#fix(template) ⇒ Packer::Output::Fix
Executes packer fix.
-
#inspect_template(template) ⇒ Packer::Output::Inspect
Excutes packer inspect.
-
#push(template, options = {}) ⇒ Packer::Output::Push
Executes packer push.
-
#validate(template, options = {}) ⇒ Packer::Output::Validate
Executes packer validate.
-
#version ⇒ Packer::Output::Version
Executes packer version.
Instance Attribute Details
#executable_path ⇒ String
Gets the path to the Packer executable. Defaults to packer.exe
on Windows and packer
on other platforms. The default values expect the Packer executable to be available via the PATH.
67 68 69 70 71 72 73 74 75 |
# File 'lib/packer/client.rb', line 67 def executable_path return @executable_path if @executable_path if OS.windows? 'packer.exe' else 'packer' end end |
#execution_timeout ⇒ Fixnum
Gets the maximum amount of time Packer may execute for before timing out. Defaults to 2 hours.
81 82 83 |
# File 'lib/packer/client.rb', line 81 def execution_timeout @execution_timeout || 7200 # 2 hours end |
Instance Method Details
#build(template, options = {}) ⇒ Packer::Output::Build
Executes packer build.
Will execute multiple builds in parallel as defined in the template. The various artifacts created by the template will be outputted.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/packer/client.rb', line 32 def build(template, = {}) args = ['build', '-machine-readable'] args << '-force' if .key?(:force) args << "-except=#{[:except].join(',')}" if .key?(:except) args << "-only=#{[:only].join(',')}" if .key?(:only) args << "-parallel=#{[:parallel]}" if .key?(:parallel) args << "-var-file=#{[:var_file]}" if .key?(:var_file) vars = [:vars] || {} vars.each { |key, val| args << "-var '#{key}=#{val}'" } args << template Packer::Output::Build.new( command(args, [:live_stream])) end |
#command(args, stream = nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 55 56 57 58 59 60 |
# File 'lib/packer/client.rb', line 52 def command(args, stream = nil) cmd = [executable_path, args].join(' ') = { timeout: execution_timeout, live_stream: stream } so = Mixlib::ShellOut.new(cmd, ) so.run_command end |
#fix(template) ⇒ Packer::Output::Fix
Executes packer fix.
Reads the JSON template and attempts to fix know backwards incompatibilities. The fixed template will be outputted to standard out.
If the template cannot be fixed due to an error, the command will exit will a non-zero exist status. Error messages will appear on standard error.
96 97 98 |
# File 'lib/packer/client.rb', line 96 def fix(template) Packer::Output::Fix.new(command(['fix', template])) end |
#inspect_template(template) ⇒ Packer::Output::Inspect
Excutes packer inspect
Inspects a template, parsing and outputting the components a template defines. This does not validate the contents of a template (other than basic syntax by necessity).
108 109 110 111 112 |
# File 'lib/packer/client.rb', line 108 def inspect_template(template) args = ['inspect', '-machine-readable', template] Packer::Output::Inspect.new(command(args)) end |
#push(template, options = {}) ⇒ Packer::Output::Push
Executes packer push.
Push the given template and supporting files to a Packer build service such as Atlas.
If a build configuration for the given template does not exist, it will be created automatically. If the build configuration already exists, a new version will be created with this template and the supporting files.
Additional configuration options (such as Atlas server URL and files to include) may be specified in the “push” section of the Packer template. Please see the online documentation about these configurables.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/packer/client.rb', line 140 def push(template, = {}) args = ['push'] args << "-message=#{[:message]}" if .key?(:message) args << "-name=#{[:name]}" if .key?(:name) args << "-token=#{[:token]}" if .key?(:token) args << "-var-file=#{[:var_file]}" if .key?(:var_file) vars = [:vars] || {} vars.each { |key, val| args << "-var '#{key}=#{val}'" } args << template Packer::Output::Push.new(command(args, [:live_stream])) end |
#validate(template, options = {}) ⇒ Packer::Output::Validate
Executes packer validate
Checks the template is valid by parsing the template and also checking the configuration with the various builders, provisioners, etc.
If it is not valid, the errors will be shown and the command will exit with a non-zero exit status. If it is valid, it will exist with a zero exist status.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/packer/client.rb', line 177 def validate(template, = {}) args = ['validate'] args << '-syntax-only' if .key?(:syntax_only) args << "-except=#{[:except].join(',')}" if .key?(:except) args << "-only=#{[:only].join(',')}" if .key?(:only) args << "-var-file=#{[:var_file]}" if .key?(:var_file) vars = [:vars] || {} vars.each { |key, val| args << "-var '#{key}=#{val}'" } args << template Packer::Output::Validate.new(command(args, [:live_stream])) end |
#version ⇒ Packer::Output::Version
Executes packer version
195 196 197 |
# File 'lib/packer/client.rb', line 195 def version Packer::Output::Version.new(command(['version', '-machine-readable'])) end |