Skip to content

Instantly share code, notes, and snippets.

@rberger
Created November 29, 2009 23:59
Show Gist options
  • Save rberger/245144 to your computer and use it in GitHub Desktop.
Save rberger/245144 to your computer and use it in GitHub Desktop.
def setup_volume(action_to_use, volume_info)
aws_ebs_volume "mysql_data_volume" do
Chef::Log.debug("in mysql_data_volume action_to_use: #{action_to_use} volume_info: #{volume_info}")
aws_access_key node[:runa][:aws_access_key]
aws_secret_access_key node[:runa][:aws_secret_access_key]
volume_id (action_to_use == :attach) ? node[:runa][:volume_id] : nil
snapshot_id (action_to_use == :create) ? node[:runa][:snapshot_id] : nil
availability_zone node[:runa][:availability_zone]
device node[:runa][:device]
action action_to_use
provider "aws_ebs_volume"
end
end
if node[:runa][:volume_id]
begin
# Try attaching. If its fails, try creating using snapshot id
Chef::Log.debug("Runtime-db recipe: Trying to attach volume: #{node[:runa][:volume_id]}")
setup_volume(:attach, node[:runa][:volume_id])
rescue RuntimeError => e
if node[:runa][:snapshot_id]
Chef::Log.debug("Runtime-db recipe rescue: Trying to create volume from snapshot: #{node[:runa][:snapshot_id]}")
setup_volume(:create, node[:runa][:snapshot_id])
else
Chef::Log.error("Runtime-db recipe: Failed to attach volume: #{node[:runa][:volume_id]} or create from snapshot: node[:runa][:snapshot_id]")
raise
end
end
else
if node[:runa][:snapshot_id]
Chef::Log.debug("Runtime-db recipe: Trying to create volume from snapshot: #{node[:runa][:snapshot_id]}")
setup_volume(:create, node[:runa][:snapshot_id])
else
raise "Runtime-db recipe: No specified volume_id or snapshot_id"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment