Class: Bosh::AwsCloud::Stemcell

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/cloud/aws/stemcell.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#cloud_error, #default_ephemeral_disk_mapping, #ebs_ephemeral_disk_mapping, #extract_security_groups

Constructor Details

#initialize(region, image) ⇒ Stemcell

Returns a new instance of Stemcell.



13
14
15
16
17
# File 'lib/cloud/aws/stemcell.rb', line 13

def initialize(region, image)
  @region = region
  @ami = image
  @snapshots = []
end

Instance Attribute Details

#amiObject (readonly)

Returns the value of attribute ami.



5
6
7
# File 'lib/cloud/aws/stemcell.rb', line 5

def ami
  @ami
end

#snapshotsObject (readonly)

Returns the value of attribute snapshots.



5
6
7
# File 'lib/cloud/aws/stemcell.rb', line 5

def snapshots
  @snapshots
end

Class Method Details

.find(region, id) ⇒ Object

Raises:

  • (Bosh::Clouds::CloudError)


7
8
9
10
11
# File 'lib/cloud/aws/stemcell.rb', line 7

def self.find(region, id)
  image = region.images[id]
  raise Bosh::Clouds::CloudError, "could not find AMI '#{id}'" unless image.exists?
  new(region, image)
end

Instance Method Details

#deleteObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloud/aws/stemcell.rb', line 19

def delete
  memoize_snapshots

  ami.deregister

  # Wait for the AMI to be deregistered, or the snapshot deletion will fail,
  # as the AMI is still in use.
  ResourceWait.for_image(image: ami, state: :deleted)

  delete_snapshots
  logger.info("deleted stemcell '#{id}'")
# The following suppression of AuthFailure is potentially dangerous
# But we have to do it here because we need to be compatible with existing
# light stemcells in BOSH DB which appear to be "heavy".
rescue AWS::EC2::Errors::AuthFailure => e
  # If we get an auth failure from the deregister call, it means we don't own the AMI
  # and we were just faking it, so we can just return pretending that we deleted it.
  logger.info("deleted fake stemcell '#{id}")
end

#delete_snapshotsObject



63
64
65
66
67
68
69
# File 'lib/cloud/aws/stemcell.rb', line 63

def delete_snapshots
  snapshots.each do |id|
    logger.info("cleaning up snapshot '#{id}'")
    snapshot = @region.snapshots[id]
    snapshot.delete
  end
end

#idObject



39
40
41
# File 'lib/cloud/aws/stemcell.rb', line 39

def id
  ami.id
end

#image_idObject



43
44
45
# File 'lib/cloud/aws/stemcell.rb', line 43

def image_id
  ami.id
end

#loggerObject



71
72
73
# File 'lib/cloud/aws/stemcell.rb', line 71

def logger
  Bosh::Clouds::Config.logger
end

#memoize_snapshotsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cloud/aws/stemcell.rb', line 51

def memoize_snapshots
  # .to_hash is used as the AWS API documentation isn't trustworthy:
  # it says block_device_mappings retruns a Hash, but in reality it flattens it!
  ami.block_device_mappings.to_hash.each do |device, map|
    snapshot_id = map[:snapshot_id]
    if id
      logger.debug("queuing snapshot '#{snapshot_id}' for deletion")
      snapshots << snapshot_id
    end
  end
end

#root_device_nameObject



47
48
49
# File 'lib/cloud/aws/stemcell.rb', line 47

def root_device_name
  ami.root_device_name
end