Class: ImageBuilder::Backends::Packer

Inherits:
Base
  • Object
show all
Defined in:
lib/image_builder/backends/packer.rb

Overview

Generic class doc comment

Constant Summary collapse

@@packer_exe =
'packer'

Constants included from ImageBuilder

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePacker



21
22
23
24
25
26
# File 'lib/image_builder/backends/packer.rb', line 21

def initialize
  @builders = []
  @post_processors = []
  @provisioners = []
  @variables = {}
end

Instance Attribute Details

#buildersObject

Returns the value of attribute builders.



14
15
16
# File 'lib/image_builder/backends/packer.rb', line 14

def builders
  @builders
end

#descriptionObject

Returns the value of attribute description.



12
13
14
# File 'lib/image_builder/backends/packer.rb', line 12

def description
  @description
end

#min_packer_versionObject

Returns the value of attribute min_packer_version.



13
14
15
# File 'lib/image_builder/backends/packer.rb', line 13

def min_packer_version
  @min_packer_version
end

#post_processorsObject

Returns the value of attribute post_processors.



15
16
17
# File 'lib/image_builder/backends/packer.rb', line 15

def post_processors
  @post_processors
end

#provisionersObject

Returns the value of attribute provisioners.



16
17
18
# File 'lib/image_builder/backends/packer.rb', line 16

def provisioners
  @provisioners
end

#variablesObject

Returns the value of attribute variables.



17
18
19
# File 'lib/image_builder/backends/packer.rb', line 17

def variables
  @variables
end

Class Method Details

.packer_path(path) ⇒ Object



28
29
30
# File 'lib/image_builder/backends/packer.rb', line 28

def self.packer_path(path)
  @@packer_exe = ::File.expand_path(path)
end

.packer_path=(path) ⇒ Object



32
33
34
# File 'lib/image_builder/backends/packer.rb', line 32

def self.packer_path=(path)
  packer_path(path)
end

Instance Method Details

#add_builder(builder_obj) ⇒ Object

rubocop:disable Metrics/LineLength



37
38
39
40
# File 'lib/image_builder/backends/packer.rb', line 37

def add_builder(builder_obj)
  fail ArgumentError, 'Invalid builder object, does not respond to packer_hash()' unless builder_obj.respond_to?(:packer_hash)
  @builders << builder_obj.packer_hash
end

#add_post_processor(post_obj) ⇒ Object



42
43
44
45
# File 'lib/image_builder/backends/packer.rb', line 42

def add_post_processor(post_obj)
  fail ArgumentError, 'Invalid post-processor object, does not respond to packer_hash()' unless post_obj.respond_to?(:packer_hash)
  @post_processors << post_obj.packer_hash
end

#add_provisioner(provisioner_obj) ⇒ Object



47
48
49
50
# File 'lib/image_builder/backends/packer.rb', line 47

def add_provisioner(provisioner_obj)
  fail ArgumentError, 'Invalid provisioner object, does not respond to packer_hash()' unless provisioner_obj.respond_to?(:packer_hash)
  @provisioners << provisioner_obj.packer_hash
end

#add_variable(name, value = '') ⇒ Object

rubocop:enable Metrics/LineLength



53
54
55
# File 'lib/image_builder/backends/packer.rb', line 53

def add_variable(name, value = '')
  @variables[name] = value
end

#build(args = []) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/image_builder/backends/packer.rb', line 57

def build(args = [])
  validate

  cmd = [@@packer_exe, 'build'].concat(args)
  cmd << '-'

  if args.include?('-debug')
    json_obj = JSON.parse(to_json)
    puts 'DEBUG: PACKER_JSON ='
    puts JSON.pretty_generate(json_obj)
  end

  # Some provisioners may take a long time to run without giving output (applying OS updates via Chef
  # to CentOS6, and AMI copying, comes to mind), so extend the timeout to 1.5 hours.
  shellout = Mixlib::ShellOut.new(cmd, input: to_json, timeout: 5400)
  shellout.live_stdout = STDOUT
  shellout.live_stderr = STDERR
  shellout.run_command
  shellout.error!

  true
end

#to_jsonObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/image_builder/backends/packer.rb', line 94

def to_json
  config = {}

  attr_to_hash(config, :description)
  attr_to_hash(config, :min_packer_version)
  attr_to_hash(config, :variables)
  attr_to_hash(config, :builders, true)
  attr_to_hash(config, :provisioners)

  # Special handling for post-processors, since the packer config name uses '-', and ruby doesn't
  # like those (you know....subtraction operator, and all that)
  config['post-processors'] = @post_processors unless @post_processors.nil? || @post_processors.empty?

  config.to_json
end

#validate(args = []) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/image_builder/backends/packer.rb', line 80

def validate(args = [])
  cmd = [@@packer_exe, 'validate'].concat(args)
  cmd << '-'

  shellout = Mixlib::ShellOut.new(cmd, input: to_json)
  shellout.inspect
  shellout.run_command
  shellout.error!

  shellout.stdout

  true
end