Skip to content

Commit

Permalink
feat/log commands to file (#165)
Browse files Browse the repository at this point in the history
* feat(logging): log commands to file to facilitate debugging

* feat(cli execution): add both print and logs

* chore(gitignore): add odtp.log to gitignore

* fix(run.py): don't log secrets to file

---------

Co-authored-by: sabinem <5292683+sabinem@users.noreply.github.com>
  • Loading branch information
sabinem and sabinem authored Jul 2, 2024
1 parent 9b4aeba commit 6922b0e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__/
!.env*.dist
.nicegui
.local
odtp/odtp.log
16 changes: 12 additions & 4 deletions odtp/cli/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ def prepare(
flowManager = WorkflowManager(execution, project_path, secrets)
flowManager.prepare_workflow()
except Exception as e:
log.error(f"ERROR: Prepare execution failed: {e}")
msg = f"ERROR: Prepare execution failed: {e}"
log.exception(msg)
print(msg)
raise typer.Abort()
else:
log.info("SUCCESS: images for the execution have been build")
msg = "SUCCESS: images for the execution have been build"
log.info(msg)
print(msg)


@app.command()
Expand Down Expand Up @@ -85,10 +89,14 @@ def run(
flowManager = WorkflowManager(execution, project_path, secrets)
flowManager.run_workflow()
except Exception as e:
log.error(f"ERROR: Prepare execution failed: {e}")
msg = f"ERROR: Prepare execution failed: {e}"
log.exception(msg)
print(msg)
raise typer.Abort()
else:
log.info("SUCCESS: containers for the execution have been run")
msg = "SUCCESS: containers for the execution have been run"
log.info(msg)
print(msg)


@app.command()
Expand Down
3 changes: 3 additions & 0 deletions odtp/dashboard/page_run/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import sys

from nicegui import ui
import odtp.helpers.settings as config
import odtp.dashboard.page_run.helpers as rh
import odtp.dashboard.page_run.folder as folder

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.addHandler(config.get_command_log_handler())


def ui_prepare_execution(dialog, result, current_run, folder_status):
Expand Down
10 changes: 10 additions & 0 deletions odtp/helpers/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
load_dotenv()

logger = logging.getLogger(__name__)
code_file_dir = os.path.abspath(__file__)

DEFAULT_LOG_LEVEL = "ERROR"
DEFAULT_RUN_LOG_LEVEL = "INFO"
Expand Down Expand Up @@ -49,3 +50,12 @@ class OdtpSettingsException(Exception):
log_levels = logging.getLevelNamesMapping()
if not RUN_LOG_LEVEL in log_levels.keys():
RUN_LOG_LEVEL = DEFAULT_RUN_LOG_LEVEL

def get_command_log_handler():
log_file_path = os.path.join(os.path.dirname(os.path.dirname(code_file_dir)), 'odtp.log')
command_log_handler = logging.FileHandler(log_file_path)
FORMATTER = logging.Formatter(
'%(asctime)s - [%(module)s:%(levelname)s] %(lineno)d %(filename)s %(funcName)s - %(message)s'
)
command_log_handler.setFormatter(FORMATTER)
return command_log_handler
16 changes: 11 additions & 5 deletions odtp/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@


log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.setLevel(logging.INFO)
log.addHandler(config.get_command_log_handler())


class OdtpRunSetupException(Exception):
Expand Down Expand Up @@ -77,7 +78,7 @@ def _checks_for_prepare(self):
)

def _checks_for_run(self, parameters, ports, image_name):
log.info("VALIDATION: check for run")
log.info("VALIDATION: check for run")
self._check_project_folder_prepared()
self._check_image_exists()
try:
Expand Down Expand Up @@ -168,7 +169,7 @@ def _create_volume(self, volume_name):
log.info(f"RUN: Creating Docker volume {volume_name}")
subprocess.run(["docker", "volume", "create", volume_name])

def run_component(self, parameters, secrets, ports, container_name, step_id=None, debug=False):
def run_component(self, parameters, secrets, ports, container_name, step_id=None):
"""
Run a Docker component with the specified parameters.
Expand Down Expand Up @@ -209,9 +210,14 @@ def run_component(self, parameters, secrets, ports, container_name, step_id=None
"--volume", f"{os.path.abspath(self.output_volume)}:/odtp/odtp-output"] + env_args + ports_args + secrets_args + [self.docker_image_name]

command_string = ' '.join(docker_run_command)
if debug:
log.debug(f"Command to be executed: {command_string}")
command_string_log_safe = command_string
for value in [parameters["ODTP_SECRET_KEY"], parameters["ODTP_ACCESS_KEY"], parameters["ODTP_MONGO_SERVER"]]:
command_string_log_safe = command_string_log_safe.replace(value, "x")
if secrets:
for value in secrets.values():
command_string_log_safe = command_string_log_safe.replace(value, "x")

log.info(command_string_log_safe)
process = subprocess.Popen(command_string, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

output, error = process.communicate()
Expand Down
4 changes: 3 additions & 1 deletion odtp/workflow.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
from odtp.run import DockerManager, OdtpRunSetupException
import odtp.helpers.utils as odtp_utils
import odtp.helpers.settings as config
import odtp.helpers.environment as env_helpers
import odtp.mongodb.db as db
import logging
import zipfile

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.setLevel(logging.INFO)
log.addHandler(config.get_command_log_handler())


class WorkflowManager:
Expand Down

0 comments on commit 6922b0e

Please sign in to comment.