Class: KnifePlugins::RackspaceLoadBalancerDeleteNode

Inherits:
Chef::Knife
  • Object
show all
Includes:
Chef::Knife::RackspaceBase, Chef::Knife::RackspaceLoadBalancerBase, Chef::Knife::RackspaceLoadBalancerNodes
Defined in:
lib/chef/knife/rackspace_load_balancer_delete_node.rb

Instance Method Summary collapse

Methods included from Chef::Knife::RackspaceLoadBalancerNodes

#nodes_by_search, #resolve_node_ips_from_config

Methods included from Chef::Knife::RackspaceLoadBalancerBase

included, #lb_connection, #rackspace_api_credentials

Instance Method Details

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/knife/rackspace_load_balancer_delete_node.rb', line 43

def run
  unless [:all, :except, :only].any? {|target| not config[target].nil?}
    ui.fatal("Must provide a target set of load balancers with --all, --except, or --only")
    show_usage
    exit 1
  end

  unless [:by_name, :by_private_ip, :by_search].any? {|addition| not config[addition].nil?}
    ui.fatal("Must provide a set of nodes to remove with --by-name, --by-private-ip, or --by-search")
    show_usage
    exit 2
  end

  node_ips = resolve_node_ips_from_config({
    :by_search     => config[:by_search],
    :by_name       => config[:by_name],
    :by_private_ip => config[:by_private_ip]
  })

  nodes = node_ips.map do |ip|
    { :address => ip }
  end

  if nodes.empty?
    ui.fatal("Node resolution did not provide a set of nodes for removal")
    exit 3
  end

  target_load_balancers = lb_connection.list_load_balancers

  if config[:only]
    only = config[:only].split(",").map(&:to_s)
    target_load_balancers = target_load_balancers.select {|lb| only.include? lb[:id].to_s}
  end

  if config[:except]
    except = config[:except].split(",").map(&:to_s)
    target_load_balancers = target_load_balancers.reject {|lb| except.include? lb[:id].to_s}
  end

  if target_load_balancers.empty?
    ui.fatal("Load balancer resolution did not provide a set of target load balancers")
    exit 4
  end

  ui.output(format_for_display({
    :targets => target_load_balancers.map {|lb| lb[:name]},
    :nodes => nodes
  }))

  unless config[:force]
    ui.confirm("Do you really want to remove these nodes")
  end

  target_load_balancers.each do |lb|
    begin
      ui.output("Opening #{lb[:name]}")
      balancer = lb_connection.get_load_balancer(lb[:id])

      lb_nodes = balancer.list_nodes
      lb_nodes.each do |lb_node_hash|
        if node_ips.include? lb_node_hash[:address].to_s
          lb_node = balancer.get_node(lb_node_hash[:id])
          ui.output("Removing node #{lb_node.address}")
          if lb_node.destroy!
            ui.output(ui.color("Success", :green))
          end
        end
      end

    rescue CloudLB::Exception::Other => e
      ui.error("Failed on #{lb[:name]}: CloudLB::Exception [#{e.class.name}] - #{e.message}")
    end
  end

  ui.output(ui.color("Complete", :green))
end