Class: VagrantPlugins::SoftLayer::Action::LoadBalancerCleanup

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

Overview

Cleanup orphaned virtual servers from load balancers.

Instance Method Summary collapse

Methods included from Util::Warden

#sl_warden

Methods included from Util::LoadBalancer

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

Constructor Details

#initialize(app, env) ⇒ LoadBalancerCleanup

Returns a new instance of LoadBalancerCleanup.



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

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

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  @env = env

  if enabled?
    setup
    cleanup!
    rebalance!
  end

  @app.call(@env)
end

#cleanup!Object



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

def cleanup!
  @env[:ui].info I18n.t("vagrant_softlayer.vm.load_balancer_cleanup")

  # Keep it safe here. We delete a virtual server only if
  # no services exist on any service group. In the future,
  # we will find a way to selectively delete empty service groups.
  @load_balancers.each do |load_balancer|
    load_balancer["virtualServers"].each do |vs|
      if vs["serviceGroups"].inject(0) { |sum, sg| sum + sg["services"].count } == 0
        @logger.debug("Found empty virtual server (ID #{vs['id']}). Deleting.")
        sl_warden { @services["VirtualServer"].object_with_id(vs["id"]).deleteObject }
      end
    end
  end
end