unit-hosting 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f29e14997354c9b0ae59fd98fe0fa9bcf3f0f5ae
4
- data.tar.gz: 56d761f0e16aa2530203724c2d5e6c15e6b68043
3
+ metadata.gz: 98119bd883670b1b0926c70ecedca0a05f895b38
4
+ data.tar.gz: 3561a6bf65164b4815a141adaa78ccc20bc7faa4
5
5
  SHA512:
6
- metadata.gz: a5de70ce5fe11cc46a048a86c3ee0f4eff116d3817d24582a9bcbba7f83957b025a3996391e9ff35830a758bb775454418a4bd979d190133ac4624e3800f5d45
7
- data.tar.gz: bae38cf9038953002ffe4f8cc0e629fd18d3da8d4c1f05af0a926ba2b91c58bbddd9beac774dfdcf1edf7f1c731ccfc755b15515e34178d44dc56027599d3960
6
+ metadata.gz: aa306e08ec7432af886974060d2d8682bffdb36f9a5ccf59b1dc898c393074991bada5881e7038a9833b6b9f880a122844c0ac30e12f04b89bd3907da5402b4a
7
+ data.tar.gz: f3c0cb3920efb4298028bc41b50a094f92e5e3a150a15ee2c7f88265142ee82a870c21a937a0d80887a6f0027361cf1271a484274c2b9e42989aea44935e41d6
@@ -0,0 +1 @@
1
+ service_name: travis-ci
@@ -4,3 +4,4 @@ rvm:
4
4
  - 1.9.2
5
5
  - 1.9.3
6
6
  - 2.0.0
7
+ - 2.1.0
data/Gemfile CHANGED
@@ -1,20 +1,25 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
2
+
5
3
  gem 'mutter'
6
4
  gem "keystorage", "~> 0.4.13"
7
- gem 'mechanize'
8
5
  gem 'highline', '> 1.6'
9
6
  gem 'progressbar', '>= 0.9.0'
10
7
  gem 'httpclient', '>= 2.1.6.1'
11
8
  gem 'command-line-utils' , '>= 0.0.1'
12
- # gem 'rdoc'
9
+ gem 'active_support'
10
+ gem 'i18n'
11
+ gem 'nokogiri','~> 1.5.9'
12
+ gem 'mechanize', '2.6.0'
13
13
 
14
- # Add dependencies to develop your gem here.
15
- # Include everything needed to run rake, tests, features, etc.
16
14
  group :development do
17
- # gem "shoulda", ">= 0"
18
- gem "bundler"
19
15
  gem "jeweler"
20
16
  end
17
+
18
+ group :development,:test do
19
+ gem 'simplecov'
20
+ gem 'coveralls', :require =>false
21
+ gem "bundler"
22
+ gem 'rake'
23
+ gem 'rspec'
24
+ gem 'webmock'
25
+ end
data/README.md CHANGED
@@ -1,14 +1,25 @@
1
- unit-hosting
2
- =============
3
-
4
- This is a command to manage virtual servers on UnitHosting(http://www.unit-hosting.com).
1
+ Unit Hosting support libs for Ruby
2
+ ==================================
5
3
 
6
4
  [![Build Status](https://travis-ci.org/tumf/unit-hosting.png?branch=master)](https://travis-ci.org/tumf/unit-hosting)
5
+ [![Coverage Status](https://coveralls.io/repos/tumf/unit-hosting/badge.png?branch=master)](https://coveralls.io/r/tumf/unit-hosting?branch=master)
6
+ [![Gem Version](https://badge.fury.io/rb/unit-hosting.png)](http://badge.fury.io/rb/unit-hosting)
7
+ [![Code Climate](https://codeclimate.com/github/tumf/unit-hosting.png)](https://codeclimate.com/github/tumf/unit-hosting)
8
+ [![Dependency Status](https://gemnasium.com/tumf/unit-hosting.png)](https://gemnasium.com/tumf/unit-hosting)
9
+
10
+
11
+ This is a library and CLI tools to manage virtual servers on [UnitHosting](http://www.unit-hosting.com).
12
+
13
+ Ruby versions 1.8.7, 1.9.2, 1.9.3, 2.0.0
14
+
15
+ > **GitHub**
16
+ > https://github.com/tumf/unit-hosting
17
+
7
18
 
8
19
  Install
9
20
  -------
10
21
 
11
- sudo gem install unit-hosting
22
+ [sudo] gem install unit-hosting
12
23
 
13
24
  > **RubyGems.org**
14
25
  > https://rubygems.org/gems/unit-hosting
@@ -16,22 +27,12 @@ Install
16
27
  Usage
17
28
  -----
18
29
 
19
- unit-hosting [global options] command [command options] args..
30
+ unit-hosting [global options] command [command options] args..
20
31
 
21
32
  Global Options
22
33
  ---------------
23
34
 
24
35
 
25
- Development
26
- -----------
27
-
28
- MySQLサーバの起動
29
-
30
- ```
31
- sudo mysql.server start
32
- ```
33
-
34
-
35
36
  Commands
36
37
  --------
37
38
 
@@ -48,6 +49,4 @@ See Also
48
49
  Copyright
49
50
  ---------
50
51
 
51
- Copyright (c) 2011 Yoshihiro TAKAHARA. See LICENSE.txt for
52
- further details.
53
-
52
+ Copyright (c) 2011-2014 Yoshihiro TAKAHARA. See LICENSE.txt for further details.
data/Rakefile CHANGED
@@ -26,14 +26,6 @@ Jeweler::Tasks.new do |gem|
26
26
  end
27
27
  Jeweler::RubygemsDotOrgTasks.new
28
28
 
29
- require 'rake/testtask'
30
- Rake::TestTask.new(:test) do |test|
31
- test.libs << 'lib' << 'test'
32
- test.pattern = 'test/**/test_*.rb'
33
- test.verbose = true
34
- end
35
-
36
- task :default => :test
37
-
38
-
39
-
29
+ require "rspec/core/rake_task"
30
+ RSpec::Core::RakeTask.new("spec")
31
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -1,5 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
+ require 'active_support/core_ext'
4
+ require 'i18n'
3
5
 
4
6
  require "highline/import"
5
7
  require "keystorage"
@@ -8,5 +10,3 @@ require 'xmlrpc/client'
8
10
  require 'rexml/document'
9
11
  require 'openssl'
10
12
 
11
- require 'unit-hosting/agent'
12
- require 'unit-hosting/commands'
@@ -1,17 +1,18 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: utf-8 -*-
3
3
  require "mechanize"
4
-
4
+ require "unit-hosting/group"
5
5
  module UnitHosting
6
- UHURL = "https://cloud.unit-hosting.com"
7
6
  class Agent < Mechanize
7
+ attr_accessor :endpoint
8
8
  def initialize
9
9
  super
10
+ @endpoint = "https://cloud.unit-hosting.com"
10
11
  max_history = 0
11
12
  end
12
13
 
13
14
  def getr path
14
- get(UHURL + path)
15
+ get(@endpoint + path)
15
16
  end
16
17
 
17
18
  def login id,password
@@ -8,7 +8,8 @@ module UnitHosting
8
8
  end
9
9
  module_function :keypath
10
10
  class Base
11
- attr_reader :instance_id,:api_key;
11
+ attr_reader :instance_id,:api_key
12
+ attr_accessor :server
12
13
  def initialize(instance_id=nil,api_key=nil)
13
14
  @instance_id = instance_id
14
15
  @api_key = api_key
@@ -40,7 +41,9 @@ module UnitHosting
40
41
  def server_call(method,param = {})
41
42
  param["instance_id"] = @instance_id
42
43
  param["api_key"] = @api_key
43
- return @server.call(method,param)
44
+ result = @server.call(method,param)
45
+ # puts @server.http_last_response.body
46
+ return result
44
47
  end
45
48
  end
46
49
  end
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
  # -*- coding: utf-8 -*-
3
+ require 'active_support/inflector'
3
4
  require 'unit-hosting/api/base'
4
5
 
5
6
  module UnitHosting
@@ -10,36 +11,31 @@ module UnitHosting
10
11
  @api_key_elm = '/server/key'
11
12
  super
12
13
  end
13
- def reboot
14
- server_call("vm.reboot")
15
- end
16
- def start
17
- server_call("vm.start")
18
- end
19
- def shutdown
20
- server_call("vm.shutdown")
21
- end
22
- def power_off
23
- server_call("vm.powerOff")
24
- end
25
- def destroy
26
- server_call("vm.destroy")
27
- end
28
- def status?
29
- server_call("vm.getStatus")
14
+
15
+ def to_api(name)
16
+ "vm.#{name}"
17
+ end
18
+
19
+ def method_missing(name, *args)
20
+ name = name.to_s
21
+ name = if /(.*)\?$/ =~ name
22
+ "get#{$1.camelize}"
23
+ else
24
+ name.camelize(:lower)
25
+ end
26
+ if args.blank?
27
+ server_call(to_api(name))
28
+ else
29
+ server_call(to_api(name),*args)
30
+ end
30
31
  end
32
+
31
33
  def memory_unit_size size
32
34
  server_call("vm.setMemoryUnitSize",{"size" => size})
33
35
  end
34
36
  def cpu_unit_num num
35
37
  server_call("vm.setCpuUnitNum",{"num" => num})
36
38
  end
37
- def memory_unit_size?
38
- server_call("vm.getMemoryUnitSize")
39
- end
40
- def cpu_unit_num?
41
- server_call("vm.getCpuUnitNum")
42
- end
43
39
  def ips
44
40
  server_call("vm.getIpInfo")
45
41
  end
@@ -67,6 +63,12 @@ module UnitHosting
67
63
  def get_vm_data(key)
68
64
  server_call("vm.getVmData",{"key" =>key})
69
65
  end
66
+ def set_pv_args(val)
67
+ server_call("vm.setPvArgs",{"pv_args" =>val})
68
+ end
69
+ def get_pv_args
70
+ server_call("vm.getPvArgs")
71
+ end
70
72
  end
71
73
  end
72
74
  end
@@ -2,6 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
  require 'unit-hosting/api/base'
4
4
  require 'unit-hosting/api/vm'
5
+ require 'unit-hosting/api/vm_recipe'
5
6
 
6
7
  module UnitHosting
7
8
  module Api
@@ -19,6 +20,7 @@ module UnitHosting
19
20
  vms.each do |vm|
20
21
  return vm["api_key"] if vm["instance_id"] == instance_id
21
22
  end
23
+ nil #missing
22
24
  end
23
25
  # instance_idに紐づくvmを返す
24
26
  def vm(instance_id)
@@ -6,7 +6,26 @@ require 'unit-hosting/group'
6
6
  module UnitHosting
7
7
  class Cache < PStore
8
8
  def groups
9
- transaction { |ps| ps["groups"] ||=[] }.extend(Groups)
9
+ transaction { |ps|
10
+ ps["groups"] ||= [].extend(UnitHosting::Groups)
11
+ }
10
12
  end
13
+
14
+ def update_all_groups! gs
15
+ transaction { |ps|
16
+ ps["groups"] = gs.update
17
+ }
18
+ end
19
+
20
+ def update_group! group
21
+ transaction { |ps|
22
+ groups = ps["groups"].reject { |g|
23
+ g.instance_id == group.instance_id
24
+ }
25
+ groups << group.update
26
+ ps["groups"] = groups
27
+ }
28
+ end
29
+
11
30
  end
12
31
  end
@@ -3,6 +3,7 @@
3
3
  require 'unit-hosting/commands'
4
4
  module UnitHosting
5
5
  class CLI < CommandLineUtils::CLI
6
+ attr_accessor :commands
6
7
  def initialize
7
8
  super
8
9
  @commands = Commands.new
@@ -2,7 +2,6 @@
2
2
  # -*- coding: utf-8 -*-
3
3
  #require "rubygems"
4
4
 
5
- require "unit-hosting"
6
5
  require "unit-hosting/api"
7
6
  require "unit-hosting/agent"
8
7
  require "unit-hosting/cache"
@@ -10,41 +9,49 @@ require "unit-hosting/cache"
10
9
 
11
10
  module UnitHosting
12
11
  class Commands < CommandLineUtils::Commands
13
- # CommandLineUtils::COMMANDS +=
14
- def initialize
15
- super
12
+ class ArgumentError < StandardError;end
13
+ class LoginError < ArgumentError; end
14
+ class GroupNotFound < ArgumentError; end
15
+ # CommandLineUtils::COMMANDS +=
16
+ attr_accessor :agent, :cache, :keyname
17
+ def initialize endpoint =nil
18
+ @keyname = "unit-hosting"
19
+ @endpoint = endpoint
20
+ @command_options = []
21
+ super()
16
22
  @commands += ["login","logout","update","groups","group"]
17
23
  @agent = Agent.new
18
- @cache_file = ENV['HOME']+"/.unit-hosting.cache"
19
- @cache = Cache.new(@cache_file)
24
+ @agent.endpoint = @endpoint
25
+ @cache = nil
26
+ end
20
27
 
28
+ def cache
29
+ @cache ||= Cache.new(cache_file)
21
30
  end
22
31
 
23
32
  def login
24
33
  opt = OptionParser.new
25
34
  opt.parse!(@command_options)
26
- @summery = "Login to https://cloud.unit-hosting.com ."
35
+ @summery = "Login to #{@endpoint} ."
27
36
  @banner = ""
28
37
  return opt if @help
29
38
 
30
- user = ask('Enter user: ') do |q|
31
- q.validate = /\w+/
32
- end
33
-
34
39
  ok = false
35
40
  while(!ok)
41
+ user = ask('Enter user: ') do |q|
42
+ q.validate = /\w+/
43
+ end
36
44
  password = ask("Enter your password: ") do |q|
37
45
  q.validate = /\w+/
38
46
  q.echo = false
39
47
  end
40
-
41
48
  @agent.login(user,password)
42
- if @agent.login? # => OK
49
+ if @agent.login?
43
50
  ok = true
44
- Keystorage.set("unit-hosting",user,password)
45
- puts "login OK"
51
+ Keystorage.set(@keyname,user,password)
52
+ $stderr.puts "login OK"
46
53
  else
47
- puts "password mismatch"
54
+ $stderr.puts "password mismatch"
48
55
  end
49
56
  end
50
57
  end
@@ -52,15 +59,14 @@ module UnitHosting
52
59
  def logout
53
60
  opt = OptionParser.new
54
61
  opt.parse!(@command_options)
55
- @summery = "Logout from https://cloud.unit-hosting.com ."
62
+ @summery = "Logout from #{@endpoint} ."
56
63
  @banner = ""
57
64
  return opt if @help
58
- Keystorage.delete("unit-hosting")
65
+ Keystorage.delete(@keyname)
59
66
  end
60
67
 
61
68
  include UnitHosting::Api
62
- def update
63
- all = false
69
+ def update all = false
64
70
  opt = OptionParser.new
65
71
  opt.on('-a','--all', 'update all cache') { all = true }
66
72
  opt.parse!(@command_options)
@@ -69,31 +75,13 @@ module UnitHosting
69
75
  return opt if @help
70
76
  gid = @command_options.shift
71
77
 
72
-
73
78
  start
74
- gs = @agent.groups.collect { |g|
75
- unless g.key.empty?
76
- v = @cache.groups.find{ |c| c.instance_id == g.instance_id }
77
- v.update if v and v.vms == nil
78
- g.vms = v.vms if v
79
- g
80
- end
81
- }.compact
82
- @cache.transaction { |ps|
83
- if all
84
- ps["groups"] = gs.find_all { |g|
85
- !g.key.empty?
86
- }.extend(Groups).update
87
- else
88
- group = ask_group(gid,gs)
89
- ps["groups"] = gs.collect{ |g|
90
- unless g.key.empty?
91
- g.update if g.instance_id == group.instance_id
92
- g
93
- end
94
- }.compact.extend(Groups)
95
- end
96
- }
79
+ if all
80
+ cache.update_all_groups!(@agent.groups)
81
+ else
82
+ group = ask_group(gid,cache.groups)
83
+ cache.update_group!(group)
84
+ end
97
85
  end
98
86
 
99
87
  def groups
@@ -102,8 +90,7 @@ module UnitHosting
102
90
  @summery = "List sever groups."
103
91
  @banner = ""
104
92
  return opt if @help
105
-
106
- puts @cache.groups.tablize
93
+ STDOUT.puts cache.groups.try(:tablize)
107
94
  end
108
95
 
109
96
  def group
@@ -114,7 +101,7 @@ module UnitHosting
114
101
  return opt if @help
115
102
 
116
103
  id = @command_options.shift
117
- group = ask_group(id,@cache.groups)
104
+ group = ask_group(id,cache.groups)
118
105
  puts group.tablize if group
119
106
  end
120
107
 
@@ -129,27 +116,30 @@ module UnitHosting
129
116
  private
130
117
  def start
131
118
  return true if @agent.login?
132
- user = Keystorage.list("unit-hosting").shift
119
+
120
+ user = Keystorage.list(@keyname).shift
133
121
  login unless user
134
122
  if user
135
- @agent.login(user,Keystorage.get("unit-hosting",user))
123
+ @agent.login(user,Keystorage.get(@keyname,user))
136
124
  login unless @agent.login?
137
125
  end
126
+ raise LoginError,"Can't start session" unless @agent.login?
138
127
  end
139
-
140
128
  def ask_group(id,gs)
141
129
  unless id
142
130
  puts gs.extend(Groups).tablize
143
- id = ask('Enter group id ([q]uit): ',gs.ids << 'q') { |q|
131
+ id = ask('Enter group id: ',gs.ids) { |q|
144
132
  q.validate = /\w+/
145
133
  q.readline = true
146
134
  }
147
135
  end
148
- raise SystemExit if id == "q"
149
136
  group = gs.find { |g| id == g.instance_id }
150
- raise "Group #{id} is not exists." unless group
137
+ raise GroupNotFound,"Group #{id} is not exists." unless group
151
138
  group
152
139
  end
153
140
 
141
+ def cache_file file = ".unit-hosting.cache"
142
+ File.join ENV['HOME'],file
143
+ end
154
144
  end
155
145
  end