diff --git a/lib/fog/rackspace/examples/auto_scale/add_webhook.rb b/lib/fog/rackspace/examples/auto_scale/add_webhook.rb new file mode 100644 index 000000000..f5720e5dc --- /dev/null +++ b/lib/fog/rackspace/examples/auto_scale/add_webhook.rb @@ -0,0 +1,74 @@ +#!/usr/bin/env ruby + +# This example demonstrates adding a webhook to an auto scaling group with the Rackpace Open Cloud + +require '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 + 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 + +def select_group(groups) + abort "\nThere are no groups in the Chicago region. Try running create_scaling_group.rb\n\n" if groups.empty? + + puts "\nSelect Group With Policy:\n\n" + groups.each_with_index do |group, i| + config = group.group_config + puts "\t #{i}. #{config.name}" + end + + select_str = get_user_input "\nEnter Group Number" + groups[select_str.to_i] +end + +def select_policy(policies) + abort "\nThere are no policies for this scaling group. Try running add_policy.rb\n\n" if policies.empty? + + puts "\nSelect Policy Triggered By Webhook:\n\n" + policies.each_with_index do |policy, i| + puts "\t #{i}. #{policy.name}" + end + + select_str = get_user_input "\nEnter Policy Number" + policies[select_str.to_i] +end + +# create auto scaling service +auto_scale_service = Fog::Rackspace::AutoScale.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :rackspace_region => :ord # Use Chicago Region +}) + +# retrieve list of scaling groups +groups = auto_scale_service.groups + +# prompt for group +group = select_group(groups) + +# retrieve list of policies for group +policies = group.policies + +# prompt for policy to delete +policy = select_policy(policies) + +# prompt for webhook name +webhook_name = get_user_input "Enter name for webhook" + +# create webhook +webhook = policy.webhooks.create :name => webhook_name + +puts "Webhook #{webhook.name} was successfully added - #{webhook.execution_url}" diff --git a/lib/fog/rackspace/examples/auto_scale/create_scaling_group.rb b/lib/fog/rackspace/examples/auto_scale/create_scaling_group.rb new file mode 100644 index 000000000..12b36edde --- /dev/null +++ b/lib/fog/rackspace/examples/auto_scale/create_scaling_group.rb @@ -0,0 +1,142 @@ +#!/usr/bin/env ruby + +# This example demonstrates creating an auto scaling group with the Rackpace Open Cloud + +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 + +def get_user_input_as_int(prompt) + str = get_user_input(prompt) + 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 + + +def select_image(images) + puts "\nSelect Image For Server:\n\n" + images.each_with_index do |image, i| + puts "\t #{i}. #{image.name}" + end + + select_str = get_user_input "\nEnter Image Number" + images[select_str.to_i] +end + + +# create auto scaling service +auto_scale_service = Fog::Rackspace::AutoScale.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :rackspace_region => :ord # Use Chicago Region +}) + + +# create Next Generation Cloud Server service to get list of flavors +compute_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 +}) + + +# prompt for scaling group name +scaling_group_name = get_user_input "Enter name of scaling group" + +# prompt for cool down period +cool_down = get_user_input_as_int "Enter cool down period in seconds" + +# prompt for miniumum number of entities +min_entities = get_user_input_as_int "Enter minimum number of servers" + +# prompt for max number of entities +max_entities = get_user_input_as_int "Enter maximum number of servers" + +# prompt for base name server name +server_name = get_user_input "Enter base name for servers in scaling group '#{scaling_group_name}'" + +# retrieve list of images from computer service +images = compute_service.images + +# prompt for server image +image = select_image(images) + +# pick first server flavor +flavor = compute_service.flavors.first + +# create server template +# server_template = compute_service.servers.new :name => server_name, +# :flavor_id => flavor.id, +# :image_id => image.id, +# :disk_config => "MANUAL", +# :metadata => { "created_by" => "autoscale sample script" }, +# :networks => [INTERNET, SERVICE_NET], +# :personality => { +# "path" => "/root/.csivh", +# "contents" => "VGhpcyBpcyBhIHRlc3QgZmlsZS4=" +# } + +server_template = { + "name" => "autoscale_server", + "imageRef" => image.id, + "flavorRef" => flavor.id, + "OS-DCF =>diskConfig" => "MANUAL", + "metadata" => { + "build_config" => "core", + "meta_key_1" => "meta_value_1", + "meta_key_2" => "meta_value_2" + }, + "networks" => [ + { + "uuid" => "11111111-1111-1111-1111-111111111111" + }, + { + "uuid" => "00000000-0000-0000-0000-000000000000" + } + ], + "personality" => [ + { + "path" => "/root/.csivh", + "contents" => "VGhpcyBpcyBhIHRlc3QgZmlsZS4=" + } + ] + } + +# create launch configuration +launch_config = Fog::Rackspace::AutoScale::LaunchConfig.new :type => :launch_server, :args => {:server => s } + +# create group configuration +group_config = Fog::Rackspace::AutoScale::GroupConfig.new :max_entities => max_entities, + :min_entities => min_entities, + :cooldown => cool_down, + :name => scaling_group_name, + :metadata => { "created_by" => "autoscale sample script" } + +# create scaling group +group = auto_scale_service.groups.create :group_config => group_config, + :launch_config => launch_config + +puts "Scaling Group #{scaling_group_name} (#{group.id}) was created!" +puts "State: #{group.state}" diff --git a/lib/fog/rackspace/examples/auto_scale/delete_policy.rb b/lib/fog/rackspace/examples/auto_scale/delete_policy.rb new file mode 100644 index 000000000..eedfb9572 --- /dev/null +++ b/lib/fog/rackspace/examples/auto_scale/delete_policy.rb @@ -0,0 +1,71 @@ +#!/usr/bin/env ruby + +# This example demonstrates deleting an auto scaling policy with the Rackpace Open Cloud + +require '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 + 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 + +def select_group(groups) + abort "\nThere are no groups in the Chicago region. Try running create_scaling_group.rb\n\n" if groups.empty? + + puts "\nSelect Group With Policy:\n\n" + groups.each_with_index do |group, i| + config = group.group_config + puts "\t #{i}. #{config.name}" + end + + select_str = get_user_input "\nEnter Group Number" + groups[select_str.to_i] +end + +def select_policy(policies) + abort "\nThere are no policies for this scaling group. Try running add_policy.rb\n\n" if policies.empty? + + puts "\nSelect Policy To Delete:\n\n" + policies.each_with_index do |policies, i| + puts "\t #{i}. #{policies.name}" + end + + select_str = get_user_input "\nEnter Policy Number" + policies[select_str.to_i] +end + +# create auto scaling service +auto_scale_service = Fog::Rackspace::AutoScale.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :rackspace_region => :ord # Use Chicago Region +}) + +# retrieve list of scaling groups +groups = auto_scale_service.groups + +# prompt for group +group = select_group(groups) + +# retrieve list of policies for group +policies = group.policies + +# prompt for policy to delete +policy = select_policy(policies) + +# delete policy +policy.destroy + +puts "\nPolicy '#{policy.name}' has been destroyed\n\n" diff --git a/lib/fog/rackspace/examples/auto_scale/delete_webhook.rb b/lib/fog/rackspace/examples/auto_scale/delete_webhook.rb new file mode 100644 index 000000000..7376ce441 --- /dev/null +++ b/lib/fog/rackspace/examples/auto_scale/delete_webhook.rb @@ -0,0 +1,91 @@ +#!/usr/bin/env ruby + +# This example demonstrates delete a webhook from an auto scaling group with the Rackpace Open Cloud + +require '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 + 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 + +def select_group(groups) + abort "\nThere are no groups in the Chicago region. Try running create_scaling_group.rb\n\n" if groups.empty? + + puts "\nSelect Group With Policy:\n\n" + groups.each_with_index do |group, i| + config = group.group_config + puts "\t #{i}. #{config.name}" + end + + select_str = get_user_input "\nEnter Group Number" + groups[select_str.to_i] +end + +def select_policy(policies) + abort "\nThere are no policies for this scaling group. Try running add_policy.rb\n\n" if policies.empty? + + puts "\nSelect Policy With Webhook:\n\n" + policies.each_with_index do |policy, i| + puts "\t #{i}. #{policy.name}" + end + + select_str = get_user_input "\nEnter Policy Number" + policies[select_str.to_i] +end + +def select_webhook(webhooks) + abort "\nThere are no webhooks for this policy. Try running add_webhook.rb\n\n" if webhooks.empty? + + puts "\nSelect Webhook:\n\n" + webhooks.each_with_index do |webhook, i| + puts "\t #{i}. #{webhook.name}" + end + + select_str = get_user_input "\nEnter Webhook Number" + webhooks[select_str.to_i] +end + + +# create auto scaling service +auto_scale_service = Fog::Rackspace::AutoScale.new({ + :rackspace_username => rackspace_username, + :rackspace_api_key => rackspace_api_key, + :rackspace_region => :ord # Use Chicago Region +}) + +# retrieve list of scaling groups +groups = auto_scale_service.groups + +# prompt for group +group = select_group(groups) + +# retrieve list of policies for group +policies = group.policies + +# prompt for policy +policy = select_policy(policies) + +# retrieve list of webhooks for policy +webhooks = policy.webhooks + +# prompt for webhook +webhook = select_webhook(webhooks) + +# delete webhook +webhook.destroy + +puts "Webhook '#{webhook.name} was destroyed" \ No newline at end of file