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
- .authtoken ⇒ Object
- .boot(server_id) ⇒ Object
- .config_file_path ⇒ Object
- .config_file_string ⇒ Object
- .create(os, ram) ⇒ Object
- .create_from_image(image_ref, ram) ⇒ Object
- .create_image(server_id, name) ⇒ Object
- .delete(server_id) ⇒ Object
- .delete_image(image_ref) ⇒ Object
- .flavor_ref(ram) ⇒ Object
- .images ⇒ Object
- .init! ⇒ Object
- .ip_address_of(server_id) ⇒ Object
- .load_config! ⇒ Object
- .public_key ⇒ Object
- .randstr ⇒ Object
- .save_config! ⇒ Object
- .servers ⇒ Object
- .shutdown(server_id) ⇒ Object
- .status_of(server_id) ⇒ Object
- .tenant_id ⇒ Object
- .vps_list ⇒ Object
Class Method Details
.authenticate! ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/conoha.rb', line 10 def self.authenticate! uri = 'https://identity.tyo1.conoha.io/v2.0/tokens' payload = { auth: { passwordCredentials: { username: @@username, password: @@password }, tenantId: @@tenant_id } } res = https_post uri, payload, nil @@authtoken = JSON.parse(res.body)["access"]["token"]["id"] save_config! end |
.authtoken ⇒ Object
176 177 178 |
# File 'lib/conoha.rb', line 176 def self.authtoken @@authtoken end |
.boot(server_id) ⇒ Object
72 73 74 75 76 |
# File 'lib/conoha.rb', line 72 def self.boot(server_id) uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action" res = https_post uri, {"os-start" => nil}, authtoken res.code == '202' ? 'OK' : 'Error' end |
.config_file_path ⇒ Object
124 125 126 |
# File 'lib/conoha.rb', line 124 def self.config_file_path ENV['HOME'] + '/.conoha-config.json' end |
.config_file_string ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/conoha.rb', line 128 def self.config_file_string unless File.exist? config_file_path STDERR.print <<EOS Create "~/.conoha-config.json". For example: cat <<EOF > ~/.conoha-config.json { "username": "gncu123456789", "password": "your-password", "tenant_id": "0123456789abcdef", "public_key": "your-registered-public-key-name" } EOF chmod 600 ~/.conoha-config.json # For security EOS exit 1 end File.open(config_file_path).read end |
.create(os, ram) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/conoha.rb', line 36 def self.create(os, ram) uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers" payload = { server: { adminPass: randstr, imageRef: image_ref_from_os(os), 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_from_image(image_ref, ram) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/conoha.rb', line 102 def self.create_from_image(image_ref, ram) uri = "https://compute.tyo1.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
90 91 92 93 94 |
# File 'lib/conoha.rb', line 90 def self.create_image(server_id, name) uri = "https://compute.tyo1.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
54 55 56 57 58 |
# File 'lib/conoha.rb', line 54 def self.delete(server_id) uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}" res = https_delete uri, authtoken res.code == '204' ? 'OK' : 'Error' end |
.delete_image(image_ref) ⇒ Object
96 97 98 99 100 |
# File 'lib/conoha.rb', line 96 def self.delete_image(image_ref) uri = "https://image-service.tyo1.conoha.io/v2/images/#{image_ref}" res = https_delete uri, authtoken res.code == '204' ? 'OK' : 'Error' end |
.flavor_ref(ram) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/conoha.rb', line 184 def self.flavor_ref(ram) dictionary = { 'g-1gb' => '7eea7469-0d85-4f82-8050-6ae742394681', 'g-2gb' => '294639c7-72ba-43a5-8ff2-513c8995b869', 'g-4gb' => '62e8fb4b-6a26-46cd-be13-e5bbf5614d15', 'g-8gb' => '965affd4-d9e8-4ffb-b9a9-624d63e2d83f', 'g-16gb' => '3aa001cd-95b6-46c9-a91e-e62d6f7f06a3', 'g-32gb' => 'a20905c6-3733-46c4-81cc-458c7dca1bae', 'g-64gb' => 'c2a97b05-1b4b-4038-bbcb-343201659279', } if dictionary.keys.include? ram dictionary[ram] else STDERR.print <<EOS select ram flavor name from the following list: #{dictionary.keys.map { |e| " #{e}" }.join("\n")} EOS exit 1 end end |
.images ⇒ Object
84 85 86 87 88 |
# File 'lib/conoha.rb', line 84 def self.images uri = "https://compute.tyo1.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
60 61 62 63 64 |
# File 'lib/conoha.rb', line 60 def self.ip_address_of(server_id) uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}" res = https_get uri, authtoken JSON.parse(res.body)["server"]["addresses"].values[0].map{ |e| e["addr"] } end |
.load_config! ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/conoha.rb', line 149 def self.load_config! unless @@config_loaded config = JSON.parse config_file_string @@username = config["username"] @@password = config["password"] @@tenant_id = config["tenant_id"] @@public_key = config["public_key"] @@authtoken = config["authtoken"] @@config_loaded = true end end |
.public_key ⇒ Object
180 181 182 |
# File 'lib/conoha.rb', line 180 def self.public_key @@public_key end |
.randstr ⇒ Object
206 207 208 |
# File 'lib/conoha.rb', line 206 def self.randstr (1..60).map{['0'..'9','a'..'z','A'..'Z'].map(&:to_a).flatten.sample}.join end |
.save_config! ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/conoha.rb', line 161 def self.save_config! s = JSON.generate({ username: @@username, password: @@password, tenant_id: @@tenant_id, public_key: @@public_key, authtoken: @@authtoken, }) File.open(config_file_path, 'w').write s end |
.servers ⇒ Object
26 27 28 29 30 |
# File 'lib/conoha.rb', line 26 def self.servers uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers" res = https_get uri, authtoken JSON.parse(res.body)["servers"] end |
.shutdown(server_id) ⇒ Object
78 79 80 81 82 |
# File 'lib/conoha.rb', line 78 def self.shutdown(server_id) uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action" res = https_post uri, {"os-stop" => nil}, authtoken res.code == '202' ? 'OK' : 'Error' end |
.status_of(server_id) ⇒ Object
66 67 68 69 70 |
# File 'lib/conoha.rb', line 66 def self.status_of(server_id) uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}" res = https_get uri, authtoken JSON.parse(res.body)["server"]["status"] end |
.tenant_id ⇒ Object
172 173 174 |
# File 'lib/conoha.rb', line 172 def self.tenant_id @@tenant_id end |
.vps_list ⇒ Object
32 33 34 |
# File 'lib/conoha.rb', line 32 def self.vps_list servers.map { |e| e["id"] } end |