diff --git a/fedn/cli/package_cmd.py b/fedn/cli/package_cmd.py index 14be22d2e..1aed91ab1 100644 --- a/fedn/cli/package_cmd.py +++ b/fedn/cli/package_cmd.py @@ -6,11 +6,13 @@ import tarfile import click +import requests from fedn.cli.main import main -from fedn.cli.shared import CONTROLLER_DEFAULTS, get_response, print_response from fedn.common.log_config import logger +from .shared import CONTROLLER_DEFAULTS, get_api_url, get_response, get_token, print_response + def create_tar_with_ignore(path: str, name: str) -> None: """Create a tar archive from a directory with an ignore and fedn.yaml file.""" @@ -114,3 +116,42 @@ def get_package(_: click.Context, protocol: str, host: str, port: str, token: st """ response = get_response(protocol=protocol, host=host, port=port, endpoint=f"packages/{id}", token=token, headers={}, usr_api=False, usr_token=False) print_response(response, "package", id) + + +@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)") +@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)") +@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)") +@click.option("-t", "--token", required=False, help="Authentication token") +@click.option("-f", "--file", required=True, help="Path to the compute package file") +@package_cmd.command("upload") +@click.pass_context +def set_active_package(ctx, protocol: str, host: str, port: str, token: str, file: str): + """Uploads compute package to package repository.""" + headers = {} + _token = get_token(token=token, usr_token=False) + if _token: + headers = {"Authorization": _token} + + if file.endswith(".tgz"): + helper = "numpyhelper" + elif file.endswith(".bin"): + helper = "binaryhelper" + else: + click.secho("Unsupported file type. Only .tgz and .bin files are supported.", fg="red") + return + + try: + # Set the active helper + url = get_api_url(protocol, host, port, "helpers/active", usr_api=False) + response = requests.put(url, json={"helper": helper}, headers=headers, verify=False) + response.raise_for_status() + + # Upload the model file + url = get_api_url(protocol, host, port, "package/", usr_api=False) + with open(file, "rb") as package_file: + response = requests.post(url, files={"file": package_file}, data={"helper": helper}, headers=headers, verify=False) + response.raise_for_status() + + click.secho("Package set as active and uploaded successfully.", fg="green") + except requests.exceptions.RequestException as e: + click.secho(f"Failed to set active package: {e}", fg="red")