scalr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Matt Hodgson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = scalr
2
+
3
+ Scalr is a cloud infrastructure management provider. This gem is for interfacing with the Scalr.net API to obtain information about your instances and farms.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Matt Hodgson. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "scalr"
8
+ gem.summary = %Q{A Scalr API wrapper gem}
9
+ gem.description = %Q{Scalr is a cloud infrastructure management provider. This gem is for interfacing with the Scalr.net API to obtain information about your instances and farms.}
10
+ gem.email = "mhodgson@youcastr.com"
11
+ gem.homepage = "http://github.com/mhodgson/scalr"
12
+ gem.authors = ["Matt Hodgson"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_dependency "activesupport", ">= 0"
15
+ gem.add_dependency "ruby-hmac", ">= 0.4.0"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "scalr #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/scalr.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ require File.dirname(__FILE__) + '/scalr/response'
5
+ require File.dirname(__FILE__) + '/scalr/request'
6
+ require File.dirname(__FILE__) + '/scalr/core_extensions/hash'
7
+ require File.dirname(__FILE__) + '/scalr/core_extensions/http'
8
+
9
+ module Scalr
10
+
11
+ mattr_accessor :endpoint
12
+ @@endpoint = "api.scalr.net"
13
+
14
+ mattr_accessor :key_id
15
+ @@key_id = nil
16
+
17
+ mattr_accessor :access_key
18
+ @@access_key = nil
19
+
20
+ mattr_accessor :version
21
+ @@version = "2009-05-07"
22
+
23
+ class << self
24
+
25
+ def method_missing(method_id, *arguments)
26
+ if matches_action? method_id
27
+ request = Scalr::Request.new(method_id, @@endpoint, @@key_id, @@access_key, @@version, arguments)
28
+ return request.process!
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def matches_action?(method_id)
37
+ Scalr::Request::ACTIONS.keys.include? method_id.to_sym
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,21 @@
1
+ class Hash
2
+
3
+ def downcase_keys
4
+ inject({}) do |options, (key, value)|
5
+ options[(key.downcase.to_sym rescue key) || key] = value
6
+ options
7
+ end
8
+ end
9
+
10
+ def downcase_keys!
11
+ self.replace(self.downcase_keys)
12
+ end
13
+
14
+ def recursive_downcase_keys!
15
+ downcase_keys!
16
+ values.each{|h| h.recursive_downcase_keys! if h.is_a?(Hash) }
17
+ values.select{|v| v.is_a?(Array) }.flatten.each{|h| h.recursive_downcase_keys! if h.is_a?(Hash) }
18
+ self
19
+ end
20
+
21
+ end
@@ -0,0 +1,10 @@
1
+ class Net::HTTP
2
+
3
+ alias_method :old_initialize, :initialize
4
+ def initialize(*args)
5
+ old_initialize(*args)
6
+ @ssl_context = OpenSSL::SSL::SSLContext.new
7
+ @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
8
+ end
9
+
10
+ end
@@ -0,0 +1,111 @@
1
+ require 'uri'
2
+ require 'hmac'
3
+ require 'hmac-sha2'
4
+ require 'base64'
5
+ require 'net/https'
6
+ require 'net/http'
7
+
8
+ module Scalr
9
+ class Request
10
+ class ScalrError < RuntimeError; end
11
+ class InvalidInputError < ScalrError; end
12
+
13
+ ACTIONS = {
14
+ :add_dns_zone_record => {:name => 'AddDNSZoneRecord', :inputs => {:domain_name => true, :type => true, :ttl => true, :key => true, :value => true, :priority => false, :weight => false, :port => false}},
15
+ :execute_script => {:name => 'ExecuteScript', :inputs => {:farm_role_id => false, :instance_id => false, :farm_id => true, :script_id => true, :timeout => true, :async => true, :revision => false, :config_variables => false}},
16
+ :get_events => {:name => 'GetEvents', :inputs => {:farm_id => true, :start_from => false, :records_limit => false}},
17
+ :get_farm_details => {:name => 'GetFarmDetails', :inputs => {:farm_id => true}},
18
+ :get_farm_stats => {:name => 'GetFarmStats', :inputs => {:farm_id => true, :date => false}},
19
+ :get_logs => {:name => 'GetLogs', :inputs => {:farm_id => true, :instance_id => true, :start_from => false, :records_limit => false}},
20
+ :get_script_details => {:name => 'GetScriptDetails', :inputs => {:script_id => true}},
21
+ :launch_farm => {:name => 'LaunchFarm', :inputs => {:farm_id => true}},
22
+ :launch_instance => {:name => 'LaunchInstance', :inputs => {:farm_role_id => true}},
23
+ :list_applications => {:name => 'ListApplications', :inputs => {}},
24
+ :list_dns_zone_records => {:name => 'ListDNSZoneRecords', :inputs => {:domain_name => true}},
25
+ :list_dns_zones => {:name => 'ListDNSZones', :inputs => {}},
26
+ :list_farms => {:name => 'ListFarms', :inputs => {}},
27
+ :list_roles => {:name => 'ListRoles', :inputs => {:region => true, :name => false, :prefix => false, :ami_id => false}},
28
+ :list_scripts => {:name => 'ListScripts', :inputs => {}},
29
+ :reboot_instance => {:name => 'RebootInstance', :inputs => {:farm_id => true, :instance_id => true}},
30
+ :remove_dns_zone_record => {:name => 'RemoveDNSZoneRecord', :inputs => {:domain_name => true, :record_id => true}},
31
+ :terminate_farm => {:name => 'TerminateFarm', :inputs => {:farm_id => true, :keep_ebs => true, :keep_eip => true, :keep_dns_zone => true}},
32
+ :terminate_instance => {:name => 'TerminateInstance', :inputs => {:farm_id => true, :instance_id => true, :keep_eip => true, :decrease_min_instances_setting => false}}
33
+ }
34
+
35
+ INPUTS = {
36
+ :domain_name => 'DomainName',
37
+ :type => 'Type',
38
+ :ttl => 'TTL',
39
+ :key => 'Key',
40
+ :value => 'Value',
41
+ :priority => 'Priority',
42
+ :weight => 'Weight',
43
+ :port => 'Port',
44
+ :farm_role_id => 'FarmRoleID',
45
+ :instance_id => 'InstanceID',
46
+ :farm_id => 'FarmID',
47
+ :script_id => 'ScriptID',
48
+ :timeout => 'Timeout',
49
+ :async => 'Async',
50
+ :revision => 'Revision',
51
+ :config_variables => 'ConfigVariables',
52
+ :start_from => 'StartFrom',
53
+ :records_limit => 'RecordsLimit',
54
+ :date => 'Date',
55
+ :domain_name => 'DomainName',
56
+ :region => 'Region',
57
+ :name => 'Name',
58
+ :prefix => 'Prefix',
59
+ :ami_id => 'AmiID',
60
+ :record_id => 'RecordID',
61
+ :keep_ebs => 'KeepEBS',
62
+ :keep_eip => 'KeepEIP',
63
+ :keep_dns_zone => 'KeepDNSZone',
64
+ :decrease_min_instances_setting => 'DecreaseMinInstancesSetting'
65
+ }
66
+
67
+ attr_accessor :inputs, :endpoint, :access_key, :signature
68
+
69
+ def initialize(action, endpoint, key_id, access_key, version, *arguments)
70
+ set_inputs(action, arguments.flatten.first)
71
+ @inputs.merge!('Action' => ACTIONS[action.to_sym][:name], 'KeyID' => key_id, 'Version' => version, 'Timestamp' => Time.now.utc.iso8601)
72
+ @endpoint = endpoint
73
+ @access_key = access_key
74
+ end
75
+
76
+ def process!
77
+ set_signature!
78
+ http = Net::HTTP.new(@endpoint, 443)
79
+ http.use_ssl = true
80
+ response, data = http.get("/?" + query_string + "&Signature=#{URI.escape(@signature)}", nil)
81
+ return Scalr::Response.new(response, data)
82
+ end
83
+
84
+ private
85
+
86
+ def set_inputs(action, input_hash)
87
+ input_hash ||= {}
88
+ raise InvalidInputError.new unless input_hash.is_a? Hash
89
+ ACTIONS[action][:inputs].each do |key, value|
90
+ raise InvalidInputError.new("Missing required input: #{key.to_s}") if value and input_hash[key].nil?
91
+ end
92
+ @inputs = {}
93
+ input_hash.each do |key, value|
94
+ raise InvalidInputError.new("Unknown input: #{key.to_s}") if ACTIONS[action][:inputs][key].nil?
95
+ @inputs[INPUTS[key]] = value.to_s
96
+ end
97
+ end
98
+
99
+ def query_string
100
+ @inputs.sort.collect { |key, value| [URI.escape(key.to_s), URI.escape(value.to_s)].join('=') }.join('&')
101
+ end
102
+
103
+ def set_signature!
104
+ string_to_sign = query_string.gsub('=','').gsub('&','')
105
+ hmac = HMAC::SHA256.new(@access_key)
106
+ hmac.update(string_to_sign)
107
+ @signature = Base64.encode64(hmac.digest).chomp
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,36 @@
1
+ require 'rexml/document'
2
+
3
+ module Scalr
4
+ class Response
5
+
6
+ attr_accessor :code, :message, :value, :error
7
+
8
+ def initialize(response, data)
9
+ @code = response.code
10
+ @message = response.message
11
+ if successful_request?
12
+ @value = parse(data)
13
+ @error = @value[:error][:message] if !success?
14
+ end
15
+ end
16
+
17
+ def successful_request?
18
+ (@code == '200')
19
+ end
20
+
21
+ def success?
22
+ (successful_request? && @value[:error].nil?)
23
+ end
24
+
25
+ def failed?
26
+ !success?
27
+ end
28
+
29
+ private
30
+
31
+ def parse(data)
32
+ Hash.from_xml(data).recursive_downcase_keys!
33
+ end
34
+
35
+ end
36
+ end
data/scalr.gemspec ADDED
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{scalr}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Matt Hodgson"]
12
+ s.date = %q{2010-02-06}
13
+ s.description = %q{Scalr is a cloud infrastructure management provider. This gem is for interfacing with the Scalr.net API to obtain information about your instances and farms.}
14
+ s.email = %q{mhodgson@youcastr.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/scalr.rb",
27
+ "lib/scalr/core_extensions/hash.rb",
28
+ "lib/scalr/core_extensions/http.rb",
29
+ "lib/scalr/request.rb",
30
+ "lib/scalr/response.rb",
31
+ "scalr.gemspec",
32
+ "test/helper.rb",
33
+ "test/test_scalr.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/mhodgson/scalr}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.5}
39
+ s.summary = %q{A Scalr API wrapper gem}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_scalr.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
52
+ s.add_runtime_dependency(%q<ruby-hmac>, [">= 0.4.0"])
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ s.add_dependency(%q<activesupport>, [">= 0"])
56
+ s.add_dependency(%q<ruby-hmac>, [">= 0.4.0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ s.add_dependency(%q<activesupport>, [">= 0"])
61
+ s.add_dependency(%q<ruby-hmac>, [">= 0.4.0"])
62
+ end
63
+ end
64
+
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'scalr'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestScalr < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scalr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Hodgson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-06 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: ruby-hmac
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.4.0
44
+ version:
45
+ description: Scalr is a cloud infrastructure management provider. This gem is for interfacing with the Scalr.net API to obtain information about your instances and farms.
46
+ email: mhodgson@youcastr.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/scalr.rb
62
+ - lib/scalr/core_extensions/hash.rb
63
+ - lib/scalr/core_extensions/http.rb
64
+ - lib/scalr/request.rb
65
+ - lib/scalr/response.rb
66
+ - scalr.gemspec
67
+ - test/helper.rb
68
+ - test/test_scalr.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/mhodgson/scalr
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options:
75
+ - --charset=UTF-8
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.5
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: A Scalr API wrapper gem
97
+ test_files:
98
+ - test/helper.rb
99
+ - test/test_scalr.rb