Class: DoubleDutch::SpaceCadet::LB

Inherits:
Object
  • Object
show all
Defined in:
lib/dd_spacecadet/lb.rb

Overview

LB is the class used for manging the Load Balancer configs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ LB

Returns a new instance of LB.



24
25
26
27
# File 'lib/dd_spacecadet/lb.rb', line 24

def initialize(env)
  @env = env
  @lbs = []
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/dd_spacecadet/lb.rb', line 22

def env
  @env
end

#lbsObject (readonly)

Returns the value of attribute lbs.



22
23
24
# File 'lib/dd_spacecadet/lb.rb', line 22

def lbs
  @lbs
end

Instance Method Details

#add_lb(lb_id) ⇒ Object

add an LB, by ID, to be managed by this class



35
36
37
# File 'lib/dd_spacecadet/lb.rb', line 35

def add_lb(lb_id)
  @lbs = (@lbs << lb_id).uniq
end

#find_lb(search) ⇒ Object

find an LB using a search string



40
41
42
# File 'lib/dd_spacecadet/lb.rb', line 40

def find_lb(search)
  DoubleDutch::SpaceCadet::Util.find_lb(@env, search)
end

#find_lb_and_use(search) ⇒ Object

the same as find_lb, but it adds each to the classs



45
46
47
# File 'lib/dd_spacecadet/lb.rb', line 45

def find_lb_and_use(search)
  find_lb(search).each { |lb| add_lb(lb[:id]) }
end

#render_statusObject

this does the same thing as status put it prints it the information to stdout



79
80
81
82
83
84
85
86
87
# File 'lib/dd_spacecadet/lb.rb', line 79

def render_status
  status.each do |st|
    puts "#{st[:name]} (#{st[:id]})"
    st[:nodes].each { |n| puts "    #{n[:name]}    #{n[:condition]}    #{n[:id]}    #{n[:ip]}" }
    puts '---'
  end

  nil
end

#resetObject

reset the class (clear the added LBs)



30
31
32
# File 'lib/dd_spacecadet/lb.rb', line 30

def reset
  @lbs.clear
end

#statusObject

gets the status of managed LBs



50
51
52
53
54
# File 'lib/dd_spacecadet/lb.rb', line 50

def status
  details = @lbs.map { |lb_id| get_lb_details(lb_id) }

  parse_lb_details(details)
end

#update_node(name, condition) ⇒ Object

updates the condition of a node with the Load Balancer this is used to move from :enabled => :draining

Raises:

  • (LoadBalancerNotFound)


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

def update_node(name, condition)
  # check whether the condition is valid
  unless [:enabled, :draining].include?(condition)
    raise ArgumentError, 'Invalid condition (can be :enabled or :draining)'
  end

  lb_details = status

  raise LoadBalancerNotFound, 'No LB details found!' if lb_details.empty?

  to_update = calculate_update(name.downcase, lb_details, condition)

  if to_update.size != lb_details.size
    raise LBInconsistentState, "We only found #{to_update.size} nodes across #{lb_details.size} LBs"
  end

  flush_updates(to_update)
end