Skip to content

Commit

Permalink
Feature: Instances operator commands
Browse files Browse the repository at this point in the history
  • Loading branch information
1yam committed Jun 12, 2024
1 parent 991ae3c commit e128dca
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 2 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package_dir =
setup_requires = pyscaffold>=3.2a0,<3.3a0
# Add here dependencies of your project (semicolon/line-separated), e.g.
install_requires =
aleph-sdk-python~=0.9.1
aleph-sdk-python@git+https://github.com/aleph-im/aleph-sdk-python.git@1yam-vm-client#egg=aleph-sdk-python
aleph-message>=0.4.3
coincurve==17.0.0
aiohttp==3.8.4
Expand Down
170 changes: 169 additions & 1 deletion src/aleph_client/commands/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typer
from aiohttp import ClientResponseError, ClientSession
from aleph.sdk import AlephHttpClient, AuthenticatedAlephHttpClient
from aleph.sdk.client.vmclient import VmClient
from aleph.sdk.account import _load_account
from aleph.sdk.conf import settings as sdk_settings
from aleph.sdk.exceptions import (
Expand All @@ -28,10 +29,13 @@
setup_logging,
validated_int_prompt,
validated_prompt,
colorful_json,
)
from aleph_client.conf import settings
from aleph_client.utils import AsyncTyper

from eth_account import Account

logger = logging.getLogger(__name__)
app = AsyncTyper(no_args_is_help=True)

Expand Down Expand Up @@ -156,7 +160,10 @@ def validate_ssh_pubkey_file(file: Union[str, Path]) -> Path:
)

memory = validated_int_prompt(
f"Maximum memory allocation on vm in MiB", memory, min_value=2000, max_value=8000
f"Maximum memory allocation on vm in MiB",
memory,
min_value=2000,
max_value=8000,
)

rootfs_size = validated_int_prompt(
Expand Down Expand Up @@ -331,3 +338,164 @@ async def list(
typer.echo(resp.json(indent=4))
else:
await _show_instances(resp.messages)


@app.command()
async def notify(
vm_id: str,
domain: str,
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
debug: bool = False,
):
"""notify crn the instance, can be use to start VM"""

setup_logging(debug)

if private_key_file:
private_key = private_key_file.read_bytes()
elif private_key:
private_key = bytes.fromhex(private_key)

if isinstance(private_key, bytes):
account = Account.from_key(private_key)
async with VmClient(account, domain) as manager:
status, result = await manager.start_instance(vm_id=vm_id)
if status != 200:
typer.echo(f"Status : {status}")
typer.echo(colorful_json(result))
else:
typer.echo("Invalid private key format")


@app.command()
async def stop(
vm_id: str,
domain: str,
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
debug: bool = False,
):
"""Stop an instance"""

setup_logging(debug)

if private_key_file:
private_key = private_key_file.read_bytes()
elif private_key:
private_key = bytes.fromhex(private_key)

if isinstance(private_key, bytes):
account = Account.from_key(private_key)
async with VmClient(account, domain) as manager:
status, result = await manager.stop_instance(vm_id=vm_id)
if status != 200:
typer.echo(f"Status : {status}")
typer.echo(result)
else:
typer.echo("Invalid private key format")


@app.command()
async def reboot(
vm_id: str,
domain: str,
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
debug: bool = False,
):
"""reboot an instance"""

setup_logging(debug)

if private_key_file:
private_key = private_key_file.read_bytes()
elif private_key:
private_key = bytes.fromhex(private_key)

if isinstance(private_key, bytes):
account = Account.from_key(private_key)
async with VmClient(account, domain) as manager:
status, result = await manager.reboot_instance(vm_id=vm_id)
if status != 200:
typer.echo(f"Status : {status}")
typer.echo(result)

else:
typer.echo("Invalid private key format")


@app.command()
async def erase(
vm_id: str,
domain: str,
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
debug: bool = False,
):
"""erase an instance"""

setup_logging(debug)

if private_key_file:
private_key = private_key_file.read_bytes()
elif private_key:
private_key = bytes.fromhex(private_key)

if isinstance(private_key, bytes):
account = Account.from_key(private_key)
async with VmClient(account, domain) as manager:
status, result = await manager.erase_instance(vm_id=vm_id)
if status != 200:
typer.echo(f"Status : {status}")
typer.echo(result)
else:
typer.echo("Invalid private key format")


@app.command()
async def expire(
vm_id: str,
domain: str,
private_key: Optional[str] = typer.Option(
sdk_settings.PRIVATE_KEY_STRING, help=help_strings.PRIVATE_KEY
),
private_key_file: Optional[Path] = typer.Option(
sdk_settings.PRIVATE_KEY_FILE, help=help_strings.PRIVATE_KEY_FILE
),
debug: bool = False,
):
"""expire an instance"""

setup_logging(debug)

if private_key_file:
private_key = private_key_file.read_bytes()
elif private_key:
private_key = bytes.fromhex(private_key)

if isinstance(private_key, bytes):
account = Account.from_key(private_key)
async with VmClient(account, domain) as manager:
status, result = await manager.expire_instance(vm_id=vm_id)
if status != 200:
typer.echo(f"Status : {status}")
typer.echo(result)
else:
typer.echo("Invalid private key format")

0 comments on commit e128dca

Please sign in to comment.