Skip to content

Instantly share code, notes, and snippets.

@lackac
Created October 5, 2012 08:48
Show Gist options
  • Save lackac/3838808 to your computer and use it in GitHub Desktop.
Save lackac/3838808 to your computer and use it in GitHub Desktop.
Script for upgrading the number of CPU cores and the amount of memory in SoftLayer Computing instances
#!/usr/bin/env ruby
require 'rubygems'
require 'softlayer_api'
if ARGV.size != 3
STDERR.puts "USAGE: #{$0} hostname cores mem"
exit 1
end
hostname, cores, mem = ARGV
cores = cores.to_i
mem = mem.to_i
def client(service)
service_options = {
:username => ENV['SL_USERNAME'],
:api_key => ENV['SL_API_KEY']
}
if %x{hostname -f}.chomp =~ /.private$/
service_options[:endpoint_url] = SoftLayer::API_PRIVATE_ENDPOINT
end
SoftLayer::Service.new "SoftLayer_#{service}", service_options
end
## Find the guest by hostname
guests = client('Account').object_mask('id', 'hostname').getHourlyVirtualGuests
unless guest = guests.detect {|g| g['hostname'] == hostname}
STDERR.puts "Cannot find host #{hostname}"
exit 2
end
## Get price ids
items = client('Product_Package').object_with_id(46).object_mask('description', 'capacity', {'prices' => 'id'}, {'categories' => 'id'}).getItems
prices = items.map do |item|
if (item['categories'].detect {|c| c['id'] == 80} and item['capacity'] == cores.to_s and item['description'] !~ /^Private/) or
(item['categories'].detect {|c| c['id'] == 3} and item['capacity'] == mem.to_s)
item['prices'].first
end
end.compact
## Submit upgrade order
order = {
'complexType' => 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade',
'virtualGuests' => [guest],
'prices' => prices,
# MAINTENANCE_WINDOW will be parsed with PHP's strtotime()
'properties' => [{ :name => 'MAINTENANCE_WINDOW', :value => 'now'}]
}
order_client = client('Product_Order')
begin
order_client.verifyOrder(order)
order_client.placeOrder(order)
rescue SoftLayer::SoftLayerAPIException => err
STDERR.puts "There has been a problem with the upgrade request:\n #{err}"
exit 3
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment