Class: KnifeAudit::Audit

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

Instance Method Summary collapse

Instance Method Details

#format_cookbook_audit_list_for_display(item) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/chef/knife/audit.rb', line 119

def format_cookbook_audit_list_for_display(item)
  key_length = item.empty? ? 0 : item.keys.map {|name| name.size }.max + 2
  if config[:show_nodelist]
    item.sort.map do |name, cookbook|
      "#{name.ljust(key_length)} #{cookbook["count"]}   [ #{cookbook["nodes"].join('  ')} ]"
    end
  else
    item.sort.map do |name, cookbook|
      "#{name.ljust(key_length)} #{cookbook["count"]}"
    end
  end
     
end

#runObject



39
40
41
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
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
# File 'lib/chef/knife/audit.rb', line 39

def run

  if @name_args.empty? 
    display_cookbooks = {}
  else
    display_cookbooks = @name_args 
  end

  self.config = Chef::Config.merge!(config)
   
  # 1) Get a list (hash, actually, with key of 'name') of cookbooks available on the current server/org
  #    unless we've been given a cookbook/cookbooks on the command line
  env   = config[:environment]
  num_versions  = config[:all_versions] ? "num_versions=all" : "num_versions=1"
  
  if display_cookbooks.empty?
    api_endpoint  = env ? "/environments/#{env}/cookbooks?#{num_versions}" : "/cookbooks?#{num_versions}"
    cookbook_list = rest.get_rest(api_endpoint)
  else
    cookbook_list = {}
    display_cookbooks.each do |cookbook_name|
      api_endpoint  = env ? "/environments/#{env}/cookbooks/#{cookbook_name}" : "cookbooks/#{cookbook_name}"
      begin
        cookbook_list.merge!(rest.get_rest(api_endpoint))
      rescue
        ui.error("Cookbook #{cookbook_name} could not be found on the server!")
        exit 1
      end 
    end 

  end


  # add count => 0 to each cookbook hash
  cookbook_list.each do |name,book|
    book["count"] = 0 
    book["nodes"] = []
  end


  # 2) Get an array of Chef::Nodes known to the current server/org

  query = "*:*"  # find all nodes

  Shef::Extensions.extend_context_object(self)
  node_list = nodes.find(query) 

  # 3) Iterate over each node

  node_list.each do |node|

    # 3a) Get node's runlist

    # using expand!.recipes catches multi-level roles (roles with roles with recipes, etc.)
    recipes = node.expand!.recipes.to_a
    node_cookbook_list = recipes.map{ |x| x.match(/[^\:]+/)[0] }.uniq

    # 3b) For each cookbook in the node runlist, if it's in our cookbook array increment its count and
    #     add the node to its running node array

    node_cookbook_list.each do |cookbook|
    if cookbook_list.has_key?(cookbook)
        # Up the cookbook count
        cookbook_list[cookbook]["count"] += 1
        # Add the node to the cookbook's nodes array 
        cookbook_list[cookbook]["nodes"] << node.name
      end
    end

  end # step 3 iterate end

  # 4) Output

  format_cookbook_audit_list_for_display(cookbook_list).each do |line|
    ui.msg(line)
  end

end