Class: Scalr
- Inherits:
-
Object
- Object
- Scalr
- Defined in:
- lib/rscalr/api/scalr.rb
Overview
Low-level Scalr API client. Instantiated with a hash containing config parameters (:key_id and :key_secret). All API methods are implemented as methods that return a ScalrResponse object. This object inherits from REXML::Document, making accessing the results pretty straightforward.
Unexpected errors (network, etc.) are also wrapped in an ad hoc ScalrResponse, with the transacation ID equal to ‘sig:’ + [signature used to sign the request] and the message equal to the error message.
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#apache_vhost_create(domain_name, farm_id, farm_role_id, document_root_dir, enable_ssl, ssl_private_key = nil, ssl_certificate = nil) ⇒ Object
API Section ===================================.
- #apache_vhosts_list ⇒ Object
-
#build_error_response(message, transaction_id) ⇒ Object
Builds an ad hoc ScalrResponse to represent an unexpected error.
- #bundle_task_get_status(bundle_task_id) ⇒ Object
- #dm_application_deploy(application_id, farm_role_id, remote_path) ⇒ Object
- #dm_applications_list ⇒ Object
- #dm_sources_list ⇒ Object
- #dns_zone_create(domain_name, farm_id = nil, farm_role_id = nil) ⇒ Object
- #dns_zone_record_add(zone_name, type, ttl, name, value, priority = nil, weight = nil, port = nil) ⇒ Object
- #dns_zone_record_remove(zone_name, record_id) ⇒ Object
- #dns_zone_records_list(zone_name) ⇒ Object
- #dns_zones_list ⇒ Object
-
#env_id ⇒ Object
Get the current environment value.
-
#env_id=(env_id) ⇒ Object
Changes the configured environment setting for this instance.
- #environments_list ⇒ Object
- #events_list(farm_id, start = nil, limit = nil) ⇒ Object
-
#execute_api_call(action, action_params = nil) ⇒ Object
Executes the specified API call, passing in the specified params.
- #farm_clone(farm_id) ⇒ Object
- #farm_get_details(farm_id) ⇒ Object
- #farm_get_stats(farm_id, date) ⇒ Object
- #farm_launch(farm_id) ⇒ Object
- #farm_role_parameters_list(farm_role_id) ⇒ Object
- #farm_role_update_parameter_value(farm_role_id, param_name, param_value) ⇒ Object
- #farm_terminate(farm_id, keep_ebs, keep_eip, keep_dns_zone) ⇒ Object
- #farms_list ⇒ Object
-
#generate_sig(action, timestamp) ⇒ Object
Generates request signature based on config, action, timestamp.
-
#generate_timestamp(time) ⇒ Object
Generates a timestamp string based on a Time object.
-
#hash_to_querystring(h) ⇒ Object
Turns a hash of request parameter key/value pairs into an escaped request query string.
-
#initialize(config = nil) ⇒ Scalr
constructor
A new instance of Scalr.
-
#load_config_from_environment ⇒ Object
Loads config hash based on environment variables.
- #logs_list(farm_id, server_id = nil, start = nil, limit = nil) ⇒ Object
- #roles_list(platform = nil, name = nil, prefix = nil, image_id = nil) ⇒ Object
- #script_execute(farm_id, script_id, timeout = 30, async = :no_async, farm_role_id = nil, server_id = nil, revsion = nil, config_vars = nil) ⇒ Object
- #script_get_details(script_id) ⇒ Object
- #scripting_logs_list(farm_id, server_id = nil, event_id = nil, start = nil, limit = nil) ⇒ Object
- #scripts_list ⇒ Object
- #server_image_create(server_id, role_name) ⇒ Object
- #server_launch(farm_role_id, increase_max_instances = nil) ⇒ Object
- #server_reboot(server_id) ⇒ Object
- #server_terminate(server_id, decrease_min_instances = nil) ⇒ Object
- #statistics_get_graph_url(object_type, object_id, watcher_name, graph_type) ⇒ Object
-
#url ⇒ Object
Get the current environment value.
-
#url=(url) ⇒ Object
Changes the configured API endpoint for this instance.
- #verbose=(setting) ⇒ Object
Constructor Details
#initialize(config = nil) ⇒ Scalr
Returns a new instance of Scalr.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rscalr/api/scalr.rb', line 20 def initialize(config=nil) if (config.nil?) @config = load_config_from_environment else @config = config end @config[:version] = $DEFAULT_VERSION if @config[:version].nil? @config[:auth_version] = $DEFAULT_AUTH_VERSION if @config[:auth_version].nil? @config[:url] = $DEFAULT_URL if @config[:url].nil? end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
13 14 15 |
# File 'lib/rscalr/api/scalr.rb', line 13 def config @config end |
Instance Method Details
#apache_vhost_create(domain_name, farm_id, farm_role_id, document_root_dir, enable_ssl, ssl_private_key = nil, ssl_certificate = nil) ⇒ Object
API Section ===================================
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rscalr/api/scalr.rb', line 34 def apache_vhost_create(domain_name, farm_id, farm_role_id, document_root_dir, enable_ssl, ssl_private_key=nil, ssl_certificate=nil) params = { :DomainName => domain_name, :FarmID => farm_id, :FarmRoleID => farm_role_id, :DocumentRootDir => document_root_dir, :EnableSSL => enable_ssl } params[:SSLPrivateKey] = ssl_private_key unless ssl_private_key.nil? params[:SSLCertificate] = ssl_certificate unless ssl_certificate.nil? execute_api_call('ApacheVhostCreate', params) end |
#apache_vhosts_list ⇒ Object
48 49 50 |
# File 'lib/rscalr/api/scalr.rb', line 48 def apache_vhosts_list execute_api_call('ApacheVhostsList') end |
#build_error_response(message, transaction_id) ⇒ Object
Builds an ad hoc ScalrResponse to represent an unexpected error
326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rscalr/api/scalr.rb', line 326 def build_error_response(, transaction_id) result = ScalrResponse.new "<?xml version='1.0?>" result.add_element("Error") ele = REXML::Element.new "TransactionID" ele.text = "sig:#{transaction_id}" result.root.elements << ele ele = REXML::Element.new "Message" ele.text = result.root.elements << ele result end |
#bundle_task_get_status(bundle_task_id) ⇒ Object
52 53 54 55 56 |
# File 'lib/rscalr/api/scalr.rb', line 52 def bundle_task_get_status(bundle_task_id) params = { :BundleTaskID => bundle_task_id } execute_api_call('BundleTaskGetStatus', params) end |
#dm_application_deploy(application_id, farm_role_id, remote_path) ⇒ Object
58 59 60 61 62 |
# File 'lib/rscalr/api/scalr.rb', line 58 def dm_application_deploy(application_id, farm_role_id, remote_path) params = { :ApplicationID => application_id, :FarmRoleID => farm_role_id, :RemotePath => remote_path } execute_api_call('DmApplicationDeploy', params) end |
#dm_applications_list ⇒ Object
64 65 66 |
# File 'lib/rscalr/api/scalr.rb', line 64 def dm_applications_list execute_api_call('DmApplicationsList') end |
#dm_sources_list ⇒ Object
68 69 70 |
# File 'lib/rscalr/api/scalr.rb', line 68 def dm_sources_list execute_api_call('DmSourcesList') end |
#dns_zone_create(domain_name, farm_id = nil, farm_role_id = nil) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/rscalr/api/scalr.rb', line 72 def dns_zone_create(domain_name, farm_id=nil, farm_role_id=nil) params = { :DomainName => domain_name } params[:FarmID] = farm_id unless farm_id.nil? params[:FarmRoleID] = farm_role_id unless farm_role_id.nil? execute_api_call('DNSZoneCreate', params) end |
#dns_zone_record_add(zone_name, type, ttl, name, value, priority = nil, weight = nil, port = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rscalr/api/scalr.rb', line 80 def dns_zone_record_add(zone_name, type, ttl, name, value, priority=nil, weight=nil, port=nil) params = { :ZoneName => zone_name, :Type => type, :TTL => ttl, :Name => name, :Value => value } params[:Priority] = priority unless priority.nil? params[:Weight] = weight unless weight.nil? params[:Port] = port unless port.nil? execute_api_call('DNSZoneRecordAdd', params) end |
#dns_zone_record_remove(zone_name, record_id) ⇒ Object
95 96 97 98 99 |
# File 'lib/rscalr/api/scalr.rb', line 95 def dns_zone_record_remove(zone_name, record_id) params = { :ZoneName => zone_name, :RecordID => record_id } execute_api_call('DNSZoneRecordRemove', params) end |
#dns_zone_records_list(zone_name) ⇒ Object
101 102 103 104 105 |
# File 'lib/rscalr/api/scalr.rb', line 101 def dns_zone_records_list(zone_name) params = { :ZoneName => zone_name } execute_api_call('DNSZoneRecordsList', params) end |
#dns_zones_list ⇒ Object
107 108 109 |
# File 'lib/rscalr/api/scalr.rb', line 107 def dns_zones_list execute_api_call('DNSZonesList') end |
#env_id ⇒ Object
Get the current environment value. A nil response means the “first” environment will be assumed for API calls, per the Scalr API docs.
352 353 354 |
# File 'lib/rscalr/api/scalr.rb', line 352 def env_id @config[:env_id] end |
#env_id=(env_id) ⇒ Object
Changes the configured environment setting for this instance
346 347 348 |
# File 'lib/rscalr/api/scalr.rb', line 346 def env_id= env_id @config[:env_id] = env_id end |
#environments_list ⇒ Object
111 112 113 |
# File 'lib/rscalr/api/scalr.rb', line 111 def environments_list execute_api_call('EnvironmentsList') end |
#events_list(farm_id, start = nil, limit = nil) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/rscalr/api/scalr.rb', line 115 def events_list(farm_id, start=nil, limit=nil) params = { :FarmID => farm_id } params[:StartFrom] = start unless start.nil? params[:RecordsLimit] = limit unless limit.nil? execute_api_call('EventsList', params) end |
#execute_api_call(action, action_params = nil) ⇒ Object
Executes the specified API call, passing in the specified params
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/rscalr/api/scalr.rb', line 289 def execute_api_call(action, action_params=nil) begin params = { :Action => action, :TimeStamp => (Time.now) } params.merge!(action_params) unless action_params.nil? params[:Signature] = generate_sig(action, params[:TimeStamp]) params[:Version] = @config[:version] params[:AuthVersion] = @config[:auth_version] params[:KeyID] = @config[:key_id] params[:EnvID] = @config[:env_id] unless @config[:env_id].nil? uri = URI(url + "?" + hash_to_querystring(params)) $stdout.puts(uri) if @config[:verbose] response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| request = Net::HTTP::Get.new uri.request_uri http.request request # Net::HTTPResponse object end case response when Net::HTTPSuccess then result = ScalrResponse.new response.body else result = build_error_response(response, params[:Signature]) end rescue => ex result = build_error_response(ex., params[:Signature]) end $stdout.puts(result.pretty_print) if @config[:verbose] result end |
#farm_clone(farm_id) ⇒ Object
123 124 125 126 127 |
# File 'lib/rscalr/api/scalr.rb', line 123 def farm_clone(farm_id) params = { :FarmID => farm_id } execute_api_call('FarmClone', params) end |
#farm_get_details(farm_id) ⇒ Object
181 182 183 |
# File 'lib/rscalr/api/scalr.rb', line 181 def farm_get_details(farm_id) execute_api_call('FarmGetDetails', { :FarmID => farm_id }) end |
#farm_get_stats(farm_id, date) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/rscalr/api/scalr.rb', line 129 def farm_get_stats(farm_id, date) params = { :FarmID => farm_id } params[:Date] = date.strftime("%m-%Y") unless date.nil? execute_api_call('FarmGetStats', params) end |
#farm_launch(farm_id) ⇒ Object
136 137 138 139 140 |
# File 'lib/rscalr/api/scalr.rb', line 136 def farm_launch(farm_id) params = { :FarmID => farm_id } execute_api_call('FarmLaunch', params) end |
#farm_role_parameters_list(farm_role_id) ⇒ Object
232 233 234 |
# File 'lib/rscalr/api/scalr.rb', line 232 def farm_role_parameters_list(farm_role_id) execute_api_call('FarmRoleParametersList', { :FarmRoleID => farm_role_id }) end |
#farm_role_update_parameter_value(farm_role_id, param_name, param_value) ⇒ Object
236 237 238 |
# File 'lib/rscalr/api/scalr.rb', line 236 def farm_role_update_parameter_value(farm_role_id, param_name, param_value) execute_api_call('FarmRoleUpdateParameterValue', { :FarmRoleID => farm_role_id, :ParamName => param_name, :ParamValue => param_value }) end |
#farm_terminate(farm_id, keep_ebs, keep_eip, keep_dns_zone) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rscalr/api/scalr.rb', line 147 def farm_terminate(farm_id, keep_ebs, keep_eip, keep_dns_zone) params = { :FarmID => farm_id, :KeepEBS => (keep_ebs ? 1 : 0), :KeepEIP => (keep_eip ? 1 : 0), :KeepDNSZone => (keep_dns_zone ? 1 : 0) } execute_api_call('FarmTerminate', params) end |
#farms_list ⇒ Object
142 143 144 145 |
# File 'lib/rscalr/api/scalr.rb', line 142 def farms_list execute_api_call('FarmsList') end |
#generate_sig(action, timestamp) ⇒ Object
Generates request signature based on config, action, timestamp
277 278 279 280 281 |
# File 'lib/rscalr/api/scalr.rb', line 277 def generate_sig(action, ) = action + ':' + @config[:key_id] + ':' + hexdigest = OpenSSL::HMAC.hexdigest('sha256', @config[:key_secret], ) [[hexdigest].pack("H*")].pack("m0") end |
#generate_timestamp(time) ⇒ Object
Generates a timestamp string based on a Time object
284 285 286 |
# File 'lib/rscalr/api/scalr.rb', line 284 def (time) time.strftime("%Y-%m-%dT%H:%M:%SZ") end |
#hash_to_querystring(h) ⇒ Object
Turns a hash of request parameter key/value pairs into an escaped request query string. Keys are expected to not require escaping.
341 342 343 |
# File 'lib/rscalr/api/scalr.rb', line 341 def hash_to_querystring(h) h.map{|k,v| "#{k.to_s}=#{CGI::escape(v.to_s)}"}.join('&') end |
#load_config_from_environment ⇒ Object
Loads config hash based on environment variables
264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/rscalr/api/scalr.rb', line 264 def load_config_from_environment config = {} config[:key_id] = ENV['SCALR_API_KEY'] unless ENV['SCALR_API_KEY'].nil? config[:key_secret] = ENV['SCALR_API_SECRET'] unless ENV['SCALR_API_SECRET'].nil? config[:url] = ENV['SCALR_URL'] unless ENV['SCALR_URL'].nil? config[:env_id] = ENV['SCALR_ENV_ID'] unless ENV['SCALR_ENV_ID'].nil? config[:version] = ENV['SCALR_API_VERSION'] unless ENV['SCALR_API_VERSION'].nil? config[:auth_version] = ENV['SCALR_API_AUTH_VERSION'] unless ENV['SCALR_API_AUTH_VERSION'].nil? config[:verbose] = true if (ENV['SCALR_CLIENT_VERBOSE'] == "true" || ENV['SCALR_CLIENT_VERBOSE'] == "1") config end |
#logs_list(farm_id, server_id = nil, start = nil, limit = nil) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/rscalr/api/scalr.rb', line 158 def logs_list(farm_id, server_id=nil, start=nil, limit=nil) params = { :FarmID => farm_id } params[:ServerID] = server_id unless server_id.nil? params[:StartFrom] = start unless start.nil? params[:RecordsLimit] = limit unless limit.nil? execute_api_call('LogsList', params) end |
#roles_list(platform = nil, name = nil, prefix = nil, image_id = nil) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/rscalr/api/scalr.rb', line 167 def roles_list(platform=nil, name=nil, prefix=nil, image_id=nil) params = {} params[:Platform] = platform unless platform.nil? params[:Name] = name unless name.nil? params[:Prefix] = prefix unless prefix.nil? params[:ImageID] = image_id unless image_id.nil? execute_api_call('RolesList', params) end |
#script_execute(farm_id, script_id, timeout = 30, async = :no_async, farm_role_id = nil, server_id = nil, revsion = nil, config_vars = nil) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rscalr/api/scalr.rb', line 185 def script_execute(farm_id, script_id, timeout=30, async=:no_async, farm_role_id=nil, server_id=nil, revsion=nil, config_vars=nil) async_int = 0 async_int = 1 if async == :async params = { :FarmID => farm_id, :ScriptID => script_id, :Timeout => timeout, :Async => async_int } params[:FarmRoleID] = farm_role_id unless farm_role_id.nil? params[:ServerID] = server_id unless server_id.nil? params[:Revision] = revsion unless revsion.nil? if config_vars != nil config_vars.each {|key, value| params["ConfigVariables[#{CGI::escape(key)}]"] = value } end execute_api_call('ScriptExecute', params) end |
#script_get_details(script_id) ⇒ Object
202 203 204 |
# File 'lib/rscalr/api/scalr.rb', line 202 def script_get_details(script_id) execute_api_call('ScriptGetDetails', { :ScriptID => script_id }) end |
#scripting_logs_list(farm_id, server_id = nil, event_id = nil, start = nil, limit = nil) ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'lib/rscalr/api/scalr.rb', line 240 def scripting_logs_list(farm_id, server_id=nil, event_id=nil, start=nil, limit=nil) params = { :FarmID => farm_id } params[:ServerID] = server_id unless server_id.nil? params[:EventID] = event_id unless event_id.nil? params[:StartFrom] = start unless start.nil? params[:RecordsLimit] = limit unless limit.nil? execute_api_call('ScriptingLogsList', params) end |
#scripts_list ⇒ Object
177 178 179 |
# File 'lib/rscalr/api/scalr.rb', line 177 def scripts_list execute_api_call('ScriptsList') end |
#server_image_create(server_id, role_name) ⇒ Object
206 207 208 209 210 |
# File 'lib/rscalr/api/scalr.rb', line 206 def server_image_create(server_id, role_name) params = { :ServerID => server_id, :RoleName => role_name } execute_api_call('ServerImageCreate', params) end |
#server_launch(farm_role_id, increase_max_instances = nil) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/rscalr/api/scalr.rb', line 212 def server_launch(farm_role_id, increase_max_instances=nil) params = { :FarmRoleID => farm_role_id } params[:IncreaseMaxInstances] = (increase_max_instances ? 1 : 0) unless increase_max_instances.nil? execute_api_call('ServerLaunch', params) end |
#server_reboot(server_id) ⇒ Object
219 220 221 222 223 |
# File 'lib/rscalr/api/scalr.rb', line 219 def server_reboot(server_id) params = { :ServerID => server_id } execute_api_call('ServerReboot', params) end |
#server_terminate(server_id, decrease_min_instances = nil) ⇒ Object
225 226 227 228 229 230 |
# File 'lib/rscalr/api/scalr.rb', line 225 def server_terminate(server_id, decrease_min_instances=nil) params = { :ServerID => server_id } params[:DecreaseMinInstancesSetting] = (decrease_min_instances ? 1 : 0) unless decrease_min_instances.nil? execute_api_call('ServerTerminate', params) end |
#statistics_get_graph_url(object_type, object_id, watcher_name, graph_type) ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/rscalr/api/scalr.rb', line 250 def statistics_get_graph_url(object_type, object_id, watcher_name, graph_type) params = { :ObjectType => object_type, :ObjectID => object_id, :WatcherName => watcher_name, :GraphType => graph_type } execute_api_call('StatisticsGetGraphURL', params) end |
#url ⇒ Object
Get the current environment value. A nil response means the “first” environment will be assumed for API calls, per the Scalr API docs.
363 364 365 |
# File 'lib/rscalr/api/scalr.rb', line 363 def url @config[:url] end |
#url=(url) ⇒ Object
Changes the configured API endpoint for this instance
357 358 359 |
# File 'lib/rscalr/api/scalr.rb', line 357 def url= url @config[:url] = url end |
#verbose=(setting) ⇒ Object
367 368 369 370 371 |
# File 'lib/rscalr/api/scalr.rb', line 367 def verbose= setting if !!setting == setting @config[:verbose] = !!setting end end |