Class: OpenStax::Aws::Packer_1_4_1

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/packer_1_4_1.rb

Instance Method Summary collapse

Constructor Details

#initialize(absolute_file_path:, dry_run: true) ⇒ Packer_1_4_1

Returns a new instance of Packer_1_4_1.



6
7
8
9
10
11
12
13
14
# File 'lib/openstax/aws/packer_1_4_1.rb', line 6

def initialize(absolute_file_path:, dry_run: true)
  @logger = OpenStax::Aws.configuration.logger
  @only = []
  @vars = {}
  @dry_run = dry_run
  @verbose = false
  @debug = false
  @absolute_file_path = absolute_file_path
end

Instance Method Details

#commandObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openstax/aws/packer_1_4_1.rb', line 32

def command
  cmd = "packer build"

  cmd = "#{cmd} --only=#{@only.join(',')}" if !@only.empty?

  @vars.each do |key, value|
    cmd = "#{cmd} --var '#{key}=#{value}'"
  end

  cmd = "PACKER_LOG=1 #{cmd}" if @verbose
  cmd = "#{cmd} --debug" if @debug

  cmd
end

#debug!Object



28
29
30
# File 'lib/openstax/aws/packer_1_4_1.rb', line 28

def debug!
  @debug = true
end

#only(builders) ⇒ Object



16
17
18
# File 'lib/openstax/aws/packer_1_4_1.rb', line 16

def only(builders)
  @only = [builders].flatten
end

#runObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openstax/aws/packer_1_4_1.rb', line 47

def run
  @logger.info("**** DRY RUN ****") if @dry_run
  @logger.info("Running: #{command} #{@absolute_file_path}")

  if !@dry_run
    @logger.info("Printing stderr for desired verbosity")

    tmpdir = nil

    begin
      config_path = @absolute_file_path

      # Can't handle modifying HCL2 templates yet
      if config_path.ends_with?('.json')
        config = JSON.parse(File.read(config_path))
        config['post-processors'] ||= []
        manifest_config = (config['post-processors']).find do |processor|
          processor['type'] == 'manifest'
        end

        # Configure a manifest post-processor if not already configured
        if manifest_config.nil?
          tmpdir = Dir.mktmpdir

          manifest_config = { 'type' => 'manifest', 'output' => "#{tmpdir}/manifest.json" }

          config['post-processors'] << manifest_config

          config_path = "#{tmpdir}/packer.json"

          File.write(config_path, JSON.dump(config))
        end
      end

      Open3.popen2e("#{command} #{config_path}") do |stdin, stdout_err, wait_thr|
        begin
          previous_interrupt_handler = Signal.trap 'INT' do
            # Interrupt Packer
            Process.kill 'INT', wait_thr.pid

            # Restore previous interrupt handler so we don't interrupt Packer again
            Signal.trap 'INT', previous_interrupt_handler

            # Disable other code that restores previous interrupt
            previous_interrupt_handler = nil
          end

          stdout_err.sync = true

          # Send all packer output to STDERR
          while char = stdout_err.getc do
            STDERR.print char
          end
        ensure
          # Restore previous interrupt unless we did so already
          Signal.trap 'INT', previous_interrupt_handler unless previous_interrupt_handler.nil?
        end

        # Read the AMI ID from the manifest file and output it to STDOUT
        unless manifest_config.nil?
          manifest = File.read(manifest_config['output']) rescue nil

          puts JSON.parse(manifest)['builds'].last['artifact_id'].split(':', 2).last \
            unless manifest.nil?
        end

        # Return Packer's exit status wrapped in a Process::Status object
        wait_thr.value
      end
    ensure
      FileUtils.remove_entry(tmpdir) unless tmpdir.nil?
    end
  end
end

#to_sObject



122
123
124
# File 'lib/openstax/aws/packer_1_4_1.rb', line 122

def to_s
  command.to_s
end

#var(key, value) ⇒ Object



20
21
22
# File 'lib/openstax/aws/packer_1_4_1.rb', line 20

def var(key, value)
  @vars[key.to_s] = value.to_s
end

#verbose!Object



24
25
26
# File 'lib/openstax/aws/packer_1_4_1.rb', line 24

def verbose!
  @verbose = true
end