Class: NonstandardPooler

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

Class Method Summary collapse

Class Method Details

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 92

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)

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

  response_body = {}

  unless hosts.is_a? Array
    hosts = hosts.split(',')
  end
  hosts.each do |host|
    response = conn.delete "host/#{host}"
    res_body = JSON.parse(response.body)
    response_body[host] = res_body
  end

  response_body
end

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

Raises:



80
81
82
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 80

def self.disk(verbose, url, hostname, token, disk)
  raise ModifyError, 'Configured service type does not support modification of disk space'
end

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



7
8
9
10
11
12
13
14
15
16
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 7

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

  response = conn.get 'status'
  response_body = JSON.parse(response.body)
  os_list = response_body.keys.sort
  os_list.delete 'ok'

  os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
end

.list_active(verbose, url, token) ⇒ Object



18
19
20
21
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 18

def self.list_active(verbose, url, token)
  status = Auth.token_status(verbose, url, token)
  status['reserved_hosts'] || []
end

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 53

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.each do |key, value|
    unless [:reason, :reserved_for_reason].include? key
      raise ModifyError, "Configured service type does not support modification of #{key}"
    end
  end

  if modify_hash[:reason]
    # "reason" is easier to type than "reserved_for_reason", but nspooler needs the latter
    modify_hash[:reserved_for_reason] = modify_hash.delete :reason
  end

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

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

  response.body.empty? ? {} : JSON.parse(response.body)
end

.query(verbose, url, hostname) ⇒ Object



129
130
131
132
133
134
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 129

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

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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 23

def self.retrieve(verbose, os_type, token, url)
  conn = Http.get_conn(verbose, url)
  conn.headers['X-AUTH-TOKEN'] = token if token

  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.empty?
    raise MissingParamError, 'No operating systems provided to obtain.'
  end

  response = conn.post "host/#{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}/host/#{os_string}. #{res_body}"
  end
end

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

Raises:



88
89
90
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 88

def self.revert(verbose, url, hostname, token, snapshot_sha)
  raise ModifyError, 'Configured service type does not support snapshots'
end

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

Raises:



84
85
86
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 84

def self.snapshot(verbose, url, hostname, token)
  raise ModifyError, 'Configured service type does not support snapshots'
end

.status(verbose, url) ⇒ Object



115
116
117
118
119
120
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 115

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

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

.summary(verbose, url) ⇒ Object



122
123
124
125
126
127
# File 'lib/vmfloaty/nonstandard_pooler.rb', line 122

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

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