chef-email-reporter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ecd2e1f89e9c4a81506e32e576ece2e5bdd24e43
4
+ data.tar.gz: facbf38e40d4c6e6eef044b55bbc92f25d61b917
5
+ SHA512:
6
+ metadata.gz: 61bd53976a9dd62e4d7ee3866d11ea5badfda55c0cdcc986c441ded19f3ccfb32b8f5658582e68570b7c897065745f01b3d202cc69d87a116ce791285cfef540
7
+ data.tar.gz: 472fdefc7fc829bb6f5ae4f1fc269bd3e16dffa6d3bcfc13b74bd998f2977c277dde7d7c172b606e6c38c2d58da3887982c2f1055688297b25cedbad32801661
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chef-email-reporter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # chef-email-reporter
2
+
3
+ This gem extends the Chef client, allowing notifications to be sent via email
4
+ to a pre-determined address when a chef-client run fails on a node. This gives
5
+ administrators the confidence that errors on Chef nodes won't go unnoticed.
6
+
7
+ While it is possible to do this with a simple handler (see
8
+ http://docs.getchef.com/handlers.html), one typically only receives the text of
9
+ the exception with a handler. `chef-email-reporter` provides information
10
+ identical to what one would receive when running chef-client at the terminal:
11
+ the exception, the resource declaration, and the compiled resource. See below
12
+ for a sample email sent by `chef-email-reporter`.
13
+
14
+ insert image here.
15
+
16
+ Having this additional information can ease troubleshooting for administrators.
17
+
18
+ ## Installation
19
+
20
+ Install the `chef-email-reporter` gem on the node:
21
+
22
+ ```
23
+ /opt/chef/embedded/bin/gem install chef-email-reporter
24
+ ```
25
+
26
+ Add something like the following to your `/etc/chef/client.rb` file (see the
27
+ **Usage** section below for more details):
28
+
29
+ ```
30
+ gem "chef-email-reporter"
31
+ require "chef-email-reporter"
32
+
33
+ Mail.defaults do
34
+ delivery_method :smtp, address: 'smtp.example.com'
35
+ end
36
+
37
+ email_sender "no.reply@example.com"
38
+ email_recipient "chef-notifications@example.com"
39
+ ```
40
+
41
+ Profit.
42
+
43
+ ## Usage
44
+
45
+ Assuming you have already installed `chef-email-reporter` on your node, you must
46
+ activate it by adding the following to your `/etc/chef/client.rb` file:
47
+
48
+ ```
49
+ gem "chef-email-reporter"
50
+ require "chef-email-reporter"
51
+ ```
52
+
53
+ You should also specify the sender and recipient email addresses for
54
+ notifications sent by `chef-email-reporter`:
55
+
56
+ ```
57
+ email_sender "no.reply@example.com"
58
+ email_recipient "chef-notifications@example.com"
59
+ ```
60
+
61
+ Finally, depending on how you want your mail delivered, you may need to specify
62
+ some settings for `Mail.defaults`. See the examples below for an idea of what
63
+ is possible.
64
+
65
+ ### Examples
66
+
67
+ `chef-email-reporter` uses the `mail` gem to deliver email, so any delivery
68
+ methods supported by `mail` are supported. For full details, see the
69
+ documentation at https://github.com/mikel/mail. Since the documentation is
70
+ lacking slightly, you are advised to have a look in the comments in the files at
71
+ https://github.com/mikel/mail/tree/master/lib/mail/network/delivery_methods.
72
+
73
+ #### Sending email via SMTP
74
+
75
+ Add the following to `/etc/chef/client.rb`:
76
+
77
+ ```ruby
78
+ gem "chef-email-reporter"
79
+ require "chef-email-reporter"
80
+
81
+ Mail.defaults do
82
+ delivery_method :smtp, {
83
+ :address => "smtp.example.com",
84
+ :port => 25,
85
+ :domain => "example.com",
86
+ :user_name => "jeff",
87
+ :password => "secret",
88
+ :authentication => "plain",
89
+ :enable_starttls_auto => true
90
+ }
91
+ end
92
+
93
+ email_sender "no.reply@example.com"
94
+ email_recipient "chef-notifications@example.com"
95
+ ```
96
+
97
+ Examples for sending email via Gmail and Amazon SES are provided in the wiki
98
+ for the `mail` gem:
99
+
100
+ * Gmail: https://github.com/mikel/mail/wiki/Sending-email-via-Gmail-SMTP
101
+ * Amazon SES: https://github.com/mikel/mail/wiki/Sending-email-via-Amazon-SES
102
+
103
+ #### Sending email via `sendmail`
104
+
105
+ ```ruby
106
+ gem "chef-email-reporter"
107
+ require "chef-email-reporter"
108
+
109
+ Mail.defaults do
110
+ delivery_method :sendmail
111
+ end
112
+
113
+ email_sender "no.reply@example.com"
114
+ email_recipient "chef-notifications@example.com"
115
+ ```
116
+
117
+ If your `sendmail` binary is not located at `/usr/sbin/sendmail`, you can
118
+ specify its location as an argument:
119
+
120
+ ```ruby
121
+ Mail.defaults do
122
+ delivery_method :sendmail, :location => '/absolute/path/to/your/sendmail'
123
+ end
124
+ ```
125
+
126
+ #### Sending email via `exim`
127
+
128
+ ```ruby
129
+ gem "chef-email-reporter"
130
+ require "chef-email-reporter"
131
+
132
+ Mail.defaults do
133
+ delivery_method :exim
134
+ end
135
+
136
+ email_sender 'no.reply@example.com'
137
+ email_recipient 'chef-notifications@example.com'
138
+ ```
139
+
140
+ If your `exim` binary is not located at `/usr/sbin/exim`, you can specify its
141
+ location as an argument:
142
+
143
+ ```ruby
144
+ Mail.defaults do
145
+ delivery_method :exim, :location => '/absolute/path/to/your/exim'
146
+ end
147
+ ```
148
+
149
+ ## Integrating the gem into your workflow
150
+
151
+ One might wonder how to integrate into one's workflow. While the method of
152
+ integration will vary from deployment to deployment, I will briefly discuss how
153
+ I've integrated it into mine.
154
+
155
+ In my organization, I've configured bare-metal deployment via Razor Server
156
+ (https://github.com/puppetlabs/razor-server). After Razor deploys the operating
157
+ system, it runs a script on the node which:
158
+
159
+ * Installs Chef
160
+ * Installs the `chef-email-reporter` gem
161
+ * Writes a basic `/etc/chef/config.rb`
162
+ * Runs `chef-client` to register and configure the node
163
+
164
+ Of course, you don't need to be running Razor Server to use this gem. Simply
165
+ have whichever bare-metal deployment solution you're running install the gem and
166
+ configure `/etc/chef/config.rb`. If you're not running a bare-metal deployment
167
+ solution, then you'll simply have to perform these steps manually.
168
+
169
+ ## Contributing
170
+
171
+ 1. Fork it (https://github.com/jeffshantz/chef-email-reporter/fork)
172
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
173
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
174
+ 4. Push to the branch (`git push origin my-new-feature`)
175
+ 5. Create a new Pull Request
176
+
177
+ ## License
178
+
179
+ ```
180
+ Copyright 2014, Jeff Shantz
181
+
182
+ Licensed under the Apache License, Version 2.0 (the "License");
183
+ you may not use this file except in compliance with the License.
184
+ You may obtain a copy of the License at
185
+
186
+ http://www.apache.org/licenses/LICENSE-2.0
187
+
188
+ Unless required by applicable law or agreed to in writing, software
189
+ distributed under the License is distributed on an "AS IS" BASIS,
190
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
191
+ See the License for the specific language governing permissions and
192
+ limitations under the License.
193
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chef-email-reporter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chef-email-reporter"
8
+ spec.version = ChefEmailReporter::VERSION
9
+ spec.authors = ["Jeff Shantz"]
10
+ spec.email = ["jeff@csd.uwo.ca"]
11
+ spec.summary = %q{Sends chef-client errors via email.}
12
+ spec.description = %q{Sends chef-client errors via email. More detailed than a simple chef_handler.}
13
+ spec.homepage = "http://jeffshantz.github.io/chef-email-reporter"
14
+ spec.license = "Apache 2.0"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.extra_rdoc_files = ["README.md", "LICENSE"]
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.6"
23
+ spec.add_development_dependency "rake", "~> 0"
24
+
25
+ spec.add_dependency "chef", "~> 11.14"
26
+ spec.add_dependency "mail", "~> 2.6"
27
+ end
@@ -0,0 +1,200 @@
1
+ # Copyright 2014, Jeff Shantz
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # Based on Chef::Formatters::IndentableOutputStream, which is packaged with the
16
+ # chef gem.
17
+
18
+ require 'stringio'
19
+ require 'cgi'
20
+
21
+ module ChefEmailReporter
22
+
23
+ module Formatters
24
+
25
+ # Handles basic indentation and colorization tasks
26
+ class MultipartOutputStream
27
+
28
+ attr_accessor :indent
29
+ attr_reader :line_started
30
+ attr_accessor :current_stream
31
+ attr_reader :semaphore
32
+
33
+ def initialize
34
+ @text_stream = StringIO.new
35
+ @html_stream = StringIO.new
36
+ @indent = 0
37
+ @line_started = false
38
+ @semaphore = Mutex.new
39
+ end
40
+
41
+ def text_part
42
+ @text_stream.rewind
43
+ @text_stream.read
44
+ end
45
+
46
+ def html_part
47
+ @html_stream.rewind
48
+ @html_stream.read
49
+ end
50
+
51
+ # Print text. This will start a new line and indent if necessary
52
+ # but will not terminate the line (future print and puts statements
53
+ # will start off where this print left off).
54
+ def color(string, *args)
55
+ print(string, from_args(args))
56
+ end
57
+
58
+ # Print the start of a new line. This will terminate any existing lines and
59
+ # cause indentation but will not move to the next line yet (future 'print'
60
+ # and 'puts' statements will stay on this line).
61
+ def start_line(string, *args)
62
+ print(string, from_args(args, :start_line => true))
63
+ end
64
+
65
+ # Print a line. This will continue from the last start_line or print,
66
+ # or start a new line and indent if necessary.
67
+ def puts(string, *args)
68
+ print(string, from_args(args, :end_line => true))
69
+ end
70
+
71
+ # Print an entire line from start to end. This will terminate any existing
72
+ # lines and cause indentation.
73
+ def puts_line(string, *args)
74
+ print(string, from_args(args, :start_line => true, :end_line => true))
75
+ end
76
+
77
+ # Print a string.
78
+ #
79
+ # == Arguments
80
+ # string: string to print.
81
+ # options: a hash with these possible options:
82
+ # - :stream => OBJ: unique identifier for a stream. If two prints have
83
+ # different streams, they will print on separate lines.
84
+ # Otherwise, they will stay together.
85
+ # - :start_line => BOOLEAN: if true, print will begin on a blank (indented) line.
86
+ # - :end_line => BOOLEAN: if true, current line will be ended.
87
+ # - :name => STRING: a name to prefix in front of a stream. It will be printed
88
+ # once (with the first line of the stream) and subsequent lines
89
+ # will be indented to match.
90
+ #
91
+ # == Alternative
92
+ #
93
+ # You may also call print('string', :red) (a list of colors a la Highline.color)
94
+
95
+ def print(string, *args)
96
+ options = from_args(args)
97
+
98
+ # Make sure each line stays a unit even with threads sending output
99
+ semaphore.synchronize do
100
+ if should_start_line?(options)
101
+ move_to_next_line
102
+ end
103
+
104
+ print_string(string, options)
105
+
106
+ if should_end_line?(options)
107
+ move_to_next_line
108
+ end
109
+ end
110
+ end
111
+
112
+ private
113
+
114
+ def should_start_line?(options)
115
+ options[:start_line] || @current_stream != options[:stream]
116
+ end
117
+
118
+ def should_end_line?(options)
119
+ options[:end_line] && @line_started
120
+ end
121
+
122
+ def from_args(colors, merge_options = {})
123
+ if colors.size == 1 && colors[0].kind_of?(Hash)
124
+ merge_options.merge(colors[0])
125
+ else
126
+ merge_options.merge({ :colors => colors })
127
+ end
128
+ end
129
+
130
+ def print_string(string, options)
131
+ if string.empty?
132
+ if options[:end_line]
133
+ print_line('', options)
134
+ end
135
+ else
136
+ string.lines.each do |line|
137
+ print_line(line, options)
138
+ end
139
+ end
140
+ end
141
+
142
+ def escape(line)
143
+ CGI::escapeHTML(line)
144
+ end
145
+
146
+ def print_line(line, options)
147
+ indent_line(options)
148
+
149
+ # Note that the next line will need to be started
150
+ if line[-1..-1] == "\n"
151
+ @line_started = false
152
+ end
153
+
154
+ if ::Chef::Config[:color] && options[:colors] && ! options[:colors].empty?
155
+ @html_stream.print %{<span style="color: #{options[:colors].first}">#{escape(line)}</span>}
156
+ @text_stream.print line
157
+ else
158
+ @html_stream.print escape(line)
159
+ @text_stream.print line
160
+ end
161
+ end
162
+
163
+ def move_to_next_line
164
+ if @line_started
165
+ @html_stream.puts ''
166
+ @text_stream.puts ''
167
+ @line_started = false
168
+ end
169
+ end
170
+
171
+ def indent_line(options)
172
+ if !@line_started
173
+
174
+ # Print indents. If there is a stream name, either print it (if we're
175
+ # switching streams) or print enough blanks to match
176
+ # the indents.
177
+ if options[:name]
178
+ if @current_stream != options[:stream]
179
+ msg = "#{(' ' * indent)}[#{options[:name]}] "
180
+ else
181
+ msg = ' ' * (indent + 3 + options[:name].size)
182
+ end
183
+ @html_stream.print msg
184
+ @text_stream.print msg
185
+ else
186
+ # Otherwise, just print indents.
187
+ @html_stream.print ' ' * indent
188
+ @text_stream.print ' ' * indent
189
+ end
190
+
191
+ if @current_stream != options[:stream]
192
+ @current_stream = options[:stream]
193
+ end
194
+
195
+ @line_started = true
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,32 @@
1
+ # Copyright 2014, Jeff Shantz
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'chef/handler'
16
+
17
+ module ChefEmailReporter
18
+
19
+ module Handlers
20
+
21
+ class EmailHandler < ::Chef::Handler
22
+
23
+ def report
24
+ email_reporter = ChefEmailReporter::Reporters::EmailReporter.new
25
+ @run_status.events.register(email_reporter)
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,102 @@
1
+ # Copyright 2014, Jeff Shantz
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'chef/event_dispatch/base'
16
+ require 'chef/formatters/error_mapper'
17
+ require 'erb'
18
+ require 'mail'
19
+ require 'ostruct'
20
+
21
+ module ChefEmailReporter
22
+
23
+ module Reporters
24
+
25
+ class EmailReporter < ::Chef::EventDispatch::Base
26
+
27
+ def initialize
28
+ @errors = []
29
+ end
30
+
31
+ def run_started(run_status)
32
+ @run_status = run_status
33
+ end
34
+
35
+ def run_failed(exception)
36
+
37
+ error_description = ::ChefEmailReporter::Formatters::MultipartOutputStream.new
38
+
39
+ @errors.each do |desc|
40
+ desc.display(error_description)
41
+ end
42
+
43
+ namespace = OpenStruct.new({
44
+ node_name: @run_status.node[:name],
45
+ node_fqdn: @run_status.node[:fqdn],
46
+ run_id: @run_status.run_id,
47
+ start_time: @run_status.start_time
48
+ })
49
+
50
+ namespace.error = error_description.text_part
51
+ text_template = File.read(File.expand_path("../../templates/email.text.erb", __FILE__))
52
+ text_body = ERB.new(text_template).result(namespace.instance_eval { binding })
53
+
54
+ namespace.error = error_description.html_part
55
+ html_template = File.read(File.expand_path("../../templates/email.html.erb", __FILE__))
56
+ html_body = ERB.new(html_template).result(namespace.instance_eval { binding })
57
+
58
+ subject_line = "chef-client run failed (#{@run_status.node[:fqdn]})"
59
+
60
+ Mail.deliver do
61
+
62
+ from ::Chef::Config[:email_sender]
63
+ to ::Chef::Config[:email_recipient]
64
+ subject subject_line
65
+
66
+ text_part do
67
+ body text_body
68
+ end
69
+
70
+ html_part do
71
+ body html_body
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+
78
+ def resource_failed(new_resource, action, exception)
79
+ description = ::Chef::Formatters::ErrorMapper.resource_failed(new_resource, action, exception)
80
+ @errors << description
81
+ end
82
+
83
+ def run_list_expand_failed(node, exception)
84
+ description = ::Chef::Formatters::ErrorMapper.run_list_expand_failed(node, exception)
85
+ @errors << description
86
+ end
87
+
88
+ def cookbook_resolution_failed(expanded_run_list, exception)
89
+ description = ::Chef::Formatters::ErrorMapper.cookbook_resolution_failed(expanded_run_list, exception)
90
+ @errors << description
91
+ end
92
+
93
+ def cookbook_sync_failed(cookbooks, exception)
94
+ description = ::Chef::Formatters::ErrorMapper.cookbook_sync_failed(cookbooks, exception)
95
+ @errors << description
96
+ end
97
+
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -0,0 +1,71 @@
1
+ <%#
2
+ # Copyright 2014, Jeff Shantz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ %>
16
+ <html>
17
+ <head>
18
+ <style>
19
+ @import url(http://fonts.googleapis.com/css?family=Droid+Sans+Mono:400|Droid+Sans:400,700);
20
+ h1 {
21
+ font-family: 'Droid Sans', sans-serif;
22
+ color: #444;
23
+ font-size: 24px;
24
+ padding-bottom: 5px;
25
+ border-bottom: 1px solid black;
26
+ margin-bottom: 5px;
27
+ }
28
+
29
+ h2 {
30
+ font-family: 'Droid Sans', sans-serif;
31
+ color: #444;
32
+ font-size: 18px;
33
+ padding-bottom: 5px;
34
+ margin-bottom: 5px;
35
+ }
36
+
37
+ pre {
38
+ background: #001e27;
39
+ color: #708284;
40
+ }
41
+
42
+ pre code {
43
+ font-family: 'Droid Sans Mono', monospace;
44
+ }
45
+
46
+ tr td:first-child {
47
+ text-align: right
48
+ }
49
+
50
+ tr td:nth-child(2) {
51
+ padding-left: 5px;
52
+ }
53
+ </style>
54
+ <body>
55
+ <h1>An error occurred during a chef-client run.</h1>
56
+ <h2>Metadata</h2>
57
+ <table>
58
+ <tr>
59
+ <td>Hostname:</td>
60
+ <td><%= node_fqdn %></td>
61
+ </tr>
62
+ <tr>
63
+ <td>Run Start:</td>
64
+ <td><%= start_time %></td>
65
+ </tr>
66
+ </table>
67
+ <h2>Error</h2>
68
+ <pre><code><%= error %></code></pre>
69
+ </body>
70
+ </head>
71
+ </html>
@@ -0,0 +1,27 @@
1
+ <%#
2
+ # Copyright 2014, Jeff Shantz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ %>
16
+ An error occurred during a chef-client run.
17
+
18
+ Metadata
19
+ ========
20
+
21
+ Hostname: <%= node_fqdn %>
22
+ Run Start: <%= start_time %>
23
+
24
+ Error
25
+ =====
26
+
27
+ <%= error %>
@@ -0,0 +1,17 @@
1
+ # Copyright 2014, Jeff Shantz
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module ChefEmailReporter
16
+ VERSION = "0.0.1"
17
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright 2014, Jeff Shantz
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'chef-email-reporter/formatters/multipart_output_stream'
16
+ require 'chef-email-reporter/handlers/email_handler'
17
+ require 'chef-email-reporter/reporters/email_reporter'
18
+ require 'chef-email-reporter/version'
19
+
20
+ module ChefEmailReporter
21
+ end
22
+
23
+ class Chef
24
+ class Config
25
+ default :email_sender, 'no.reply@example.com'
26
+ default :email_recipient, nil
27
+ end
28
+ end
29
+
30
+ ::Chef::Config.start_handlers << ChefEmailReporter::Handlers::EmailHandler.new
31
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-email-reporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Shantz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: chef
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11.14'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '11.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mail
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.6'
69
+ description: Sends chef-client errors via email. More detailed than a simple chef_handler.
70
+ email:
71
+ - jeff@csd.uwo.ca
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - README.md
76
+ - LICENSE
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - chef-email-reporter.gemspec
84
+ - lib/chef-email-reporter.rb
85
+ - lib/chef-email-reporter/formatters/multipart_output_stream.rb
86
+ - lib/chef-email-reporter/handlers/email_handler.rb
87
+ - lib/chef-email-reporter/reporters/email_reporter.rb
88
+ - lib/chef-email-reporter/templates/email.html.erb
89
+ - lib/chef-email-reporter/templates/email.text.erb
90
+ - lib/chef-email-reporter/version.rb
91
+ homepage: http://jeffshantz.github.io/chef-email-reporter
92
+ licenses:
93
+ - Apache 2.0
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Sends chef-client errors via email.
115
+ test_files: []