Skip to content

Commit

Permalink
support deploy to hal9
Browse files Browse the repository at this point in the history
  • Loading branch information
javierluraschi committed May 15, 2024
1 parent c801821 commit bf9ca42
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 21 deletions.
6 changes: 6 additions & 0 deletions python/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
5 changes: 3 additions & 2 deletions python/hal9/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions python/hal9/deploy.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.")
2 changes: 1 addition & 1 deletion python/hal9/targets/docker.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
80 changes: 65 additions & 15 deletions python/hal9/targets/hal9.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "hal9"
version = "2.0.5"
version = "2.0.6"
description = ""
authors = ["Javier Luraschi <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit bf9ca42

Please sign in to comment.