Class: Packer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/packer/client.rb

Overview

Ruby client for HashiCorp Packer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#executable_pathString

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.

Returns:

  • (String)

    path to Packer executable



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_timeoutFixnum

Gets the maximum amount of time Packer may execute for before timing out. Defaults to 2 hours.

Returns:

  • (Fixnum)

    execution timeout



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.

Parameters:

  • template (String, Packer::Template)

    the Packer template

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :force (Boolean)

    force a build to continue if artifacts exist, deletes existing artifacts

  • :except (Array<String>)

    build all builds other than these

  • :only (Array<String>)

    only build the given builds by name

  • :parallel (Boolean)

    disable parallelization (on by default)

  • :vars (Hash)

    variables for templates

  • :var_file (String)

    path to JSON file containing user variables

  • :live_stream (IO)

    an IO object to stream packer output to in addition to saving output in the output object.

Returns:



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, options = {})
  args = ['build', '-machine-readable']
  args << '-force' if options.key?(:force)
  args << "-except=#{options[:except].join(',')}" if options.key?(:except)
  args << "-only=#{options[:only].join(',')}" if options.key?(:only)
  args << "-parallel=#{options[:parallel]}" if options.key?(:parallel)
  args << "-var-file=#{options[:var_file]}" if options.key?(:var_file)

  vars = options[:vars] || {}
  vars.each { |key, val| args << "-var '#{key}=#{val}'" }

  args << template

  Packer::Output::Build.new(
    command(args, options[: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.

Parameters:

  • args (Array<String>)

    to pass to Packer

  • Boolean

    set to true to stream output to $stdout



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(' ')
  options = { 
    timeout: execution_timeout,
    live_stream: stream
  }
  so = Mixlib::ShellOut.new(cmd, options)
  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.

Parameters:

  • template (String, Packer::Template)

    the Packer template

Returns:



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).

Parameters:

  • template (String, Packer::Template)

    the Packer template

Returns:



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.

Parameters:

  • template (String, Packer::Template)

    the Packer template

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :message (String)

    a message to identify the purpose of changes in this Packer template much like a VCS commit message

  • :name (String)

    the destination build in Atlas. This is in a format “username/name”.

  • :token (String)

    the access token to use when uploading

  • :vars (Hash)

    variables for templates

  • :var_file (String)

    path to JSON file containing user variables

  • :live_stream (IO)

    an IO object to stream packer output to in addition to saving output in the output object.

Returns:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/packer/client.rb', line 140

def push(template, options = {})
  args = ['push']
  args << "-message=#{options[:message]}" if options.key?(:message)
  args << "-name=#{options[:name]}" if options.key?(:name)
  args << "-token=#{options[:token]}" if options.key?(:token)
  args << "-var-file=#{options[:var_file]}" if options.key?(:var_file)

  vars = options[:vars] || {}
  vars.each { |key, val| args << "-var '#{key}=#{val}'" }

  args << template

  Packer::Output::Push.new(command(args, options[: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.

Parameters:

  • template (String, Packer::Template)

    the Packer template

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :syntax_only (Boolean)

    only check syntax. Do not verify config of the template.

  • :except (Array<String>)

    validate all builds other than these

  • :only (Array<String>)

    validate only these builds

  • :vars (Hash)

    variables for templates

  • :var_file (String)

    path to JSON file containing user

  • :live_stream (IO)

    an IO object to stream packer output to in addition to saving output in the output object. variables

Returns:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/packer/client.rb', line 177

def validate(template, options = {})
  args = ['validate']
  args << '-syntax-only' if options.key?(:syntax_only)
  args << "-except=#{options[:except].join(',')}" if options.key?(:except)
  args << "-only=#{options[:only].join(',')}" if options.key?(:only)
  args << "-var-file=#{options[:var_file]}" if options.key?(:var_file)

  vars = options[:vars] || {}
  vars.each { |key, val| args << "-var '#{key}=#{val}'" }

  args << template

  Packer::Output::Validate.new(command(args, options[:live_stream]))
end

#versionPacker::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