11require 'beekeeper/logging'
22require 'beekeeper/monkeypatch/docker'
3+ require 'beekeeper/slack_notifier'
34require 'docker'
4- require 'slack'
55
66module Beekeeper
77 class Watcher
@@ -15,19 +15,6 @@ class Watcher
1515 # still be avoided, as there is a race condition in which events could slip by unnoticed.
1616 MAX_TIME_BETWEEN_EVENTS = ENV . fetch ( 'READ_TIMEOUT' , 0 ) . to_i
1717
18- # Name of the environment variable containing the string value of
19- # the Slack API/OAuth token.
20- SLACK_TOKEN_ENV = 'SLACK_API_TOKEN' . freeze
21-
22- # Name of the environment variable containing the path to file
23- # containing the value of the Slack token. This is used only if
24- # SLACK_TOKEN_ENV is empty.
25- SLACK_TOKEN_FILE_ENV = 'SLACK_API_TOKEN_FILE' . freeze
26-
27- # Default path to the file that should contain the Slack token, if
28- # SLACK_TOKEN_ENV and SLACK_TOKEN_FILE_ENV are not set.
29- SLACK_TOKEN_DEFAULT_FILE = '/run/secrets/SLACK_API_TOKEN' . freeze
30-
3118 # Comma-separated list of Slack channels/users. If a container or
3219 # service fails and does not have its own custom watchers, these
3320 # are notified.
@@ -41,8 +28,10 @@ class Watcher
4128 # of watchers for the service.
4229 WATCHERS_LABEL = 'beekeeper.watchers' . freeze
4330
44- def initialize ( slack = nil )
45- @slack = slack || default_slack_client
31+ attr_reader :slack
32+
33+ def initialize ( slack = Beekeeper ::SlackNotifier . new )
34+ @slack = slack
4635 end
4736
4837 def clean_watchlist ( watchlist )
@@ -53,18 +42,6 @@ def clean_watchlist(watchlist)
5342 . uniq
5443 end
5544
56- def default_slack_client
57- Slack ::Web ::Client . new ( token : default_slack_token ) . tap do |client |
58- client . auth_test
59- end
60- end
61-
62- def default_slack_token
63- ENV . fetch ( SLACK_TOKEN_ENV ) do
64- File . read ( ENV [ SLACK_TOKEN_FILE_ENV ] || SLACK_TOKEN_DEFAULT_FILE ) . chomp
65- end
66- end
67-
6845 def default_watchers
6946 ENV . fetch ( WATCHERS_ENV , WATCHERS_DEFAULT ) . split ( ',' )
7047 end
@@ -103,30 +80,7 @@ def handle(event)
10380 end
10481
10582 def notify! ( recipient , event )
106- subject = event . service_name \
107- ? "Service \" #{ event . service_name } \" exited #{ event . exit_code } "
108- : "Container '#{ event . actor . id [ ..7 ] } ' exited #{ event . exit_code } "
109-
110- @slack . chat_postMessage (
111- channel : recipient ,
112- as_user : true ,
113- blocks : [
114- { type : 'header' , text : { type : 'plain_text' , text : subject } } ,
115- # Event Summary
116- { type : 'section' , fields : [ {
117- 'Host Name' => Docker . info [ 'Name' ] ,
118- 'Swarm Node' => event . swarm_node_id ,
119- 'Service Name' => event . service_name ,
120- 'Container ID' => event . actor_short_id ,
121- 'Exit Code' => event . exit_code ,
122- 'Image' => event . image_shortname ,
123- } . map { |h , t | { type : 'mrkdwn' , text : "*#{ h } *: #{ t } " } }
124- ] } ,
125- # Container Logs (if available)
126- { type : 'section' , text : { type : 'mrkdwn' , text : '*Logs:*' } } ,
127- { type : 'section' , text : { type : 'plain_text' , text : event . get_logs } } ,
128- ] ,
129- )
83+ slack . notify_event ( event :, recipient :)
13084 end
13185
13286 def stream_options
0 commit comments