Class: Conoha
- Inherits:
-
Object
- Object
- Conoha
- Defined in:
- lib/conoha.rb,
lib/conoha/config.rb
Defined Under Namespace
Classes: Config
Class Method Summary collapse
- .authenticate! ⇒ Object
- .authenticate_user!(user_id) ⇒ Object
- .boot(server_id) ⇒ Object
- .create(os, ram, name_tag = nil) ⇒ Object
- .create_from_image(image_ref, ram) ⇒ Object
- .create_image(server_id, name) ⇒ Object
- .delete(server_id) ⇒ Object
- .delete_image(image_ref) ⇒ Object
- .images ⇒ Object
- .init! ⇒ Object
- .ip_address_of(server_id) ⇒ Object
- .name_tag(server_id) ⇒ Object
- .reboot(server_id, type = "SOFT") ⇒ Object
- .rebuild(server_id, os) ⇒ Object
- .region ⇒ Object
- .server_action(server_id, action, action_value = nil) ⇒ Object
- .servers ⇒ Object
- .shutdown(server_id) ⇒ Object
- .status_of(server_id) ⇒ Object
- .username ⇒ Object
- .vps_list ⇒ Object
-
.whoami ⇒ Fixnum|String
Fixnum: 1: conoha-config.json doesn’t have “accounts” key 2: “accounts” doesn’t have deafult “username” String: “id” of “accounts”.
Class Method Details
.authenticate! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/conoha.rb', line 14 def self.authenticate! uri = "https://identity.#{region}.conoha.io/v2.0/tokens" payload = { auth: { passwordCredentials: { username: @@username, password: @@password }, tenantId: @@tenant_id } } res = https_post uri, payload, nil if res.code == '401' raise StandardError.new 'Authentication failure' end @@authtoken = JSON.parse(res.body)["access"]["token"]["id"] save_config! end |
.authenticate_user!(user_id) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/conoha.rb', line 33 def self.authenticate_user!(user_id) uri = "https://identity.#{region}.conoha.io/v2.0/tokens" credential = @@accounts[user_id] if credential.nil? raise StandardError.new "User \"#{user_id}\" doesn't exist." end payload = { auth: { passwordCredentials: { username: credential['username'], password: credential['password'] }, tenantId: credential['tenant_id'] } } res = https_post uri, payload, nil if res.code == '401' raise StandardError.new 'Authentication failure' end @@username = credential['username'] @@password = credential['password'] @@tenant_id = credential['tenant_id'] @@public_key = credential['public_key'] @@authtoken = JSON.parse(res.body)["access"]["token"]["id"] save_config! end |
.boot(server_id) ⇒ Object
160 161 162 |
# File 'lib/conoha.rb', line 160 def self.boot(server_id) server_action server_id, "os-start" end |
.create(os, ram, name_tag = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/conoha.rb', line 100 def self.create(os, ram, name_tag = nil) image_ref = image_ref_from_image_tag(image_tag_dictionary(os)) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers" payload = { server: { adminPass: randstr, imageRef: image_ref, flavorRef: flavor_ref(ram), key_name: public_key, security_groups: [{name: 'default'}, {name: 'gncs-ipv4-all'}], } } if name_tag payload[:server].merge!({metadata: {instance_name_tag: name_tag}}) end res = https_post uri, payload, authtoken JSON.parse(res.body)["server"]["id"] end |
.create_from_image(image_ref, ram) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/conoha.rb', line 192 def self.create_from_image(image_ref, ram) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers" payload = { server: { adminPass: randstr, imageRef: image_ref, flavorRef: flavor_ref(ram), key_name: public_key, security_groups: [{name: 'default'}, {name: 'gncs-ipv4-all'}], } } res = https_post uri, payload, authtoken JSON.parse(res.body)["server"]["id"] end |
.create_image(server_id, name) ⇒ Object
180 181 182 183 184 |
# File 'lib/conoha.rb', line 180 def self.create_image(server_id, name) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action" res = https_post uri, {"createImage" => {"name" => name}}, authtoken res.code == '202' ? 'OK' : 'Error' end |
.delete(server_id) ⇒ Object
133 134 135 136 137 |
# File 'lib/conoha.rb', line 133 def self.delete(server_id) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}" res = https_delete uri, authtoken res.code == '204' ? 'OK' : 'Error' end |
.delete_image(image_ref) ⇒ Object
186 187 188 189 190 |
# File 'lib/conoha.rb', line 186 def self.delete_image(image_ref) uri = "https://image-service.#{region}.conoha.io/v2/images/#{image_ref}" res = https_delete uri, authtoken res.code == '204' ? 'OK' : 'Error' end |
.images ⇒ Object
174 175 176 177 178 |
# File 'lib/conoha.rb', line 174 def self.images uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/images" res = https_get uri, authtoken JSON.parse(res.body)["images"].map { |e| [e["name"], e["id"]] } end |
.init! ⇒ Object
6 7 8 |
# File 'lib/conoha.rb', line 6 def self.init! load_config! end |
.ip_address_of(server_id) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/conoha.rb', line 139 def self.ip_address_of(server_id) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}" res = https_get uri, authtoken # NOTE: values[1] is needed if eth1 exists. JSON.parse(res.body)["server"]["addresses"].values[0].map{ |e| e["addr"] } end |
.name_tag(server_id) ⇒ Object
207 208 209 210 211 |
# File 'lib/conoha.rb', line 207 def self.name_tag(server_id) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}/metadata" res = https_get uri, authtoken JSON.parse(res.body)["metadata"]["instance_name_tag"] end |
.reboot(server_id, type = "SOFT") ⇒ Object
170 171 172 |
# File 'lib/conoha.rb', line 170 def self.reboot(server_id, type = "SOFT") server_action(server_id, "reboot", { "type" => type }) end |
.rebuild(server_id, os) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/conoha.rb', line 119 def self.rebuild(server_id, os) image_ref = image_ref_from_image_tag(image_tag_dictionary(os)) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action" payload = { rebuild: { imageRef: image_ref, adminPass: randstr, key_name: public_key } } res = https_post uri, payload, authtoken res.code == '202' ? 'OK' : 'Error' end |
.region ⇒ Object
10 11 12 |
# File 'lib/conoha.rb', line 10 def self.region @@region || 'tyo1' end |
.server_action(server_id, action, action_value = nil) ⇒ Object
154 155 156 157 158 |
# File 'lib/conoha.rb', line 154 def self.server_action(server_id, action, action_value = nil) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action" res = https_post uri, {action => action_value}, authtoken res.code == '202' ? 'OK' : 'Error' end |
.servers ⇒ Object
84 85 86 87 88 |
# File 'lib/conoha.rb', line 84 def self.servers uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers" res = https_get uri, authtoken JSON.parse(res.body)["servers"] end |
.shutdown(server_id) ⇒ Object
164 165 166 |
# File 'lib/conoha.rb', line 164 def self.shutdown(server_id) server_action server_id, "os-stop" end |
.status_of(server_id) ⇒ Object
146 147 148 149 150 |
# File 'lib/conoha.rb', line 146 def self.status_of(server_id) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers/#{server_id}" res = https_get uri, authtoken JSON.parse(res.body)["server"]["status"] end |
.username ⇒ Object
63 64 65 |
# File 'lib/conoha.rb', line 63 def self.username @@username end |
.vps_list ⇒ Object
90 91 92 |
# File 'lib/conoha.rb', line 90 def self.vps_list servers.map { |e| e["id"] } end |
.whoami ⇒ Fixnum|String
Returns Fixnum:
1: conoha-config.json doesn't have "accounts" key
2: "accounts" doesn't have deafult "username"
String: “id” of “accounts”.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/conoha.rb', line 72 def self.whoami if @@accounts.nil? 1 else if result = @@accounts.find { |k, v| v['username'] == @@username } result.first else 2 end end end |