comrad 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c08f09d9ad560e88eeb41d1f03af82ed514696f
4
+ data.tar.gz: 10f8befdd3ef2a86856c95a2cb3269885173460e
5
+ SHA512:
6
+ metadata.gz: e9ec6b7f175bc18fbf22bbb93a0041342d971011c40931e3b189e88ca7cdcf522a5547bc02137ddbccb3154fa1cead87383cea15e05a1d1c557f9f1518da98b7
7
+ data.tar.gz: a8561367d76155d69958ebe26e80a67921ea4d2623deecb284c9a5943dfc760cf07375c9f24dd0d4bba39dc458e4ae37ac08a7afdaa3822d7d35065f3fbee9bf
data/.gitignore ADDED
@@ -0,0 +1,43 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
35
+
36
+ # OS X
37
+ .DS_Store
38
+
39
+ # reagan related files
40
+ config.yml
41
+
42
+ # vi files
43
+ *.swp
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**
4
+
5
+ AlignParameters:
6
+ Enabled: false
7
+ CyclomaticComplexity:
8
+ Enabled: false
9
+ Encoding:
10
+ Enabled: false
11
+ HashSyntax:
12
+ Enabled: false
13
+ LineLength:
14
+ Enabled: false
15
+ MethodLength:
16
+ Enabled: false
17
+ Metrics/AbcSize:
18
+ Enabled: false
19
+ NumericLiterals:
20
+ Enabled: false
21
+ SingleSpaceBeforeFirstArg:
22
+ Enabled: false
23
+ PerceivedComplexity:
24
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
4
+ - 2.2.0
5
+ script:
6
+ - bundle exec rake test
7
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'chef', '>= 11.0'
4
+ gem 'ridley', '~> 4.1'
5
+ gem 'rest-client', '~> 1.7.0'
6
+
7
+ group :test do
8
+ gem 'rubocop', '~> 0.30.0'
9
+ gem 'rake', '~> 10.4'
10
+ end
data/LICENSE ADDED
@@ -0,0 +1,202 @@
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.
202
+
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Comrad
2
+ Application to sync changes from git to Chef Server via Jenkins
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ task default: %w(test)
2
+
3
+ task :test do
4
+ sh 'rubocop'
5
+ end
data/bin/comrad ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ #
4
+ # Author:: Tim Smith (<tim@cozy.co>)
5
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+
20
+ # load the libs
21
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
22
+
23
+ require 'application'
24
+
25
+ Comrad::Application.new.run
data/comrad.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'comrad'
3
+ s.version = '0.0.1'
4
+ s.date = Date.today.to_s
5
+ s.platform = Gem::Platform::RUBY
6
+ s.extra_rdoc_files = ['README.md', 'LICENSE']
7
+ s.summary = 'Your rad comrade - Sync changes from git to Chef Server via Jenkins'
8
+ s.description = s.summary
9
+ s.authors = ['Tim Smith']
10
+ s.email = 'tim@cozy.co'
11
+ s.homepage = 'http://www.github.com/tas50/Comrad'
12
+ s.license = 'Apache-2.0'
13
+
14
+ s.required_ruby_version = '>= 1.9.3'
15
+ s.add_dependency 'rest-client', '~> 1.7.0'
16
+ s.add_dependency 'chef', '>= 11.0'
17
+ s.add_dependency 'ridley', '~> 4.0'
18
+ s.add_development_dependency 'rake', '~> 10.0'
19
+ s.add_development_dependency 'rubocop', '~> 0.29.0'
20
+
21
+ s.files = `git ls-files -z`.split("\x0")
22
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ s.require_paths = ['lib']
24
+ s.extra_rdoc_files = ['README.md']
25
+ s.rdoc_options = ['--line-numbers', '--inline-source', '--title', 'reagan', '--main', 'README.md']
26
+ end
@@ -0,0 +1,8 @@
1
+ slack:
2
+ webhook_url: 1234abcde1234abcde
3
+ jenkins:
4
+ workspace_dir: '/home/jenkins/workspace/chef-repo/'
5
+ chef:
6
+ pem_path: '/home/jenkins/.chef/jenkins.pem'
7
+ client_name: 'jenkins'
8
+ server_url: https://chef.myco.co
@@ -0,0 +1,66 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ module Comrad
20
+ # the main class for the comrad app. gets called by the comrad bin
21
+ class Application
22
+ require 'config'
23
+ require 'change'
24
+ require 'chef'
25
+ require 'string'
26
+ require 'slack'
27
+
28
+ def initialize
29
+ @config_obj = Comrad::Config.new
30
+ @config = @config_obj.settings
31
+ @changes = Comrad::Change.new(@config).changes
32
+ end
33
+
34
+ # exit with a friendly message if nothing we test has been changed
35
+ def check_empty_update
36
+ objects_updated = false
37
+ %w(cookbooks roles environments data_bags).each do |object|
38
+ objects_updated = true unless @changes[object].empty?
39
+ end
40
+
41
+ ('No objects to test. Exiting'.to_green && exit) unless objects_updated
42
+ end
43
+
44
+ # check and see if the -p flag was passed and if so print the config hash
45
+ def check_print_config
46
+ if @config['flags']['print_config']
47
+ 'Current config file / CLI flag values'.marquee
48
+ @config_obj.print
49
+ exit
50
+ end
51
+ end
52
+
53
+ # run tests on each changed cookbook
54
+ def run
55
+ check_print_config
56
+ ('No objects updated by this commit. Exiting'.to_green && exit) if check_empty_update
57
+
58
+ # print objects that will be uploaded
59
+ 'The following chef objects will be changed'.marquee
60
+ puts @changes
61
+
62
+ 'Making Chef Changes'.marquee
63
+ Comrad::Chef.new(@config, @changes).run
64
+ end
65
+ end
66
+ end
data/lib/change.rb ADDED
@@ -0,0 +1,100 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ begin
20
+ require 'json'
21
+ require 'rest_client'
22
+ require 'uri'
23
+ rescue LoadError => e
24
+ raise "Missing gem or lib #{e}"
25
+ end
26
+
27
+ module Comrad
28
+ # interacts with jenkins API to determine changes files
29
+ # parses changed files to determine relevant files that will
30
+ # need to be uploaded to chef server
31
+ class Change < Application
32
+ attr_accessor :changes
33
+ def initialize(config)
34
+ @config = config
35
+ @protocol = URI(@config['jenkins']['url']).scheme
36
+ @jenkins_host = URI(@config['jenkins']['url']).host
37
+ @username = @config['jenkins']['username']
38
+ @password = @config['jenkins']['password']
39
+ @job_name = @config['jenkins']['job_name']
40
+ @jenkins_workspace_path = @config['jenkins']['workspace_dir']
41
+ @build_num = ENV['BUILD_NUM'] || 5
42
+
43
+ @build_data = query_build_data
44
+ @changes = create_change_hash(changed_files_array)
45
+ end
46
+
47
+ # fetch build data using Jenkins API
48
+ def query_build_data
49
+ conn = RestClient::Resource.new("#{@protocol}://#{@jenkins_host}/job/#{@job_name}/#{@build_num}/api/json?", @username, @password)
50
+ JSON.parse(conn.get)
51
+ end
52
+
53
+ # parse jenkins build data to determine unique list of all files that have changed accross multiple commits
54
+ def changed_files_array
55
+ changed_files = []
56
+
57
+ @build_data['changeSet']['items'].each do |change|
58
+ changed_files.concat(change['affectedPaths'])
59
+ end
60
+
61
+ changed_files.uniq
62
+ end
63
+
64
+ # create the empty hash that stores the changed objects
65
+ def empty_chef_object_hash
66
+ objects = {}
67
+ objects['environments'] = {}
68
+ objects['roles'] = {}
69
+ objects['data_bags'] = {}
70
+ objects['cookbooks'] = {}
71
+
72
+ objects
73
+ end
74
+
75
+ # return an action based on the status of the object in the workspace
76
+ def action(file)
77
+ ::File.exist?(::File.join(@jenkins_workspace_path, file)) ? 'update' : 'delete'
78
+ end
79
+
80
+ def print
81
+ end
82
+
83
+ # takes an array of files that have changes and returns hash of chef objects and an action to take on the object
84
+ def create_change_hash(files_array)
85
+ objects = empty_chef_object_hash
86
+ files_array.each do |file|
87
+ case
88
+ when file.match(/^[cookbook|roles|environments]/)
89
+ split_file = file.split('/')
90
+ objects[split_file[0]][split_file[1]] = action(split_file[0..1].join('/'))
91
+ when file.match(/^data_bags/)
92
+ split_file = file.split('/')
93
+ objects[split_file[0]][split_file[1]] = {} unless objects[split_file[0]].key?(split_file[1])
94
+ objects[split_file[0]][split_file[1]][split_file[2]] = action(split_file[0..2].join('/'))
95
+ end
96
+ end
97
+ objects
98
+ end
99
+ end
100
+ end
data/lib/chef.rb ADDED
@@ -0,0 +1,93 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ begin
20
+ require 'ridley'
21
+ rescue LoadError => e
22
+ raise "Missing gem or lib #{e}"
23
+ end
24
+
25
+ module Comrad
26
+ # uploads / removes objects that changed from the chef server
27
+ class Chef < Application
28
+ def initialize(config, changes)
29
+ @config = config
30
+ @changes = changes
31
+ @slack = Comrad::Slack.new(config)
32
+ end
33
+
34
+ def action_string(action, trailing_text)
35
+ string = @config['flags']['dryrun'] ? " - I would #{action} " : " - #{action.capitalize.chomp('e')}ing "
36
+ string + trailing_text
37
+ end
38
+
39
+ # a really horrible method to build knife commands based on item / action / item type (cookbook/role/environment/data_bag)
40
+ def build_knife_cmd(type, action, item1, item2 = nil)
41
+ if type == 'data_bags'
42
+ action == 'delete' ? "knife data bag delete #{item1} #{item2}" : "knife data bag from file #{item1} data_bags/#{item1}/#{item2}"
43
+ elsif action == 'delete'
44
+ "knife #{type.chomp('s')} delete #{item1} #{item2}"
45
+ elsif type == 'cookbooks'
46
+ "knife cookbook #{action == 'update' ? 'upload' : 'delete'} #{item1}"
47
+ else
48
+ "knife #{type.chomp('s')} from file #{type}s/#{item1}"
49
+ end
50
+ end
51
+
52
+ # run the provided knife command
53
+ def excute_knife_cmd(cmd)
54
+ if @config['flags']['dryrun']
55
+ @slack.slack_put(" - I would be running #{cmd}")
56
+ else
57
+ @slack.slack_put(' - Non-dry mode is not implemented. Doing nothing')
58
+ end
59
+ end
60
+
61
+ # Perform actual actions on chef server and log to slack
62
+ def take_actions
63
+ @changes.each_pair do |type, name|
64
+ next if name.empty?
65
+ case
66
+ when type.match(/^['environments|roles']/)
67
+ name.each_pair do |item, action|
68
+ @slack.slack_put(action_string(action, "#{item}"))
69
+ excute_knife_cmd(build_knife_cmd(type, action, item))
70
+ end
71
+ when type == 'cookbooks'
72
+ name.each_pair do |item, action|
73
+ @slack.slack_put(action_string(action, "#{item}"))
74
+ excute_knife_cmd(build_knife_cmd(type, action, item))
75
+ end
76
+ when type == 'data_bags'
77
+ name.each_pair do |bag, item|
78
+ item.each_pair do |bag_item_name, action|
79
+ @slack.slack_put(action_string(action, "#{bag}::#{bag_item_name} data bag item"))
80
+ excute_knife_cmd(build_knife_cmd(type, action, bag, bag_item_name))
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ # called by application to perform actions
88
+ def run
89
+ @slack.slack_put("Comrad action for chef repo build # #{ENV['BUILD_NUM'] || 5}:")
90
+ take_actions
91
+ end
92
+ end # Chef class
93
+ end # Comrad Module
data/lib/config.rb ADDED
@@ -0,0 +1,110 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ begin
20
+ require 'yaml'
21
+ require 'optparse'
22
+ rescue LoadError => e
23
+ raise "Missing gem or lib #{e}"
24
+ end
25
+
26
+ module Comrad
27
+ # builds a single config from passed flags, yaml config, and knife.rb
28
+ class Config
29
+ attr_accessor :settings
30
+ def initialize
31
+ @flags = parse_flags
32
+ @config_file = load_file
33
+ @settings = merge_configs
34
+ end
35
+
36
+ # grabs the flags passed in via command line
37
+ def parse_flags
38
+ flags = { config: '/etc/comrad.yml', print_config: false, quiet: false }
39
+ OptionParser.new do |opts|
40
+ opts.banner = 'Usage: comrad [options]'
41
+
42
+ opts.on('-p', '--print', 'Print the config options that will be used') do |config|
43
+ flags[:print_config] = config
44
+ end
45
+
46
+ opts.on('-c', '--config comrad.yml', 'Path to config file (defaults to /etc/comrad.yml)') do |config|
47
+ flags[:config] = config
48
+ end
49
+
50
+ opts.on('-q', '--quiet', "Don't post actions to Slack") do |config|
51
+ flags[:quiet] = config
52
+ end
53
+
54
+ opts.on('-d', '--dry-run', "Print what you would do, but don't actually do it") do |dryrun|
55
+ flags[:dryrun] = dryrun
56
+ end
57
+
58
+ opts.on('-s', '--scary-mode', "Enable the deletion of objects if they've been removed from git") do |scary|
59
+ flags[:scary] = scary
60
+ end
61
+
62
+ opts.on('-h', '--help', 'Displays Help') do
63
+ puts opts
64
+ exit
65
+ end
66
+ end.parse!
67
+
68
+ flags
69
+ end
70
+
71
+ # loads the comrad.yml config file from /etc/comrad.yml or the passed location
72
+ def load_file
73
+ config = YAML.load_file(@flags[:config])
74
+ if config == false
75
+ puts "ERROR: Comrad config at #{@flags[:config]} does not contain any configuration data"
76
+ exit 1
77
+ end
78
+ config
79
+ rescue Errno::ENOENT
80
+ puts "ERROR: Cannot load Comrad config file at #{@flags[:config]}"
81
+ exit 1
82
+ rescue Psych::SyntaxError
83
+ puts "ERROR: Syntax error in Comrad config file at #{@flags[:config]}"
84
+ exit 1
85
+ end
86
+
87
+ # join the config file with the passed flags into a single object
88
+ def merge_configs
89
+ config = @config_file
90
+ config['flags'] = {}
91
+ @flags.each { |k, v| config['flags'][k.to_s] = v }
92
+ config
93
+ end
94
+
95
+ # pretty print the config hash
96
+ def print(hash = nil, spaces = 0)
97
+ hash = @settings if hash.nil?
98
+ hash.each do |k, v|
99
+ spaces.times { print ' ' }
100
+ print k.to_s + ': '
101
+ if v.class == Hash
102
+ print "\n"
103
+ print_config(v, spaces + 2)
104
+ else
105
+ puts v
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
data/lib/slack.rb ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ require 'rest-client'
20
+ require 'json'
21
+
22
+ # base module
23
+ module Comrad
24
+ # send notifications to slack
25
+ class Slack
26
+ def initialize(config)
27
+ @config = config
28
+ @url = config['slack']['webhook_url']
29
+ end
30
+
31
+ def slack_put(text)
32
+ puts text
33
+ post(text) unless @config['flags']['quiet']
34
+ end
35
+
36
+ def post(text)
37
+ RestClient.post @url, create_message(text).to_json
38
+ end
39
+
40
+ def create_message(text)
41
+ message = {}
42
+ message['text'] = text
43
+ message
44
+ end
45
+ end
46
+ end
data/lib/string.rb ADDED
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author:: Tim Smith (<tim@cozy.co>)
4
+ # Copyright:: Copyright (c) 2014-2015 Tim Smith
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+
19
+ # add a few methods for manipulating text
20
+ class String
21
+ def to_green
22
+ "\033[32m#{self}\033[0m"
23
+ end
24
+
25
+ def to_red
26
+ "\033[31m#{self}\033[0m"
27
+ end
28
+
29
+ def indent(double_space_count = 1)
30
+ double_space_count.times { insert(0, ' ') }
31
+ self
32
+ end
33
+
34
+ def marquee
35
+ puts "\n#{self}"
36
+ length.times { printf '-' }
37
+ puts "\n"
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: comrad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tim Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: chef
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '11.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '11.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ridley
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.29.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.29.0
83
+ description: Your rad comrade - Sync changes from git to Chef Server via Jenkins
84
+ email: tim@cozy.co
85
+ executables:
86
+ - comrad
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - README.md
90
+ files:
91
+ - .gitignore
92
+ - .rubocop.yml
93
+ - .travis.yml
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/comrad
99
+ - comrad.gemspec
100
+ - comrad.yml.EXAMPLE
101
+ - lib/application.rb
102
+ - lib/change.rb
103
+ - lib/chef.rb
104
+ - lib/config.rb
105
+ - lib/slack.rb
106
+ - lib/string.rb
107
+ homepage: http://www.github.com/tas50/Comrad
108
+ licenses:
109
+ - Apache-2.0
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options:
113
+ - --line-numbers
114
+ - --inline-source
115
+ - --title
116
+ - reagan
117
+ - --main
118
+ - README.md
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.9.3
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Your rad comrade - Sync changes from git to Chef Server via Jenkins
137
+ test_files: []
138
+ has_rdoc: