Class: Capistrano::ASG::Rolling::AMI
- Inherits:
-
Object
- Object
- Capistrano::ASG::Rolling::AMI
- Includes:
- AWS
- Defined in:
- lib/capistrano/asg/rolling/ami.rb
Overview
AWS EC2 Machine Image.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
Class Method Summary collapse
-
.create(instance:, name:, description: nil, tags: nil) ⇒ Object
Create an AMI from an instance and wait until the AMI is available.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(id, instance = nil) ⇒ AMI
constructor
A new instance of AMI.
- #snapshots ⇒ Object
- #tag?(key) ⇒ Boolean
- #tags ⇒ Object
Methods included from AWS
#aws_autoscaling_client, #aws_ec2_client
Constructor Details
#initialize(id, instance = nil) ⇒ AMI
Returns a new instance of AMI.
12 13 14 15 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 12 def initialize(id, instance = nil) @id = id @instance = instance end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 10 def id @id end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
10 11 12 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 10 def instance @instance end |
Class Method Details
.create(instance:, name:, description: nil, tags: nil) ⇒ Object
Create an AMI from an instance and wait until the AMI is available.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 18 def self.create(instance:, name:, description: nil, tags: nil) aws_ec2_client = instance.aws_ec2_client = { instance_id: instance.id, name: name, description: description } if tag_specifications = .map { |key, value| { key: key, value: value } } [:tag_specifications] = [ { resource_type: 'image', tags: tag_specifications }, { resource_type: 'snapshot', tags: tag_specifications } ] end response = aws_ec2_client.create_image() begin aws_ec2_client.wait_until(:image_available, image_ids: [response.image_id]) rescue Aws::Waiters::Errors::TooManyAttemptsError # When waiting for the AMI takes longer than the default (10 minutes), # then assume it will eventually succeed and just continue. end new(response.image_id, instance) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
76 77 78 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 76 def ==(other) id == other.id end |
#delete ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 52 def delete # Retrieve the snapshots first because we can't call #describe_images anymore # after deregistering the image. image_snapshots = snapshots aws_ec2_client.deregister_image(image_id: id) image_snapshots.each(&:delete) end |
#exists? ⇒ Boolean
48 49 50 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 48 def exists? aws_ec2_image.exists? end |
#hash ⇒ Object
82 83 84 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 82 def hash id.hash end |
#snapshots ⇒ Object
62 63 64 65 66 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 62 def snapshots @snapshots ||= aws_ec2_image.block_device_mappings.filter_map do |mapping| Snapshot.new(mapping.ebs.snapshot_id) if mapping.ebs end end |
#tag?(key) ⇒ Boolean
72 73 74 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 72 def tag?(key) .key?(key) end |
#tags ⇒ Object
68 69 70 |
# File 'lib/capistrano/asg/rolling/ami.rb', line 68 def @tags ||= aws_ec2_image..to_h { |tag| [tag.key, tag.value] } end |