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

#action_needed, #action_needed_file_path, #completion_message, included, #rest, #server, #server_warnings_file_path, #tidy

Instance Method Details

#runObject



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

def run
  reports_dir = tidy.reports_dir
  report_file_suffixes = ["_unused_cookbooks.json", "_cookbook_count.json", "_stale_nodes.json"]
  # Only grab the files matching the report_file_suffixes
  report_files = Dir["#{reports_dir}/*{#{report_file_suffixes.join(",")}}"]

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

  # Fetch list of organization names from reports directory
  begin
    org_names = report_files.map { |r_file| r_file.match("#{reports_dir}\/(.*)(#{report_file_suffixes.join("|")})").captures.first }.uniq
  rescue NoMethodError
    ui.stderr.puts "Failed to parse json reports files. Please ensure your reports are valid."
    return
  end
  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 organizations and parse any report files into JSON objects

  unless org_names
    ui.std.puts "No valid org reports found to send notifications. Exiting."
    return
  end

  org_names.each do |org|
    ui.info("Fetching report data for organization #{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] = tidy.json_file_to_hash(json_string, symbolize_names: false)
      rescue Errno::ENOENT
        ui.info("    Skipping file #{file_name} - not found for organization #{org}")
        reports[org][report] = {}
      end
    end

    # Fetch a list of admin users for the current organization
    ui.info("Fetching admins users for organization #{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 organization #{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 organization
    ui.info "Sending email reports for organization #{org}"
    email_content = generate_email(reports, org, email_recipients, report_file_suffixes)
    send_email(email_content, email_recipients)
  end
end