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, #run_without_bundler, #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
57
# 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
  invoke :share, [profile], options
end

#remote_tag(profile) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/builderator/tasks/packer.rb', line 133

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

  allowed_cred_keys = %w(access_key_id secret_access_key session_token)

  images.each do |image_name, (image, build)|
    ami_regions = build.ami_regions
    ami_regions << Config.aws.region
    ami_regions.uniq!
    ami_regions.each do |region|

      sts_client = Aws::STS::Client.new(region: region)

      filters = [{
        :name => 'name',
        :values => [image_name]
      }]

      if build.tagging_role.nil?
        say_status :complete, 'No remote tagging to be performed as no IAM role is defined'
        return
      end

      regional_image = Util.ec2(region).describe_images(:filters => filters).images.first

      build.ami_users.each do ||
        role_arn = "arn:aws:iam::#{account}:role/#{build.tagging_role}"
        begin
          response = sts_client.assume_role( :role_arn => role_arn, :role_session_name => "tag-new-ami")
          raise "Could not assume role [#{role_arn}].  Perhaps it does not exist?" unless response.successful?
        rescue => e
          say_status :skip, "Got error when trying to assume role: #{e.message} - continuing."
          next
        end

        creds_hash = response.credentials.to_h.keep_if { |k,v| allowed_cred_keys.include?(k.to_s) }

        say_status :remote_tag, "Tag AMI #{image_name} (#{regional_image.image_id}) in #{region} (#{account})"
        Util.ec2(region, creds_hash)
            .create_tags(:dry_run => false, :resources => [regional_image.image_id], :tags => image.tags)
      end
    end
  end
  say_status :complete, 'Remote tagging complete'
end

#share(profile) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/builderator/tasks/packer.rb', line 180

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

  shared = false

  images.each do |image_name, (image, build)|
    build.ami_regions.each do |region|
      build.ami_users.each do |user|
        shared = true

        filters = [{
          :name => 'name',
          :values => [image_name]
        }]

        regional_image = Util.ec2(region).describe_images(:filters => filters).images.first

        say_status :share, "image #{image_name} (#{regional_image.image_id}) with #{user}"

        share_image_parameters = {
          :image_id => regional_image.image_id,
          :launch_permission => {
            :add => [
              {
                :user_id => user
              }
            ]
          }
        }

        Util.ec2(region).modify_image_attribute(share_image_parameters)
      end
    end
  end
  say_status :complete, 'All images are shared' if shared
end

#tag(profile) ⇒ Object



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

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



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

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