kylichuku-grate-handle 0.1.1

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,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kirill Ishanov
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.md ADDED
@@ -0,0 +1,54 @@
1
+ = grate-handle
2
+
3
+ Grate Handle is a ruby library and a CLI tool which simplify work with [GoGrid](www.gogrid.com). It is built on top of [GoGrid REST API](http://wiki.gogrid.com/wiki/index.php/API).
4
+
5
+ Currently Grate Handle is under lazy development, but I hope that it will become usable in a couple of weeks.
6
+
7
+ == Getting Started
8
+
9
+ First install grate-handle.
10
+
11
+ <pre>
12
+ <code>
13
+ sudo gem install grate-handle
14
+ </code>
15
+ </pre>
16
+
17
+ Then create a .ghrc file in your home directory. This file will store the GoGrid's API credentials. It's a YAML file, so the structure will look like this:
18
+
19
+ <pre>
20
+ <code>
21
+ % cat ~/.ghrc
22
+ ---
23
+ - account: default
24
+ apikey: YOUR_APIKEY
25
+ secret: YOUR_SECRET
26
+ </code>
27
+ </pre>
28
+
29
+ After putting correct credentials into config file it's good to synchronize the time with internet, cause GoGrid uses timestamps in API
30
+
31
+ <pre>
32
+ <code>
33
+ sudo ntpdate pool.ntp.org
34
+ </code>
35
+ </pre>
36
+ Finally, you can run gh command to see the usage. Now you are ready to work with GoGrid using CLI!
37
+
38
+ <pre>
39
+ <code>
40
+ % gh
41
+ usage: gh ACTION ENTITY [ARGS]
42
+
43
+ Note that not all ACTIONs applicable to every ENTITY.
44
+ Available combinations are:
45
+ list servers Shows all servers in all states
46
+ list images Shows all available server images (both legacy and MyGSI)
47
+ list passwords Shows root login credentials for all servers
48
+ list ips Shows all all ips in all states of all types.
49
+ </code>
50
+ </pre>
51
+
52
+ == Copyright
53
+
54
+ Copyright (c) 2009 Kirill Ishanov. See LICENSE for details.
data/README.rdoc ADDED
@@ -0,0 +1,9 @@
1
+ = grate-handle
2
+
3
+ Grate Handle is a ruby library and a CLI tool which simplify work with [GoGrid](www.gogrid.com). It is built on top of [GoGrid REST API](http://wiki.gogrid.com/wiki/index.php/API).
4
+
5
+ Currently Grate Handle is under lazy development, but I hope that it will become usable in a couple of weeks.
6
+
7
+ == Copyright
8
+
9
+ Copyright (c) 2009 Kirill Ishanov. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "grate-handle"
8
+ gem.summary = %Q{A ruby library and a CLI client to GoGrid API }
9
+ gem.description = %Q{ Grate Handle is a small gem which simplifies the with GoGrid's API from ruby code. Also ships with a CLI tool called 'gh' which allows to perform most of the actions from http://my.gogrid.com but using true Unix CLI way. }
10
+ gem.email = "kirill.ishanov@gmail.com"
11
+ gem.homepage = "http://github.com/kylichuku/grate-handle"
12
+ gem.authors = ["Kirill Ishanov"]
13
+
14
+ %w(rspec jeweler json).each do |dependency|
15
+ gem.add_development_dependency dependency
16
+ end
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ if File.exist?('VERSION')
42
+ version = File.read('VERSION')
43
+ else
44
+ version = ""
45
+ end
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "grate-handle #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/bin/gh ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+
5
+ require 'grate-handle'
6
+
7
+ GrateHandle::CLIProcessor.new(ARGV).process
8
+
@@ -0,0 +1,76 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{grate-handle}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kirill Ishanov"]
12
+ s.date = %q{2009-08-26}
13
+ s.default_executable = %q{gh}
14
+ s.description = %q{ Grate Handle is a small gem which simplifies the with GoGrid's API from ruby code. Also ships with a CLI tool called 'gh' which allows to perform most of the actions from http://my.gogrid.com but using true Unix CLI way. }
15
+ s.email = %q{kirill.ishanov@gmail.com}
16
+ s.executables = ["gh"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ ".gitignore",
25
+ "LICENSE",
26
+ "README.md",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "bin/gh",
31
+ "grate-handle.gemspec",
32
+ "lib/cli_processor.rb",
33
+ "lib/gogrid_client.rb",
34
+ "lib/gogrid_manager.rb",
35
+ "lib/grate-handle.rb",
36
+ "lib/monkey_patches.rb",
37
+ "spec/cli_processor_spec.rb",
38
+ "spec/fixtures/all.rb",
39
+ "spec/fixtures/grid_server_list.json",
40
+ "spec/fixtures/grid_server_list.xml",
41
+ "spec/gogrid_manager_spec.rb",
42
+ "spec/monkey_patches_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+ s.homepage = %q{http://github.com/kylichuku/grate-handle}
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.3}
49
+ s.summary = %q{A ruby library and a CLI client to GoGrid API}
50
+ s.test_files = [
51
+ "spec/gogrid_manager_spec.rb",
52
+ "spec/spec_helper.rb",
53
+ "spec/monkey_patches_spec.rb",
54
+ "spec/fixtures/all.rb",
55
+ "spec/cli_processor_spec.rb"
56
+ ]
57
+
58
+ if s.respond_to? :specification_version then
59
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
+ s.specification_version = 3
61
+
62
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
63
+ s.add_development_dependency(%q<rspec>, [">= 0"])
64
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
65
+ s.add_development_dependency(%q<json>, [">= 0"])
66
+ else
67
+ s.add_dependency(%q<rspec>, [">= 0"])
68
+ s.add_dependency(%q<jeweler>, [">= 0"])
69
+ s.add_dependency(%q<json>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<rspec>, [">= 0"])
73
+ s.add_dependency(%q<jeweler>, [">= 0"])
74
+ s.add_dependency(%q<json>, [">= 0"])
75
+ end
76
+ end
@@ -0,0 +1,85 @@
1
+ require 'gogrid_manager'
2
+
3
+ module GrateHandle
4
+ class CLIProcessor
5
+ #{{{1 Initialization and accessors
6
+ attr_writer :manager
7
+
8
+ def initialize(args, manager=GoGridManager.new, out=STDOUT)
9
+ @args = args
10
+ @extra_params = {}
11
+ @out = out
12
+ @manager = manager
13
+ end
14
+ #}}}
15
+ #{{{1 Ugly Options processing
16
+ def process
17
+ case @args.shift(1).first
18
+ when 'list' then list_action
19
+ when 'help', nil then print_usage
20
+ else @out.puts "unknown action"
21
+ end
22
+ end
23
+
24
+ def list_action
25
+ case @args.shift(1).first
26
+ when 'servers' then print_servers
27
+ when 'images' then print_images
28
+ when 'ips' then print_ips
29
+ when 'passwords' then print_passwords
30
+ when 'help', nil then print_usage
31
+ else print_list_error
32
+ end
33
+ end
34
+ #}}}
35
+ #{{{1 Help Messages
36
+ def print_list_error
37
+ @out.puts "Unknown command. Please check gh help for the list of available commands"
38
+ end
39
+
40
+ def print_usage
41
+ # don't like multilines strings, sorry
42
+ usage = []
43
+ usage << 'usage: gh ACTION ENTITY [ARGS]'
44
+ usage << ''
45
+ usage << 'Note that not all ACTIONs applicable to every ENTITY.'
46
+ usage << 'Available combinations are:'
47
+
48
+ actions = []
49
+ actions << ['list servers', 'Shows all servers in all states']
50
+ actions << ['list images', 'Shows all available server images (both legacy and MyGSI)']
51
+ actions << ['list passwords', 'Shows root login credentials for all servers']
52
+ actions << ['list ips', 'Shows all all ips in all states of all types.']
53
+
54
+ actions.map! { |action| sprintf("\t%-20s%s", action[0], action[1]) }
55
+
56
+ (usage + actions).each { |line| @out.puts line }
57
+ end
58
+ #}}}
59
+ #{{{1 listings print
60
+ def print_images
61
+ @manager.list_images.each do |image|
62
+ @out.puts(sprintf("%-5s\t%s", image.id, image.name))
63
+ end
64
+ end
65
+
66
+ def print_servers
67
+ @manager.list_servers.each do |server|
68
+ @out.puts(sprintf("%-6s\t%-20s\t%-15s\t%s", server.id, server.name, server.ip.ip, server.state.name))
69
+ end
70
+ end
71
+
72
+ def print_ips
73
+ @manager.list_ips.each do |ip|
74
+ @out.puts(sprintf("%-15s\t%-7s\t%s", ip.ip, (ip.public ? "Public" : "Private"), ip.state.name))
75
+ end
76
+ end
77
+
78
+ def print_passwords
79
+ @manager.list_passwords.each do |passwd|
80
+ @out.puts(sprintf("%-20s\t%s@%-15s\t%s", passwd.server.name, passwd.username, passwd.server.ip.ip, passwd.password)) if passwd.has_key?('server') # hack against CloudStorage
81
+ end
82
+ end
83
+ #}}}
84
+ end
85
+ end
@@ -0,0 +1,44 @@
1
+ require 'digest/md5'
2
+ require 'cgi'
3
+ require 'open-uri'
4
+
5
+ module GrateHandle
6
+ class GoGridClient
7
+
8
+ def initialize(apikey = 'YOUR API KEY',
9
+ secret = 'YOUR SHARED SECRET',
10
+ format = 'json',
11
+ version = '1.2')
12
+ @server = 'https://api.gogrid.com/api'
13
+ @secret = secret
14
+ @default_params = { 'format' => format,
15
+ 'v' => version,
16
+ 'api_key' => apikey }
17
+ end
18
+
19
+ def credentials(apikey, secret)
20
+ @default_params['api_key'] = apikey
21
+ @secret = secret
22
+ end
23
+
24
+ def send_api_request(method, params={})
25
+ open(request_url(method, params)).read
26
+ end
27
+
28
+ private
29
+
30
+ def request_url(method, params)
31
+ call_params = @default_params.merge(params).merge(signature)
32
+ url = "#{@server}/#{method}?#{encoded(call_params)}"
33
+ end
34
+
35
+ def signature
36
+ { 'sig' => Digest::MD5.hexdigest(@default_params['api_key'] + @secret + "%.0f"%Time.new.to_f) }
37
+ end
38
+
39
+ def encoded(params)
40
+ params.map {|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ module GrateHandle
2
+ class GoGridManager
3
+ # {{{1 Attributes
4
+
5
+ attr_accessor :client
6
+
7
+ #}}}
8
+ # {{{1 Initialization
9
+
10
+ def initialize(arg1=nil, arg2=nil)
11
+ if (arg1 && arg2)
12
+ from_params(arg1, arg2)
13
+ else
14
+ from_config(arg1)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def from_config(account=nil)
21
+ configs = YAML::load(File.read(File.expand_path('~/.ghrc')))
22
+ config = (account ? configs.select { |conf| conf['account'] == account }.first : configs.first)
23
+ from_params(config['apikey'], config['secret'])
24
+ end
25
+
26
+ def from_params(apikey, secret)
27
+ @client = GoGridClient.new(apikey, secret)
28
+ end
29
+
30
+ # }}}
31
+ # {{{1 GoGrid Request Wrappers
32
+
33
+ public
34
+
35
+ def list_servers
36
+ list_request_for 'grid/server/list'
37
+ end
38
+
39
+ def list_images
40
+ list_request_for 'grid/image/list'
41
+ end
42
+
43
+ def list_ips
44
+ list_request_for 'grid/ip/list'
45
+ end
46
+
47
+ def list_passwords
48
+ list_request_for 'support/password/list'
49
+ end
50
+
51
+ private
52
+
53
+ def list_request_for(url, extra_params={})
54
+ response = @client.send_api_request(url, extra_params)
55
+ JSON.parse(response).list
56
+ end
57
+
58
+ # }}}
59
+ end
60
+ end
61
+
@@ -0,0 +1,11 @@
1
+ require 'gogrid_client'
2
+ require 'monkey_patches'
3
+ require 'gogrid_manager'
4
+ require 'cli_processor'
5
+
6
+ # gems
7
+ require 'rubygems'
8
+ require 'yaml'
9
+ require 'json'
10
+
11
+ include GrateHandle
@@ -0,0 +1,17 @@
1
+ class Hash
2
+ undef_method :id
3
+
4
+ def method_missing(methodname, *args)
5
+ humps = methodname.to_s.split('_')
6
+ key = humps.shift(1).first
7
+ key = key + humps.map { |hump| hump.capitalize }.join if humps.length
8
+
9
+ if self.include?(key)
10
+ self[key]
11
+ elsif key == 'id'
12
+ "N/A"
13
+ else
14
+ super
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,61 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe CLIProcessor do
4
+ #{{{1 list servers
5
+ describe "list servers" do
6
+ before do
7
+ stub(STDOUT).puts
8
+ @servers = [{
9
+ 'id' => '123',
10
+ 'name' => 'myserver',
11
+ 'ip' => { 'ip' => '1.1.1.1' },
12
+ 'state' => { 'name' => 'started' }
13
+ }]
14
+ end
15
+
16
+ it "should call list_servers on GoGridManager" do
17
+ manager = mock!.list_servers { @servers }.subject
18
+ command = %w(list servers)
19
+ cli = CLIProcessor.new(command, manager)
20
+ cli.process
21
+ end
22
+ end
23
+ #}}}
24
+ #{{{1 list ips
25
+ describe "list ips" do
26
+ before do
27
+ stub(STDOUT).puts
28
+ @ips = [{
29
+ 'ip' => '1.1.1.1',
30
+ 'public' => true,
31
+ 'state' => { 'name' => 'assigned' }
32
+ }]
33
+ end
34
+
35
+ it "should call list_ips on GoGridManager" do
36
+ manager = mock!.list_ips { @ips }.subject
37
+ command = %w(list ips)
38
+ cli = CLIProcessor.new(command, manager)
39
+ cli.process
40
+ end
41
+ end
42
+ #}}}
43
+ #{{{1 list images
44
+ describe "list ips" do
45
+ before do
46
+ stub(STDOUT).puts
47
+ @images = [{
48
+ 'id' => '123',
49
+ 'name' => 'my super image'
50
+ }]
51
+ end
52
+
53
+ it "should call list_images on GoGridManager" do
54
+ manager = mock!.list_images { @images }.subject
55
+ command = %w(list images)
56
+ cli = CLIProcessor.new(command, manager)
57
+ cli.process
58
+ end
59
+ end
60
+ #}}}
61
+ end
@@ -0,0 +1,2 @@
1
+ SERVERS = File.read('spec/fixtures/grid_server_list.json')
2
+
@@ -0,0 +1,60 @@
1
+ {
2
+ "list": [{
3
+ "description": "Test Sandbox Server",
4
+ "image": {
5
+ "description": "Windows 2008 (32-bit) w/ IIS 7.0 + PHP 5.2 + MySQL 5.0",
6
+ "id": 57,
7
+ "name": "w2k8_32_iis_php_mysql",
8
+ "object": "option"
9
+ },
10
+ "ip": {
11
+ "id": 499,
12
+ "ip": "111.111.11.111",
13
+ "object": "ip",
14
+ "public": true,
15
+ "state": {
16
+ "description": "IP is reserved or in use",
17
+ "id": 2,
18
+ "name": "Assigned",
19
+ "object": "option"
20
+ },
21
+ "subnet": "111.111.11.110/255.255.255.240"
22
+ },
23
+ "isSandbox": true,
24
+ "name": "Sandbox Server",
25
+ "object": "server",
26
+ "os": {
27
+ "description": "Windows 2008 Server (32-bit)",
28
+ "id": 11,
29
+ "name": "Windows 2008 Server (32-bit)",
30
+ "object": "option"
31
+ },
32
+ "ram": {
33
+ "description": "SandBox Server with 2GB RAM",
34
+ "id": 7,
35
+ "name": "2GB",
36
+ "object": "option"
37
+ },
38
+ "state": {
39
+ "description": null,
40
+ "id": 3,
41
+ "name": "Off",
42
+ "object": "option"
43
+ },
44
+ "type": {
45
+ "description": "This server has a public connection to the Internet.",
46
+ "id": 1,
47
+ "name": "Web Server",
48
+ "object": "option"
49
+ }
50
+ }],
51
+ "method": "/grid/server/list",
52
+ "status": "success",
53
+ "summary": {
54
+ "numpages": 0,
55
+ "returned": 1,
56
+ "start": 0,
57
+ "total": 1
58
+ }
59
+ }
60
+