Class: Chef::Knife::RaxClusterChange

Inherits:
Chef::Knife show all
Includes:
RaxClusterBase
Defined in:
lib/chef/knife/rax_cluster_change.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RaxClusterBase

#authenticate, included, #make_web_call, #msg_pair, #populate_environment

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/chef/knife/rax_cluster_change.rb', line 4

def headers
  @headers
end

#lb_idObject

Returns the value of attribute lb_id.



4
5
6
# File 'lib/chef/knife/rax_cluster_change.rb', line 4

def lb_id
  @lb_id
end

#rax_endpointObject

Returns the value of attribute rax_endpoint.



4
5
6
# File 'lib/chef/knife/rax_cluster_change.rb', line 4

def rax_endpoint
  @rax_endpoint
end

Instance Method Details

#change_chef_vars(instances, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/chef/knife/rax_cluster_change.rb', line 33

def change_chef_vars(instances, &block)
  instances.each { |inst|
    query = "name:#{inst['server_name']}"
    query_nodes = Chef::Search::Query.new
    query_nodes.search('node', query) do |node_item|
      yield node_item
    end
  }
end

#runObject



42
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
# File 'lib/chef/knife/rax_cluster_change.rb', line 42

def run
  if !config[:run_list] and !config[:chef_env]
    ui.fatal "Please specify either --run-list or --chef-env to change on your cluster"
    exit(1)
  end
  if @name_args.empty?
    ui.fatal "Please specify a load balancer ID to update"
    exit(1)
  end
  lb_auth = authenticate()
  headers = {"x-auth-token" => lb_auth['auth_token'], "content-type" => "application/json"}
  lb_url = ""
  lb_auth['lb_urls'].each {|lb|
    if config[:lb_region].to_s.downcase ==  lb['region'].to_s.downcase
      lb_url = lb['publicURL']
      break
    end
    lb_url = lb['publicURL']
    }
  @name_args.each {|arg|
    lb_url = lb_url + "/loadbalancers/#{arg}"
    lb_data = make_web_call("get", lb_url, headers)
    lb_data = JSON.parse(lb_data.body)          
    instances = []
    lb_data['loadBalancer']['metadata'].each{|md|
      instances << {"server_name" => md['key'], "uuid" => md['uuid']}
      }
    
    if config[:run_list]
      config[:run_list] = config[:run_list].split(",")
      change_chef_vars(instances) { |node_item|
        ui.msg "Changing #{node_item.name} run list to #{config[:run_list]}"
        node_item.run_list(config[:run_list])
        node_item.save
      }
    end
    if config[:chef_env]
      change_chef_vars(instances){|node_item|
        ui.msg "Changing #{node_item.name} chef environment to #{config[:chef_env]}"
        node_item.chef_environment(config[:chef_env])
        node_item.save
        
        }
    end
    
  }
  
end