Class: VagrantPlugins::SoftLayer::Action::JoinLoadBalancer

Inherits:
Object
  • Object
show all
Includes:
Util::LoadBalancer, Util::Network, Util::Warden
Defined in:
lib/vagrant-softlayer/action/join_load_balancer.rb

Overview

Look for defined load balancers and perform join operations.

Instance Method Summary collapse

Methods included from Util::Warden

#sl_warden

Methods included from Util::Network

#hostname, #ip_address, #ip_address_id, #ip_address_record, #ssh_keys

Methods included from Util::LoadBalancer

#enabled?, #read_load_balancers, #rebalance!, #setup

Constructor Details

#initialize(app, env) ⇒ JoinLoadBalancer

Returns a new instance of JoinLoadBalancer.



10
11
12
13
# File 'lib/vagrant-softlayer/action/join_load_balancer.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_softlayer::action::join_load_balancer")
end

Instance Method Details

#append_service_group(cfg, idx) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-softlayer/action/join_load_balancer.rb', line 28

def append_service_group(cfg, idx)
  {}.tap do |virtual_server|
    virtual_server["allocation"]    = 1
    virtual_server["port"]          = cfg[:port]
    virtual_server["serviceGroups"] = [
      {
        "routingMethodId" => (@enums["Routing_Method"][cfg[:method]] || 10),
        "routingTypeId"   => (@enums["Routing_Type"][cfg[:type]] || 3),
        "services"        => []
      }
    ]
    @load_balancers[idx]["virtualServers"] << virtual_server
  end
end

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vagrant-softlayer/action/join_load_balancer.rb', line 15

def call(env)
  @env = env

  if enabled?
    setup
    prepare
    join!
    rebalance!
  end

  @app.call(@env)
end

#join!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-softlayer/action/join_load_balancer.rb', line 43

def join!
  @pending = []

  until @queue.empty?
    job = @queue.pop
    merge(job[:cfg], job[:idx])
  end

  # Perform the API calls for join.
  @load_balancers.each_with_index do |lb, idx|
    next unless @pending[idx]
    @logger.debug("Updating VIP #{lb['id']} with: #{lb['virtualServers']}")
    vip_id = @services["VirtualIpAddress"].object_with_id(lb["id"])
    sl_warden { vip_id.editObject("virtualServers" => lb["virtualServers"]) }  
  end
end

#merge(cfg, idx) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant-softlayer/action/join_load_balancer.rb', line 60

def merge(cfg, idx)
  # Get the service group. Create it if not found.
  sg = @load_balancers[idx]["virtualServers"].find(lambda { append_service_group(cfg, idx) }) { |g| g["port"] == cfg[:port] }
  # Get the IP address ID of the current machine.
  ip_id = ip_address_id(@env)
  unless sg["serviceGroups"].first["services"].index { |s| s["ipAddressId"] == ip_id }
    @logger.debug("Merging service: #{cfg[:service]}")
    # Add the service to the group.
    sg["serviceGroups"].first["services"] << {
      "enabled"         => 1,
      "ipAddressId"     => ip_id,
      "groupReferences" => [ { "weight" => cfg[:service].weight } ],
      "healthChecks"    => [ { "healthCheckTypeId" => (@enums["Health_Check_Type"][cfg[:service].health_check] || 21) } ],
      "port"            => cfg[:service].destination_port
    }
    # Mark the load balancer object as pending update
    @pending[idx] = true
  end
end

#prepareObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/vagrant-softlayer/action/join_load_balancer.rb', line 80

def prepare
  @env[:ui].info I18n.t("vagrant_softlayer.vm.joining_load_balancers")
  
  # For each definition, check if the load balancer exists and enqueue
  # the join operation.
  @queue = []
  @env[:machine].provider_config.load_balancers.each do |cfg|
    idx = @load_balancers.index { |lb| lb["ipAddress"]["ipAddress"] == cfg[:vip] }
    raise Errors::SLLoadBalancerNotFound unless idx
    @queue << { :cfg => cfg, :idx => idx }
  end
end