diff --git a/Makefile b/Makefile index a8a28bf109f84a74476c35333e0a606baa96dc1a..8e44f0a419515a4f5ed3c1059ad37629d5ad88a9 100644 --- a/Makefile +++ b/Makefile @@ -30,9 +30,6 @@ gr runner gitlab-runner: gitlab-runner exec docker test gitlab-runner exec docker deploy -test_pipeline: install - $(POETRY) run bitia_runner run tests/test_20220727 - release: rm -rf dist/*.whl bash ./.ci/realese.sh @@ -41,5 +38,5 @@ doc html: cd docs && make html .PHONY : copr fix test install lint build \ - all check test_pipeline \ + all check \ runner gitlab-runner image image_upload diff --git a/bitia/__main__.py b/bitia/__main__.py index 9402df0acc612815efc9ab8d337d3ceaf5518054..03f71448c06736842138f7a5faa052ae54908419 100644 --- a/bitia/__main__.py +++ b/bitia/__main__.py @@ -6,6 +6,7 @@ https://bitia.link """ import typing as T +import os import functools import bitia.helper as bhelper @@ -42,16 +43,14 @@ def session(func): @app.command("create-container") @session -def create_remote_container( - user_input: str, recreate: bool = False, server: str = bconfig.DEFAULT_SERVER -): +def create_remote_container(user_input: str, recreate: bool = False): """Run a pipeline""" pipeline_zip, _ = bhelper.user_input_to_pipeline(user_input) res = bhelper.post_pipeline_task( pipeline_zip, - server=server, endpoint="container/create", params=dict(recreate="true" if recreate else "false"), + server=bconfig.server(), stream=True, ) res.raise_for_status() @@ -62,15 +61,15 @@ def create_remote_container( @app.command("list-container") @session def list_remote_container( - user_input: str, server: str = bconfig.DEFAULT_SERVER + user_input: str, server: str = bconfig.g_server ) -> T.List[str]: """List the remote server associated with the pipeline.""" _, pipeline_hash = bhelper.user_input_to_pipeline(user_input) logger.info(f"Finding container for {user_input}, sha256sum={pipeline_hash}") res = bhelper.get( endpoint="container/list", + server=bconfig.server(), params=dict(pipeline_sha256=pipeline_hash), - server=server, ) res.raise_for_status() _json = res.json() @@ -80,14 +79,14 @@ def list_remote_container( @app.command("logs") @session -def stream_log(user_input: str, server: str = bconfig.DEFAULT_SERVER) -> T.List[str]: +def stream_log(user_input: str, server: str = bconfig.g_server): """Stream logs for the most recent run of a given pipeline.""" _, pipeline_hash = bhelper.user_input_to_pipeline(user_input) logger.info(f"Finding container for {user_input}, sha256sum={pipeline_hash}") res = bhelper.get( endpoint="logs", params=dict(pipeline_sha256=pipeline_hash), - server=server, + server=bconfig.server(), stream=True, ) res.raise_for_status() @@ -97,9 +96,7 @@ def stream_log(user_input: str, server: str = bconfig.DEFAULT_SERVER) -> T.List[ @app.command("submit") @session -def submit_pipeline( - user_input: str, rerun: bool = False, server: str = bconfig.DEFAULT_SERVER -): +def submit_pipeline(user_input: str, rerun: bool = False): """Submit your pipelin (url, directory, zip_file). Prepare the user directory to send to the server. User can also provide link @@ -113,13 +110,31 @@ def submit_pipeline( @app.command("run") @session -def run_pipeline( - user_input: str, *, rerun: bool = False, server: str = bconfig.DEFAULT_SERVER -): +def run_pipeline(user_input: str, *, rerun: bool = False): """Run a pipeline""" - create_remote_container(user_input, recreate=rerun, server=server) - containers = list_remote_container(user_input, server=server) - return [bhelper.log_container(container, server=server) for container in containers] + create_remote_container(user_input, recreate=rerun) + containers = list_remote_container(user_input) + return [ + bhelper.log_container(container, server=bconfig.server()) + for container in containers + ] + + +@app.callback() +def main(verbose: bool = False, server: T.Optional[str] = None): + """ + Callback + """ + if verbose: + logger.info("--verbose mode enabled") + + if server is not None: + bconfig.set_server(server) + elif os.environ.get("BITIA_SERVER") is not None: + bconfig.set_server(os.environ["BITIA_SERVER"]) + else: + pass + logger.info(f"Using server {bconfig.server()}") @app.command() diff --git a/bitia/config.py b/bitia/config.py index 4afc62749e0534fef3cbf7cfe30dd856d6265bf3..b52a799d7594d3e9c2fba3e01cdf33b3bb64b9ac 100644 --- a/bitia/config.py +++ b/bitia/config.py @@ -3,7 +3,8 @@ from pathlib import Path import tempfile BITIA_MAIN_SCRIPT_NAME: T.Final[str] = "__main__.bitia.sh" -DEFAULT_SERVER: T.Final[str] = "https://public.bitia.link/api/v1" + +g_server = "https://public.bitia.link/api/v1" def bitia_dir() -> Path: @@ -11,3 +12,14 @@ def bitia_dir() -> Path: bdir = Path(tempfile.gettempdir()) / "bitia" bdir.mkdir(parents=True, exist_ok=True) return bdir + + +def server() -> str: + """Server to use""" + return g_server + + +def set_server(server: str): + """set bitia server""" + global g_server + g_server = server