ggoodale-rackspace_cloud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Cosmin Radoi
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 ADDED
@@ -0,0 +1,21 @@
1
+ Example:
2
+
3
+ >> require 'rackspace_cloud'
4
+ >> cloud = RackspaceCloud::Base.new(:user => "gomer", :access_key => "aabbccddaabbccddaabbccdd")
5
+ => #<RackspaceCloud::Base:0x1065f44 @access_key="aabbccddaabbccddaabbccdd", @user="gomer">
6
+ >> cloud.connect
7
+ => nil
8
+ >> cloud.servers
9
+ => []
10
+ >> new_server = cloud.create_server('development', 1, 8) # create a 256MB Ubunty 9.04 instance)
11
+ => #<RackspaceCloud::Server:0x1054a78 @image=#<RackspaceCloud::Image:0x10598e8 @name="Ubuntu 9.04 (jaunty)", @updated="2009-07-20T09:14:37-05:00", @created="2009-07-20T09:14:37-05:00", @status="ACTIVE", @rackspace_id=8>, @public_ips=["174.xxx.xxx.xxx"], @flavor=#<RackspaceCloud::Flavor:0x105d40c @name="256 slice", @ram=256, @disk=10, @rackspace_id=1>, @name="development", @progress=80, @adminPass="developmentU5fG3", @host_id="a7765fde333eead221", @private_ips=["10.xxx.xxx.xxx"], @rackspace_id=1, @status="BUILD">
12
+
13
+ ... wait a while ...
14
+
15
+ >> new_server.refresh.status
16
+ => "ACTIVE"
17
+ >> new_server.hard_reboot
18
+ => nil
19
+ >> new_server.refresh.status
20
+ => "HARD_REBOOT"
21
+
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.pattern = 'test/**/*_test.rb'
10
+ test.verbose = true
11
+ end
12
+
13
+ begin
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gemspec|
16
+ gemspec.name = "rackspace_cloud"
17
+ gemspec.summary = "Gem enabling the management of Rackspace Cloud Server instances"
18
+ gemspec.description = "Gem enabling the management of Rackspace Cloud Server instances."
19
+ gemspec.email = "grant@moreblinktag.com"
20
+ gemspec.homepage = "http://github.com/ggoodale/rackspace_cloud"
21
+ gemspec.authors = ["Grant Goodale"]
22
+ gemspec.files.exclude("**/.gitignore")
23
+ gemspec.add_dependency('patron', '>= 0.4.0')
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
File without changes
data/install.rb ADDED
File without changes
@@ -0,0 +1,44 @@
1
+ module RackspaceCloud
2
+ class Base
3
+ attr_reader :user, :access_key, :auth_token
4
+
5
+ def initialize(config={})
6
+ validate_config(config)
7
+ @user = config[:user]
8
+ @access_key = config[:access_key]
9
+ end
10
+
11
+ def connect
12
+ RackspaceCloud.request_authorization(@user, @access_key)
13
+ RackspaceCloud.populate_flavors
14
+ RackspaceCloud.populate_images
15
+ end
16
+
17
+ def servers
18
+ RackspaceCloud.request("/servers/detail")["servers"].collect {|server_json|
19
+ RackspaceCloud::Server.new(server_json)
20
+ }
21
+ end
22
+
23
+ def create_server(name, flavor, image, metadata={}, personality=[])
24
+ new_server_data = {'server' => {
25
+ 'name' => name,
26
+ 'flavorId' => flavor.to_i,
27
+ 'imageId' => image.to_i,
28
+ 'metadata' => metadata,
29
+ 'personality' => personality
30
+ }}
31
+
32
+ RackspaceCloud::Server.new(RackspaceCloud.request("/servers", :method => :post, :data => new_server_data)['server'])
33
+ end
34
+
35
+ protected
36
+
37
+ def validate_config(config)
38
+ errors = []
39
+ (config[:user] && config[:user].length) || errors << "missing username"
40
+ (config[:access_key] && config[:access_key].length) || errors << "missing access_key"
41
+ raise(ArgumentError, "Error: invalid configuration: #{errors.join(';')}") unless errors.empty?
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,23 @@
1
+ module RackspaceCloud
2
+ class Flavor
3
+ attr_reader :name, :disk, :ram, :rackspace_id
4
+
5
+ class << self
6
+ def lookup_by_id(id)
7
+ RackspaceCloud::FLAVORS[id]
8
+ end
9
+ end
10
+
11
+ def initialize(flavor_json)
12
+ @rackspace_id = flavor_json['id']
13
+ @name = flavor_json['name']
14
+ @disk = flavor_json['disk']
15
+ @ram = flavor_json['ram']
16
+ end
17
+
18
+ def to_i
19
+ @rackspace_id
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,26 @@
1
+ module RackspaceCloud
2
+ class Image
3
+ attr_reader :name, :created, :updated, :status, :rackspace_id
4
+
5
+ class << self
6
+ def lookup_by_id(id)
7
+ RackspaceCloud::IMAGES[id]
8
+ end
9
+ end
10
+
11
+ def initialize(image_json)
12
+ @rackspace_id = image_json['id']
13
+ @name = image_json['name']
14
+ @created = image_json['created']
15
+ @updated = image_json['updated']
16
+ @status = image_json['status']
17
+ end
18
+
19
+ def to_i
20
+ @rackspace_id
21
+ end
22
+
23
+ def refresh
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,70 @@
1
+ module RackspaceCloud
2
+ class Server
3
+ attr_reader :name, :status, :flavor, :image
4
+ attr_reader :rackspace_id, :host_id, :progress
5
+ attr_reader :public_ips, :private_ips, :adminPass
6
+
7
+ STATUS_VALUES = %w{ACTIVE BUILD REBUILD SUSPENDED QUEUE_RESIZE PREP_RESIZE VERIFY_RESIZE PASSWORD RESCUE REBOOT HARD_REBOOT SHARE_IP SHARE_IP_NO_CONFIG DELETE_IP UNKNOWN}
8
+
9
+ def initialize(server_json)
10
+ populate(server_json)
11
+ end
12
+
13
+ def ready?
14
+ @status == 'ACTIVE'
15
+ end
16
+
17
+ def reboot
18
+ action_request('reboot' => {'type' => 'SOFT'})
19
+ end
20
+
21
+ def hard_reboot
22
+ action_request('reboot' => {'type' => 'HARD'})
23
+ end
24
+
25
+ def rebuild(new_image=nil)
26
+ action_request('rebuild' => {'imageId' => new_image ? new_image.to_i : image.to_i})
27
+ end
28
+
29
+ def resize(new_flavor)
30
+ action_request('resize' => {'flavorId' => new_flavor.to_i})
31
+ end
32
+
33
+ def confirm_resize
34
+ action_request('confirmResize' => nil)
35
+ end
36
+
37
+ def revert_resize
38
+ action_request('revertResize' => nil)
39
+ end
40
+
41
+ def delete
42
+ RackspaceCloud.request("/servers/#{@rackspace_id}", :method => :delete)
43
+ end
44
+
45
+ # update this server's status and progress by calling /servers/<id>
46
+ def refresh
47
+ populate(RackspaceCloud.request("/servers/#{@rackspace_id}")['server'])
48
+ self
49
+ end
50
+
51
+ protected
52
+
53
+ def action_request(data)
54
+ RackspaceCloud.request("/servers/#{@rackspace_id}/action", :method => :post, :data => data)
55
+ end
56
+
57
+ def populate(server_json)
58
+ @name = server_json['name']
59
+ @status = server_json['status']
60
+ @flavor = RackspaceCloud::Flavor.lookup_by_id(server_json['flavorId'])
61
+ @image = RackspaceCloud::Image.lookup_by_id(server_json['imageId'])
62
+ @rackspace_id = server_json['id']
63
+ @host_id = server_json['hostId']
64
+ @progress = server_json['progress']
65
+ @public_ips = server_json['addresses']['public']
66
+ @private_ips = server_json['addresses']['private']
67
+ @adminPass = server_json['adminPass']
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,88 @@
1
+ require "patron"
2
+ require "json"
3
+ require "uri"
4
+ require "digest/sha1"
5
+ require "base64"
6
+
7
+ # Bring in all of our submodules
8
+ Dir[File.join(File.dirname(__FILE__), 'rackspace_cloud/**/*.rb')].sort.each { |lib| require lib }
9
+
10
+ module RackspaceCloud
11
+
12
+ class ConfigurationError < StandardError; end
13
+ class AuthorizationError < StandardError; end
14
+
15
+ API_VERSION = "1.0"
16
+ BASE_AUTH_URI = "https://auth.api.rackspacecloud.com"
17
+
18
+ def RackspaceCloud.auth_url
19
+ @@auth_uri ||= URI.parse("#{BASE_AUTH_URI}/v#{API_VERSION}")
20
+ end
21
+
22
+ protected
23
+
24
+ # storage for the lists of flavors and images we request at auth time
25
+ FLAVORS = {}
26
+ def RackspaceCloud.populate_flavors
27
+ request("/flavors/detail")['flavors'].each do |flavor|
28
+ FLAVORS[flavor['id']] = RackspaceCloud::Flavor.new(flavor)
29
+ end
30
+ nil
31
+ end
32
+
33
+ IMAGES = {}
34
+ def RackspaceCloud.populate_images
35
+ request("/images/detail")['images'].each do |image|
36
+ IMAGES[image['id']] = RackspaceCloud::Image.new(image)
37
+ end
38
+ nil
39
+ end
40
+
41
+ def RackspaceCloud.request_authorization(user, access_key)
42
+ session = Patron::Session.new
43
+ session.base_url = BASE_AUTH_URI
44
+ session.headers["X-Auth-User"] = user
45
+ session.headers["X-Auth-Key"] = access_key
46
+ session.headers["User-Agent"] = "rackspacecloud_ruby_gem"
47
+ response = session.get("/v#{API_VERSION}")
48
+
49
+ case response.status
50
+ when 204 # "No Content", which means success
51
+ @server_management_url = response.headers['X-Server-Management-Url']
52
+ @storage_url = response.headers['X-Storage-Url']
53
+ @storage_token = response.headers['X-Storage-Token']
54
+ @cdn_management_url = response.headers['X-CDN-Management-Url']
55
+ @auth_token = response.headers['X-Auth-Token']
56
+ @@authorized = true
57
+ else
58
+ raise AuthorizationError, "Error during authorization: #{curl.response_code}"
59
+ end
60
+ nil
61
+ end
62
+
63
+ def RackspaceCloud.request(path, options={})
64
+ raise RuntimeError, "Please authorize before using by calling connect()" unless defined?(@@authorized) && @@authorized
65
+ session = Patron::Session.new
66
+ session.base_url = @server_management_url
67
+ session.headers['X-Auth-Token'] = @auth_token
68
+ session.headers["User-Agent"] = "rackspacecloud_ruby_gem"
69
+ session.timeout = 10
70
+ response = case options[:method]
71
+ when :post
72
+ session.headers['Accept'] = "application/json"
73
+ session.headers['Content-Type'] = "application/json"
74
+ session.post("#{path}", options[:data].to_json)
75
+ when :delete
76
+ session.delete("#{path}")
77
+ else
78
+ session.get("#{path}.json")
79
+ end
80
+
81
+ case response.status
82
+ when 200, 202, 204
83
+ JSON.parse(response.body) unless response.body.empty?
84
+ else
85
+ raise RuntimeError, "Error fetching #{path}: #{response.status}"
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{rackspace_cloud}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Grant Goodale"]
9
+ s.date = %q{2009-07-20}
10
+ s.description = %q{Gem enabling the management of Rackspace Cloud Server instances.}
11
+ s.email = %q{grant@moreblinktag.com}
12
+ s.extra_rdoc_files = [
13
+ "README"
14
+ ]
15
+ s.files = [
16
+ "MIT-LICENSE",
17
+ "README",
18
+ "Rakefile",
19
+ "VERSION",
20
+ "init.rb",
21
+ "install.rb",
22
+ "lib/rackspace_cloud.rb",
23
+ "lib/rackspace_cloud/base.rb",
24
+ "lib/rackspace_cloud/flavor.rb",
25
+ "lib/rackspace_cloud/image.rb",
26
+ "lib/rackspace_cloud/server.rb",
27
+ "rackspace_cloud.gemspec",
28
+ "test/authentication_test.rb",
29
+ "test/configuration_test.rb",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/ggoodale/rackspace_cloud}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.3}
36
+ s.summary = %q{Gem enabling the management of Rackspace Cloud Server instances}
37
+ s.test_files = [
38
+ "test/authentication_test.rb",
39
+ "test/configuration_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<patron>, [">= 0.4.0"])
49
+ else
50
+ s.add_dependency(%q<patron>, [">= 0.4.0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<patron>, [">= 0.4.0"])
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class AuthenticationTest < Test::Unit::TestCase
4
+ context "A valid configuration" do
5
+ should "be able to authenticate successfully" do
6
+ true
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+ class ConfigurationTest < Test::Unit::TestCase
4
+ context "A RackspaceCloud configuration" do
5
+ setup do
6
+ @config = {:user => "foo", :access_key => "aabbccdd"}
7
+ end
8
+
9
+ should "contain a username" do
10
+ @config[:user] = nil
11
+ assert_raise(ArgumentError) {
12
+ RackspaceCloud::Base.new(@config)
13
+ }
14
+ end
15
+
16
+ should "contain an authorization token" do
17
+ @config[:access_key] = nil
18
+ assert_raise(ArgumentError) {
19
+ RackspaceCloud::Base.new(@config)
20
+ }
21
+ end
22
+
23
+ should "should be valid if all required parameters are specified" do
24
+ assert_nothing_raised { RackspaceCloud::Base.new(@config) }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ gem 'thoughtbot-shoulda'
4
+
5
+ begin
6
+ require 'shoulda'
7
+ rescue LoadError
8
+ abort "Unable to find thoughtbot-shoulda gem - please check that Shoulda is installed"
9
+ end
10
+
11
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'rackspace_cloud')
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ggoodale-rackspace_cloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Grant Goodale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: patron
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.0
24
+ version:
25
+ description: Gem enabling the management of Rackspace Cloud Server instances.
26
+ email: grant@moreblinktag.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - MIT-LICENSE
35
+ - README
36
+ - Rakefile
37
+ - VERSION
38
+ - init.rb
39
+ - install.rb
40
+ - lib/rackspace_cloud.rb
41
+ - lib/rackspace_cloud/base.rb
42
+ - lib/rackspace_cloud/flavor.rb
43
+ - lib/rackspace_cloud/image.rb
44
+ - lib/rackspace_cloud/server.rb
45
+ - rackspace_cloud.gemspec
46
+ - test/authentication_test.rb
47
+ - test/configuration_test.rb
48
+ - test/test_helper.rb
49
+ has_rdoc: false
50
+ homepage: http://github.com/ggoodale/rackspace_cloud
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Gem enabling the management of Rackspace Cloud Server instances
75
+ test_files:
76
+ - test/authentication_test.rb
77
+ - test/configuration_test.rb
78
+ - test/test_helper.rb