Class: TP::RoleReplace

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/replace.rb

Instance Method Summary collapse

Instance Method Details

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/knife/replace.rb', line 36

def run
  s = search('role', "#{config[:search_role]}*")
  s.map do |host|
    @node = Chef::Node.load(host)

    list = @node.run_list
    ui.msg("Here is the current set of roles on this host: #{list}")

    @new_list = list.map do |ele|
      old_role_name = config[:old_role]

      if ele == "role[#{config[:old_role]}]"
        "role[#{config[:new_role]}]"
      else
        "#{ele}"
      end
    end

    ui.msg("Fixing up the run_list!\n")
    ui.msg("Here is the modified set of roles: #{@new_list}")

    @node.run_list(@new_list)
    @node.save
    ui.msg("Node run_list has been saved on #{host}.") unless !$?.success?
  end
end

#search(attribute = "*", value = "*", include_node_data = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chef/knife/replace.rb', line 24

def search(attribute = "*", value = "*", include_node_data = false)
  response = include_node_data ? {} : []
  Chef::Search::Query.new.search(:node, attribute+":"+value) do |n|
    if include_node_data then
      response[n.name] = n unless n.nil?
    else
      response << n.name unless n.nil?
    end
  end
  response
end