diff --git a/lib/fog/rackspace/examples/compute_v2/create_network.rb b/lib/fog/rackspace/examples/compute_v2/create_network.rb new file mode 100644 index 000000000..01127897d --- /dev/null +++ b/lib/fog/rackspace/examples/compute_v2/create_network.rb @@ -0,0 +1,81 @@ +#!/usr/bin/env ruby + +# This example demonstrates creating a private network and attaching it to a new server with the Rackpace Open Cloud + +require 'rubygems' #required for Ruby 1.8.x +require 'fog' + +# UUID for INTERNET +INTERNET = '00000000-0000-0000-0000-000000000000' + +# UUID for Rackspace's service net +SERVICE_NET = '11111111-1111-1111-1111-111111111111' + +def get_user_input(prompt) + print "#{prompt}: " + gets.chomp +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 +service = Fog::Compute.new({ + :provider => 'rackspace', + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :version => :v2, # Use Next Gen Cloud Servers + :rackspace_region => :ord #Use Chicago Region +}) + +#create private network called my_private_net with an ip range between 192.168.0.1 - 192.168.0.255 (Note this will accept IPv6 CIDRs as well) +net = service.networks.create :label => 'my_private_net', :cidr => '192.168.0.0/24' + +puts "\nCreating #{net.label} Network with CIDR #{net.cidr}" + +# pick the first flavor +flavor = service.flavors.first + +# pick the first Ubuntu image we can find +image = service.images.find {|image| image.name =~ /Ubuntu/} + +# Create a server called alphabits connected our private network as well as the internet +server = service.servers.create :name => 'alphabits', + :flavor_id => flavor.id, + :image_id => image.id, + :networks => [net.id, INTERNET] + +puts "\nNow creating server '#{server.name}' connected the the Internet and '#{net.label}'\n" + +begin + # Check every 5 seconds to see if server is in the active state (ready?). + # If the server has not been built in 5 minutes (600 seconds) an exception will be raised. + server.wait_for(600, 5) do + print "." + STDOUT.flush + ready? + end + + puts "[DONE]\n\n" + + puts "The server has been successfully created, to login onto the server:\n\n" + puts "\t ssh #{server.username}@#{server.public_ip_address}\n\n" + +rescue Fog::Errors::TimeoutError + puts "[TIMEOUT]\n\n" + + puts "This server is currently #{server.progress}% into the build process and is taking longer to complete than expected." + puts "You can continute to monitor the build process through the web console at https://mycloud.rackspace.com/\n\n" +end + +puts "The #{server.username} password is #{server.password}\n\n" +puts "To delete the server and private network please execute the delete_network.rb script\n\n" diff --git a/lib/fog/rackspace/examples/compute_v2/delete_network.rb b/lib/fog/rackspace/examples/compute_v2/delete_network.rb new file mode 100644 index 000000000..a5f8efd78 --- /dev/null +++ b/lib/fog/rackspace/examples/compute_v2/delete_network.rb @@ -0,0 +1,78 @@ +#!/usr/bin/env ruby + +# This example demonstrates deletes a private network and attaching it to a new server with the Rackpace Open Cloud + +require 'rubygems' #required for Ruby 1.8.x +require 'fog' + +def wait_for_server_deletion(server) + begin + server.wait_for { state = 'DELETED' } + rescue Fog::Compute::RackspaceV2::NotFound => e + # do nothing + end +end + +# I have notice that cloud networks is slow to acknowledge deleted servers. This method tries to mitigate this. +def delete_network(network) + attempt = 0 + begin + network.destroy + rescue Fog::Compute::RackspaceV2::ServiceError => e + if attempt == 3 + puts "Unable to delete #{network.label}" + return false + end + puts "Network #{network.label} Delete Fail Attempt #{attempt}- #{e.inspect}" + attempt += 1 + sleep 60 + retry + end + return true +end + +def get_user_input(prompt) + print "#{prompt}: " + gets.chomp +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 +service = Fog::Compute.new({ + :provider => 'rackspace', + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :version => :v2, # Use Next Gen Cloud Servers + :rackspace_region => :ord #Use Chicago Region +}) + +# NOTE: The network must not be connected to any servers before deletion + +# Find alpha bits server +server = service.servers.find {|s| s.name == 'alphabits'} + +puts "\n" +if server + puts "Deleting alphabits server..." + server.destroy +else + puts "Unable to find server alphabits" +end + +wait_for_server_deletion(server) + +network = service.networks.find {|n| n.label == 'my_private_net'} +delete_network(network) + +puts "The network '#{network.label}' has been successfully deleted" \ No newline at end of file