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, user_data: nil) ⇒ Object
- .create_from_image(image_ref, ram, user_data: nil) ⇒ 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
135 136 137 |
# File 'lib/conoha.rb', line 135 def self.boot(server_id) server_action server_id, "os-start" end |
.create(os, ram, name_tag = nil, user_data: nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/conoha.rb', line 83 def self.create(os, ram, name_tag = nil, user_data: 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, user_data: user_data 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, user_data: nil) ⇒ Object
167 168 169 170 171 172 |
# File 'lib/conoha.rb', line 167 def self.create_from_image(image_ref, ram, user_data: nil) uri = "https://compute.#{region}.conoha.io/v2/#{tenant_id}/servers" payload = payload_for_create_vps image_ref, ram, public_key, user_data: user_data res = https_post uri, payload, authtoken JSON.parse(res.body)["server"]["id"] end |
.create_image(server_id, name) ⇒ Object
155 156 157 158 159 |
# File 'lib/conoha.rb', line 155 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
108 109 110 111 112 |
# File 'lib/conoha.rb', line 108 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
161 162 163 164 165 |
# File 'lib/conoha.rb', line 161 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
149 150 151 152 153 |
# File 'lib/conoha.rb', line 149 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
114 115 116 117 118 119 |
# File 'lib/conoha.rb', line 114 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
174 175 176 177 178 |
# File 'lib/conoha.rb', line 174 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
145 146 147 |
# File 'lib/conoha.rb', line 145 def self.reboot(server_id, type = "SOFT") server_action(server_id, "reboot", { "type" => type }) end |
.rebuild(server_id, os) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/conoha.rb', line 94 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
129 130 131 132 133 |
# File 'lib/conoha.rb', line 129 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
139 140 141 |
# File 'lib/conoha.rb', line 139 def self.shutdown(server_id) server_action server_id, "os-stop" end |
.status_of(server_id) ⇒ Object
121 122 123 124 125 |
# File 'lib/conoha.rb', line 121 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 |