mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
moving examples; added readme
This commit is contained in:
parent
adb53b4a29
commit
23acd5ec33
8 changed files with 47 additions and 452 deletions
46
lib/fog/rackspace/examples/READ_ME.md
Normal file
46
lib/fog/rackspace/examples/READ_ME.md
Normal file
|
@ -0,0 +1,46 @@
|
|||
# Getting Started Examples
|
||||
|
||||
## Download
|
||||
|
||||
Examples using the Rackspace Open Cloud and Fog can be found in the [fog repository](https://github.com/fog/fog) under `/fog/lib/fog/rackspace/examples`.
|
||||
|
||||
This repository can be downloaded via `git` by executing the following:
|
||||
|
||||
git clone git://github.com/fog/fog.git
|
||||
|
||||
Optionally, you can download a zip by clicking on this [link](https://github.com/fog/fog/archive/master.zip)
|
||||
|
||||
## Requirements
|
||||
|
||||
These examples require the following:
|
||||
|
||||
* Cloud Files Account
|
||||
* Ruby 1.8.x or 1.9.x
|
||||
* `fog` gem
|
||||
|
||||
For more information please refer to the [Getting Started with Fog and the Rackspace Open Cloud](https://github.com/fog/fog/blob/master/lib/fog/rackspace/docs/getting_started.md) document.
|
||||
|
||||
## Credentials
|
||||
|
||||
Examples will prompt for Rackspace Open Cloud Credentials. To skip these prompts create a `.fog` with containing the following:
|
||||
|
||||
default:
|
||||
rackspace_username: RACKSPACE_USERNAME
|
||||
rackspace_api_key: RACkSPACE_API_KEY
|
||||
|
||||
Replace captialized values with the appropriate information.
|
||||
|
||||
## Executing
|
||||
|
||||
To execute these using `bundler`:
|
||||
|
||||
bundle exec ruby <script>
|
||||
|
||||
To execute these without `bundler`:
|
||||
|
||||
ruby <script>
|
||||
|
||||
## Support and Feedback
|
||||
Your feedback is appreciated! If you have specific issues with the **fog** SDK, developers should file an [issue via Github](https://github.com/fog/fog/issues).
|
||||
|
||||
For general feedback and support requests, send an email to: <sdk-support@rackspace.com>.
|
|
@ -18,9 +18,7 @@ def select_image(snapshot_images)
|
|||
puts "\t #{i}. #{image.name}"
|
||||
end
|
||||
|
||||
delete_str = get_user_input "\nEnter Image Number"
|
||||
abort "Unrecognized input. Exiting without deleting images." unless delete_str =~ /^ALL|\d+$/
|
||||
|
||||
delete_str = get_user_input "\nEnter Image Number"
|
||||
snapshot_images[delete_str.to_i]
|
||||
end
|
||||
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This example demonstrates creating a server image with the Rackpace Open Cloud
|
||||
|
||||
require 'rubygems' #required for Ruby 1.8.x
|
||||
require './lib/fog'
|
||||
|
||||
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
|
||||
username = Fog.credentials[:rackspace_username]
|
||||
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
|
||||
api_key = Fog.credentials[:rackspace_api_key]
|
||||
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_endpoint => Fog::Compute::RackspaceV2::ORD_ENDPOINT #Use Chicago Region
|
||||
})
|
||||
|
||||
#retrieve list of servers
|
||||
servers = service.servers
|
||||
|
||||
abort "\nThere are not any servers avaliable to image. Try running create_server.rb\n\n" if servers.empty?
|
||||
|
||||
puts "\nSelect server image:\n\n"
|
||||
servers.each_with_index do |server, i|
|
||||
puts "\t #{i}. #{server.name} [#{server.public_ip_address}]"
|
||||
end
|
||||
|
||||
selected_str = get_user_input "\nEnter number"
|
||||
server = servers[selected_str.to_i]
|
||||
|
||||
image_name = get_user_input "Enter name for image"
|
||||
|
||||
#creates image for server
|
||||
server.create_image image_name
|
||||
|
||||
puts "\n\nImage #{image_name} is being created for server #{server.name}.\n\n"
|
||||
|
||||
puts "To delete the image please execute the delete_image.rb script\n\n"
|
||||
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This example demonstrates creating a server with the Rackpace Open Cloud
|
||||
|
||||
require 'rubygems' #required for Ruby 1.8.x
|
||||
require './lib/fog'
|
||||
require "base64" #required to encode files for personality functionality
|
||||
|
||||
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
|
||||
username = Fog.credentials[:rackspace_username]
|
||||
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
|
||||
api_key = Fog.credentials[:rackspace_api_key]
|
||||
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_endpoint => Fog::Compute::RackspaceV2::ORD_ENDPOINT #Use Chicago Region
|
||||
})
|
||||
|
||||
# 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 server
|
||||
server = service.servers.create :name => 'cumulus',
|
||||
:flavor_id => flavor.id,
|
||||
:image_id => image.id,
|
||||
:metadata => { 'fog_sample' => 'true'},
|
||||
:personality => [{
|
||||
:path => '/root/fog.txt',
|
||||
:contents => Base64.encode64('Fog was here!')
|
||||
}]
|
||||
|
||||
# reload flavor in order to retrieve all of its attributes
|
||||
flavor.reload
|
||||
|
||||
puts "\nNow creating server '#{server.name}' the following with specifications:\n"
|
||||
puts "\t* #{flavor.ram} MB RAM"
|
||||
puts "\t* #{flavor.disk} GB"
|
||||
puts "\t* #{flavor.vcpus} CPU(s)"
|
||||
puts "\t* #{image.name}"
|
||||
|
||||
puts "\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 please execute the delete_server.rb script\n\n"
|
||||
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This example demonstrates deleting a server image with the Rackpace Open Cloud
|
||||
|
||||
require 'rubygems' #required for Ruby 1.8.x
|
||||
require './lib/fog'
|
||||
|
||||
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
|
||||
username = Fog.credentials[:rackspace_username]
|
||||
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
|
||||
api_key = Fog.credentials[:rackspace_api_key]
|
||||
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_endpoint => Fog::Compute::RackspaceV2::ORD_ENDPOINT #Use Chicago Region
|
||||
})
|
||||
|
||||
#retrieve list of images
|
||||
images = service.images
|
||||
|
||||
# select all of the snapshot type images. base images are not user deletable
|
||||
snapshot_images = images.select do |image|
|
||||
image.metadata["image_type"] == "snapshot"
|
||||
end
|
||||
|
||||
abort "\nThere are not any images avaliable to delete. Try running create_image.rb\n\n" if snapshot_images.empty?
|
||||
|
||||
puts "\nSelect image to delete:\n\n"
|
||||
snapshot_images.each_with_index do |image, i|
|
||||
puts "\t #{i}. #{image.name}"
|
||||
end
|
||||
|
||||
delete_str = get_user_input "\nEnter number (type 'ALL' to delete all images)"
|
||||
abort "Unrecognized input. Exiting without deleting images." unless delete_str =~ /^ALL|\d+$/
|
||||
|
||||
confirm = get_user_input "Are you sure? (Y/N)"
|
||||
abort "Exiting without deleting images" unless confirm == 'Y'
|
||||
|
||||
puts "\n\n"
|
||||
|
||||
if delete_str == 'ALL'
|
||||
snapshot_images.each {|image| image.destroy }
|
||||
puts "All images have been destroyed"
|
||||
else
|
||||
image = snapshot_images[delete_str.to_i]
|
||||
image.destroy
|
||||
puts "#{image.name} has been destroyed"
|
||||
end
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This example demonstrates how to delete servers with Fog and the Rackspace Open Cloud
|
||||
|
||||
require 'rubygems' #required for Ruby 1.8.x
|
||||
require './lib/fog'
|
||||
|
||||
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
|
||||
username = Fog.credentials[:rackspace_username]
|
||||
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
|
||||
api_key = Fog.credentials[:rackspace_api_key]
|
||||
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_endpoint => Fog::Compute::RackspaceV2::ORD_ENDPOINT #Use Chicago Region
|
||||
})
|
||||
|
||||
#retrieve list of servers
|
||||
servers = service.servers
|
||||
|
||||
abort "\nThere are not any servers to delete. Try running create_server.rb\n\n" if servers.empty?
|
||||
|
||||
puts "\nSelect server delete:\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 number (type 'ALL' to delete all servers)"
|
||||
abort "Unrecognized input. Exiting without deleting servers." unless delete_str =~ /^ALL|\d+$/
|
||||
|
||||
confirm = get_user_input "Are you sure? (Y/N)"
|
||||
abort "Exiting without deleting servers" unless confirm == 'Y'
|
||||
|
||||
puts "\n\n"
|
||||
|
||||
if delete_str == 'ALL'
|
||||
servers.each {|server| server.destroy }
|
||||
puts "All servers have been destroyed"
|
||||
else
|
||||
server = servers[delete_str.to_i]
|
||||
server.destroy
|
||||
puts "#{server.name} has been destroyed"
|
||||
end
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This example demonstrates how to resize servers with Fog and the Rackspace Open Cloud
|
||||
|
||||
require 'rubygems' #required for Ruby 1.8.x
|
||||
require './lib/fog'
|
||||
|
||||
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
|
||||
username = Fog.credentials[:rackspace_username]
|
||||
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
|
||||
api_key = Fog.credentials[:rackspace_api_key]
|
||||
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_endpoint => Fog::Compute::RackspaceV2::ORD_ENDPOINT #Use Chicago Region
|
||||
})
|
||||
|
||||
#retrieve list of servers
|
||||
servers = service.servers
|
||||
|
||||
abort "\nThere are not any servers to resize. Try running create_server.rb\n\n" if servers.empty?
|
||||
|
||||
puts "\nSelect server resize:\n\n"
|
||||
servers.each_with_index do |server, i|
|
||||
puts "\t #{i}. #{server.name} [#{server.public_ip_address}]"
|
||||
end
|
||||
|
||||
selected_str = get_user_input "\nEnter number"
|
||||
server = servers[selected_str.to_i]
|
||||
|
||||
puts "\n"
|
||||
|
||||
# retrieve list of avaliable flavors
|
||||
flavors = service.flavors
|
||||
|
||||
puts "\nSelect flavor to resize to:\n\n"
|
||||
flavors.each_with_index do |flavor, i|
|
||||
next if server.flavor_id == flavor.id
|
||||
puts "\t #{i}. #{flavor.name}"
|
||||
end
|
||||
|
||||
selected_flavor_str = get_user_input "\nEnter number"
|
||||
selected_flavor = flavors[selected_flavor_str.to_i]
|
||||
|
||||
# resize server
|
||||
server.resize selected_flavor.id
|
||||
|
||||
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"
|
||||
rescue Fog::Errors::TimeoutError
|
||||
puts "[TIMEOUT]\n\n"
|
||||
|
||||
puts "This server is currently #{server.progress}% into the resize 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"
|
||||
|
||||
exit
|
||||
end
|
||||
|
||||
puts "Server has been successfull resized!"
|
||||
action = get_user_input "Press 'C' to confirm or 'R' to revert resize (R/C)"
|
||||
|
||||
case action
|
||||
when 'C'
|
||||
puts "\nConfirming resize operation"
|
||||
server.confirm_resize
|
||||
when 'R'
|
||||
puts "\nReverting resize operation"
|
||||
server.revert_resize
|
||||
else
|
||||
puts "\nUnrecognized input. Exiting."
|
||||
end
|
|
@ -1,77 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
# This example demonstrates creating a server with the Rackpace Open Cloud
|
||||
require 'rubygems' #required for Ruby 1.8.x
|
||||
require './lib/fog'
|
||||
|
||||
# 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
|
||||
username = Fog.credentials[:rackspace_username]
|
||||
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
|
||||
api_key = Fog.credentials[:rackspace_api_key]
|
||||
api_key ||= get_user_input "Enter Rackspace API key: "
|
||||
end
|
||||
|
||||
def print_metadata(server)
|
||||
server.metadata.each do |metadatum|
|
||||
puts "#{metadatum.key}: #{metadatum.value}"
|
||||
end
|
||||
puts "\n"
|
||||
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_endpoint => Fog::Compute::RackspaceV2::ORD_ENDPOINT #Use Chicago Region
|
||||
})
|
||||
|
||||
# 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 server
|
||||
server = service.servers.create :name => 'cumulus',
|
||||
:flavor_id => flavor.id,
|
||||
:image_id => image.id,
|
||||
:metadata => { 'color' => 'red'}
|
||||
|
||||
puts "metadata from server creation"
|
||||
print_metadata(server)
|
||||
|
||||
puts "adding new metadata\n"
|
||||
server.metadata["environment"] = "demo"
|
||||
|
||||
puts "updating existing metadata\n"
|
||||
server.metadata["color"] = "blue"
|
||||
|
||||
puts "saving latest metadata change\n"
|
||||
server.metadata.save
|
||||
|
||||
puts "reload latest server metadata"
|
||||
server.metadata.reload
|
||||
|
||||
print_metadata(server)
|
||||
|
||||
puts "delete environment metadata"
|
||||
metadatum = server.metadata.find {|metadataum| metadataum.key == 'environment'}
|
||||
metadatum.destroy
|
||||
|
||||
puts "reload latest server metadata"
|
||||
server.metadata.reload
|
||||
|
||||
puts "To delete the server please execute the delete_server.rb script\n\n"
|
||||
|
||||
|
||||
|
||||
|
Loading…
Add table
Reference in a new issue