Class: KnifePlugins::RackspaceLoadBalancerAddNode

Inherits:
Chef::Knife
  • Object
show all
Includes:
Chef::Knife::RackspaceBase, Chef::Knife::RackspaceLoadBalancerBase, Chef::Knife::RackspaceLoadBalancerNodes
Defined in:
lib/chef/knife/rackspace_load_balancer_add_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



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chef/knife/rackspace_load_balancer_add_node.rb', line 62

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 additional nodes 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,
      :port => config[:auto_resolve_port] ? "Auto resolve" : config[:port],
      :condition => config[:condition],
      :weight => config[:weight]
    }
  end

  if nodes.empty?
    ui.fatal("Node resolution did not provide a set of nodes for addition")
    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 add 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_node_ips = lb_nodes.map {|lbn| lbn[:address]}

      if config[:auto_resolve_port]
        nodes_for_balancer = nodes.dup

        port = lb_nodes.first[:port]
        ui.output(ui.color("Auto resolved port to: #{port}", :cyan))

        nodes_for_balancer.each do |nfb|
          nfb[:port] = port
        end
      else
        nodes_for_balancer = nodes
      end

      nodes_for_balancer.each do |node|
        if lb_node_ips.include?(node[:address])
          ui.warn("Skipping node #{node[:address]}")
        else
          ui.output("Adding node #{node[:address]}")
          if balancer.create_node(node)
            ui.output(ui.color("Success", :green))
          else
            ui.output(ui.color("Failed", :red))
          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