diff --git a/lib/fog/rackspace/examples/block_storage/create_snapshot.rb b/lib/fog/rackspace/examples/block_storage/create_snapshot.rb new file mode 100644 index 000000000..9cd0d23b0 --- /dev/null +++ b/lib/fog/rackspace/examples/block_storage/create_snapshot.rb @@ -0,0 +1,116 @@ +#!/usr/bin/env ruby + +# This example demonstrates creating a snapshot from a Cloud Block Storage volume with the Rackpace Open Cloud + +require 'rubygems' #required for Ruby 1.8.x +require 'fog' + +def get_user_input(prompt) + print "\n#{prompt}: " + gets.chomp +end + +def select_server(servers) + abort "\nThere are not any servers. Try running create_server.rb\n\n" if servers.empty? + + puts "\nSelect Server For Volume Detachment:\n\n" + servers.each_with_index do |server, i| + puts "\t #{i}. #{server.name} [#{server.public_ip_address}]" + end + + delete_str = get_user_input "\nEnter Server Number" + servers[delete_str.to_i] +end + +def select_attachment(attachments) + abort "\nThis server does not contain any volumes. Try running server_attachments.rb\n\n" if attachments.empty? + + puts "\nSelect Volume To Detach:\n\n" + attachments.each_with_index do |attachment, i| + puts "\t #{i}. #{attachment.device}" + end + + delete_str = get_user_input "\nEnter Volume Number" + attachments[delete_str.to_i] +end + +# Use username defined in ~/.fog file, if absent prompt for username. +# For more details on ~/.fog refer to http://fog.io/about/getting_started.html +def rackspace_username + Fog.credentials[:rackspace_username] || get_user_input("Enter Rackspace Username") +end + +# Use api key defined in ~/.fog file, if absent prompt for api key +# For more details on ~/.fog refer to http://fog.io/about/getting_started.html +def rackspace_api_key + Fog.credentials[:rackspace_api_key] || get_user_input("Enter Rackspace API key") +end + +#create Next Generation Cloud Server service +compute_service = Fog::Compute.new({ + :provider => 'rackspace', + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :version => :v2, # Use Next Gen Cloud Servers +}) + +#create Cloud Block Storage service +cbs_service = Fog::Rackspace::BlockStorage.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, +}) + +# retrieve list of servers +servers = compute_service.servers + +# prompt user for server +server = select_server(servers) + +# get attached volumes --also know as attachments +attachments = server.attachments + +# prompt user for volume to detach +attachment = select_attachment(attachments) + +# prompt for snapshot name +snapshot_name = get_user_input "Enter Snapshot Name" + +puts "\n\n" +puts "******************** NOTE ******************************************" +puts "* Volume must be unmounted from operating system before detaching. *" +puts "* This script assumes volume has been unmounted. *" +puts "********************************************************************\n\n" + + +volume = cbs_service.volumes.get attachment.volume_id + +# The snapshot process requires all writes to be flushed to disk. This requires unmounting the file systems or detaching the volume. +puts "Detaching Volume #{volume.display_name}" +attachment.detach + +volume.wait_for { ready? } + +puts "Now Creating Snapshot #{snapshot_name}" +snapshot = cbs_service.snapshots.create :display_name => snapshot_name, :volume_id => attachment.volume_id + +begin + # Check every 5 seconds to see if snapshot is in the available state (ready?). + # If the available has not been built in 5 minutes (600 seconds) an exception will be raised. + snapshot.wait_for(600, 5) do + print "." + STDOUT.flush + ready? + end + + puts "[DONE]\n\n" + + puts "Re-attaching Volume #{volume.display_name}" + attachment.save + +rescue Fog::Errors::TimeoutError + puts "[TIMEOUT]\n\n" + + puts "The snapshot #{snapshot.display_name} is still being preformed and is taking longer to complete than expected." + puts "You can continute to monitor the process through the web console at https://mycloud.rackspace.com/\n\n" +end + diff --git a/lib/fog/rackspace/examples/block_storage/create_volume.rb b/lib/fog/rackspace/examples/block_storage/create_volume.rb new file mode 100644 index 000000000..728a6013c --- /dev/null +++ b/lib/fog/rackspace/examples/block_storage/create_volume.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby + +# This example demonstrates creating Cloud Block Storage volume with the Rackpace Open Cloud + +require 'rubygems' #required for Ruby 1.8.x +require 'fog' + +def get_user_input(prompt) + print "\n#{prompt}: " + gets.chomp +end + +def select_volume_type(volume_types) + puts "\nSelect Volume Type:\n\n" + volume_types.each_with_index do |volume_type, i| + puts "\t #{i}. #{volume_type.name}" + end + + selected_str = get_user_input "Enter Volume Type Number" + volume_types[selected_str.to_i] +end + +# Use username defined in ~/.fog file, if absent prompt for username. +# For more details on ~/.fog refer to http://fog.io/about/getting_started.html +def rackspace_username + Fog.credentials[:rackspace_username] || get_user_input("Enter Rackspace Username") +end + +# Use api key defined in ~/.fog file, if absent prompt for api key +# For more details on ~/.fog refer to http://fog.io/about/getting_started.html +def rackspace_api_key + Fog.credentials[:rackspace_api_key] || get_user_input("Enter Rackspace API key") +end + +# create Cloud Block Storage service +service = Fog::Rackspace::BlockStorage.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, +}) + +# retrieve list of volume types +volume_types = service.volume_types + +#prompt user for volume type +volume_type = select_volume_type(volume_types) + +# prompt for volume size +volume_size = get_user_input "Enter Size of Volume (100 GB Minimum)" + +# prompt for name of volume +volume_name = get_user_input "Enter Name for Volume" + +#create volume +volume = service.volumes.create(:size => volume_size, :display_name => volume_name, :volume_type => volume_type.name) + +puts "\nVolume #{volume_name} is being created.\n\n" +puts "To delete the volume please execute the delete_volume.rb script\n\n" diff --git a/lib/fog/rackspace/examples/block_storage/delete_volume.rb b/lib/fog/rackspace/examples/block_storage/delete_volume.rb new file mode 100644 index 000000000..9dac55b5e --- /dev/null +++ b/lib/fog/rackspace/examples/block_storage/delete_volume.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby + +# This example demonstrates deleting Cloud Block Storage volume with the Rackpace Open Cloud + +require 'rubygems' #required for Ruby 1.8.x +require 'fog' + +def get_user_input(prompt) + print "\n#{prompt}: " + gets.chomp +end + +def select_volume(volumes) + abort "\nThere are not any volumes to delete. Try running create_volume.rb\n\n" if volumes.empty? + + puts "\nSelect Volume:\n\n" + volumes.each_with_index do |volume, i| + puts "\t #{i}. #{volume.display_name}" + end + + selected_str = get_user_input "Enter Volume Type Number" + volumes[selected_str.to_i] +end + +# Use username defined in ~/.fog file, if absent prompt for username. +# For more details on ~/.fog refer to http://fog.io/about/getting_started.html +def rackspace_username + Fog.credentials[:rackspace_username] || get_user_input("Enter Rackspace Username") +end + +# Use api key defined in ~/.fog file, if absent prompt for api key +# For more details on ~/.fog refer to http://fog.io/about/getting_started.html +def rackspace_api_key + Fog.credentials[:rackspace_api_key] || get_user_input("Enter Rackspace API key") +end + +# create Cloud Block Storage service +service = Fog::Rackspace::BlockStorage.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, +}) + +# retrieve list of volumes +volumes = service.volumes + +# prompt user for volume +volume = select_volume(volumes) + +# delete volume +volume.destroy + +puts "\nVolume #{volume.display_name} is being destroyed.\n\n"