s/ucloud/uncloud/g
Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
parent
267828e85a
commit
280043659d
10 changed files with 10 additions and 10 deletions
141
uncloud_cli/commands/vm.py
Executable file
141
uncloud_cli/commands/vm.py
Executable file
|
|
@ -0,0 +1,141 @@
|
|||
import click
|
||||
import json
|
||||
import requests
|
||||
import subprocess as sp
|
||||
|
||||
from uncloud_cli.commands.helper import OTPCredentials, load_dump_pretty
|
||||
from uncloud_cli.config import env_vars
|
||||
from os.path import join as join_path
|
||||
|
||||
|
||||
def vm_command(command, otp, vm_name, **kwargs):
|
||||
data = {**otp.get_json(), "vm_name": vm_name, "action": command, **kwargs}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "action"), json=data
|
||||
)
|
||||
return r
|
||||
|
||||
|
||||
@click.group()
|
||||
def vm():
|
||||
pass
|
||||
|
||||
|
||||
@vm.command("create")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--cpu", required=True, type=int)
|
||||
@click.option("--ram", required=True)
|
||||
@click.option("--os-ssd", required=True)
|
||||
@click.option("--hdd", default=list(), multiple=True)
|
||||
@click.option("--image", required=True)
|
||||
@click.option("--network", default=list(), multiple=True)
|
||||
def create(name, realm, seed, vm_name, cpu, ram, os_ssd, hdd, image, network):
|
||||
data = {
|
||||
**OTPCredentials(name, realm, seed).get_json(),
|
||||
"vm_name": vm_name,
|
||||
"specs": {"cpu": cpu, "ram": ram, "os-ssd": os_ssd, "hdd": hdd},
|
||||
"network": network,
|
||||
"image": image,
|
||||
}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "create"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@vm.command("start")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--in_support_of")
|
||||
def start(name, realm, seed, vm_name, in_support_of):
|
||||
r = vm_command(
|
||||
"start", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@vm.command("stop")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--in_support_of")
|
||||
def stop(name, realm, seed, vm_name, in_support_of):
|
||||
r = vm_command(
|
||||
"stop", OTPCredentials(name, realm, seed), vm_name, in_support_of=in_support_of
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@vm.command("delete")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--in_support_of")
|
||||
def delete(name, realm, seed, vm_name, in_support_of):
|
||||
r = vm_command(
|
||||
"delete",
|
||||
OTPCredentials(name, realm, seed),
|
||||
vm_name,
|
||||
in_support_of=in_support_of,
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@vm.command("status")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--in_support_of")
|
||||
def status(name, realm, seed, vm_name, in_support_of):
|
||||
otp = OTPCredentials(name, realm, seed)
|
||||
data = {**otp.get_json(), "vm_name": vm_name, "in_support_of": in_support_of}
|
||||
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "status"), json=data)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@vm.command("migrate")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--destination", required=True)
|
||||
@click.option("--in_support_of")
|
||||
def vm_migration(name, realm, seed, vm_name, destination, in_support_of):
|
||||
otp = OTPCredentials(name, realm, seed)
|
||||
data = {
|
||||
**otp.get_json(),
|
||||
"vm_name": vm_name,
|
||||
"destination": destination,
|
||||
"in_support_of": in_support_of,
|
||||
}
|
||||
r = requests.post(
|
||||
join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "migrate"), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
|
||||
|
||||
@vm.command("ssh")
|
||||
@click.option("--name", required=True, default=env_vars.get("OTP_NAME"))
|
||||
@click.option("--realm", required=True, default=env_vars.get("OTP_REALM"))
|
||||
@click.option("--seed", required=True, default=env_vars.get("OTP_SEED"))
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--in_support_of")
|
||||
def ssh(name, realm, seed, vm_name, in_support_of):
|
||||
otp = OTPCredentials(name, realm, seed)
|
||||
data = {**otp.get_json(), "vm_name": vm_name, "in_support_of": in_support_of}
|
||||
r = requests.get(join_path(env_vars.get("UCLOUD_API_SERVER"), "vm", "status"), json=data)
|
||||
try:
|
||||
_json = json.loads(r.content)
|
||||
sp.run(['ssh', '-o', 'ConnectTimeout=10',
|
||||
'root@{}'.format(_json['ip'][0])])
|
||||
except Exception as err:
|
||||
print("Some error occurred while accessing VM."
|
||||
"Make sure VM is running", err)
|
||||
Loading…
Add table
Add a link
Reference in a new issue