Class: Builderator::Tasks::Packer
- Includes:
- Thor::Actions
- Defined in:
- lib/builderator/tasks/packer.rb
Overview
Wrap Packer commands
Class Method Summary collapse
Instance Method Summary collapse
- #build(profile = :default, *args) ⇒ Object
- #configure(profile = :default) ⇒ Object
- #copy(profile) ⇒ Object
- #remote_tag(profile) ⇒ Object
- #share(profile) ⇒ Object
- #tag(profile) ⇒ Object
- #wait(profile) ⇒ Object
Methods included from Thor::Actions
#run, #run_with_input, #run_without_bundler, #template, #thor_run
Class Method Details
.exit_on_failure? ⇒ Boolean
17 18 19 |
# File 'lib/builderator/tasks/packer.rb', line 17 def self.exit_on_failure? true end |
Instance Method Details
#build(profile = :default, *args) ⇒ Object
32 33 34 35 |
# File 'lib/builderator/tasks/packer.rb', line 32 def build(profile = :default, *args) invoke :configure, [profile], run_with_input "#{Interface.packer.command} build #{options['debug'] ? '-debug -on-error=abort' : ''} - #{args.join('')}", Interface.packer.render end |
#configure(profile = :default) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/builderator/tasks/packer.rb', line 24 def configure(profile = :default) Config.profile.use(profile) invoke Tasks::Version, :current, [], puts Interface.packer.render if ['debug'] end |
#copy(profile) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/builderator/tasks/packer.rb', line 38 def copy(profile) invoke :configure, [profile], 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}" copy_image(region, parameters) end end invoke :wait, [profile], invoke :tag, [profile], invoke :share, [profile], end |
#remote_tag(profile) ⇒ Object
124 125 126 127 128 129 130 131 132 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 |
# File 'lib/builderator/tasks/packer.rb', line 124 def remote_tag(profile) invoke :configure, [profile], 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) if build.tagging_role.nil? say_status :complete, 'No remote tagging to be performed as no IAM role is defined' return end regional_image = find_image(region, image_name) build.ami_users.each do |account| 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) .(:dry_run => false, :resources => [regional_image.image_id], :tags => image.) end end end say_status :complete, 'Remote tagging complete' end |
#share(profile) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/builderator/tasks/packer.rb', line 166 def share(profile) invoke :configure, [profile], shared = false images.each do |image_name, (image, build)| build.ami_regions.each do |region| build.ami_users.each do |user| shared = true regional_image = find_image(region, image_name) 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/builderator/tasks/packer.rb', line 61 def tag(profile) invoke :configure, [profile], images.each do |image_name, (image, build)| ## Add some additional tags about the regional source image. << { :key => 'source_region', :value => Config.aws.region } image. << { :key => 'source_ami', :value => image.image_id } build.ami_regions.each do |region| regional_image = find_image(region, image_name) say_status :tag, "AMI #{image_name} (#{regional_image.image_id}) in #{region}" Util.ec2(region).(:resources => [regional_image.image_id], :tags => image.) end end end |
#wait(profile) ⇒ Object
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 121 |
# File 'lib/builderator/tasks/packer.rb', line 85 def wait(profile) invoke :configure, [profile], 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)| build.ami_regions.each do |region| regional_image = find_image(region, image_name) ## 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(20) if waiting end say_status :complete, 'All copied images are available' end |