From bf9ca42e0c8d3a9956f7040c87cb56e336be7100 Mon Sep 17 00:00:00 2001 From: Javier Luraschi Date: Tue, 14 May 2024 20:40:03 -0700 Subject: [PATCH] support deploy to hal9 --- python/CONTRIBUTING.md | 6 +++ python/hal9/cli.py | 5 ++- python/hal9/deploy.py | 6 ++- python/hal9/targets/docker.py | 2 +- python/hal9/targets/hal9.py | 80 ++++++++++++++++++++++++++++------- python/pyproject.toml | 2 +- 6 files changed, 80 insertions(+), 21 deletions(-) diff --git a/python/CONTRIBUTING.md b/python/CONTRIBUTING.md index 7fb3a112..e1f5b0b8 100644 --- a/python/CONTRIBUTING.md +++ b/python/CONTRIBUTING.md @@ -8,3 +8,9 @@ Start by installing the package: pip install poetry poetry install ``` + +YOu can then run commands locally as follows: + +```bash +poetry run hal9 create my-project +``` \ No newline at end of file diff --git a/python/hal9/cli.py b/python/hal9/cli.py index b7b47d4d..4b131ab4 100644 --- a/python/hal9/cli.py +++ b/python/hal9/cli.py @@ -37,13 +37,14 @@ def run(path :str): @click.command() @click.argument('path') @click.option('--target', default="hal9", help='Deployment target') -def deploy(path :str, target :str): +@click.option('--url', default="https://api.hal9.com", help='Deployment url') +def deploy(path :str, target :str, url :str): """ Deploy Project PATH: The path to the project. Required argument. """ - api_deploy(path, target) + api_deploy(path, target, url) cli.add_command(create) cli.add_command(run) diff --git a/python/hal9/deploy.py b/python/hal9/deploy.py index d939d9b0..3d2af1c8 100644 --- a/python/hal9/deploy.py +++ b/python/hal9/deploy.py @@ -1,10 +1,12 @@ from hal9.targets.docker import deploy as deploy_docker +from hal9.targets.hal9 import deploy as deploy_hal9 targets = { 'docker': deploy_docker, + 'hal9': deploy_hal9, } -def deploy(path :str, target :str) -> str: +def deploy(path :str, target :str, url :str) -> str: """Deploy an application Parameters @@ -16,6 +18,6 @@ def deploy(path :str, target :str) -> str: """ if target in targets: - targets[target](path) + targets[target](path, url) else: raise Exception(f"Deployment target '{target}' is unsupported.") diff --git a/python/hal9/targets/docker.py b/python/hal9/targets/docker.py index 3f3b18ee..5774cdfc 100644 --- a/python/hal9/targets/docker.py +++ b/python/hal9/targets/docker.py @@ -1,7 +1,7 @@ import shutil from pathlib import Path -def deploy(path :str) -> str: +def deploy(path :str, url :str) -> str: package_dir = Path(__file__).parent.parent source_path = package_dir / 'templates' / 'docker' / 'Dockerfile' destination_path = Path(path) / 'Dockerfile' diff --git a/python/hal9/targets/hal9.py b/python/hal9/targets/hal9.py index 534ead78..9595282a 100644 --- a/python/hal9/targets/hal9.py +++ b/python/hal9/targets/hal9.py @@ -1,20 +1,70 @@ import requests +import os +import tempfile +import zipfile +from pathlib import Path +import time +import base64 +import json -def deploy(path :str) -> str: - response = requests.post('https://api.hal9.com/api/v1/deploy', json = { - 'name': 'name', - 'title': 'title', - 'description': 'description', - 'access': 'access', - 'code': 'code', - 'prompt': 'prompt', - 'thumbnail': 'thumbnail', - 'token': 'token', - 'user': 'user', - }) +def project_from_path(path :str) -> str: + return os.path.basename(os.path.abspath(path)) +def create_deployment(path :str) -> str: + temp_dir = Path(tempfile.mkdtemp()) + zip_path = temp_dir / 'archive.zip' + + with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: + for root, dirs, files in os.walk(path): + for file in files: + file_path = os.path.join(root, file) + zipf.write(file_path, os.path.relpath(file_path, path)) + + print(f"Created {zip_path}") + return zip_path + +def request_deploy(path :str, url :str) -> str: + project_name = project_from_path(path) + zip_path = create_deployment(path) + + unixtime = int(time.time()) + + with open(zip_path, 'rb') as file: + file_content = file.read() + encoded_content = base64.b64encode(file_content).decode('utf-8') + upload_name = f'{project_name}-{unixtime}.zip' + + # use app.py until backend supports zip content + with open(Path(path) / 'app.py', 'rb') as file: + file_content = file.read() + encoded_content = base64.b64encode(file_content).decode('utf-8') + upload_name = f'{project_name}-{unixtime}.py' + + payload = { + 'filename': upload_name, + 'content': encoded_content, + 'type': 'ability', + } + + print(f'Uploading {upload_name}') + + headers = { + 'Content-Type': 'application/json', + 'ApiKey': os.environ['HAL9_TOKEN'], + } + response = requests.post(url + '/api/v1/asset', headers = headers, data = json.dumps(payload)) + if not response.ok: - print('Failed to deploy') - exit() + response.raise_for_status() + + response_data = response.json() + +def deploy(path :str, url :str) -> str: + if 'HAL9_TOKEN' in os.environ: + hal9_token = os.environ['HAL9_TOKEN'] + else: + exit('HAL9_TOKEN environment variable missing, see https://hal9.com/deploy') + # hal9_token = browser_login() + + request_deploy(path, url) - return \ No newline at end of file diff --git a/python/pyproject.toml b/python/pyproject.toml index 0a391c54..48ba5778 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hal9" -version = "2.0.5" +version = "2.0.6" description = "" authors = ["Javier Luraschi "] readme = "README.md"