mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|auto_scale] update create scaling group to use GroupBuilder.
This commit is contained in:
parent
6aa1d848eb
commit
238e0d98c6
3 changed files with 244 additions and 36 deletions
|
@ -3,6 +3,7 @@
|
|||
# This example demonstrates creating an auto scaling group with the Rackpace Open Cloud
|
||||
|
||||
require 'fog'
|
||||
require 'fog/rackspace/models/auto_scale/group_builder'
|
||||
|
||||
# UUID for INTERNET
|
||||
INTERNET = '00000000-0000-0000-0000-000000000000'
|
||||
|
@ -66,7 +67,7 @@ compute_service = Fog::Compute.new({
|
|||
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"
|
||||
cooldown = 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"
|
||||
|
@ -74,9 +75,6 @@ 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
|
||||
print "Loading available server images...."
|
||||
images = compute_service.images.all
|
||||
|
@ -88,45 +86,30 @@ image = select_image(images)
|
|||
# pick first server flavor
|
||||
flavor = compute_service.flavors.first
|
||||
|
||||
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" => [
|
||||
attributes = {
|
||||
:server_name => "autoscale_server",
|
||||
:image => image,
|
||||
:flavor => flavor,
|
||||
:networks => [INTERNET, SERVICE_NET],
|
||||
:personality => [
|
||||
{
|
||||
"path" => "/root/.csivh",
|
||||
"contents" => "VGhpcyBpcyBhIHRlc3QgZmlsZS4="
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# create launch configuration
|
||||
launch_config = Fog::Rackspace::AutoScale::LaunchConfig.new :type => :launch_server, :args => {:server => server_template }
|
||||
|
||||
# create group configuration
|
||||
group_config = Fog::Rackspace::AutoScale::GroupConfig.new :max_entities => max_entities,
|
||||
],
|
||||
:max_entities => max_entities,
|
||||
:min_entities => min_entities,
|
||||
:cooldown => cool_down,
|
||||
:cooldown => cooldown,
|
||||
:name => scaling_group_name,
|
||||
:metadata => { "created_by" => "autoscale sample script" }
|
||||
:metadata => { "created_by" => "autoscale sample script" },
|
||||
:launch_config_type => :launch_server
|
||||
}
|
||||
|
||||
# create scaling group
|
||||
group = auto_scale_service.groups.create :group_config => group_config,
|
||||
:launch_config => launch_config
|
||||
# Use builder to create group
|
||||
group = Fog::Rackspace::AutoScale::GroupBuilder.build(auto_scale_service, attributes)
|
||||
|
||||
# save the built group
|
||||
group.save
|
||||
|
||||
puts "\nScaling Group #{scaling_group_name} (#{group.id}) was created!"
|
||||
puts "State: #{group.state}"
|
||||
|
|
81
lib/fog/rackspace/models/auto_scale/group_builder.rb
Normal file
81
lib/fog/rackspace/models/auto_scale/group_builder.rb
Normal file
|
@ -0,0 +1,81 @@
|
|||
require 'fog/core/model'
|
||||
require 'fog/rackspace/models/auto_scale/group_config'
|
||||
require 'fog/rackspace/models/auto_scale/launch_config'
|
||||
require 'fog/rackspace/models/auto_scale/policies'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class AutoScale
|
||||
class GroupBuilder
|
||||
|
||||
class << self
|
||||
def build(service, attributes)
|
||||
service.groups.new :group_config => build_group_config(attributes), :launch_config => build_server_launch_config(attributes)
|
||||
end
|
||||
|
||||
def build_group_config(attributes)
|
||||
Fog::Rackspace::AutoScale::GroupConfig.new :max_entities => attributes[:max_entities],
|
||||
:min_entities => attributes[:min_entities],
|
||||
:cooldown => attributes[:cooldown],
|
||||
:name => attributes[:name],
|
||||
:metadata => attributes[:metadata] || {}
|
||||
end
|
||||
|
||||
def build_server_launch_config(attributes)
|
||||
return nil unless attributes[:launch_config_type] == :launch_server
|
||||
args = {"server" => build_server_template(attributes) }
|
||||
args["loadBalancers"] = build_load_balancers(attributes) if attributes[:load_balancers]
|
||||
|
||||
Fog::Rackspace::AutoScale::LaunchConfig.new :type => :launch_server, :args => args
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def build_load_balancers(attributes)
|
||||
return nil unless attributes[:load_balancers]
|
||||
|
||||
load_balancers = attributes[:load_balancers].is_a?(Array) ? attributes[:load_balancers] : [attributes[:load_balancers]]
|
||||
load_balancers.collect do |obj|
|
||||
obj.is_a?(Hash) ? obj : load_balancer_to_hash(obj)
|
||||
end
|
||||
end
|
||||
|
||||
def load_balancer_to_hash(lb)
|
||||
raise ArgumentError.new("Expected LoadBalancer") unless lb.respond_to?(:id) && lb.respond_to?(:port)
|
||||
{
|
||||
"port" => lb.port,
|
||||
"loadBalancerId" => lb.id
|
||||
}
|
||||
end
|
||||
|
||||
def build_server_template(attributes)
|
||||
image_id = get_id(:image, attributes)
|
||||
flavor_id = get_id(:flavor, attributes)
|
||||
|
||||
server_template = {
|
||||
"name" => attributes[:server_name],
|
||||
"imageRef" => image_id,
|
||||
"flavorRef" => flavor_id,
|
||||
"OS-DCF =>diskConfig" => attributes[:disk_config] || "MANUAL",
|
||||
"metadata" => attributes[:server_metadata] || {}
|
||||
}
|
||||
|
||||
server_template["personality"] = attributes[:personality] if attributes[:personality]
|
||||
server_template["networks"] = networks_to_hash(attributes[:networks]) if attributes[:networks]
|
||||
server_template
|
||||
end
|
||||
|
||||
def get_id(type, attributes)
|
||||
id = attributes["#{type}_id".to_sym]
|
||||
type_key = type.to_sym
|
||||
id ||= attributes[type_key].respond_to?(:id) ? attributes[type_key].id : attributes[type_key]
|
||||
end
|
||||
|
||||
def networks_to_hash(networks)
|
||||
networks.collect {|n| {"uuid" => n}}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
144
tests/rackspace/models/auto_scale/group_builder_tests.rb
Normal file
144
tests/rackspace/models/auto_scale/group_builder_tests.rb
Normal file
|
@ -0,0 +1,144 @@
|
|||
require 'fog/rackspace/models/auto_scale/group'
|
||||
require 'fog/rackspace/models/compute_v2/flavor'
|
||||
require 'fog/rackspace/models/load_balancers/load_balancer'
|
||||
|
||||
Shindo.tests('Fog::Rackspace::AutoScale | group builder', ['rackspace', 'rackspace_autoscale']) do
|
||||
builder = Fog::Rackspace::AutoScale::GroupBuilder
|
||||
|
||||
tests('#get_id') do
|
||||
tests('widget_id').returns(5) do
|
||||
builder.send(:get_id, 'widget', {:widget_id => 5, :noise => 3})
|
||||
end
|
||||
tests('widget').returns(5) do
|
||||
Fog::Rackspace::AutoScale::GroupBuilder.send(:get_id, 'widget', {:widget => 5, :noise => 3})
|
||||
end
|
||||
tests('Flavor object').returns(2) do
|
||||
flavor = Fog::Compute::RackspaceV2::Flavor.new(:id => 2)
|
||||
builder.send(:get_id, 'flavor', {:flavor => flavor, :noise => 3})
|
||||
end
|
||||
end
|
||||
|
||||
tests('networks_to_hash').returns([{"uuid" => '00000000-0000-0000-0000-000000000000'}]) do
|
||||
builder.send(:networks_to_hash, ['00000000-0000-0000-0000-000000000000'])
|
||||
end
|
||||
|
||||
tests('#build_server_template').returns(LAUNCH_CONFIG_OPTIONS["args"]["server"]) do
|
||||
attributes = {
|
||||
:server_name => "autoscale_server",
|
||||
:image => "0d589460-f177-4b0f-81c1-8ab8903ac7d8",
|
||||
:flavor => "2",
|
||||
:disk_config => "AUTO",
|
||||
:server_metadata => {
|
||||
"build_config" => "core",
|
||||
"meta_key_1" => "meta_value_1",
|
||||
"meta_key_2" => "meta_value_2"
|
||||
},
|
||||
:networks => ["11111111-1111-1111-1111-111111111111", "00000000-0000-0000-0000-000000000000"],
|
||||
:personality => [
|
||||
{
|
||||
"path" => "/root/.csivh",
|
||||
"contents" => "VGhpcyBpcyBhIHRlc3QgZmlsZS4="
|
||||
}
|
||||
]
|
||||
}
|
||||
builder.send(:build_server_template, attributes)
|
||||
end
|
||||
|
||||
tests('#load_balancer_to_hash') do
|
||||
lb_test_hash = {
|
||||
"port" => 80,
|
||||
"loadBalancerId" => 1234
|
||||
}
|
||||
tests('hash').raises(ArgumentError, "Expected LoadBalancer") do
|
||||
builder.send(:load_balancer_to_hash, lb_test_hash)
|
||||
end
|
||||
tests('LoadBalancer').returns(lb_test_hash) do
|
||||
lb = Fog::Rackspace::LoadBalancers::LoadBalancer.new :port => 80, :id => 1234
|
||||
builder.send(:load_balancer_to_hash, lb)
|
||||
end
|
||||
end
|
||||
|
||||
tests('build_load_balancers') do
|
||||
lb_test_hash = {
|
||||
"port" => 80,
|
||||
"loadBalancerId" => 1234
|
||||
}
|
||||
tests('nil').returns(nil) do
|
||||
builder.send(:build_load_balancers, {})
|
||||
end
|
||||
tests('hash').returns([lb_test_hash]) do
|
||||
builder.send(:build_load_balancers, :load_balancers => [lb_test_hash])
|
||||
end
|
||||
tests('LoadBalancer').returns([lb_test_hash]) do
|
||||
lb = Fog::Rackspace::LoadBalancers::LoadBalancer.new :port => 80, :id => 1234
|
||||
builder.send(:build_load_balancers, :load_balancers => [lb])
|
||||
end
|
||||
tests('multiple lbs').returns([lb_test_hash, lb_test_hash]) do
|
||||
lb = Fog::Rackspace::LoadBalancers::LoadBalancer.new :port => 80, :id => 1234
|
||||
builder.send(:build_load_balancers, :load_balancers => [lb, lb])
|
||||
end
|
||||
end
|
||||
|
||||
tests('build_server_launch_config') do
|
||||
tests('no launch_config_type').returns(nil) do
|
||||
builder.build_server_launch_config({:pancakes => true})
|
||||
end
|
||||
tests('wrong launch_config_type').returns(nil) do
|
||||
builder.build_server_launch_config({:launch_config_type => :something_else})
|
||||
end
|
||||
tests('valid launch config').returns(LAUNCH_CONFIG_OPTIONS["args"]) do
|
||||
attributes = {
|
||||
:server_name => "autoscale_server",
|
||||
:image => "0d589460-f177-4b0f-81c1-8ab8903ac7d8",
|
||||
:flavor => "2",
|
||||
:disk_config => "AUTO",
|
||||
:server_metadata => {
|
||||
"build_config" => "core",
|
||||
"meta_key_1" => "meta_value_1",
|
||||
"meta_key_2" => "meta_value_2"
|
||||
},
|
||||
:networks => ["11111111-1111-1111-1111-111111111111", "00000000-0000-0000-0000-000000000000"],
|
||||
:personality => [
|
||||
{
|
||||
"path" => "/root/.csivh",
|
||||
"contents" => "VGhpcyBpcyBhIHRlc3QgZmlsZS4="
|
||||
}
|
||||
],
|
||||
:launch_config_type => :launch_server,
|
||||
:load_balancers => { "port" => 8080, "loadBalancerId" => 9099}
|
||||
}
|
||||
|
||||
builder.build_server_launch_config(attributes).args
|
||||
end
|
||||
end
|
||||
|
||||
tests('#build_group_config') do
|
||||
attributes = {
|
||||
:max_entities => 3,
|
||||
:min_entities => 0,
|
||||
:cooldown => 360,
|
||||
:name => "testscalinggroup198547",
|
||||
:metadata => {
|
||||
"gc_meta_key_2" => "gc_meta_value_2",
|
||||
"gc_meta_key_1" => "gc_meta_value_1"
|
||||
}
|
||||
}
|
||||
|
||||
config = builder.build_group_config(attributes)
|
||||
returns(3) { config.max_entities }
|
||||
returns(0) { config.min_entities }
|
||||
returns(360) { config.cooldown }
|
||||
returns("testscalinggroup198547") { config.name }
|
||||
returns(attributes[:metadata]) { config.metadata }
|
||||
end
|
||||
|
||||
tests('build') do
|
||||
pending if Fog.mocking?
|
||||
service = Fog::Rackspace::AutoScale.new :rackspace_region => :ord
|
||||
|
||||
group = builder.build(service, :launch_config_type => :launch_server, :server_name => 'test', :cooldown => 500)
|
||||
returns(500) { group.group_config.cooldown }
|
||||
returns('test') { group.launch_config.args["server"]["name"]}
|
||||
end
|
||||
|
||||
end
|
Loading…
Add table
Reference in a new issue