lib/vmpooler/api/helpers.rb in vmpooler-0.10.3 vs lib/vmpooler/api/helpers.rb in vmpooler-0.11.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Vmpooler class API module Helpers @@ -69,47 +71,48 @@ :password => password_str } ) return true if ldap.bind + return false end 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 + 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 + if ldap_base.is_a? Array + ldap_base.each do |search_base| result = authenticate_ldap( ldap_port, auth[:ldap]['host'], auth[:ldap]['user_object'], - ldap_base, + search_base, username_str, - password_str, + password_str ) - return result + return true if result == true end - - return false + 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 def export_tags(backend, hostname, tags) tags.each_pair do |tag, value| next if value.nil? or value.empty? @@ -122,10 +125,11 @@ 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 @@ -159,11 +163,11 @@ res = backend.pipelined do pools.each do |pool| backend.scard(key + pool['name']) end end - res.inject(0){ |m, x| m+x }.to_i + res.inject(0) { |m, x| m + x }.to_i end # Takes the pools and a key to run scard on # returns a hash with each pool name as key and the value being the count as integer def get_list_across_pools_redis_scard(pools, key, backend) @@ -199,39 +203,39 @@ end def get_capacity_metrics(pools, backend) capacity = { current: 0, - total: 0, + total: 0, percent: 0 } pools.each do |pool| - capacity[:total] += pool['size'].to_i + capacity[:total] += pool['size'].to_i end capacity[:current] = get_total_across_pools_redis_scard(pools, 'vmpooler__ready__', backend) if capacity[:total] > 0 - capacity[:percent] = ((capacity[:current].to_f / capacity[:total].to_f) * 100.0).round(1) + capacity[:percent] = (capacity[:current].fdiv(capacity[:total]) * 100.0).round(1) end capacity end def get_queue_metrics(pools, backend) queue = { - pending: 0, - cloning: 0, - booting: 0, - ready: 0, - running: 0, + pending: 0, + cloning: 0, + booting: 0, + ready: 0, + running: 0, completed: 0, - total: 0 + total: 0 } - queue[:pending] = get_total_across_pools_redis_scard(pools,'vmpooler__pending__', backend) + queue[:pending] = get_total_across_pools_redis_scard(pools, 'vmpooler__pending__', backend) queue[:ready] = get_total_across_pools_redis_scard(pools, 'vmpooler__ready__', backend) queue[:running] = get_total_across_pools_redis_scard(pools, 'vmpooler__running__', backend) queue[:completed] = get_total_across_pools_redis_scard(pools, 'vmpooler__completed__', backend) queue[:cloning] = backend.get('vmpooler__tasks__clone').to_i @@ -304,15 +308,15 @@ opts = {:bypool => false, :only => false}.merge(opts) task = { duration: { average: 0, - min: 0, - max: 0, - total: 0 + min: 0, + max: 0, + total: 0 }, - count: { + count: { total: 0 } } task[:count][:total] = backend.hlen('vmpooler__' + task_str + '__' + date_str).to_i @@ -448,28 +452,29 @@ end def pool_index(pools) pools_hash = {} index = 0 - for pool in pools + pools.each do |pool| pools_hash[pool['name']] = index index += 1 end pools_hash end 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 def is_integer?(x) Integer(x) true - rescue + rescue StandardError false end def open_socket(host, domain = nil, timeout = 1, port = 22, &_block) Timeout.timeout(timeout) do @@ -485,10 +490,10 @@ end def vm_ready?(vm_name, domain = nil) begin open_socket(vm_name, domain) - rescue => _err + rescue StandardError => _e return false end true end