Module: VagrantPlugins::SoftLayer::Util::LoadBalancer

Included in:
Action::JoinLoadBalancer, Action::LoadBalancerCleanup
Defined in:
lib/vagrant-softlayer/util/load_balancer.rb

Overview

This mixin contains utility methods for load balancer management.

Instance Method Summary collapse

Instance Method Details

#enabled?Boolean

Whether load balancer management is enabled or not.

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vagrant-softlayer/util/load_balancer.rb', line 7

def enabled?
  if @env[:machine].provider_config.load_balancers.empty?
    @logger.debug("No load balancer has been defined. Going ahead.")
    return false
  end

  # Currently we don't do load balancing for private machines.
  if @env[:machine].provider_config.private_only
    @logger.info("Load balancing doesn't work for private machines. Going ahead.")
    return false
  end
  true
end

#read_load_balancersObject

Get existing stuff.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-softlayer/util/load_balancer.rb', line 22

def read_load_balancers
  mask    = [
    "id",
    "ipAddress.ipAddress",
    "virtualServers.serviceGroups.services.groupReferences",
    "virtualServers.serviceGroups.services.healthChecks"
  ]
  @logger.debug("Looking for existing load balancers.")
  @load_balancers = sl_warden { @services["Account"].object_mask(mask).getAdcLoadBalancers }
  @logger.debug("Got load balancer configuration:")
  @logger.debug("-- #{@load_balancers}")
end

#rebalance!Object

For each load balancer, check if total connections are less than 100% and, if so, rebalance the allocations.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagrant-softlayer/util/load_balancer.rb', line 37

def rebalance!
  read_load_balancers
  
  @load_balancers.each do |load_balancer|
    next if load_balancer["virtualServers"].empty?
    next if 100 == load_balancer["virtualServers"].inject(0) { |sum, vs| sum += vs["allocation"] }

    # Create allocation slots.
    count      = load_balancer["virtualServers"].count
    allocation = [100 / count] * count
    (100 % count).times { |i| allocation[i] += 1 }

    # Rebalance allocations.
    load_balancer["virtualServers"].each do |vs|
      vs["allocation"] = allocation.pop
    end

    # Update the VIP object.
    @logger.debug("Rebalancing VIP #{load_balancer['id']}")
    @logger.debug("-- #{load_balancer}")
    @services["VirtualIpAddress"].object_with_id(load_balancer["id"]).editObject("virtualServers" => load_balancer["virtualServers"])
  end
end

#setupObject

Initial setup.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vagrant-softlayer/util/load_balancer.rb', line 62

def setup
  # A plethora of service objects is required for managing
  # load balancers. We instanciate'em all here.
  @services = {
    "Account" => ::SoftLayer::Service.new("SoftLayer_Account", @env[:sl_credentials])
  }
  [
    "Health_Check_Type",
    "Routing_Method",
    "Routing_Type",
    "Service",
    "Service_Group",
    "VirtualIpAddress",
    "VirtualServer"
  ].each do |service|
    @services[service] = ::SoftLayer::Service.new(
      "SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_#{service}",
      @env[:sl_credentials]
    )
  end

  # We create enumerations for the various configurables.
  @enums = {}
  [
    "Health_Check_Type",
    "Routing_Method",
    "Routing_Type"
  ].each do |service|
    {}.tap do |enum|
      sl_warden { @services[service].getAllObjects }.each do |record|
        enum[record["name"].upcase] = record["id"]
      end
      @enums[service] = enum
    end
  end

  read_load_balancers
end