Class: Pooler

Inherits:
Object
  • Object
show all
Defined in:
lib/vmfloaty/pooler.rb

Class Method Summary collapse

Class Method Details

.delete(verbose, url, hosts, token) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/vmfloaty/pooler.rb', line 108

def self.delete(verbose, url, hosts, token)
  if token.nil?
    raise TokenError, "Token provided was nil. Request cannot be made to delete vm"
  end

  conn = Http.get_conn(verbose, url)

  if token
    conn.headers['X-AUTH-TOKEN'] = token
  end

  response_body = {}

  hosts.each do |host|
    response = conn.delete "vm/#{host}"
    res_body = JSON.parse(response.body)
    response_body[host] = res_body
  end

  response_body
end

.disk(verbose, url, hostname, token, disk) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vmfloaty/pooler.rb', line 94

def self.disk(verbose, url, hostname, token, disk)
  if token.nil?
    raise TokenError, "Token provided was nil. Request cannot be made to modify vm"
  end

  conn = Http.get_conn(verbose, url)
  conn.headers['X-AUTH-TOKEN'] = token

  response = conn.post "vm/#{hostname}/disk/#{disk}"

  res_body = JSON.parse(response.body)
  res_body
end

.list(verbose, url, os_filter = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vmfloaty/pooler.rb', line 7

def self.list(verbose, url, os_filter=nil)
  conn = Http.get_conn(verbose, url)

  response = conn.get 'vm'
  response_body = JSON.parse(response.body)

  if os_filter
    hosts = response_body.select { |i| i[/#{os_filter}/] }
  else
    hosts = response_body
  end

  hosts
end

.list_active(verbose, url, token) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/vmfloaty/pooler.rb', line 22

def self.list_active(verbose, url, token)
  status = Auth.token_status(verbose, url, token)
  vms = []
  if status[token] && status[token]['vms']
    vms = status[token]['vms']['running']
  end
  vms
end

.modify(verbose, url, hostname, token, modify_hash) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vmfloaty/pooler.rb', line 66

def self.modify(verbose, url, hostname, token, modify_hash)
  if token.nil?
    raise TokenError, "Token provided was nil. Request cannot be made to modify vm"
  end

  modify_hash.keys.each do |key|
    unless [:tags, :lifetime, :disk].include? key
      raise ModifyError, "Configured service type does not support modification of #{key}."
    end
  end

  conn = Http.get_conn(verbose, url)
  conn.headers['X-AUTH-TOKEN'] = token

  if modify_hash['disk']
    disk(verbose, url, hostname, token, modify_hash['disk'])
    modify_hash.delete 'disk'
  end

  response = conn.put do |req|
    req.url "vm/#{hostname}"
    req.body = modify_hash.to_json
  end

  res_body = JSON.parse(response.body)
  res_body
end

.query(verbose, url, hostname) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/vmfloaty/pooler.rb', line 146

def self.query(verbose, url, hostname)
  conn = Http.get_conn(verbose, url)

  response = conn.get "vm/#{hostname}"
  res_body = JSON.parse(response.body)

  res_body
end

.retrieve(verbose, os_type, token, url) ⇒ Object



31
32
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
62
63
64
# File 'lib/vmfloaty/pooler.rb', line 31

def self.retrieve(verbose, os_type, token, url)
  # NOTE:
  #   Developers can use `Utils.generate_os_hash` to
  #   generate the os_type param.
  conn = Http.get_conn(verbose, url)
  if token
    conn.headers['X-AUTH-TOKEN'] = token
  end

  os_string = ""
  os_type.each do |os,num|
    num.times do |i|
      os_string << os+"+"
    end
  end

  os_string = os_string.chomp("+")

  if os_string.size == 0
    raise MissingParamError, "No operating systems provided to obtain."
  end

  response = conn.post "vm/#{os_string}"

  res_body = JSON.parse(response.body)

  if res_body["ok"]
    res_body
  elsif response.status == 401
    raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
  else
    raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
  end
end

.revert(verbose, url, hostname, token, snapshot_sha) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/vmfloaty/pooler.rb', line 168

def self.revert(verbose, url, hostname, token, snapshot_sha)
  if token.nil?
    raise TokenError, "Token provided was nil. Request cannot be made to revert vm"
  end

  conn = Http.get_conn(verbose, url)
  conn.headers['X-AUTH-TOKEN'] = token

  if snapshot_sha.nil?
    raise "Snapshot SHA provided was nil, could not revert #{hostname}"
  end

  response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}"
  res_body = JSON.parse(response.body)
  res_body
end

.snapshot(verbose, url, hostname, token) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/vmfloaty/pooler.rb', line 155

def self.snapshot(verbose, url, hostname, token)
  if token.nil?
    raise TokenError, "Token provided was nil. Request cannot be made to snapshot vm"
  end

  conn = Http.get_conn(verbose, url)
  conn.headers['X-AUTH-TOKEN'] = token

  response = conn.post "vm/#{hostname}/snapshot"
  res_body = JSON.parse(response.body)
  res_body
end

.status(verbose, url) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/vmfloaty/pooler.rb', line 130

def self.status(verbose, url)
  conn = Http.get_conn(verbose, url)

  response = conn.get '/status'
  res_body = JSON.parse(response.body)
  res_body
end

.summary(verbose, url) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/vmfloaty/pooler.rb', line 138

def self.summary(verbose, url)
  conn = Http.get_conn(verbose, url)

  response = conn.get '/summary'
  res_body = JSON.parse(response.body)
  res_body
end