conoha 0.7.2 → 0.8.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: 3ab3ff94a231bbb1a106e9be65d7b409742c7032
4
- data.tar.gz: c24d9fddfbc25952b0032dc2d747edd51a6fc1eb
3
+ metadata.gz: 151619e4a592951d23e53b1ce1f62226107120f8
4
+ data.tar.gz: 32f638a9b154a366c300d37d80f15ecfe74e9dc9
5
5
  SHA512:
6
- metadata.gz: 5d1d48a1c8ebdb485454b41eb9fd54b0e21adc3fa6c67dbf219def62ce836311f56d07b0e0e8339408e5b2d89adcd9b870a2fd4e7ace1afd21f5963a42a7e258
7
- data.tar.gz: f7638572a90b3fe49882fd97b3148cddf37e80ef5543e1aec23baa0346dbc30b9832c51db6abe79704bad769bbb8eda7c5768722cbd3d41c843f9f092a9b62b5
6
+ metadata.gz: 744f04a08ba3d8670fdbb3660a8ca5ceab22a5ff074b93225bdc70638170439fa299c1861f3c449f2dc031b70ba9c826583491d7b41caea8063f09302d4e2a80
7
+ data.tar.gz: 535a1f635648db1f8ff79a85b40ab64dc2bc4f6ef7e247869b5b4415db384d2c3063047ce4b74410718cd3d40f26f595a4b421ef4216950f033cfd44d50f8f35
data/.travis.yml CHANGED
@@ -3,4 +3,4 @@ rvm:
3
3
  - 2.2.3
4
4
  - 2.3.0
5
5
  - 2.3.1
6
- before_install: gem install bundler -v 1.11.2
6
+ before_install: gem install bundler -v 1.12.5
data/README.md CHANGED
@@ -171,6 +171,51 @@ conoharant shutdown
171
171
  conoharant destroy
172
172
  ```
173
173
 
174
+ ## Experimental feature: multiple accounts management
175
+
176
+ Edit `~/.conoha-config.json` like following:
177
+
178
+ ```sh
179
+ {
180
+ "username": "gncu123456789",
181
+ "password": "your-password",
182
+ "tenant_id": "0123456789abcdef",
183
+ "public_key": "your-registered-public-key-name",
184
+ "accounts": {
185
+ "user1": {
186
+ "username": "gncu123456789",
187
+ "password": "your-password",
188
+ "tenant_id": "0123456789abcdef",
189
+ "public_key": "your-registered-public-key-name"
190
+ },
191
+ "user2": {
192
+ "username": "gncu123456790",
193
+ "password": "your-user2-password",
194
+ "tenant_id": "0123456789abcdf0",
195
+ "public_key": "your-registered-public-key-name"
196
+ }
197
+ }
198
+ }
199
+ ```
200
+
201
+ ```sh
202
+ conoha authenticate user1
203
+
204
+ conoha whoami
205
+ #=> user1
206
+
207
+ conoha create ubuntu g-1gb
208
+ # Create VM of user1
209
+
210
+ conoha authenticate user2
211
+
212
+ conoha whoami
213
+ #=> user2
214
+
215
+ conoha create ubuntu g-1gb
216
+ # Create VM of user2
217
+ ```
218
+
174
219
  ## License
175
220
 
176
221
  [MIT](LICENSE.txt)
data/exe/conoha CHANGED
@@ -35,13 +35,32 @@ when 'version', '--version', '-v'
35
35
  puts ConohaVersion::ITSELF
36
36
  when 'authenticate'
37
37
  begin
38
- Conoha.authenticate!
38
+ if ARGV.size == 0
39
+ Conoha.authenticate!
40
+ else
41
+ Conoha.authenticate_user! ARGV.first
42
+ end
39
43
  rescue => e
44
+ STDERR.puts e
40
45
  STDERR.puts "Failed to authenticate."
41
46
  STDERR.puts "Retry after modifying \"~/.conoha-config.json\"."
42
47
  exit 1
43
48
  end
44
49
  puts 'Succeeded!'
50
+ when 'whoami'
51
+ result = Conoha.whoami
52
+ if result.class == Fixnum
53
+ case result
54
+ when 1
55
+ puts "\"accounts\" doesn't exist in \"~/.conoha-config.json\"."
56
+ when 2
57
+ puts "\"accounts\" doesn't have \"#{Conoha.username}\"."
58
+ else
59
+ puts "fixnum else"
60
+ end
61
+ else
62
+ puts result
63
+ end
45
64
  when 'vpslist'
46
65
  pp Conoha.vps_list
47
66
  when 'status'
data/lib/conoha.rb CHANGED
@@ -26,6 +26,57 @@ class Conoha
26
26
  save_config!
27
27
  end
28
28
 
29
+ def self.authenticate_user!(user_id)
30
+ uri = 'https://identity.tyo1.conoha.io/v2.0/tokens'
31
+
32
+ credential = @@accounts[user_id]
33
+ if credential.nil?
34
+ raise StandardError.new "User \"#{user_id}\" doesn't exist."
35
+ end
36
+
37
+ payload = {
38
+ auth: {
39
+ passwordCredentials: {
40
+ username: credential['username'],
41
+ password: credential['password']
42
+ },
43
+ tenantId: credential['tenant_id']
44
+ }
45
+ }
46
+ res = https_post uri, payload, nil
47
+ if res.code == '401'
48
+ raise StandardError.new 'Authentication failure'
49
+ end
50
+
51
+ @@username = credential['username']
52
+ @@password = credential['password']
53
+ @@tenant_id = credential['tenant_id']
54
+ @@public_key = credential['public_key']
55
+ @@authtoken = JSON.parse(res.body)["access"]["token"]["id"]
56
+ save_config!
57
+ end
58
+
59
+ def self.username
60
+ @@username
61
+ end
62
+
63
+ # @return [Fixnum|String]
64
+ # Fixnum:
65
+ # 1: conoha-config.json doesn't have "accounts" key
66
+ # 2: "accounts" doesn't have deafult "username"
67
+ # String: "id" of "accounts".
68
+ def self.whoami
69
+ if @@accounts.nil?
70
+ 1
71
+ else
72
+ if result = @@accounts.find { |k, v| v['username'] == @@username }
73
+ result.first
74
+ else
75
+ 2
76
+ end
77
+ end
78
+ end
79
+
29
80
  def self.servers
30
81
  uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
31
82
  res = https_get uri, authtoken
@@ -36,12 +87,16 @@ class Conoha
36
87
  servers.map { |e| e["id"] }
37
88
  end
38
89
 
90
+ # @raise [StandardError]
91
+ # when "os" doesn't exist in image_tag_dictionary
92
+ # when "image_tag" doesn't exist in images
39
93
  def self.create(os, ram)
94
+ image_ref = image_ref_from_image_tag(image_tag_dictionary(os))
40
95
  uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers"
41
96
  payload = {
42
97
  server: {
43
98
  adminPass: randstr,
44
- imageRef: image_ref_from_os(os),
99
+ imageRef: image_ref,
45
100
  flavorRef: flavor_ref(ram),
46
101
  key_name: public_key,
47
102
  security_groups: [{name: 'default'}, {name: 'gncs-ipv4-all'}],
@@ -52,10 +107,11 @@ class Conoha
52
107
  end
53
108
 
54
109
  def self.rebuild(server_id, os)
110
+ image_ref_from_image_tag(image_tag_dictionary(os))
55
111
  uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
56
112
  payload = {
57
113
  rebuild: {
58
- imageRef: image_ref_from_os(os),
114
+ imageRef: image_ref,
59
115
  adminPass: randstr,
60
116
  key_name: public_key
61
117
  }
@@ -167,6 +223,7 @@ EOS
167
223
  @@tenant_id = config["tenant_id"]
168
224
  @@public_key = config["public_key"]
169
225
  @@authtoken = config["authtoken"]
226
+ @@accounts = config["accounts"]
170
227
  @@config_loaded = true
171
228
  end
172
229
  end
@@ -178,6 +235,7 @@ EOS
178
235
  tenant_id: @@tenant_id,
179
236
  public_key: @@public_key,
180
237
  authtoken: @@authtoken,
238
+ accounts: @@accounts
181
239
  })
182
240
  File.open(config_file_path, 'w') do |f|
183
241
  f.write s
@@ -196,6 +254,17 @@ EOS
196
254
  @@public_key
197
255
  end
198
256
 
257
+ # @return [String] server ID (UUID)
258
+ # @param [String] image_tag e.g. "vmi-centos-7-amd64"
259
+ # @raise [StandardError] when image_tag doesn't exist in images
260
+ def self.image_ref_from_image_tag(image_tag)
261
+ if image = images.find { |e| e[0] == image_tag }
262
+ image[1]
263
+ else
264
+ raise StandardError.new "Tag \"#{tag}\" doesn't exist in image list."
265
+ end
266
+ end
267
+
199
268
  def self.flavor_ref(ram)
200
269
  dictionary = {
201
270
  'g-1gb' => '7eea7469-0d85-4f82-8050-6ae742394681',
data/lib/conoha/util.rb CHANGED
@@ -61,30 +61,6 @@ def ipv4(ip_address)
61
61
  ip_address.select { |e| e =~ /\d+\.\d+\.\d+\.\d+/ }.first
62
62
  end
63
63
 
64
- # @return [String] UUID of image
65
- # @params [String] os OS name
66
- # @raise [StandardError] When the OS name isn't contained.
67
- def image_ref_from_os(os)
68
- dictionary = {
69
- 'ubuntu' => '793be3e1-3c33-4ab3-9779-f4098ea90eb5', # Ubuntu 14.04 amd64
70
- 'debian' => 'c14d5dd5-debc-464c-9cc3-ada6e48f5d0c', # Debian 8 amd64
71
- 'fedora23' => 'ed6364b8-9fb2-479c-a5a8-bde9ba1101f3', # Fedora 23 amd64
72
- 'centos67' => 'cd13a8b9-6b57-467b-932e-eee5edcd8d6c', # CentOS 6.7
73
- 'centos72' => 'e141fc06-632e-42a9-9c2d-eec9201427ec', # CentOS 7.2
74
- 'arch' => 'f5e5b475-ebec-4973-99c7-bc8add5d16c4', # Arch
75
- }
76
- if dictionary.keys.include? os
77
- dictionary[os]
78
- else
79
- raise StandardError.new <<EOS
80
- "#{os}" doesn't exist.
81
- Select os name from the following list:
82
-
83
- #{dictionary.keys.join("\n")}
84
- EOS
85
- end
86
- end
87
-
88
64
  # @return [String] Image name tag
89
65
  # @params [String] os OS name
90
66
  # @raise [StandardError] When the OS name isn't included in the dictionary.
@@ -96,17 +72,17 @@ def image_tag_dictionary(os)
96
72
  'centos67' => 'vmi-centos-6.7-amd64', # CentOS 6.7
97
73
  'centos72' => 'vmi-centos-7.2-amd64', # CentOS 7.2
98
74
  'arch' => 'vmi-arch-amd64', # Arch
75
+ 'opensuse' => 'vmi-opensuse-42.1-amd64', # openSUSE
76
+ 'openbsd' => 'vmi-openbsd-5.8-amd64', # OpenBSD
77
+ 'netbsd' => 'vmi-netbsd-7.0-amd64', # NetBSD
78
+ 'freebsd' => 'vmi-freebsd-10.1-x86_64', # FreeBSD
99
79
  }
100
80
 
101
- # 'opensuse' => 'vmi-opensuse-42.1-amd64' # openSUSE
102
- # 'openbsd' => 'vmi-openbsd-5.8-amd64', # OpenBSD
103
- # 'netbsd' => 'vmi-netbsd-7.0-amd64', # NetBSD
104
- # 'freebsd' => 'vmi-freebsd-10.1-x86_64', # FreeBSD
105
-
106
81
  if dictionary.keys.include? os
107
82
  dictionary[os]
108
83
  else
109
84
  raise StandardError.new <<EOS
85
+ "#{os}" doesn't exist.
110
86
  Select os name from the following list:
111
87
 
112
88
  #{dictionary.keys.join("\n")}
@@ -1,3 +1,3 @@
1
1
  module ConohaVersion
2
- ITSELF = "0.7.2"
2
+ ITSELF = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conoha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-29 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler