Class: Capistrano::Autoscale::AWS::AMI
- Includes:
- Taggable
- Defined in:
- lib/capistrano/autoscale/aws/ami.rb
Constant Summary collapse
- DEPLOY_GROUP_TAG =
'Autoscale-Deploy-group'
Instance Attribute Summary collapse
-
#aws_counterpart ⇒ Object
readonly
Returns the value of attribute aws_counterpart.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#snapshots ⇒ Object
readonly
Returns the value of attribute snapshots.
Class Method Summary collapse
Instance Method Summary collapse
- #create_tags(deploy_group) ⇒ Object
- #delete ⇒ Object
- #deploy_group ⇒ Object
-
#initialize(id, block_device_mappings = []) ⇒ AMI
constructor
A new instance of AMI.
Methods included from Taggable
Methods inherited from Base
#autoscaling_client, #aws_access_key_id, #aws_autoscale_ami_tags, #aws_autoscale_snapshot_tags, #aws_credentials, #aws_options, #aws_region, #aws_secret_access_key, #ec2_client
Constructor Details
#initialize(id, block_device_mappings = []) ⇒ AMI
Returns a new instance of AMI.
13 14 15 16 17 18 19 20 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 13 def initialize(id, block_device_mappings = []) @id = id @aws_counterpart = ::Aws::EC2::Image.new id, client: ec2_client @snapshots = block_device_mappings.map do |mapping| Capistrano::Autoscale::AWS::Snapshot.new mapping&.ebs&.snapshot_id end end |
Instance Attribute Details
#aws_counterpart ⇒ Object (readonly)
Returns the value of attribute aws_counterpart.
11 12 13 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 11 def aws_counterpart @aws_counterpart end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 11 def id @id end |
#snapshots ⇒ Object (readonly)
Returns the value of attribute snapshots.
11 12 13 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 11 def snapshots @snapshots end |
Class Method Details
.create(instance, prefix: 'autoscale', no_reboot: false) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 31 def self.create(instance, prefix: 'autoscale', no_reboot: false) name = "#{prefix}-#{Time.now.to_i}" image = instance.aws_counterpart.create_image( name: name, instance_id: instance.id, no_reboot: no_reboot ) image = image.wait_until_exists block_device_mappings = nil # Wait until block_device_mappings/snapshots are available. loop do block_device_mappings = image.block_device_mappings snapshot_ids = block_device_mappings.map { |mapping| mapping.ebs&.snapshot_id }.compact break if !snapshot_ids.empty? || image.state == 'failed' sleep 1 image.load end raise Capistrano::Autoscale::Errors::CreateImageFailed, image if image.state == 'failed' ami = new image.id, block_device_mappings ami.tag 'Name', name ami.snapshots.each { |snapshot| snapshot.tag 'Name', name } ami end |
Instance Method Details
#create_tags(deploy_group) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 60 def (deploy_group) tag(DEPLOY_GROUP_TAG, deploy_group) .each { |key, value| tag(key, value) } snapshots.each(&:create_tags) end |
#delete ⇒ Object
26 27 28 29 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 26 def delete ec2_client.deregister_image image_id: id snapshots.each(&:delete) end |
#deploy_group ⇒ Object
22 23 24 |
# File 'lib/capistrano/autoscale/aws/ami.rb', line 22 def deploy_group [DEPLOY_GROUP_TAG] end |