-
Notifications
You must be signed in to change notification settings - Fork 7
/
trouper.py
executable file
·147 lines (130 loc) · 4.71 KB
/
trouper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from slackbot.bot import listen_to
import re
from slacker import Slacker
import local_settings
import os
import sys
import shutil
from time import sleep
import requests
from threading import Thread
from slackmq import slackmq
import json
import chatops
version = open(os.path.join(
os.path.dirname(__file__), 'VERSION')).read().strip()
base_dir = os.environ['BASE_DIR']
onlaunch = os.environ['ONLAUNCH']
nextlaunch = os.environ['NEXTLAUNCH']
device_name = os.environ['DEVICE_NAME']
os.environ['CHANNEL_NAME'] = local_settings.CHANNEL_NAME
os.environ['API_TOKEN'] = local_settings.API_TOKEN
if eval(os.environ['DEBUG']):
debug = "[{}] ".format(device_name)
else:
debug = ""
try:
os.makedirs(base_dir + '/.cache')
except Exception:
pass
# Seconds to wait for avoiding the Slack rate_limit
rate_limit = 3
def launch():
tmp_launch = base_dir + '/.tmplaunch'
# Concat onlaunch and nextlaunch files
with open(tmp_launch, 'wb') as wfd:
for f in [onlaunch, nextlaunch]:
if os.path.isfile(f):
with open(f, 'rb') as fd:
shutil.copyfileobj(fd, wfd, 1024*1024*10)
if os.path.isfile(tmp_launch):
with requests.sessions.Session() as session:
slack = Slacker(local_settings.API_TOKEN, session=session)
slack.chat.post_message(
local_settings.CHANNEL_NAME, ':performing_arts: [' +
device_name + '] Troupe ' + version +
' started.')
while True:
if 'devops' in sys.modules:
sleep(rate_limit)
break
else:
sleep(1)
with open(tmp_launch, 'r') as launch_file:
for line in launch_file:
line = line.format(device_name=device_name)
if line[0] != '#':
slack.chat.post_message(local_settings.CHANNEL_NAME,
line)
sleep(rate_limit)
os.remove(tmp_launch)
if os.path.isfile(nextlaunch):
os.remove(nextlaunch)
t1 = Thread(target=launch)
t1.start()
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
flags = os.O_CREAT | os.O_APPEND
with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
os.utime(f.fileno() if os.utime in os.supports_fd
else fname, dir_fd=None if os.supports_fd
else dir_fd, **kwargs)
@listen_to('^roll call', re.IGNORECASE)
def roll_call(message):
"""Ask all Troupe bots to report in.
"""
sleep(rate_limit)
message.send(device_name + ' is here, at version ' + version)
@listen_to('^debug$', re.IGNORECASE)
@listen_to('^debug (.*)', re.IGNORECASE)
def setupDebug(message, action='toggle'):
"""Reveal which Troupe bot device is handling requests.
"""
post = slackmq(os.environ['API_TOKEN'],
message.body['channel'], message.body['ts'])
action = action.lower()
if action == 'toggle':
if eval(os.environ['DEBUG']):
os.environ['DEBUG'] = 'False'
if not post.ack():
return
message.send(':gear: Debug is disabled')
post.unack()
else:
os.environ['DEBUG'] = 'True'
if not post.ack():
return
message.send(':gear: [{}] Debug is enabled.'
.format(os.environ['DEVICE_NAME']))
post.unack()
return
else:
if action in ['on', 'enabled', 'true']:
action = True
debug = "[{}] ".format(os.environ['DEVICE_NAME'])
else:
action = False
debug = ""
os.environ['DEBUG'] = str(action)
if not post.ack():
return
message.send(':gear: {}Debug is {}.'
.format(debug, ['disabled', 'enabled'][action]))
post.unack()
@listen_to('^bot count$', re.IGNORECASE)
def count(message):
"""Report on the current connections count.
Slack has a limit on the number of concurrent Bot connections. Max: 17
Reference: https://github.com/slackapi/node-slack-sdk/issues/166
"""
if (os.environ['TARGET_DEVICE'] != 'all' and
os.environ['TARGET_DEVICE'] != device_name):
return
post = slackmq(os.environ['API_TOKEN'],
message.body['channel'], message.body['ts'])
if post.ack():
check = chatops.Chatops('https://slack.com/api/users.getPresence')
result = json.loads(check.getpresence())
connection_count = result['connection_count']
message.send('{}I have {} connections to Slack.'
.format(debug, connection_count))
post.unack()