Class: Pooler

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

Class Method Summary collapse

Class Method Details

.check_ondemandvm(verbose, request_id, url) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vmfloaty/pooler.rb', line 71

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

  response = conn.get "ondemandvm/#{request_id}"
  res_body = JSON.parse(response.body)
  return res_body if response.status == 200

  return false if response.status == 202

  raise "HTTP #{response.status}: The request cannot be found, or an unknown error occurred" if response.status == 404

  false
end

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

Raises:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vmfloaty/pooler.rb', line 128

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

  conn = Http.get_conn(verbose, url)

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

  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

Raises:



117
118
119
120
121
122
123
124
125
126
# File 'lib/vmfloaty/pooler.rb', line 117

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

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

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

  JSON.parse(response.body)
end

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



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

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
    response_body.select { |i| i[/#{os_filter}/] }
  else
    response_body
  end
end

.list_active(verbose, url, token, _user) ⇒ Object



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

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

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

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vmfloaty/pooler.rb', line 85

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

  modify_hash.each_key do |key|
    raise ModifyError, "Configured service type does not support modification of #{key}." unless %i[tags lifetime
                                                                                                    disk].include? key
  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)

  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 ModifyError, "HTTP #{response.status}: Failed to modify VMs from the pooler vm/#{hostname}. #{res_body}"
  end
end

.query(verbose, url, hostname) ⇒ Object



160
161
162
163
164
165
# File 'lib/vmfloaty/pooler.rb', line 160

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

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

.retrieve(verbose, os_type, token, url, _user, _options, ondemand = nil, _continue = nil) ⇒ Object

Raises:



29
30
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
# File 'lib/vmfloaty/pooler.rb', line 29

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

  os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
  raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?

  response = conn.post "vm/#{os_string}" unless ondemand
  response ||= conn.post "ondemandvm/#{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}"
  elsif response.status == 403
    raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. Request exceeds the configured per pool maximum. #{res_body}"
  else
    unless ondemand
      raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
    end

    raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/ondemandvm/#{os_string}. #{res_body}"
  end
end

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

Raises:



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/vmfloaty/pooler.rb', line 177

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

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

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

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

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

Raises:



167
168
169
170
171
172
173
174
175
# File 'lib/vmfloaty/pooler.rb', line 167

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

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

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

.status(verbose, url) ⇒ Object



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

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

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

.summary(verbose, url) ⇒ Object



153
154
155
156
157
158
# File 'lib/vmfloaty/pooler.rb', line 153

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

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

.wait_for_request(verbose, request_id, url, timeout = 300) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vmfloaty/pooler.rb', line 59

def self.wait_for_request(verbose, request_id, url, timeout = 300)
  start_time = Time.now
  while check_ondemandvm(verbose, request_id, url) == false
    return false if (Time.now - start_time).to_i > timeout

    FloatyLogger.info "waiting for request #{request_id} to be fulfilled"
    sleep 5
  end
  FloatyLogger.info 'The request has been fulfilled'
  check_ondemandvm(verbose, request_id, url)
end