Class: Builderator::Tasks::Packer

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/builderator/tasks/packer.rb

Overview

Wrap Packer commands

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Thor::Actions

#run_with_input, #template

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/builderator/tasks/packer.rb', line 16

def self.exit_on_failure?
  true
end

Instance Method Details

#build(profile = :default, *args) ⇒ Object



31
32
33
34
# File 'lib/builderator/tasks/packer.rb', line 31

def build(profile = :default, *args)
  invoke :configure, [profile], options
  run_with_input "#{Interface.packer.command} build - #{ args.join('') }", Interface.packer.render
end

#configure(profile = :default) ⇒ Object



23
24
25
26
27
28
# File 'lib/builderator/tasks/packer.rb', line 23

def configure(profile = :default)
  Config.profile.use(profile)

  invoke Tasks::Version, :current, [], options
  puts Interface.packer.render if options['debug']
end

#copy(profile) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/builderator/tasks/packer.rb', line 37

def copy(profile)
  invoke :configure, [profile], options

  images.each do |image_name, (image, build)|
    parameters = {
      :source_region => Config.aws.region,
      :source_image_id => image.image_id,
      :name => image_name,
      :description => image.description
    }

    build.ami_regions.each do |region|
      say_status :copy, "image #{image_name} (#{image.image_id}) from #{Config.aws.region} to #{region}"
      Util.ec2(region).copy_image(parameters)
    end
  end

  invoke :wait, [profile], options
  invoke :tag, [profile], options
end

#tag(profile) ⇒ Object



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
# File 'lib/builderator/tasks/packer.rb', line 59

def tag(profile)
  invoke :configure, [profile], options

  images.each do |image_name, (image, build)|
    filters = [{
      :name => 'name',
      :values => [image_name]
    }]

    ## Add some additional tags about the regional source
    image.tags << {
      :key => 'source_region',
      :value => Config.aws.region
    }
    image.tags << {
      :key => 'source_ami',
      :value => image.image_id
    }

    build.ami_regions.each do |region|
      regional_image = Util.ec2(region).describe_images(:filters => filters).images.first

      say_status :tag, "AMI #{image_name} (#{regional_image.image_id}) in #{region}"
      Util.ec2(region).create_tags(:resources => [regional_image.image_id], :tags => image.tags)
    end
  end
end

#wait(profile) ⇒ Object



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
121
122
123
124
125
126
127
128
129
# File 'lib/builderator/tasks/packer.rb', line 88

def wait(profile)
  invoke :configure, [profile], options

  waiting = true

  images.each do |image_name, (image, build)|
    say_status :wait, "for #{image.image_id} (#{image_name}) to be available in #{build.ami_regions.join(', ')}", :yellow
  end

  while waiting
    waiting = false

    images.each do |image_name, (image, build)|
      filters = [{
        :name => 'name',
        :values => [image_name]
      }]

      build.ami_regions.each do |region|
        regional_image = Util.ec2(region).describe_images(:filters => filters).images.first

        ## It takes a few seconds for the new AMI to show up in the `describe_images` response-set
        state = regional_image.nil? ? 'unknown' : regional_image.state
        image_id = regional_image.nil? ? 'unknown' : regional_image.image_id

        waiting = (state != 'available') || waiting
        status_color = case state
                       when 'pending', 'unknown' then :yellow
                       when 'available' then :green
                       else :red
                       end

        say_status :image, "#{image_id} (#{image.name}) is #{state} in #{region}", status_color
      end
    end

    ## If waiting == false, loop immediately to break
    sleep(10) if waiting
  end

  say_status :complete, 'All copied images are available'
end