Class: OpsBuild::Packer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePacker

Returns a new instance of Packer.



8
9
10
11
# File 'lib/ops_build/packer.rb', line 8

def initialize
  Validations::check_binary!('packer')
  @user_vars = {}
end

Instance Attribute Details

#user_var_fileObject

Returns the value of attribute user_var_file.



6
7
8
# File 'lib/ops_build/packer.rb', line 6

def user_var_file
  @user_var_file
end

#user_varsObject

Returns the value of attribute user_vars.



6
7
8
# File 'lib/ops_build/packer.rb', line 6

def user_vars
  @user_vars
end

Instance Method Details

#add_user_variable(name, value) ⇒ Object

Add name/value pair to users_vars hash which is going to be used later for packer var-file



15
16
17
18
19
# File 'lib/ops_build/packer.rb', line 15

def add_user_variable(name, value)
  unless name.nil? and value.nil?
    @user_vars[name.to_sym] = value
  end
end

#build(config) ⇒ Object

Run packer build



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ops_build/packer.rb', line 23

def build(config)
  options = ''

  create_var_file

  unless @user_var_file.nil?
    OpsBuild.logger.info("Changing packer build with variables file from: #{@user_var_file.path}")
    options = " -var-file #{@user_var_file.path}"
  end

  Utils::execute("packer build -color=false #{options} #{config}",
                 log_prefix: 'packer:',
                 env: { "AWS_TIMEOUT_SECONDS" => "1200" })
end

#cleanupObject

Clean user_var-file/log from system



55
56
57
58
59
60
# File 'lib/ops_build/packer.rb', line 55

def cleanup
  unless @user_var_file.nil?
    @user_var_file.unlink
    @user_var_file.close
  end
end

#validate(config) ⇒ Object

Validate packer template



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ops_build/packer.rb', line 40

def validate(config)
  options = ''

  create_var_file

  unless @user_var_file.nil?
    OpsBuild.logger.info("Customizing packer build with variable file from: #{@user_var_file.path}")
    options = "-var-file #{@user_var_file.path}"
  end

  Utils::execute("packer validate #{options} #{config}", log_prefix: 'packer:')
end