knife-tidy 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 574027ab3af26d4383f69f812b5d4eb29c6ab51a
4
- data.tar.gz: 1b94365fec7c20cd8428873fc30cebc9e3e3fc3d
3
+ metadata.gz: 4e939b5f2365a5bc785f96d981cb13689a2d2fe8
4
+ data.tar.gz: 173c5171a7aa7370076f9ffa62d5b5aa0ad57f93
5
5
  SHA512:
6
- metadata.gz: 422a2d9b1d01215eff25ce18d261a144dd05dac39b1097baae5ba1c685135b1555449fd92b970adec770b72535d8c6c8405adfad35e0c60ea221e51326aba158
7
- data.tar.gz: 691b7f8399fb3cd3f6edb0ebd9c77a6c3b5a7685b19110200e94b568f4340f04bf075a2e59f1b18e28fb71227c00c2951e5a03651a1162be9b32f26983e0381e
6
+ metadata.gz: 63f3d78e8a54eaf7ebebbc68a60d3dc929abac189631e1d08d47cc856d6a51e366c1cf3b0e24223430ca9883bfb50cc6a2aff8325e84ff8c016189d34c25e268
7
+ data.tar.gz: 2c9fdce8962ee8e7dcbebc780733d40e3dc4297dfc6542b2367b4718f01837543e9cbff6566be7c5f89af7a93c5c1299243a68072a55a1391e236a561767dcb5
data/.travis.yml CHANGED
@@ -11,9 +11,8 @@ before_install:
11
11
  - rm -f .bundle/config
12
12
 
13
13
  rvm:
14
- - 2.2.7
15
- - 2.3.4
16
- - 2.4.1
14
+ - 2.3.5
15
+ - 2.4.2
17
16
  - ruby-head
18
17
 
19
18
  matrix:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Change Log
2
2
 
3
+ ## [0.6.0](https://github.com/chef-customers/knife-tidy/tree/0.6.0) (2017-10-23)
4
+ [Full Changelog](https://github.com/chef-customers/knife-tidy/compare/0.5.2...0.6.0)
5
+
6
+ **Merged pull requests:**
7
+
8
+ - fix travis [\#51](https://github.com/chef-customers/knife-tidy/pull/51) ([jeremymv2](https://github.com/jeremymv2))
9
+ - Add knife tidy notify command [\#50](https://github.com/chef-customers/knife-tidy/pull/50) ([jonlives](https://github.com/jonlives))
10
+ - bump to 0.5.2 [\#49](https://github.com/chef-customers/knife-tidy/pull/49) ([jeremymv2](https://github.com/jeremymv2))
11
+
3
12
  ## [0.5.2](https://github.com/chef-customers/knife-tidy/tree/0.5.2) (2017-10-20)
4
13
  [Full Changelog](https://github.com/chef-customers/knife-tidy/compare/0.5.1...0.5.2)
5
14
 
data/README.md CHANGED
@@ -111,6 +111,46 @@ knife tidy backup clean --backup-path backups/ --gsub-file substitutions.json
111
111
  }
112
112
  ```
113
113
 
114
+ ## $ knife tidy notify
115
+
116
+ The ```knife tidy notify```command is used to send a summary of the reports generated by ```knife tidy server report``` to your organisation admins.
117
+
118
+ When run from the directory containing your reports, it will iterate through the reports for each organisation in turn, and query the Chef server specified in your ```knife.rb``` for all admins of that organisation.
119
+
120
+ It will then generate a summary email from your knife tidy reports, and email it to all admins for that organisation.
121
+
122
+ This command assumes you have access to an SMTP server you can use for sending outgoing emails.
123
+
124
+ ## Options
125
+
126
+ * `--smtp_server `:
127
+ The SMTP Server to use (defaults to localhost)
128
+ * `--smtp_port `:
129
+ The SMTP Port to be used (defaults to 25)
130
+ * `--smtp_username `:
131
+ The SMTP Username to be used
132
+ * `--smtp_password `:
133
+ The SMTP Password to be used
134
+ * `--smtp_from `:
135
+ The From email address to be used when sending email reports
136
+ * `--smtp_enable_tls `:
137
+ Whether or not to enable TLS when sending reports via SMTP (defaults to false)
138
+ * `--smtp_helo `:
139
+ The SMTP HELO to be used (defaults to localhost)
140
+
141
+ Run the following example before attempting the `knife ec backup restore` operation:
142
+ ```bash
143
+ $> knife tidy notify --smtp_server smtp.myserver.com --smtp_port 587 --smtp_from myuser@myserver.com --smtp_username myuser --smtp_password mypassword --smtp_use_tls
144
+
145
+ Reading from /home/myuser/knife_tidy/reports directory
146
+ Fetching report data for organisation mytestorg
147
+ Parsing file /home/myuser/knife_tidy/reports/mytestorg_unused_cookbooks.json
148
+ Parsing file /home/myuser/knife_tidy/reports/mytestorg_cookbook_count.json
149
+ Parsing file /home/myuser/knife_tidy/reports/mytestorg_stale_nodes.json
150
+ Fetching admins users for organisation mytestorg
151
+ Sending email reports for organisation mytestorg
152
+ ```
153
+
114
154
  ## Summary and Credits
115
155
 
116
156
  * Server Report was ported from Nolan Davidson's [chef-cleanup](https://github.com/nsdavidson/chef-cleanup)
@@ -0,0 +1,171 @@
1
+ require 'chef/knife/tidy_base'
2
+
3
+ class Chef
4
+ class Knife
5
+ class TidyNotify < Knife
6
+
7
+ deps do
8
+ require 'ffi_yajl'
9
+ require 'net/smtp'
10
+ end
11
+
12
+ banner "knife tidy notify (options)"
13
+
14
+ option :smtp_server,
15
+ :short => '-s SERVER_NAME',
16
+ :long => '--smtp_server SERVER_NAME',
17
+ :default => 'localhost',
18
+ :description => 'SMTP Server to be used for emailling reports to organisation admins (defaults to localhost)'
19
+
20
+ option :smtp_port,
21
+ :short => '-p SMTP_PORT',
22
+ :long => '--smtp_port SMTP_PORT',
23
+ :default => 25,
24
+ :description => 'SMTP port to be used for emailling reports to organisation admins (defaults to 25)'
25
+
26
+ option :smtp_helo,
27
+ :short => '-h SMTP_HELO',
28
+ :long => '--smtp_helo SMTP_HELO',
29
+ :default => 'localhost',
30
+ :description => 'SMTP HELO to be used for emailling reports to organisation admins (defaults to localhost)'
31
+
32
+ option :smtp_username,
33
+ :short => '-u SMTP_USERNAME',
34
+ :long => '--smtp_username SMTP_USERNAME',
35
+ :description => 'SMTP Username to be used for emailling reports to organisation admins'
36
+
37
+ option :smtp_password,
38
+ :long => '--smtp_password SMTP_PASSWORD',
39
+ :description => 'SMTP Password to be used for emailling reports to organisation admins'
40
+
41
+ option :smtp_from,
42
+ :long => '--smtp_from SMTP_FROM',
43
+ :description => 'SMTP From address to be used for emailling reports to organisation admins'
44
+
45
+ option :smtp_use_tls,
46
+ :long => '--smtp_use_tls',
47
+ :short => '-t',
48
+ :default => false,
49
+ :boolean => true | false,
50
+ :description => 'Whether TLS should be used for emailling reports to organisation admins (defaults to false if omitted)'
51
+
52
+
53
+ include Knife::TidyBase
54
+
55
+ def run
56
+
57
+ reports_dir = tidy.reports_dir
58
+
59
+ reports_files = Dir["#{reports_dir}/*"]
60
+
61
+
62
+ ui.info "Reading from #{tidy.reports_dir} directory"
63
+
64
+ report_file_suffixes = ["_unused_cookbooks.json", "_cookbook_count.json", "_stale_nodes.json"]
65
+
66
+ # Fetch list of organisation names from reports directory
67
+ org_names = reports_files.map{|r_file|r_file.split("/").last.split("_").first}.sort.uniq
68
+
69
+ reports = {}
70
+
71
+ # Iterate through list of collected organisations and parse any report files into JSON objects
72
+
73
+ org_names.each do |org|
74
+ ui.info("Fetching report data for organisation #{org}")
75
+ reports[org] = {}
76
+ report_file_suffixes.each do |report|
77
+ begin
78
+ file_name = "#{reports_dir}/#{org}#{report}"
79
+ ui.info(" Parsing file #{file_name}")
80
+ json_string = File.read(file_name)
81
+ reports[org][report] = FFI_Yajl::Parser.parse( json_string)
82
+ rescue Errno::ENOENT
83
+ ui.info(" Skipping file #{file_name} - not found for organisation #{org}")
84
+ reports[org][report] = {}
85
+ end
86
+ end
87
+
88
+ # Fetch a list of admin users for the current organisation
89
+ ui.info("Fetching admins users for organisation #{org}")
90
+ begin
91
+ admins = org_admins(org)
92
+ reports[org]['admins'] = admins.map{|name,data| org_user(org,name) unless name == "pivotal"}
93
+ rescue Net::HTTPServerException
94
+ ui.info(" Cannot fetch admin users for organisation #{org} as it does not exist on the server")
95
+ end
96
+
97
+ # Build list of email recipients from the collected admin users (display name and email address of each)
98
+ email_recipients = reports[org]['admins'].map{|admin|{name: admin['display_name'], email: admin['email']} unless admin.nil?}.compact
99
+
100
+ # Send a report email to all admin users of the organisation
101
+ ui.info "Sending email reports for organisation #{org}"
102
+ email_content = generate_email(reports,org, email_recipients)
103
+ send_email(email_content,email_recipients)
104
+ end
105
+
106
+
107
+
108
+ end
109
+
110
+ private
111
+
112
+ def generate_email(report_data,organisation, recipients)
113
+ message = <<MESSAGE_END
114
+ From: Knife Tidy <#{config[:smtp_from]}>
115
+ To: #{recipients.map{|recipient|"#{recipient[:name]} <#{recipient[:email]}>"}.join(", ")}
116
+ Content-type: text/html
117
+ Subject: Knife Tidy Cleanup Report for Organisation "#{organisation}"
118
+
119
+ The following report was generated by <a href="https://github.com/chef-customers/knife-tidy">knife-tidy</a>, and contains a list of unused cookbooks and stale nodes for the Chef server organisation "#{organisation}"
120
+ #{generate_cookbook_table(report_data,organisation)}
121
+ <br>
122
+ #{generate_node_table(report_data,organisation)}
123
+ MESSAGE_END
124
+
125
+ message
126
+ end
127
+
128
+ def generate_cookbook_table(report_data, organisation)
129
+
130
+ table_start = "<h2>Unused Cookbooks</h2><p>This table contains cookbook names and versions that are not currently in the runlists of any nodes.<p><table border='1' cellpadding='1' cellspacing='0'>"
131
+ table_end = "</table>"
132
+ if !report_data[organisation]['_unused_cookbooks.json'].empty?
133
+ header_string = "<tr><th>Cookbook Name</th><th>Unused Versions</th></tr>"
134
+ table_body = report_data[organisation]['_unused_cookbooks.json'].map{|cookbook_name,cookbook_versions|"<tr><td>#{cookbook_name}</td><td>#{cookbook_versions.join("<br>")}</td></tr>"}.join("\n")
135
+ else
136
+ header_string = "<tr><th>Cookbook Name</th><th>Unused Version Count</th></tr>"
137
+ table_body = report_data[organisation]['_cookbook_count.json'].map{|cookbook_name,cookbook_count|"<tr><td>#{cookbook_name}</td><td>#{cookbook_count}</td></tr>"}.join("\n")
138
+ end
139
+ table_start + header_string + table_body + table_end
140
+ end
141
+
142
+ def generate_node_table(report_data,organisation)
143
+ table_start = "<h2>Stale Nodes</h2><p>This table contains nodes that have not checked in to the Chef Server in #{report_data[organisation]['_stale_nodes.json']['threshold_days']} days.<p><table border='1' cellpadding='1' cellspacing='0'>"
144
+ table_end = "</table>"
145
+ header_string = "<tr><th>Node Name</th></tr>"
146
+ table_body = report_data[organisation]['_stale_nodes.json']['list'].map{|node_name|"<tr><td>#{node_name}</td></tr>"}.join("\n")
147
+ table_start + header_string + table_body + table_end
148
+ end
149
+
150
+ def send_email(mail_content,recipients)
151
+ smtp = Net::SMTP.new(config[:smtp_server],config[:smtp_port])
152
+ smtp.enable_starttls if config[:smtp_use_tls]
153
+ smtp.start(config[:smtp_helo],config[:smtp_username],config[:smtp_password],:login) do |server|
154
+ server.send_message(mail_content, config[:smtp_from], recipients.map{|recipient|recipient[:email]})
155
+ end
156
+ end
157
+
158
+ def org_admins(org)
159
+ admins = {}
160
+ rest.get("/organizations/#{org}/groups/admins")["users"].each do |name|
161
+ admins[name] = {}
162
+ end
163
+ admins
164
+ end
165
+
166
+ def org_user(org,username)
167
+ rest.get("/organizations/#{org}/users/#{username}")
168
+ end
169
+ end
170
+ end
171
+ end
@@ -1,4 +1,4 @@
1
1
  module KnifeTidy
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  MAJOR, MINOR, TINY = VERSION.split('.')
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-tidy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Miller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-20 00:00:00.000000000 Z
11
+ date: 2017-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -114,6 +114,7 @@ files:
114
114
  - knife-tidy.gemspec
115
115
  - lib/chef/knife/tidy_backup_clean.rb
116
116
  - lib/chef/knife/tidy_base.rb
117
+ - lib/chef/knife/tidy_notify.rb
117
118
  - lib/chef/knife/tidy_server_clean.rb
118
119
  - lib/chef/knife/tidy_server_report.rb
119
120
  - lib/chef/tidy_acls.rb