diff --git a/code/app.py b/code/app.py index a241eaa..c4e9a43 100644 --- a/code/app.py +++ b/code/app.py @@ -5,7 +5,7 @@ from dotenv import load_dotenv import logging import os -from utils import clone_repository, load_runner_script +from utils import clone_repository, execute_runner_script from setup_logging import setup_logging from gitea_request import GiteaRequest @@ -60,34 +60,23 @@ def handle_event(request: GiteaRequest): status_code=200, detail="no runner_script.yml in repository root" ) - # load runner script - runner_script = load_runner_script(path=runner_script_path) - # determine pipeline to run - if branch_name not in runner_script.pipelines.keys(): - # get default pipeline - if 'default' not in runner_script.pipelines.keys(): - raise HTTPException( - status_code=404, - detail='no pipelines defined in runner_script.yml' - ) - pipeline = runner_script.pipelines['default'] - else: - # get custom pipeline - pipeline = runner_script.pipelines[branch_name] - logging.info(f'running pipeline {pipeline.name}') - # TODO: create virtualenv? - # install requirements - assert pipeline.requirements is not None - requirements_path = local_repo_dir / pipeline.requirements - os.system(f'pip install -r {requirements_path}') - for cmd in pipeline.script: - try: - os.system(cmd) - except Exception as e: - raise HTTPException( - status_code=503, - detail=f'failed running {cmd}: {e}' - ) + # execute runner script + logging.info('rexecuting runner script') + execute_runner_script(path=runner_script_path) + + # # TODO: create virtualenv? + # # install requirements + # assert pipeline.requirements is not None + # requirements_path = local_repo_dir / pipeline.requirements + # os.system(f'pip install -r {requirements_path}') + # for cmd in pipeline.script: + # try: + # os.system(cmd) + # except Exception as e: + # raise HTTPException( + # status_code=503, + # detail=f'failed running {cmd}: {e}' + # ) # send response raise HTTPException( diff --git a/code/runner_script.py b/code/runner_script.py index 9ac4775..7b1642e 100644 --- a/code/runner_script.py +++ b/code/runner_script.py @@ -15,7 +15,7 @@ class RunnerScript(BaseModel): content_dict = yaml.safe_load(fh) script_list = content_dict['script'].split('\n') logging.debug('finished') - return RunnerScript.model_validate({'script': script_list}) + return RunnerScript.model_validate({'script': script_list}) # type: ignore if __name__ == '__main__': diff --git a/code/setup_logging.py b/code/setup_logging.py index 7f75836..03af383 100644 --- a/code/setup_logging.py +++ b/code/setup_logging.py @@ -12,7 +12,7 @@ LOGGER_LEVEL = config.get('main', 'logger_level') def setup_logging( - logger_level: Literal['debug', 'info', 'warning', 'error'] = LOGGER_LEVEL + logger_level: str = LOGGER_LEVEL ): fmt = ( '%(asctime)s | ' diff --git a/code/utils.py b/code/utils.py index f92f267..35d8cd6 100644 --- a/code/utils.py +++ b/code/utils.py @@ -4,7 +4,6 @@ from git import Repo import logging import shutil import os -import yaml from runner_script import RunnerScript @@ -24,10 +23,8 @@ def clone_repository(repo_url: AnyHttpUrl) -> Path: return dst_dir -def load_runner_script(path: Path) -> RunnerScript: - assert path.exists() - with open(path, 'r') as fh: - script = yaml.safe_load(fh) - runner_script = RunnerScript.parse_obj(script) +def execute_runner_script(path: Path) -> None: + script_list = RunnerScript.from_file(path) + for cmd in script_list: + os.system(cmd) logging.debug('finished') - return runner_script