Skip to content

Commit

Permalink
Logging system reimplemented
Browse files Browse the repository at this point in the history
  • Loading branch information
davafons committed Apr 22, 2019
1 parent 8c7a84b commit a47a4ef
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
ignore = E203, F401, F403
ignore = E203, E999, F401, F403
max-line-length = 88
7 changes: 2 additions & 5 deletions cmsysbot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

from telegram.error import InvalidToken
from telegram.ext import Updater

Expand All @@ -8,11 +6,10 @@
from utils import Config, states


# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
# Error handlers also receive the raised TelegramError object in error.
def error(bot, update, error):
"""Log Errors caused by Updates."""
logging.getLogger().warning('Update "%s" caused error "%s"', update, error)
log.getLogger().error('Update "%s" caused error "%s"', update, error)


def main():
Expand Down
1 change: 0 additions & 1 deletion cmsysbot/controller/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def start_plugin_from_callback(bot: Bot, update: Updater, user_data: dict):

# Plugin path in server
plugin_path_server = re.search(State.START_PLUGIN, query.data).group(1)
print(plugin_path_server)

user_data["plugin"] = Plugin(plugin_path_server)

Expand Down
6 changes: 5 additions & 1 deletion cmsysbot/system/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ def generate_log_config():
ih.setFormatter(formatter)

# Logger
logger = logging.getLogger("CMSysBot")
logger = logging.getLogger()
logger.addHandler(eh)
logger.addHandler(wh)
logger.addHandler(ih)

return logger


def getLogger():
return logging.getLogger()
30 changes: 30 additions & 0 deletions cmsysbot/utils/session.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import paramiko
from paramiko import ssh_exception

Expand Down Expand Up @@ -34,14 +36,30 @@ def start_connection(self):
# TODO: Check if sshpass is installed
# .........

logging.getLogger().info(
f"User {self.username} connected to "
f"{self.bridge_ip} in {'/'.join(self.route)}"
)

except (
ssh_exception.NoValidConnectionsError,
ssh_exception.AuthenticationException,
):
# TODO: Rethrow the error?
self.connected = False

logging.getLogger().warning(
f"User {self.username} tried to connect to "
f"{self.bridge_ip} in {'/'.join(self.route)}"
)

def end_connetion(self):

logging.getLogger().info(
f"User {self.username} disconnected from {self.bridge_ip} "
f"in {'/'.join(self.route)}"
)

self.username = ""
self.password = ""
self.bridge_ip = ""
Expand All @@ -62,6 +80,8 @@ def copy_to_bridge(self, source_path: str, bridge_path: str, permissions: int):

def run_on_bridge(self, command: str, root: bool):

self.__log_command(self.bridge_ip, command, root)

if root:
command = f" echo {self.password} | sudo -kS {command}"

Expand All @@ -83,6 +103,8 @@ def copy_to_remote(self, target_host: str, source_path: str, remote_path: str):

def run_on_remote(self, target_host: str, command: str, root: bool):

self.__log_command(target_host, command, root)

if root:
command = f" echo {self.password} | sudo -kS {command}"

Expand All @@ -94,3 +116,11 @@ def run_on_remote(self, target_host: str, command: str, root: bool):
_, stdout, stderr = self.client.exec_command(ssh_command)

return stdout.read().decode("utf-8"), stderr.read().decode("utf-8")

def __log_command(self, target_ip: str, command: str, root: bool):
msg = (
f"User {self.username} executed {'AS ROOT' if root else ''} the command "
f"{command} on {target_ip} in {'/'.join(self.route)}"
)

logging.getLogger().info(msg)

0 comments on commit a47a4ef

Please sign in to comment.