kitchen_hooks 2.0.2 → 2.1.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: ba54f91fdcaa672cb23cc618b70e3ce6146f7f5e
4
- data.tar.gz: 11d5dbebc002f951ed058746c9dd04a4c7a3dcd5
3
+ metadata.gz: 15e8bd6a3195606d749b2e9a21ac940dbc93964b
4
+ data.tar.gz: 7ad4d119770e45e838c7d3b8c978445c4b0b9681
5
5
  SHA512:
6
- metadata.gz: 3e5b28855dc3075f3ccb71c927d8dcd850d7b2542cdb6db1324a43d3be67d8e1953229901f5c56991bab810d884229150cccb075b9f88cec11ab04ab096d9439
7
- data.tar.gz: 12859577b41a687be8f2d37af00dcd220cbfcefc9066185dfdd97ae615cf305b02d0b40564e74c5cf1cc12d282668af012e709b2fa472d6297045a12a989d392
6
+ metadata.gz: 501330fc75d7b1496321b74502b51f0710062efeb7914456ee822e6de3c68d3250e6d9398fe7858f89c1e9c80800222ec93588bca70d2395d7bfb7a0ad3cd9b0
7
+ data.tar.gz: 6384176faaae34e194562ae5adc741c7b1a52788bcba881150626f166bef58a362badbcd850b80d5167192fa5c100dd346adab865a23a1c567e6117d85a69f48
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.2
1
+ 2.1.0
data/etc/config.json CHANGED
@@ -1,6 +1,10 @@
1
1
  {
2
2
  "sync_interval": null,
3
3
  "hipchat": null,
4
+ "slack": {
5
+ "token": "xoxb-xyz-...",
6
+ "channel": "tools-test"
7
+ },
4
8
  "knives": {
5
9
  "chef-z": "~/.chef/chef-z.rb"
6
10
  }
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.add_runtime_dependency 'thin', '= 1.6.3'
29
29
  s.add_runtime_dependency 'pmap', '~> 1'
30
30
  s.add_runtime_dependency 'json', '~> 1.8'
31
+ s.add_runtime_dependency 'slog', '~> 2'
31
32
 
32
33
  s.files = `git ls-files`.split("\n")
33
34
  s.test_files = `git ls-files -- test/*`.split("\n")
@@ -64,6 +64,13 @@ module KitchenHooks
64
64
  @@hipchat_nick = config['hipchat']['nick'] || raise('No HipChat "nick" provided')
65
65
  @@hipchat_room = config['hipchat']['room'] || raise('No HipChat "room" provided')
66
66
  end
67
+ @@slack = nil
68
+ if config['slack']
69
+ @@slack_token = config['slack']['token'] || raise('No Slack "token" provided')
70
+ @@slack_channel = config['slack']['channel'] || raise('No Slack "channel" provided')
71
+ require_relative 'slack_api'
72
+ @@slack = SlackAPI.new token: @@slack_token
73
+ end
67
74
  @@git_protocol = config.fetch('git_protocol', 'daemon')
68
75
  @@cookbook_upload_method = config.fetch('cookbook_upload_method', 'berkshelf')
69
76
  @@knives = config['knives'].map do |_, knife|
@@ -122,8 +129,18 @@ module KitchenHooks
122
129
  color: color, notify: false, message_format: 'html'
123
130
  end
124
131
 
132
+ def self.slack entry
133
+ return if @@slack.nil?
134
+ @@slack.send 'chat.postMessage', \
135
+ channel: @@slack_channel,
136
+ attachments: JSON.generate([
137
+ slack_notification(entry).merge(mrkdwn_in: %w[ text ])
138
+ ])
139
+ end
140
+
125
141
 
126
142
  def self.notify entry
143
+ slack entry
127
144
  color = case entry[:type]
128
145
  when 'failure' ; 'red'
129
146
  when 'release' ; 'purple'
@@ -535,6 +535,79 @@ module KitchenHooks
535
535
  end.strip
536
536
  end
537
537
 
538
+ def slack_notification e ; App.slack_notification e end
539
+
540
+ def self.slack_notification entry
541
+ if entry[:error]
542
+ return {
543
+ color: 'danger',
544
+ title: 'Error!',
545
+ text: entry[:error]
546
+ }
547
+ end
548
+
549
+ event = entry[:event]
550
+
551
+ case entry[:type]
552
+ when 'synced', 'unsynced'
553
+ title = if event.is_a? String
554
+ event
555
+ else
556
+ num_deletions = event[:num_deletions].to_i
557
+ deletions = '%s, ' % pluralize(num_deletions, 'deletion')
558
+ deletions = '' unless num_deletions > 0
559
+ 'Synced *%d* of *%d* nodes (%s%s elapsed)' % [
560
+ event[:num_successes],
561
+ event[:num_nodes],
562
+ deletions,
563
+ humanize_seconds(event[:elapsed])
564
+ ]
565
+ end
566
+ {
567
+ color: 'warning',
568
+ title: 'Sync',
569
+ text: title
570
+ }
571
+ when 'kitchen upload'
572
+ {
573
+ color: 'good',
574
+ text: "_#{author(event)}_ updated the Kitchen",
575
+ title: 'Kitchen Upload',
576
+ title_link: gitlab_url(event)
577
+ }
578
+ when 'cookbook upload'
579
+ {
580
+ color: 'good',
581
+ text: "_#{author(event)}_ released *#{tag_name(event)}* of #{cookbook_name(event)}",
582
+ title: 'Cookbook Upload',
583
+ title_link: gitlab_url(event)
584
+ }
585
+ when 'constraint application'
586
+ v = event['version'] ? " at *#{event['version']}*" : ''
587
+ {
588
+ color: 'good',
589
+ text: "_#{author(event)}_ constrained *#{tag_name(event)}* with #{cookbook_name(event)}#{v}",
590
+ title: 'Constraint',
591
+ title_link: gitlab_url(event)
592
+ }
593
+ when 'release'
594
+ {
595
+ color: '#439FE0',
596
+ text: "Kitchen Hooks *v#{event}* released!",
597
+ title: 'Release'
598
+ }
599
+ when 'upload from files'
600
+ {
601
+ color: 'good',
602
+ text: "_#{author(event)}_ uploaded files to Chef",
603
+ title: 'File Upload',
604
+ title_link: gitlab_url(event)
605
+ }
606
+ else
607
+ raise entry.inspect
608
+ end
609
+ end
610
+
538
611
 
539
612
  def generic_details e ; App.generic_details e end
540
613
 
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ require 'slog'
5
+
6
+ Thread.abort_on_exception = true
7
+
8
+
9
+ module KitchenHooks
10
+ class SlackAPI
11
+ def initialize options={}
12
+ @token = options.fetch :token
13
+ @logger = options.fetch :logger, Slog.new
14
+ @api_url = options.fetch :api_url, 'https://slack.com/api'
15
+ log.debug event: 'API client initialized'
16
+ end
17
+
18
+ def send method, options={}
19
+ uri = URI File.join(@api_url, method)
20
+ options = { token: @token }.merge(options)
21
+ log.debug event: 'sending api request', method: method, options: options
22
+ res = Net::HTTP.post_form uri, options
23
+ log.debug event: 'sent api request', method: method, options: options, response: res
24
+ JSON.parse res.body
25
+ end
26
+
27
+ private
28
+ def log ; @logger end
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kitchen_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Clemmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-06 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hipchat
@@ -206,6 +206,20 @@ dependencies:
206
206
  - - "~>"
207
207
  - !ruby/object:Gem::Version
208
208
  version: '1.8'
209
+ - !ruby/object:Gem::Dependency
210
+ name: slog
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: '2'
216
+ type: :runtime
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: '2'
209
223
  description: GitLab WebHoook for automated Chef Server uploads.
210
224
  email: sclemmer@bluejeans.com
211
225
  executables:
@@ -233,6 +247,7 @@ files:
233
247
  - lib/kitchen_hooks/helpers/sync_servers.rb
234
248
  - lib/kitchen_hooks/main.rb
235
249
  - lib/kitchen_hooks/metadata.rb
250
+ - lib/kitchen_hooks/slack_api.rb
236
251
  - tasks/package/Dockerfile
237
252
  - tasks/package/_package.sh
238
253
  - tasks/package/kitchen_hooks.sh