Class: Automan::Ec2::Image
Constant Summary
Mixins::AwsCaller::S3_PROTO
Instance Attribute Summary
Attributes inherited from Base
#logger, #wait
#as, #cfn, #eb, #ec, #ec2, #elb, #r53, #rds, #s3
Instance Method Summary
collapse
#region_from_az
Methods inherited from Base
add_option, #log_options, #wait_until
#account, #configure_aws, #looks_like_s3_path?, #parse_s3_path, #s3_object_exists?, #s3_read
Constructor Details
#initialize(options = {}) ⇒ Image
Returns a new instance of Image.
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/automan/ec2/image.rb', line 10
def initialize(options={})
super
@wait = Wait.new({
delay: 5,
attempts: 24, debug: true,
rescuer: WaitRescuer.new,
logger: @logger
})
end
|
Instance Method Details
#create ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/automan/ec2/image.rb', line 29
def create
inst = find_inst
myname = default_image_name
logger.info "Creating image #{myname} for #{inst.id}"
newami = ec2.images.create(instance_id: instance, name: myname, no_reboot: true)
if prune == true
set_prunable(newami)
end
end
|
#default_image_name ⇒ Object
47
48
49
50
|
# File 'lib/automan/ec2/image.rb', line 47
def default_image_name
stime = Time.now.utc.iso8601.gsub(/[:+]/, '')
return name + "-" + stime
end
|
#delete_snapshots(snaplist) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/automan/ec2/image.rb', line 104
def delete_snapshots(snaplist)
snaplist.each do |snap|
if snap.status != :completed
logger.warn "Snapshot #{snap.id} could not be deleted because its status is #{snap.status}"
next
end
logger.info "Deleting snapshot #{snap.id}"
snap.delete
end
end
|
#deregister_images(snaplist) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/automan/ec2/image.rb', line 86
def deregister_images(snaplist)
my_images.each do |image|
my_snapshot = image_snapshot(image)
next unless snaplist.include?(my_snapshot)
if image.state != :available
logger.warn "AMI #{image.id} could not be deleted because its state is #{image.state}"
next
end
if image.tags["CanPrune"] == "yes"
logger.info "Deregistering AMI #{image.id}"
image.delete
end
end
end
|
#find_inst ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/automan/ec2/image.rb', line 21
def find_inst
inst = nil
if !instance.nil?
inst = ec2.instances[instance]
end
inst
end
|
#image_snapshot(image) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/automan/ec2/image.rb', line 56
def image_snapshot(image)
image.block_devices.each do |device|
if !device.nil? &&
!device[:ebs].nil? &&
!device[:ebs][:snapshot_id].nil?
return device[:ebs][:snapshot_id]
end
end
nil
end
|
#image_snapshot_exists?(image) ⇒ Boolean
52
53
54
|
# File 'lib/automan/ec2/image.rb', line 52
def image_snapshot_exists?(image)
!image_snapshot(image).nil?
end
|
#is_more_than_month_old?(mytime) ⇒ Boolean
39
40
41
42
43
44
45
|
# File 'lib/automan/ec2/image.rb', line 39
def is_more_than_month_old?(mytime)
if mytime.class == Time && mytime < Time.now.utc - (60*60*24*30)
true
else
false
end
end
|
#my_images ⇒ Object
82
83
84
|
# File 'lib/automan/ec2/image.rb', line 82
def my_images
ec2.images.with_owner('self')
end
|
#prune_amis ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/automan/ec2/image.rb', line 117
def prune_amis
condemned_snaps = []
allsnapshots = ec2.snapshots.with_owner('self')
allsnapshots.each do |onesnapshot|
next unless onesnapshot.status == :completed
mycreatetime = onesnapshot.start_time
if is_more_than_month_old?(mycreatetime) and onesnapshot.tags["CanPrune"] == "yes"
logger.info "Adding snapshot #{onesnapshot.id} to condemed list"
condemned_snaps.push onesnapshot
end
end
unless condemned_snaps.empty?
deregister_images condemned_snaps.map {|s| s.id }
delete_snapshots condemned_snaps
end
end
|
#set_prunable(newami) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/automan/ec2/image.rb', line 68
def set_prunable(newami)
logger.info "Setting prunable for AMI #{newami.image_id}"
newami.tags["CanPrune"] = "yes"
wait.until do
logger.info "Waiting for a valid snapshot so we can tag it."
image_snapshot_exists? newami
end
snapshot = image_snapshot(newami)
logger.info "Setting prunable for snapshot #{snapshot}"
ec2.snapshots[snapshot].tags["CanPrune"] = "yes"
end
|