Class: Conoha
- Inherits:
-
Object
- Object
- Conoha
- Defined in:
- lib/conoha.rb,
lib/conoha/config.rb
Overview
:nocov:
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
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/conoha.rb', line 15 def self.authenticate! uri = "https://identity.#{region}.conoha.io/v2.0/tokens" payload = payload_for_authentication @@username, @@password, @@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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/conoha.rb', line 26 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 = payload_for_authentication credential['username'], credential['password'], credential['tenant_id'] res = https_post uri, payload, nil if res.code == '401' raise StandardError.new 'Authentication failure' end auth_token = JSON.parse(res.body)["access"]["token"]["id"] set_class_variables_from_credential! credential, auth_token save_config! end |
.boot(server_id) ⇒ Object
134 135 136 |
# File 'lib/conoha.rb', line 134 def self.boot(server_id) server_action server_id, "os-start" end |
.create(os, ram, name_tag = nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/conoha.rb', line 82 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 = payload_for_create_vps image_ref, ram, public_key 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
166 167 168 169 170 171 |
# File 'lib/conoha.rb', line 166 def self.create_from_image(image_ref, ram) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers" payload = payload_for_create_vps image_ref, ram, public_key res = https_post uri, payload, authtoken JSON.parse(res.body)["server"]["id"] end |
.create_image(server_id, name) ⇒ Object
154 155 156 157 158 |
# File 'lib/conoha.rb', line 154 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
107 108 109 110 111 |
# File 'lib/conoha.rb', line 107 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
160 161 162 163 164 |
# File 'lib/conoha.rb', line 160 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
148 149 150 151 152 |
# File 'lib/conoha.rb', line 148 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
7 8 9 |
# File 'lib/conoha.rb', line 7 def self.init! load_config! end |
.ip_address_of(server_id) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/conoha.rb', line 113 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
173 174 175 176 177 |
# File 'lib/conoha.rb', line 173 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
144 145 146 |
# File 'lib/conoha.rb', line 144 def self.reboot(server_id, type = "SOFT") server_action(server_id, "reboot", { "type" => type }) end |
.rebuild(server_id, os) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/conoha.rb', line 93 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
11 12 13 |
# File 'lib/conoha.rb', line 11 def self.region @@region || 'tyo1' end |
.server_action(server_id, action, action_value = nil) ⇒ Object
128 129 130 131 132 |
# File 'lib/conoha.rb', line 128 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
66 67 68 69 70 |
# File 'lib/conoha.rb', line 66 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
138 139 140 |
# File 'lib/conoha.rb', line 138 def self.shutdown(server_id) server_action server_id, "os-stop" end |
.status_of(server_id) ⇒ Object
120 121 122 123 124 |
# File 'lib/conoha.rb', line 120 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
45 46 47 |
# File 'lib/conoha.rb', line 45 def self.username @@username end |
.vps_list ⇒ Object
72 73 74 |
# File 'lib/conoha.rb', line 72 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”.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/conoha.rb', line 54 def self.whoami if @@accounts.nil? 1 else if result = @@accounts.find { |k, v| v['username'] == @@username } result.first else 2 end end end |