Class: PackerTemplate

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

Overview

instance of a single packer template with methods to validate and extract data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PackerTemplate



25
26
27
28
29
# File 'lib/template.rb', line 25

def initialize(path)
  @path = File.expand_path(path)
  @name = File.basename(@path, '.json')
  @file = File.read(path)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/template.rb', line 23

def name
  @name
end

#pathObject

Returns the value of attribute path.



23
24
25
# File 'lib/template.rb', line 23

def path
  @path
end

Instance Method Details

#buildersObject



35
36
37
# File 'lib/template.rb', line 35

def builders
  json['builders'].map { |b| b['name'] }
end

#builders_hashObject



39
40
41
42
43
44
45
# File 'lib/template.rb', line 39

def builders_hash
  builders = {}
  json['builders'].each do |builder|
    builders[builder['name']] = builder['type']
  end
  builders
end

#jsonObject



31
32
33
# File 'lib/template.rb', line 31

def json
  @parsed || @parsed = JSON.parse(@file)
end

#validates?Boolean

shell out to packer to validate the config files for correctness



48
49
50
51
52
# File 'lib/template.rb', line 48

def validates?
  Dir.chdir(File.dirname(@path))
  `packer validate #{@path} 2>&1`
  $CHILD_STATUS.success? ? true : false
end