Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Updated delete action to work without requiring device to be defined. #321

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions resources/ebs_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
property :volume_id, String
property :description, String
property :timeout, default: 3 * 60 # 3 mins, nil or 0 for no timeout
property :interval, default: 15 # delay in seconds per attempt
property :max_attempts, default: 5 # max number of attempts before bailing
property :snapshots_to_keep, default: 2
property :volume_type, String
property :piops, Integer, default: 0
Expand Down Expand Up @@ -103,9 +105,23 @@
end

action :delete do
vol = determine_volume
converge_by("delete volume with id: #{vol[:volume_id]}") do
delete_volume(vol[:volume_id], new_resource.timeout)
nvid = volume_id_in_node_data
vol = nvid ? volume_by_id(nvid) : determine_volume

raise "Cannot delete volume #{vol[:volume_id]} as it is attached to #{vol[:attachments].size} node(s)" unless vol[:attachments].empty?
converge_by("Delete volume with id: #{vol[:volume_id]}") do
begin
ec2.delete_volume(volume_id: vol[:volume_id], dry_run: false)
ec2.wait_until(:volume_deleted, volume_ids: [vol[:volume_id]]) do |w|
w.max_attempts = new_resource.max_attempts
w.interval = new_resource.interval
end
Chef::Log.info("Deleting volume #{vol[:volume_id]}.")
rescue Aws::Waiters::Errors::WaiterFailed => error
raise "Failed waiting for volume #{vol[:volume_id]} to be deleted: #{error.message}"
end
node.normal['aws']['ebs_volume'][new_resource.name] = {}
reload_ohai
end
end

Expand Down Expand Up @@ -305,31 +321,6 @@ def detach_volume(volume_id, timeout)
end
end

# Deletes the volume and blocks until done (or times out)
def delete_volume(volume_id, timeout)
raise "Cannot delete volume #{volume_id} as it is currently attached to #{volume_by_id(volume_id)[:attachments].size} node(s)" unless vol[:attachments].empty?

Chef::Log.debug("Deleting #{volume_id}")
ec2.delete_volume(volume_id: volume_id)

# block until deleted
begin
Timeout.timeout(timeout) do
loop do
vol = volume_by_id(volume_id)
if vol[:state] == 'deleting' || vol[:state] == 'deleted'
Chef::Log.debug("Volume #{volume_id} entered #{vol[:state]} state")
node.normal['aws']['ebs_volume'][new_resource.name] = {}
break
end
sleep 3
end
end
rescue Timeout::Error
raise "Timed out waiting for volume to enter after #{timeout} seconds"
end
end

def mark_delete_on_termination(device_name, volume_id, instance_id)
Chef::Log.debug("Marking volume #{volume_id} with device name #{device_name} attached to instance #{instance_id} #{new_resource.delete_on_termination} for deletion on instance termination")
ec2.modify_instance_attribute(block_device_mappings: [{ device_name: device_name, ebs: { volume_id: volume_id, delete_on_termination: new_resource.delete_on_termination } }], instance_id: instance_id)
Expand Down
27 changes: 13 additions & 14 deletions test/fixtures/cookbooks/aws_test/recipes/ebs_volume.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
aws_ebs_volume 'ssd_ebs_volume' do
aws_ebs_volume 'standard_ebs_vol' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
size 1
device '/dev/sdi'
device '/dev/sdj'
delete_on_termination true
action [:create, :attach]
volume_type 'gp2' # test that specifying type works
end

aws_ebs_volume 'standard_ebs_vol' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
size 1
device '/dev/sdj'
delete_on_termination true
action [:create, :attach]
action [:detach]
end

aws_ebs_volume 'ssd_ebs_volume' do
aws_ebs_volume 'standard_ebs_vol' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
device '/dev/sdi'
action [:detach]
action [:delete]
end

aws_ebs_volume 'ssd_ebs_volume' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
action [:delete]
size 1
device '/dev/sdi'
delete_on_termination true
action [:create, :attach]
volume_type 'gp2' # test that specifying type works
end

aws_ebs_volume 'standard_ebs_vol' do
aws_ebs_volume 'ssd_ebs_volume' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
device '/dev/sdj'
device '/dev/sdi'
action [:detach]
end

aws_ebs_volume 'standard_ebs_vol' do
aws_ebs_volume 'ssd_ebs_volume' do
aws_access_key node['aws_test']['key_id']
aws_secret_access_key node['aws_test']['access_key']
device '/dev/sdi'
action [:delete]
end