(maint) Fix rubocop violations

This commit fixes minor rubocopy violations in eleven source files.  Minor
violations are those that include formatting, single quotes, and recently added
classes.
This commit is contained in:
Glenn Sarti 2017-03-16 15:39:15 -07:00
parent 42b7d2d222
commit 06100ddea6
11 changed files with 204 additions and 210 deletions

View file

@ -22,7 +22,7 @@ module Vmpooler
# Load dashboard components # Load dashboard components
begin begin
require "dashboard" require 'dashboard'
rescue LoadError rescue LoadError
require File.expand_path(File.join(File.dirname(__FILE__), 'dashboard')) require File.expand_path(File.join(File.dirname(__FILE__), 'dashboard'))
end end
@ -30,7 +30,7 @@ module Vmpooler
use Vmpooler::Dashboard use Vmpooler::Dashboard
# Load API components # Load API components
%w( helpers dashboard reroute v1 ).each do |lib| %w(helpers dashboard reroute v1).each do |lib|
begin begin
require "api/#{lib}" require "api/#{lib}"
rescue LoadError rescue LoadError

View file

@ -1,146 +1,143 @@
module Vmpooler module Vmpooler
class API class API
class Dashboard < Sinatra::Base class Dashboard < Sinatra::Base
# handle to the App's configuration information
# handle to the App's configuration information def config
def config @config ||= Vmpooler::API.settings.config
@config ||= Vmpooler::API.settings.config
end
# configuration setting for server hosting graph URLs to view
def graph_server
return @graph_server if @graph_server
if config[:graphs]
return false unless config[:graphs]['server']
@graph_server = config[:graphs]['server']
elsif config[:graphite]
return false unless config[:graphite]['server']
@graph_server = config[:graphite]['server']
else
false
end
end
# configuration setting for URL prefix for graphs to view
def graph_prefix
return @graph_prefix if @graph_prefix
if config[:graphs]
return "vmpooler" unless config[:graphs]['prefix']
@graph_prefix = config[:graphs]['prefix']
elsif config[:graphite]
return false unless config[:graphite]['prefix']
@graph_prefix = config[:graphite]['prefix']
else
false
end
end
# what is the base URL for viewable graphs?
def graph_url
return false unless graph_server && graph_prefix
@graph_url ||= "http://#{graph_server}/render?target=#{graph_prefix}"
end
# return a full URL to a viewable graph for a given metrics target (graphite syntax)
def graph_link(target = "")
return "" unless graph_url
graph_url + target
end
get '/dashboard/stats/vmpooler/pool/?' do
content_type :json
result = {}
Vmpooler::API.settings.config[:pools].each do |pool|
result[pool['name']] ||= {}
result[pool['name']]['size'] = pool['size']
result[pool['name']]['ready'] = Vmpooler::API.settings.redis.scard('vmpooler__ready__' + pool['name'])
end end
if params[:history] # configuration setting for server hosting graph URLs to view
if graph_url def graph_server
history ||= {} return @graph_server if @graph_server
begin if config[:graphs]
buffer = open(graph_link('.ready.*&from=-1hour&format=json')).read return false unless config[:graphs]['server']
history = JSON.parse(buffer) @graph_server = config[:graphs]['server']
elsif config[:graphite]
return false unless config[:graphite]['server']
@graph_server = config[:graphite]['server']
else
false
end
end
history.each do |pool| # configuration setting for URL prefix for graphs to view
if pool['target'] =~ /.*\.(.*)$/ def graph_prefix
pool['name'] = Regexp.last_match[1] return @graph_prefix if @graph_prefix
if result[pool['name']] if config[:graphs]
pool['last'] = result[pool['name']]['size'] return 'vmpooler' unless config[:graphs]['prefix']
result[pool['name']]['history'] ||= Array.new @graph_prefix = config[:graphs]['prefix']
elsif config[:graphite]
return false unless config[:graphite]['prefix']
@graph_prefix = config[:graphite]['prefix']
else
false
end
end
pool['datapoints'].each do |metric| # what is the base URL for viewable graphs?
8.times do |_n| def graph_url
if metric[0] return false unless graph_server && graph_prefix
pool['last'] = metric[0].to_i @graph_url ||= "http://#{graph_server}/render?target=#{graph_prefix}"
result[pool['name']]['history'].push(metric[0].to_i) end
else
result[pool['name']]['history'].push(pool['last']) # return a full URL to a viewable graph for a given metrics target (graphite syntax)
def graph_link(target = '')
return '' unless graph_url
graph_url + target
end
get '/dashboard/stats/vmpooler/pool/?' do
content_type :json
result = {}
Vmpooler::API.settings.config[:pools].each do |pool|
result[pool['name']] ||= {}
result[pool['name']]['size'] = pool['size']
result[pool['name']]['ready'] = Vmpooler::API.settings.redis.scard('vmpooler__ready__' + pool['name'])
end
if params[:history]
if graph_url
history ||= {}
begin
buffer = open(graph_link('.ready.*&from=-1hour&format=json')).read
history = JSON.parse(buffer)
history.each do |pool|
if pool['target'] =~ /.*\.(.*)$/
pool['name'] = Regexp.last_match[1]
if result[pool['name']]
pool['last'] = result[pool['name']]['size']
result[pool['name']]['history'] ||= []
pool['datapoints'].each do |metric|
8.times do |_n|
if metric[0]
pool['last'] = metric[0].to_i
result[pool['name']]['history'].push(metric[0].to_i)
else
result[pool['name']]['history'].push(pool['last'])
end
end end
end end
end end
end end
end end
rescue
end
else
Vmpooler::API.settings.config[:pools].each do |pool|
result[pool['name']] ||= {}
result[pool['name']]['history'] = [Vmpooler::API.settings.redis.scard('vmpooler__ready__' + pool['name'])]
end end
rescue
end
else
Vmpooler::API.settings.config[:pools].each do |pool|
result[pool['name']] ||= {}
result[pool['name']]['history'] = [Vmpooler::API.settings.redis.scard('vmpooler__ready__' + pool['name'])]
end end
end end
end JSON.pretty_generate(result)
JSON.pretty_generate(result)
end
get '/dashboard/stats/vmpooler/running/?' do
content_type :json
result = {}
Vmpooler::API.settings.config[:pools].each do |pool|
running = Vmpooler::API.settings.redis.scard('vmpooler__running__' + pool['name'])
pool['major'] = Regexp.last_match[1] if pool['name'] =~ /^(\w+)\-/
result[pool['major']] ||= {}
result[pool['major']]['running'] = result[pool['major']]['running'].to_i + running.to_i
end end
if params[:history] get '/dashboard/stats/vmpooler/running/?' do
if graph_url content_type :json
begin result = {}
buffer = open(graph_link('.running.*&from=-1hour&format=json')).read
JSON.parse(buffer).each do |pool|
if pool['target'] =~ /.*\.(.*)$/
pool['name'] = Regexp.last_match[1]
pool['major'] = Regexp.last_match[1] if pool['name'] =~ /^(\w+)\-/
result[pool['major']]['history'] ||= Array.new
for i in 0..pool['datapoints'].length Vmpooler::API.settings.config[:pools].each do |pool|
if running = Vmpooler::API.settings.redis.scard('vmpooler__running__' + pool['name'])
pool['datapoints'][i] && pool['major'] = Regexp.last_match[1] if pool['name'] =~ /^(\w+)\-/
pool['datapoints'][i][0] result[pool['major']] ||= {}
pool['last'] = pool['datapoints'][i][0] result[pool['major']]['running'] = result[pool['major']]['running'].to_i + running.to_i
result[pool['major']]['history'][i] ||= 0 end
result[pool['major']]['history'][i] = result[pool['major']]['history'][i].to_i + pool['datapoints'][i][0].to_i
else if params[:history]
result[pool['major']]['history'][i] = result[pool['major']]['history'][i].to_i + pool['last'].to_i if graph_url
begin
buffer = open(graph_link('.running.*&from=-1hour&format=json')).read
JSON.parse(buffer).each do |pool|
if pool['target'] =~ /.*\.(.*)$/
pool['name'] = Regexp.last_match[1]
pool['major'] = Regexp.last_match[1] if pool['name'] =~ /^(\w+)\-/
result[pool['major']]['history'] ||= []
for i in 0..pool['datapoints'].length
if pool['datapoints'][i] && pool['datapoints'][i][0]
pool['last'] = pool['datapoints'][i][0]
result[pool['major']]['history'][i] ||= 0
result[pool['major']]['history'][i] = result[pool['major']]['history'][i].to_i + pool['datapoints'][i][0].to_i
else
result[pool['major']]['history'][i] = result[pool['major']]['history'][i].to_i + pool['last'].to_i
end
end end
end end
end end
rescue
end end
rescue
end end
end end
JSON.pretty_generate(result)
end end
JSON.pretty_generate(result)
end end
end end
end
end end

View file

@ -1,71 +1,71 @@
module Vmpooler module Vmpooler
class API class API
class Reroute < Sinatra::Base class Reroute < Sinatra::Base
api_version = '1' api_version = '1'
get '/status/?' do get '/status/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/status") call env.merge('PATH_INFO' => "/api/v#{api_version}/status")
end end
get '/summary/?' do get '/summary/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/summary") call env.merge('PATH_INFO' => "/api/v#{api_version}/summary")
end end
get '/summary/:route/?:key?/?' do get '/summary/:route/?:key?/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/summary/#{params[:route]}/#{params[:key]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/summary/#{params[:route]}/#{params[:key]}")
end end
get '/token/?' do get '/token/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/token") call env.merge('PATH_INFO' => "/api/v#{api_version}/token")
end end
post '/token/?' do post '/token/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/token") call env.merge('PATH_INFO' => "/api/v#{api_version}/token")
end end
get '/token/:token/?' do get '/token/:token/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/token/#{params[:token]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/token/#{params[:token]}")
end end
delete '/token/:token/?' do delete '/token/:token/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/token/#{params[:token]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/token/#{params[:token]}")
end end
get '/vm/?' do get '/vm/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm")
end end
post '/vm/?' do post '/vm/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm")
end end
post '/vm/:template/?' do post '/vm/:template/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:template]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:template]}")
end end
get '/vm/:hostname/?' do get '/vm/:hostname/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:hostname]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}")
end end
delete '/vm/:hostname/?' do delete '/vm/:hostname/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:hostname]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}")
end end
put '/vm/:hostname/?' do put '/vm/:hostname/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:hostname]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}")
end end
post '/vm/:hostname/snapshot/?' do post '/vm/:hostname/snapshot/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:hostname]}/snapshot") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}/snapshot")
end end
post '/vm/:hostname/snapshot/:snapshot/?' do post '/vm/:hostname/snapshot/:snapshot/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:hostname]}/snapshot/#{params[:snapshot]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}/snapshot/#{params[:snapshot]}")
end end
put '/vm/:hostname/disk/:size/?' do put '/vm/:hostname/disk/:size/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/vm/#{params[:hostname]}/disk/#{params[:size]}") call env.merge('PATH_INFO' => "/api/v#{api_version}/vm/#{params[:hostname]}/disk/#{params[:size]}")
end
end end
end end
end
end end

View file

@ -2,18 +2,18 @@ module Vmpooler
class DummyStatsd class DummyStatsd
attr_reader :server, :port, :prefix attr_reader :server, :port, :prefix
def initialize(params = {}) def initialize(*)
end end
def increment(label) def increment(*)
true true
end end
def gauge(label, value) def gauge(*)
true true
end end
def timing(label, duration) def timing(*)
true true
end end
end end

View file

@ -5,13 +5,13 @@ module Vmpooler
attr_reader :server, :port, :prefix attr_reader :server, :port, :prefix
def initialize(params = {}) def initialize(params = {})
if params["server"].nil? || params["server"].empty? if params['server'].nil? || params['server'].empty?
raise ArgumentError, "Graphite server is required. Config: #{params.inspect}" raise ArgumentError, "Graphite server is required. Config: #{params.inspect}"
end end
@server = params["server"] @server = params['server']
@port = params["port"] || 2003 @port = params['port'] || 2003
@prefix = params["prefix"] || "vmpooler" @prefix = params['prefix'] || 'vmpooler'
end end
def increment(label) def increment(label)

View file

@ -1,4 +1,4 @@
%w( base vsphere ).each do |lib| %w(base vsphere).each do |lib|
begin begin
require "vmpooler/providers/#{lib}" require "vmpooler/providers/#{lib}"
rescue LoadError rescue LoadError

View file

@ -19,16 +19,16 @@ module Vmpooler
# returns # returns
# hashtable # hashtable
# name : name of the device <---- TODO is this all? # name : name of the device <---- TODO is this all?
def vms_in_pool(pool) def vms_in_pool(_pool)
fail "#{self.class.name} does not implement vms_in_pool" raise("#{self.class.name} does not implement vms_in_pool")
end end
# inputs # inputs
# vm_name: string # vm_name: string
# returns # returns
# [String] hostname = Name of the host computer running the vm. If this is not a Virtual Machine, it returns the vm_name # [String] hostname = Name of the host computer running the vm. If this is not a Virtual Machine, it returns the vm_name
def get_vm_host(vm_name) def get_vm_host(_vm_name)
fail "#{self.class.name} does not implement get_vm_host" raise("#{self.class.name} does not implement get_vm_host")
end end
# inputs # inputs
@ -36,8 +36,8 @@ module Vmpooler
# returns # returns
# [String] hostname = Name of the most appropriate host computer to run this VM. Useful for load balancing VMs in a cluster # [String] hostname = Name of the most appropriate host computer to run this VM. Useful for load balancing VMs in a cluster
# If this is not a Virtual Machine, it returns the vm_name # If this is not a Virtual Machine, it returns the vm_name
def find_least_used_compatible_host(vm_name) def find_least_used_compatible_host(_vm_name)
fail "#{self.class.name} does not implement find_least_used_compatible_host" raise("#{self.class.name} does not implement find_least_used_compatible_host")
end end
# inputs # inputs
@ -45,8 +45,8 @@ module Vmpooler
# dest_host_name: string (Name of the host to migrate `vm_name` to) # dest_host_name: string (Name of the host to migrate `vm_name` to)
# returns # returns
# [Boolean] Returns true on success or false on failure # [Boolean] Returns true on success or false on failure
def migrate_vm_to_host(vm_name, dest_host_name) def migrate_vm_to_host(_vm_name, _dest_host_name)
fail "#{self.class.name} does not implement migrate_vm_to_host" raise("#{self.class.name} does not implement migrate_vm_to_host")
end end
# inputs # inputs
@ -61,8 +61,8 @@ module Vmpooler
# [Time] boottime = Time when the VM was created/booted # [Time] boottime = Time when the VM was created/booted
# [String] powerstate = Current power state of a VM. Valid values (as per vCenter API) # [String] powerstate = Current power state of a VM. Valid values (as per vCenter API)
# - 'PoweredOn','PoweredOff' # - 'PoweredOn','PoweredOff'
def get_vm(vm_name) def get_vm(_vm_name)
fail "#{self.class.name} does not implement get_vm" raise("#{self.class.name} does not implement get_vm")
end end
# inputs # inputs
@ -70,8 +70,8 @@ module Vmpooler
# new_vmname : string Name the new VM should use # new_vmname : string Name the new VM should use
# returns # returns
# Hashtable of the VM as per get_vm # Hashtable of the VM as per get_vm
def create_vm(pool,new_vmname) def create_vm(_pool, _new_vmname)
fail "#{self.class.name} does not implement create_vm" raise("#{self.class.name} does not implement create_vm")
end end
# inputs # inputs
@ -79,8 +79,8 @@ module Vmpooler
# pool: string # pool: string
# returns # returns
# boolean : true if success, false on error # boolean : true if success, false on error
def destroy_vm(vm_name,pool) def destroy_vm(_vm_name, _pool)
fail "#{self.class.name} does not implement destroy_vm" raise("#{self.class.name} does not implement destroy_vm")
end end
# inputs # inputs
@ -89,8 +89,8 @@ module Vmpooler
# timeout: int (Seconds) # timeout: int (Seconds)
# returns # returns
# result: boolean # result: boolean
def is_vm_ready?(vm,pool,timeout) def vm_ready?(_vm, _pool, _timeout)
fail "#{self.class.name} does not implement is_vm_ready?" raise("#{self.class.name} does not implement vm_ready?")
end end
# inputs # inputs
@ -100,7 +100,6 @@ module Vmpooler
def vm_exists?(vm) def vm_exists?(vm)
!get_vm(vm).nil? !get_vm(vm).nil?
end end
end end
end end
end end

View file

@ -1,16 +1,14 @@
module Vmpooler module Vmpooler
class PoolManager class PoolManager
class Provider class Provider
class VSphere < Vmpooler::PoolManager::Provider::Base class VSphere < Vmpooler::PoolManager::Provider::Base
def initialize(options) def initialize(options)
super(options) super(options)
end end
def name def name
'vsphere' 'vsphere'
end end
end end
end end
end end

View file

@ -6,30 +6,30 @@ module Vmpooler
attr_reader :server, :port, :prefix attr_reader :server, :port, :prefix
def initialize(params = {}) def initialize(params = {})
if params["server"].nil? || params["server"].empty? if params['server'].nil? || params['server'].empty?
raise ArgumentError, "Statsd server is required. Config: #{params.inspect}" raise ArgumentError, "Statsd server is required. Config: #{params.inspect}"
end end
host = params["server"] host = params['server']
@port = params["port"] || 8125 @port = params['port'] || 8125
@prefix = params["prefix"] || 'vmpooler' @prefix = params['prefix'] || 'vmpooler'
@server = ::Statsd.new(host, @port) @server = ::Statsd.new(host, @port)
end end
def increment(label) def increment(label)
server.increment(prefix + "." + label) server.increment(prefix + '.' + label)
rescue => err rescue => err
$stderr.puts "Failure incrementing #{prefix}.#{label} on statsd server [#{server}:#{port}]: #{err}" $stderr.puts "Failure incrementing #{prefix}.#{label} on statsd server [#{server}:#{port}]: #{err}"
end end
def gauge(label, value) def gauge(label, value)
server.gauge(prefix + "." + label, value) server.gauge(prefix + '.' + label, value)
rescue => err rescue => err
$stderr.puts "Failure updating gauge #{prefix}.#{label} on statsd server [#{server}:#{port}]: #{err}" $stderr.puts "Failure updating gauge #{prefix}.#{label} on statsd server [#{server}:#{port}]: #{err}"
end end
def timing(label, duration) def timing(label, duration)
server.timing(prefix + "." + label, duration) server.timing(prefix + '.' + label, duration)
rescue => err rescue => err
$stderr.puts "Failure updating timing #{prefix}.#{label} on statsd server [#{server}:#{port}]: #{err}" $stderr.puts "Failure updating timing #{prefix}.#{label} on statsd server [#{server}:#{port}]: #{err}"
end end

View file

@ -66,9 +66,9 @@ describe 'Vmpooler::PoolManager::Provider::Base' do
end end
end end
describe '#is_vm_ready?' do describe '#vm_ready?' do
it 'should raise error' do it 'should raise error' do
expect{subject.is_vm_ready?('vm','pool','timeout')}.to raise_error(/does not implement is_vm_ready?/) expect{subject.vm_ready?('vm','pool','timeout')}.to raise_error(/does not implement vm_ready?/)
end end
end end

View file

@ -63,9 +63,9 @@ describe 'Vmpooler::PoolManager::Provider::VSphere' do
end end
end end
describe '#is_vm_ready?' do describe '#vm_ready?' do
it 'should raise error' do it 'should raise error' do
expect{subject.is_vm_ready?('vm','pool','timeout')}.to raise_error(/does not implement is_vm_ready?/) expect{subject.vm_ready?('vm','pool','timeout')}.to raise_error(/does not implement vm_ready?/)
end end
end end