Class: Conoha

Inherits:
Object
  • Object
show all
Defined in:
lib/conoha.rb,
lib/conoha/config.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Class Method Details

.authenticate!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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
  if res.code == '401'
    raise StandardError.new 'Authentication failure'
  end
  @@authtoken = JSON.parse(res.body)["access"]["token"]["id"]
  save_config!
end

.boot(server_id) ⇒ Object



92
93
94
# File 'lib/conoha.rb', line 92

def self.boot(server_id)
  server_action server_id, "os-start"
end

.create(os, ram) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/conoha.rb', line 39

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/conoha.rb', line 118

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



106
107
108
109
110
# File 'lib/conoha.rb', line 106

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



67
68
69
70
71
# File 'lib/conoha.rb', line 67

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



112
113
114
115
116
# File 'lib/conoha.rb', line 112

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

.imagesObject



100
101
102
103
104
# File 'lib/conoha.rb', line 100

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



73
74
75
76
77
# File 'lib/conoha.rb', line 73

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

.rebuild(server_id, os) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/conoha.rb', line 54

def self.rebuild(server_id, os)
  uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
  payload = {
    rebuild: {
      imageRef: image_ref_from_os(os),
      adminPass: randstr,
      key_name: public_key
    }
  }
  res = https_post uri, payload, authtoken
  res.code == '202' ? 'OK' : 'Error'
end

.server_action(server_id, action) ⇒ Object

Parameters:

  • action (String)

    “os-start” or “os-stop”



86
87
88
89
90
# File 'lib/conoha.rb', line 86

def self.server_action(server_id, action)
  uri = "https://compute.tyo1.conoha.io/v2/#{tenant_id}/servers/#{server_id}/action"
  res = https_post uri, {action => nil}, authtoken
  res.code == '202' ? 'OK' : 'Error'
end

.serversObject



29
30
31
32
33
# File 'lib/conoha.rb', line 29

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



96
97
98
# File 'lib/conoha.rb', line 96

def self.shutdown(server_id)
  server_action server_id, "os-stop"
end

.status_of(server_id) ⇒ Object



79
80
81
82
83
# File 'lib/conoha.rb', line 79

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

.vps_listObject



35
36
37
# File 'lib/conoha.rb', line 35

def self.vps_list
  servers.map { |e| e["id"] }
end