Module: Vmpooler::API::Helpers

Defined in:
lib/vmpooler/api/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(auth, username_str, password_str) ⇒ Object



77
78
79
80
81
82
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
# File 'lib/vmpooler/api/helpers.rb', line 77

def authenticate(auth, username_str, password_str)
  case auth['provider']
    when 'dummy'
      return (username_str != password_str)
    when 'ldap'
      ldap_base = auth[:ldap]['base']
      ldap_port = auth[:ldap]['port'] || 389

      if ldap_base.is_a? Array
        ldap_base.each do |search_base|
          result = authenticate_ldap(
            ldap_port,
            auth[:ldap]['host'],
            auth[:ldap]['user_object'],
            search_base,
            username_str,
            password_str,
          )
          return true if result == true
        end
      else
        result = authenticate_ldap(
          ldap_port,
          auth[:ldap]['host'],
          auth[:ldap]['user_object'],
          ldap_base,
          username_str,
          password_str,
        )
        return result
      end

      return false
    end
end

#authenticate_ldap(port, host, user_object, base, username_str, password_str) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vmpooler/api/helpers.rb', line 57

def authenticate_ldap(port, host, user_object, base, username_str, password_str)
  ldap = Net::LDAP.new(
    :host => host,
    :port => port,
    :encryption => {
      :method => :start_tls,
      :tls_options => { :ssl_version => 'TLSv1' }
    },
    :base => base,
    :auth => {
      :method => :simple,
      :username => "#{user_object}=#{username_str},#{base}",
      :password => password_str
    }
  )

  return true if ldap.bind
  return false
end

#authorized?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vmpooler/api/helpers.rb', line 43

def authorized?
  @auth ||= Rack::Auth::Basic::Request.new(request.env)

  if @auth.provided? and @auth.basic? and @auth.credentials
    username, password = @auth.credentials

    if authenticate(Vmpooler::API.settings.config[:auth], username, password)
      return true
    end
  end

  return false
end

#export_tags(backend, hostname, tags) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/vmpooler/api/helpers.rb', line 113

def export_tags(backend, hostname, tags)
  tags.each_pair do |tag, value|
    next if value.nil? or value.empty?

    backend.hset('vmpooler__vm__' + hostname, 'tag:' + tag, value)
    backend.hset('vmpooler__tag__' + Date.today.to_s, hostname + ':' + tag, value)
  end
end

#filter_tags(tags) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/vmpooler/api/helpers.rb', line 122

def filter_tags(tags)
  return unless Vmpooler::API.settings.config[:tagfilter]

  tags.each_pair do |tag, value|
    next unless filter = Vmpooler::API.settings.config[:tagfilter][tag]
    tags[tag] = value.match(filter).captures.join if value.match(filter)
  end

  tags
end

#get_capacity_metrics(pools, backend) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/vmpooler/api/helpers.rb', line 154

def get_capacity_metrics(pools, backend)
  capacity = {
      current: 0,
      total:   0,
      percent: 0
  }

  pools.each do |pool|
    pool['capacity'] = backend.scard('vmpooler__ready__' + pool['name']).to_i

    capacity[:current] += pool['capacity']
    capacity[:total]   += pool['size'].to_i
  end

  if capacity[:total] > 0
    capacity[:percent] = ((capacity[:current].to_f / capacity[:total].to_f) * 100.0).round(1)
  end

  capacity
end

#get_queue_metrics(pools, backend) ⇒ Object



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
# File 'lib/vmpooler/api/helpers.rb', line 175

def get_queue_metrics(pools, backend)
  queue = {
      pending:   0,
      cloning:   0,
      booting:   0,
      ready:     0,
      running:   0,
      completed: 0,
      total:     0
  }

  pools.each do |pool|
    queue[:pending]   += backend.scard('vmpooler__pending__' + pool['name']).to_i
    queue[:ready]     += backend.scard('vmpooler__ready__' + pool['name']).to_i
    queue[:running]   += backend.scard('vmpooler__running__' + pool['name']).to_i
    queue[:completed] += backend.scard('vmpooler__completed__' + pool['name']).to_i
  end

  queue[:cloning] = backend.get('vmpooler__tasks__clone').to_i
  queue[:booting] = queue[:pending].to_i - queue[:cloning].to_i
  queue[:booting] = 0 if queue[:booting] < 0
  queue[:total]   = queue[:pending].to_i + queue[:ready].to_i + queue[:running].to_i + queue[:completed].to_i

  queue
end

#get_tag_metrics(backend, date_str, opts = {}) ⇒ Object



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
# File 'lib/vmpooler/api/helpers.rb', line 201

def get_tag_metrics(backend, date_str, opts = {})
  opts = {:only => false}.merge(opts)

  tags = {}

  backend.hgetall('vmpooler__tag__' + date_str).each do |key, value|
    hostname = 'unknown'
    tag = 'unknown'

    if key =~ /\:/
      hostname, tag = key.split(':', 2)
    end

    if opts[:only]
      next unless tag == opts[:only]
    end

    tags[tag] ||= {}
    tags[tag][value] ||= 0
    tags[tag][value] += 1

    tags[tag]['total'] ||= 0
    tags[tag]['total'] += 1
  end

  tags
end

#get_tag_summary(backend, from_date, to_date, opts = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/vmpooler/api/helpers.rb', line 229

def get_tag_summary(backend, from_date, to_date, opts = {})
  opts = {:only => false}.merge(opts)

  result = {
    tag: {},
    daily: []
  }

  (from_date..to_date).each do |date|
    daily = {
      date: date.to_s,
      tag: get_tag_metrics(backend, date.to_s, opts)
    }
    result[:daily].push(daily)
  end

  result[:daily].each do |daily|
    daily[:tag].each_key do |tag|
      result[:tag][tag] ||= {}

      daily[:tag][tag].each do |key, value|
        result[:tag][tag][key] ||= 0
        result[:tag][tag][key] += value
      end
    end
  end

  result
end

#get_task_metrics(backend, task_str, date_str, opts = {}) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/vmpooler/api/helpers.rb', line 259

def get_task_metrics(backend, task_str, date_str, opts = {})
  opts = {:bypool => false, :only => false}.merge(opts)

  task = {
      duration: {
          average: 0,
          min:     0,
          max:     0,
          total:   0
      },
      count:    {
          total: 0
      }
  }

  task[:count][:total] = backend.hlen('vmpooler__' + task_str + '__' + date_str).to_i

  if task[:count][:total] > 0
    if opts[:bypool] == true
      task_times_bypool = {}

      task[:count][:pool]    = {}
      task[:duration][:pool] = {}

      backend.hgetall('vmpooler__' + task_str + '__' + date_str).each do |key, value|
        pool     = 'unknown'
        hostname = 'unknown'

        if key =~ /\:/
          pool, hostname = key.split(':')
        else
          hostname = key
        end

        task[:count][:pool][pool]    ||= {}
        task[:duration][:pool][pool] ||= {}

        task_times_bypool[pool] ||= []
        task_times_bypool[pool].push(value.to_f)
      end

      task_times_bypool.each_key do |pool|
        task[:count][:pool][pool][:total] = task_times_bypool[pool].length

        task[:duration][:pool][pool][:total]                                   = task_times_bypool[pool].reduce(:+).to_f
        task[:duration][:pool][pool][:average]                                 = (task[:duration][:pool][pool][:total] / task[:count][:pool][pool][:total]).round(1)
        task[:duration][:pool][pool][:min], task[:duration][:pool][pool][:max] = task_times_bypool[pool].minmax
      end
    end

    task_times = get_task_times(backend, task_str, date_str)

    task[:duration][:total]                      = task_times.reduce(:+).to_f
    task[:duration][:average]                    = (task[:duration][:total] / task[:count][:total]).round(1)
    task[:duration][:min], task[:duration][:max] = task_times.minmax
  end

  if opts[:only]
    task.each_key do |key|
      task.delete(key) unless key.to_s == opts[:only]
    end
  end

  task
end

#get_task_summary(backend, task_str, from_date, to_date, opts = {}) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/vmpooler/api/helpers.rb', line 325

def get_task_summary(backend, task_str, from_date, to_date, opts = {})
  opts = {:bypool => false, :only => false}.merge(opts)

  task_sym = task_str.to_sym

  result = {
    task_sym => {},
    daily: []
  }

  (from_date..to_date).each do |date|
    daily = {
      date: date.to_s,
      task_sym => get_task_metrics(backend, task_str, date.to_s, opts)
    }
    result[:daily].push(daily)
  end

  daily_task = {}
  daily_task_bypool = {} if opts[:bypool] == true

  result[:daily].each do |daily|
    daily[task_sym].each_key do |type|
      result[task_sym][type] ||= {}
      daily_task[type] ||= {}

      ['min', 'max'].each do |key|
        if daily[task_sym][type][key]
          daily_task[type][:data] ||= []
          daily_task[type][:data].push(daily[task_sym][type][key])
        end
      end

      result[task_sym][type][:total] ||= 0
      result[task_sym][type][:total] += daily[task_sym][type][:total]

      if opts[:bypool] == true
        result[task_sym][type][:pool] ||= {}
        daily_task_bypool[type] ||= {}

        next unless daily[task_sym][type][:pool]

        daily[task_sym][type][:pool].each_key do |pool|
          result[task_sym][type][:pool][pool] ||= {}
          daily_task_bypool[type][pool] ||= {}

          ['min', 'max'].each do |key|
            if daily[task_sym][type][:pool][pool][key.to_sym]
              daily_task_bypool[type][pool][:data] ||= []
              daily_task_bypool[type][pool][:data].push(daily[task_sym][type][:pool][pool][key.to_sym])
            end
          end

          result[task_sym][type][:pool][pool][:total] ||= 0
          result[task_sym][type][:pool][pool][:total] += daily[task_sym][type][:pool][pool][:total]
        end
      end
    end
  end

  result[task_sym].each_key do |type|
    if daily_task[type][:data]
      result[task_sym][type][:min], result[task_sym][type][:max] = daily_task[type][:data].minmax
      result[task_sym][type][:average] = mean(daily_task[type][:data])
    end

    if opts[:bypool] == true
      result[task_sym].each_key do |type|
        result[task_sym][type][:pool].each_key do |pool|
          if daily_task_bypool[type][pool][:data]
            result[task_sym][type][:pool][pool][:min], result[task_sym][type][:pool][pool][:max] = daily_task_bypool[type][pool][:data].minmax
            result[task_sym][type][:pool][pool][:average] = mean(daily_task_bypool[type][pool][:data])
          end
        end
      end
    end
  end

  result
end

#get_task_times(backend, task, date_str) ⇒ Object



150
151
152
# File 'lib/vmpooler/api/helpers.rb', line 150

def get_task_times(backend, task, date_str)
  backend.hvals("vmpooler__#{task}__" + date_str).map(&:to_f)
end

#has_token?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/vmpooler/api/helpers.rb', line 7

def has_token?
  request.env['HTTP_X_AUTH_TOKEN'].nil? ? false : true
end

#hostname_shorten(hostname, domain = nil) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/vmpooler/api/helpers.rb', line 142

def hostname_shorten(hostname, domain=nil)
  if domain && hostname =~ /^\w+\.#{domain}$/
    hostname = hostname[/[^\.]+/]
  end

  hostname
end

#is_integer?(x) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
426
427
428
# File 'lib/vmpooler/api/helpers.rb', line 423

def is_integer?(x)
  Integer(x)
  true
rescue
  false
end

#mean(list) ⇒ Object



133
134
135
136
# File 'lib/vmpooler/api/helpers.rb', line 133

def mean(list)
  s = list.map(&:to_f).reduce(:+).to_f
  (s > 0 && list.length > 0) ? s / list.length.to_f : 0
end

#pool_index(pools) ⇒ Object



406
407
408
409
410
411
412
413
414
# File 'lib/vmpooler/api/helpers.rb', line 406

def pool_index(pools)
  pools_hash = {}
  index = 0
  for pool in pools
    pools_hash[pool['name']] = index
    index += 1
  end
  pools_hash
end

#template_ready?(pool, backend) ⇒ Boolean

Returns:

  • (Boolean)


416
417
418
419
420
421
# File 'lib/vmpooler/api/helpers.rb', line 416

def template_ready?(pool, backend)
  prepared_template = backend.hget('vmpooler__template__prepared', pool['name'])
  return false if prepared_template.nil?
  return true if pool['template'] == prepared_template
  return false
end

#valid_token?(backend) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/vmpooler/api/helpers.rb', line 11

def valid_token?(backend)
  return false unless has_token?

  backend.exists('vmpooler__token__' + request.env['HTTP_X_AUTH_TOKEN']) ? true : false
end

#validate_auth(backend) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/vmpooler/api/helpers.rb', line 32

def validate_auth(backend)
  return if authorized?

  content_type :json

  result = { 'ok' => false }

  headers['WWW-Authenticate'] = 'Basic realm="Authentication required"'
  halt 401, JSON.pretty_generate(result)
end

#validate_date_str(date_str) ⇒ Object



138
139
140
# File 'lib/vmpooler/api/helpers.rb', line 138

def validate_date_str(date_str)
  /^\d{4}-\d{2}-\d{2}$/ === date_str
end

#validate_token(backend) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vmpooler/api/helpers.rb', line 17

def validate_token(backend)
  if valid_token?(backend)
    backend.hset('vmpooler__token__' + request.env['HTTP_X_AUTH_TOKEN'], 'last', Time.now)

    return true
  end

  content_type :json

  result = { 'ok' => false }

  headers['WWW-Authenticate'] = 'Basic realm="Authentication required"'
  halt 401, JSON.pretty_generate(result)
end