Skip to content
Snippets Groups Projects
Verified Commit 0888e430 authored by Tamas Gal's avatar Tamas Gal :speech_balloon:
Browse files

Improve chatbot logging and error handling

parent 0667c6b3
No related branches found
No related tags found
No related merge requests found
Pipeline #51670 passed
......@@ -30,6 +30,7 @@ CONFIG = "pipeline.toml"
RECONNECT_INTERVAL = 30
with open(CONFIG, 'r') as fobj:
print(f"Reading configuration from {CONFIG}")
config = toml.load(fobj)
BOTNAME = config['Alerts']['botname']
PASSWORD = config['Alerts']['password']
......@@ -37,6 +38,7 @@ with open(CONFIG, 'r') as fobj:
def get_channel_id(channel):
print(f"Getting channel ID for channel: {channel}")
tries = 0
while True:
tries += 1
......@@ -44,9 +46,9 @@ def get_channel_id(channel):
try:
rocket = RocketChat(BOTNAME, PASSWORD, server_url=URL)
except requests.exceptions.ConnectionError as e:
log.error("Unable to connect to the RocketChat server: %s", e)
log.error(f"Unable to connect to the RocketChat server: {e}")
except Exception as e:
log.error("Unknown error occured: %s", e)
log.error(f"Unknown error occured: {e}")
else:
break
......@@ -54,9 +56,11 @@ def get_channel_id(channel):
print(f"Reconnecting in {interval} seconds...")
time.sleep(interval)
channels = rocket.channels_list().json()['channels']
channels = rocket.channels_list(count=0).json()['channels']
for c in channels:
print(f" -> {c['name']} => {c['_id']}")
if c['name'] == channel:
print(f"Found channel ID for {channel} is {c['_id']}")
return c['_id']
......@@ -64,28 +68,42 @@ CHANNEL_ID = get_channel_id(CHANNEL)
def run():
print("Running the monitoring bot system")
bot = spawn_bot()
register_handlers(bot)
bot.run()
def spawn_bot():
print("Spawning the bot")
return RocketChatBot(BOTNAME, PASSWORD, URL)
def is_shifter(user):
print(f"Checking if {user} is a shifter")
with open(CONFIG, 'r') as fobj:
config = toml.load(fobj)
return user in config['Alerts']['shifters']
try:
alerts_config = config['Alerts']
except KeyError:
log.error("No 'Alerts' section found in the configuration file")
return False
try:
return user in alerts_config['shifters']
except KeyError:
log.error("No 'shifters' section found in 'Alerts' of the configuration file")
return False
def is_operator(user):
print(f"Checking if {user} is an operator")
with open(CONFIG, 'r') as fobj:
config = toml.load(fobj)
return user in config['Alerts']['operators']
def register_handlers(bot):
print("Registering API handlers")
def greet(msg, user, channel_id):
if channel_id != CHANNEL_ID:
print("skipping")
......@@ -93,19 +111,22 @@ def register_handlers(bot):
bot.send_message('hello @' + user, channel_id)
def status(msg, user, channel_id):
print(f"Reporting status to channel {channel_id}")
if channel_id != CHANNEL_ID:
print("skipping")
print(f"Skipping channel with ID {channel_id}")
return
if not is_shifter(user) and not is_operator(user):
bot.send_message(
"Sorry @{}, only operators and shifters are allowed to mess "
"with me, sorry...".format(user), channel_id)
return
print("Asking subservisorctl for the status")
try:
status = "```\n" + subprocess.check_output(
['supervisorctl', 'status']).decode('ascii') + "\n```"
except subprocess.CalledProcessError as e:
status = "```\n{}\n```".format(e.output.decode('ascii'))
print("Sending status")
bot.send_message(status, channel_id)
def supervisorctl(msg, user, channel_id):
......@@ -172,6 +193,7 @@ def register_handlers(bot):
(['supervisorctl'], supervisorctl)]
for trigger, handler in handlers:
bot.add_dm_handler(trigger, handler)
print("All handlers are registered")
def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment