Class: ABS

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

Class Method Summary collapse

Class Method Details

.all_job_resources_accounted_for(allocated_resources, hosts) ⇒ Object



78
79
80
81
# File 'lib/vmfloaty/abs.rb', line 78

def self.all_job_resources_accounted_for(allocated_resources, hosts)
  allocated_host_list = allocated_resources.map { |ar| ar['hostname'] }
  (allocated_host_list - hosts).empty?
end

.check_queue(conn, job_id, req_obj) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/vmfloaty/abs.rb', line 252

def self.check_queue(conn, job_id, req_obj)
  queue_info_res = conn.get "status/queue/info/#{job_id}"
  queue_info = JSON.parse(queue_info_res.body)

  res = conn.post 'request', req_obj.to_json

  unless res.body.empty?
    res_body = JSON.parse(res.body)
    return queue_info['queue_place'], res_body
  end
  [queue_info['queue_place'], nil]
end

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



83
84
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/vmfloaty/abs.rb', line 83

def self.delete(verbose, url, hosts, token, user)
  # In ABS terms, this is a "returned" host.
  conn = Http.get_conn(verbose, url)
  conn.headers['X-AUTH-TOKEN'] = token if token

  puts "Trying to delete hosts #{hosts}" if verbose
  requests = get_active_requests(verbose, url, user)

  jobs_to_delete = []

  ret_status = {}
  hosts.each do |host|
    ret_status[host] = {
      'ok' => false,
    }
  end

  requests.each do |req_hash|
    next unless req_hash['state'] == 'allocated' || req_hash['state'] == 'filled'

    req_hash['allocated_resources'].each do |vm_name, _i|
      if hosts.include? vm_name['hostname']
        if all_job_resources_accounted_for(req_hash['allocated_resources'], hosts)
          ret_status[vm_name['hostname']] = {
            'ok' => true,
          }
          jobs_to_delete.push(req_hash)
        else
          puts "When using ABS you must delete all vms that you requested at the same time: Can't delete #{req_hash['request']['job']['id']}: #{hosts} does not include all of #{req_hash['allocated_resources']}"
        end
      end
    end
  end

  response_body = {}

  jobs_to_delete.each do |job|
    req_obj = {
      'job_id' => job['request']['job']['id'],
      'hosts'  => job['allocated_resources'],
    }

    puts "Deleting #{req_obj}" if verbose

    return_result = conn.post 'return', req_obj.to_json
    req_obj['hosts'].each do |host|
      response_body[host['hostname']] = { 'ok' => true } if return_result.body == 'OK'
    end
  end

  response_body
end

.get_active_requests(verbose, url, user) ⇒ Object



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

def self.get_active_requests(verbose, url, user)
  conn = Http.get_conn(verbose, url)
  res = conn.get 'status/queue'
  requests = JSON.parse(res.body)

  ret_val = []

  requests.each do |req|
    next if req == 'null'

    req_hash = JSON.parse(req)

    begin
      next unless user == req_hash['request']['job']['user']

      ret_val.push(req_hash)
    rescue NoMethodError
      puts "Warning: couldn't parse line returned from abs/status/queue: ".yellow
    end
  end

  ret_val
end

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

List available VMs in ABS



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vmfloaty/abs.rb', line 137

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

  os_list = []

  res = conn.get 'status/platforms/vmpooler'

  res_body = JSON.parse(res.body)
  os_list << '*** VMPOOLER Pools ***'
  os_list += JSON.parse(res_body['vmpooler_platforms'])

  res = conn.get 'status/platforms/nspooler'
  res_body = JSON.parse(res.body)
  os_list << ''
  os_list << '*** NSPOOLER Pools ***'
  os_list += JSON.parse(res_body['nspooler_platforms'])

  res = conn.get 'status/platforms/aws'
  res_body = JSON.parse(res.body)
  os_list << ''
  os_list << '*** AWS Pools ***'
  os_list += JSON.parse(res_body['aws_platforms'])

  os_list.delete 'ok'

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

.list_active(verbose, url, _token, user) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vmfloaty/abs.rb', line 42

def self.list_active(verbose, url, _token, user)
  all_jobs = []
  @active_hostnames = {}

  get_active_requests(verbose, url, user).each do |req_hash|
    all_jobs.push(req_hash['request']['job']['id'])
    @active_hostnames[req_hash['request']['job']['id']] = req_hash
  end

  all_jobs
end

.query(verbose, url, hostname) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/vmfloaty/abs.rb', line 284

def self.query(verbose, url, hostname)
  return @active_hostnames if @active_hostnames

  puts "For vmpooler/snapshot information, use '--service vmpooler' (even for vms checked out with ABS)"
  conn = Http.get_conn(verbose, url)

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

.retrieve(verbose, os_types, token, url, user, options) ⇒ Object

Retrieve an OS from ABS.

Raises:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/vmfloaty/abs.rb', line 166

def self.retrieve(verbose, os_types, token, url, user, options)
  #
  # Contents of post must be like:
  #
  # {
  #   "resources": {
  #     "centos-7-i386": 1,
  #     "ubuntu-1404-x86_64": 2
  #   },
  #   "job": {
  #     "id": "12345",
  #     "tags": {
  #       "user": "username",
  #     }
  #   }
  # }

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

  saved_job_id = DateTime.now.strftime('%Q')

  req_obj = {
    :resources => os_types,
    :job       => {
      :id   => saved_job_id,
      :tags => {
        :user => user,
      },
    },
  }

  if options['priority']
    req_obj[:priority] = if options['priority'] == 'high'
                           1
                         elsif options['priority'] == 'medium'
                           2
                         elsif options['priority'] == 'low'
                           3
                         else
                           options['priority'].to_i
                         end
  end

  puts "Posting to ABS #{req_obj.to_json}" if verbose

  # os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
  # raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
  puts "Requesting VMs with job_id: #{saved_job_id}.  Will retry for up to an hour."
  res = conn.post 'request', req_obj.to_json

  retries = 360

  raise AuthError, "HTTP #{res.status}: The token provided could not authenticate to the pooler.\n#{res_body}" if res.status == 401

  (1..retries).each do |i|
    queue_place, res_body = check_queue(conn, saved_job_id, req_obj)
    return translated(res_body) if res_body

    sleep_seconds = 10 if i >= 10
    sleep_seconds = i if i < 10
    puts "Waiting #{sleep_seconds} seconds to check if ABS request has been filled.  Queue Position: #{queue_place}... (x#{i})"

    sleep(sleep_seconds)
  end
  nil
end

.snapshot(_verbose, _url, _hostname, _token) ⇒ Object



265
266
267
# File 'lib/vmfloaty/abs.rb', line 265

def self.snapshot(_verbose, _url, _hostname, _token)
  puts "Can't snapshot with ABS, use '--service vmpooler' (even for vms checked out with ABS)"
end

.status(verbose, url) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/vmfloaty/abs.rb', line 269

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

  res = conn.get 'status'

  res.body == 'OK'
end

.summary(verbose, url) ⇒ Object



277
278
279
280
281
282
# File 'lib/vmfloaty/abs.rb', line 277

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

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

.translated(res_body) ⇒ Object

We should fix the ABS API to be more like the vmpooler or nspooler api, but for now



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/vmfloaty/abs.rb', line 237

def self.translated(res_body)
  vmpooler_formatted_body = {}

  res_body.each do |host|
    if vmpooler_formatted_body[host['type']] && vmpooler_formatted_body[host['type']]['hostname'].class == Array
      vmpooler_formatted_body[host['type']]['hostname'] << host['hostname']
    else
      vmpooler_formatted_body[host['type']] = { 'hostname' => [host['hostname']] }
    end
  end
  vmpooler_formatted_body['ok'] = true

  vmpooler_formatted_body
end