class Amphibian::BalancerManager
Public Class Methods
new(balancer_manager_url, dry_run = false)
click to toggle source
# File lib/amphibian/balancerManagerDocument.rb, line 4 def initialize(balancer_manager_url, dry_run = false) @balancer_manager_url = balancer_manager_url @dry_run = dry_run end
Public Instance Methods
balancer_manager_url()
click to toggle source
Returns the url of the BalancerManager
page.
# File lib/amphibian/balancerManagerDocument.rb, line 55 def balancer_manager_url @balancer_manager_url end
balancer_name()
click to toggle source
Returns the name of the balancer on the BalancerManager
page.
# File lib/amphibian/balancerManagerDocument.rb, line 47 def balancer_name @balancer_name ||= get_doc2().css('h3').to_s.scan(/\/\/([^"]*)</)[0][0] end
disable_host(host)
click to toggle source
Disables a host from the balancer.
# File lib/amphibian/balancerManagerDocument.rb, line 10 def disable_host(host) # TODO: Check Status toggle_host(host, 'Disable') end
disabled_hosts()
click to toggle source
# Get an array of hosts that are not in ‘Ok’ state +
# File lib/amphibian/balancerManagerDocument.rb, line 40 def disabled_hosts hosts_array = [] hosts_with_status.select{|host,state| state != 'Ok'}.each{|host, state| hosts_array << host} hosts_array end
dry_run?()
click to toggle source
# File lib/amphibian/balancerManagerDocument.rb, line 69 def dry_run? @dry_run end
enable_host(host)
click to toggle source
Enables a host in the balancer
# File lib/amphibian/balancerManagerDocument.rb, line 16 def enable_host(host) # TODO: Check Status toggle_host(host, 'Enable') end
enabled_hosts()
click to toggle source
# Get an array of hosts that are in ‘Ok’ state +
# File lib/amphibian/balancerManagerDocument.rb, line 30 def enabled_hosts hosts_array = [] hosts_with_status.select{|host,state| state == 'Ok'}.each{|host, state| hosts_array << host} hosts_array end
host_disabled?(host)
click to toggle source
TODO: Optionally force refresh
# File lib/amphibian/balancerManagerDocument.rb, line 65 def host_disabled?(host) !enabled_hosts.include?(host) end
host_enabled?(host)
click to toggle source
TODO: Optionally force refresh
# File lib/amphibian/balancerManagerDocument.rb, line 60 def host_enabled?(host) enabled_hosts.include?(host) end
hosts()
click to toggle source
Returns an array of strings indicated the balancer members parsed out of the BalancerManager
page.
# File lib/amphibian/balancerManagerDocument.rb, line 22 def hosts @hosts ||= (get_doc/'a').select{|a_tag| a_tag.inner_text =~ /^http[s]?:/}.map{|a_tag| a_tag.inner_text} end
hosts_with_status()
click to toggle source
# File lib/amphibian/balancerManagerDocument.rb, line 73 def hosts_with_status host_to_status = {} (get_doc/'a').select{|a_tag| a_tag.inner_text =~ /^http[s]?:/}.each do |a_tag| host_to_status[a_tag.inner_text] = (a_tag.parent.parent.children[6].inner_text.strip) end host_to_status end
nonce()
click to toggle source
# File lib/amphibian/balancerManagerDocument.rb, line 51 def nonce @nonce ||= get_doc2().css('a').first.to_s.scan(/nonce=([^"]*)">/)[0][0] end
Private Instance Methods
get_doc()
click to toggle source
Returns the Hpricot document of the BalancerManager
page.
# File lib/amphibian/balancerManagerDocument.rb, line 103 def get_doc @doc ||= begin Hpricot(open(@balancer_manager_url)) rescue Exception => e if e =~ /403/ log_error "Balancer Manager is getting a 403: Forbidden response. Make sure it is accessable from this location." else log_error "Error opening the balancer manager: #{e}" end nil end end
get_doc2()
click to toggle source
# File lib/amphibian/balancerManagerDocument.rb, line 115 def get_doc2 @doc ||= begin Nokogiri::HTML(open(@balancer_manager_url)) rescue Exception => e if e =~ /403/ log_error "Balancer Manager is getting a 403: Forbidden response. Make sure it is accessable from this location." else log_error "Error opening the balancer manager: #{e}" end nil end end
log_error(error = "")
click to toggle source
# File lib/amphibian/balancerManagerDocument.rb, line 98 def log_error(error = "") puts "ERROR: #{error}" end
run(cmd)
click to toggle source
runs a command if not in a dry_run? mode.
# File lib/amphibian/balancerManagerDocument.rb, line 91 def run(cmd) #puts "#{dry_run? ? 'Dry ' : ''}Running: #{cmd}" if !dry_run? `#{cmd}` end end
toggle_host(host, state)
click to toggle source
Sets the state of the host to the specified state
# File lib/amphibian/balancerManagerDocument.rb, line 84 def toggle_host(host, state) log_error("#{state} is an invalid state") if state != "Enable" && state != "Disable" Curl.get("#{@balancer_manager_url}\?b=#{balancer_name}&w=#{host}&dw=#{state}&nonce=#{nonce}") # TODO: Check status end