Skip to content

Commit

Permalink
fix deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
hoh committed Dec 12, 2024
1 parent e317c7e commit 3eec98a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
run: echo "deployment" | hatch run deployment:aleph account create --chain ETH --private-key ${{ secrets.ALEPH_ETH_PRIVATE_KEY }}

- name: Deploy on aleph.im
run: hatch run deployment:deploy
run: echo "deployment" | hatch run deployment:deploy
88 changes: 88 additions & 0 deletions deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import asyncio
import logging
import sys
from base64 import b32encode, b16decode
from pathlib import Path
from zipfile import BadZipFile

from aleph.sdk import AuthenticatedAlephHttpClient
from aleph.sdk.account import _load_account
from aleph.sdk.conf import settings
from aleph.sdk.types import AccountFromPrivateKey, StorageEnum
from aleph_client.utils import create_archive
from aleph_message.models import StoreMessage, ItemHash
from aleph_message.status import MessageStatus

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

channel = "aleph-infrastructure"

async def deploy_program():
path = Path(__file__).parent / "src"
try:
path_object, encoding = create_archive(path)
except BadZipFile:
print("Invalid zip archive")
sys.exit(3)
except FileNotFoundError:
print("No such file or directory")
sys.exit(4)

account: AccountFromPrivateKey = _load_account()
runtime = settings.DEFAULT_RUNTIME_ID

async with AuthenticatedAlephHttpClient(account=account, api_server=settings.API_HOST) as client:
# Upload the source code
with open(path_object, "rb") as fd:
logger.debug("Reading file")
# TODO: Read in lazy mode instead of copying everything in memory
file_content = fd.read()
storage_engine = StorageEnum.ipfs
logger.debug("Uploading file")
user_code: StoreMessage
status: MessageStatus
user_code, status = await client.create_store(
file_content=file_content,
storage_engine=storage_engine,
channel=channel,
guess_mime_type=True,
ref=None,
)
print(f"{user_code.json(indent=4)}")
logger.debug("Upload finished")
program_ref = user_code.item_hash

# Register the program
message, status = await client.create_program(
program_ref=program_ref,
entrypoint="monitoring_proxy:app",
runtime=runtime,
storage_engine=StorageEnum.storage,
channel=channel,
memory=512,
vcpus=1,
timeout_seconds=60,
persistent=False,
encoding=encoding,
volumes=None,
subscriptions=None,
)
logger.debug("Upload finished")

print(f"{message.json(indent=4)}")

item_hash: ItemHash = message.item_hash
hash_base32 = b32encode(b16decode(item_hash.upper())).strip(b"=").lower().decode()

print(
f"Your program has been uploaded on aleph.im\n\n"
"Available on:\n"
f" {settings.VM_URL_PATH.format(hash=item_hash)}\n"
f" {settings.VM_URL_HOST.format(hash_base32=hash_base32)}\n"
"Visualise on:\n https://explorer.aleph.im/address/"
f"{message.chain.value}/{message.sender}/message/PROGRAM/{item_hash}\n"
)

if __name__ == "__main__":
asyncio.run(deploy_program())
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ all = [
detached = true
dependencies = [
"aleph-client==1.3.0",
"aleph-sdk-python==1.2.1",
]
[tool.hatch.envs.deployment.scripts]
deploy = "aleph program upload src monitoring_proxy:app"
deploy = "python ./deploy.py"
update = "aleph program update $ITEM_HASH monitoring_proxy:app"

[tool.mypy]
Expand Down

0 comments on commit 3eec98a

Please sign in to comment.