Class: Chef::Knife::TidyNotify

Inherits:
Chef::Knife show all
Includes:
TidyBase
Defined in:
lib/chef/knife/tidy_notify.rb

Instance Method Summary collapse

Methods included from TidyBase

#completion_message, included, #rest, #server, #tidy

Instance Method Details

#runObject



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

def run

  reports_dir = tidy.reports_dir

  reports_files = Dir["#{reports_dir}/*"]


  ui.info "Reading from #{tidy.reports_dir} directory"

  report_file_suffixes = ["_unused_cookbooks.json", "_cookbook_count.json", "_stale_nodes.json"]

  # Fetch list of organisation names from reports directory
  org_names = reports_files.map{|r_file|r_file.split("/").last.split("_").first}.sort.uniq
  if config[:org_list]
    filter_orgs = config[:org_list].split(',')
    # Take the intersection of org_names and filter_orgs
    org_names &= filter_orgs
  end

  reports = {}

  # Iterate through list of collected organisations and parse any report files into JSON objects

  org_names.each do |org|
    ui.info("Fetching report data for organisation #{org}")
    reports[org] = {}
    report_file_suffixes.each do |report|
      begin
        file_name = "#{reports_dir}/#{org}#{report}"
        ui.info("  Parsing file #{file_name}")
        json_string = File.read(file_name)
        reports[org][report] = FFI_Yajl::Parser.parse( json_string)
      rescue Errno::ENOENT
        ui.info("    Skipping file #{file_name} - not found for organisation #{org}")
        reports[org][report] = {}
      end
    end

    # Fetch a list of admin users for the current organisation
    ui.info("Fetching admins users for organisation #{org}")
    begin
      admins = org_admins(org)
      reports[org]['admins'] = admins.map{|name,data| org_user(org,name) unless name == "pivotal"}
    rescue Net::HTTPServerException
      ui.info("  Cannot fetch admin users for organisation #{org} as it does not exist on the server")
    end

    # Build list of email recipients from the collected admin users (display name and email address of each)
    email_recipients = reports[org]['admins'].map{|admin|{name: admin['display_name'], email: admin['email']} unless admin.nil?}.compact

    # Send a report email to all admin users of the organisation
    ui.info "Sending email reports for organisation #{org}"
    email_content = generate_email(reports,org, email_recipients)
    send_email(email_content,email_recipients)
  end



end