2019-07-17 14:58:39 +00:00
|
|
|
import json
|
|
|
|
|
2019-09-13 12:33:19 +00:00
|
|
|
from commands.helper import OTPCredentials, load_dump_pretty
|
2019-07-17 14:58:39 +00:00
|
|
|
from decouple import config
|
2019-09-13 12:33:19 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
import requests
|
2019-07-17 14:58:39 +00:00
|
|
|
|
|
|
|
|
2019-08-01 10:05:38 +00:00
|
|
|
def vm_command(command, otp, uuid):
|
|
|
|
data = {**otp.get_json(), "uuid": uuid, "action": command}
|
2019-09-13 12:33:19 +00:00
|
|
|
r = requests.post("{}/vm/action".format(config('UCLOUD_API_SERVER')), json=data)
|
2019-07-17 14:58:39 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
def vm():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@vm.command("create")
|
2019-08-12 12:56:19 +00:00
|
|
|
@click.option("--name", envvar="OTP_NAME", required=True)
|
|
|
|
@click.option("--realm", envvar="OTP_REALM", required=True)
|
|
|
|
@click.option("--seed", envvar="OTP_SEED", required=True)
|
2019-07-17 14:58:39 +00:00
|
|
|
@click.option("--specs", required=True)
|
|
|
|
@click.option("--image_uuid", required=True)
|
|
|
|
def create(name, realm, seed, specs, image_uuid):
|
|
|
|
with open(specs, "r") as specs_f:
|
|
|
|
specs = json.loads(specs_f.read())
|
|
|
|
data = {
|
|
|
|
**OTPCredentials(name, realm, seed).get_json(),
|
|
|
|
"specs": specs,
|
|
|
|
"image_uuid": image_uuid,
|
|
|
|
}
|
2019-09-13 12:33:19 +00:00
|
|
|
r = requests.post("{}/vm/create".format(config('UCLOUD_API_SERVER')), json=data)
|
2019-08-12 12:56:19 +00:00
|
|
|
print(load_dump_pretty(r.content))
|
2019-07-17 14:58:39 +00:00
|
|
|
|
|
|
|
|
2019-09-01 17:09:08 +00:00
|
|
|
@vm.command("start")
|
2019-08-12 12:56:19 +00:00
|
|
|
@click.option("--name", envvar="OTP_NAME", required=True)
|
|
|
|
@click.option("--realm", envvar="OTP_REALM", required=True)
|
|
|
|
@click.option("--seed", envvar="OTP_SEED", required=True)
|
2019-08-01 10:05:38 +00:00
|
|
|
@click.option("--uuid", required=True)
|
2019-09-01 17:09:08 +00:00
|
|
|
def start(name, realm, seed, uuid):
|
|
|
|
r = vm_command("start", OTPCredentials(name, realm, seed), uuid)
|
|
|
|
print(load_dump_pretty(r.content))
|
|
|
|
|
|
|
|
|
|
|
|
@vm.command("stop")
|
|
|
|
@click.option("--name", envvar="OTP_NAME", required=True)
|
|
|
|
@click.option("--realm", envvar="OTP_REALM", required=True)
|
|
|
|
@click.option("--seed", envvar="OTP_SEED", required=True)
|
|
|
|
@click.option("--uuid", required=True)
|
|
|
|
def stop(name, realm, seed, uuid):
|
|
|
|
r = vm_command("stop", OTPCredentials(name, realm, seed), uuid)
|
2019-08-12 12:56:19 +00:00
|
|
|
print(load_dump_pretty(r.content))
|
2019-07-17 14:58:39 +00:00
|
|
|
|
|
|
|
|
2019-09-04 13:21:28 +00:00
|
|
|
@vm.command("delete")
|
|
|
|
@click.option("--name", envvar="OTP_NAME", required=True)
|
|
|
|
@click.option("--realm", envvar="OTP_REALM", required=True)
|
|
|
|
@click.option("--seed", envvar="OTP_SEED", required=True)
|
|
|
|
@click.option("--uuid", required=True)
|
|
|
|
def delete(name, realm, seed, uuid):
|
|
|
|
r = vm_command("delete", OTPCredentials(name, realm, seed), uuid)
|
|
|
|
print(load_dump_pretty(r.content))
|
|
|
|
|
|
|
|
|
2019-08-01 10:05:38 +00:00
|
|
|
@vm.command("status")
|
2019-08-12 12:56:19 +00:00
|
|
|
@click.option("--name", envvar="OTP_NAME", required=True)
|
|
|
|
@click.option("--realm", envvar="OTP_REALM", required=True)
|
|
|
|
@click.option("--seed", envvar="OTP_SEED", required=True)
|
2019-08-01 10:05:38 +00:00
|
|
|
@click.option("--uuid", required=True)
|
|
|
|
def status(name, realm, seed, uuid):
|
|
|
|
data = {"name": name, "realm": realm, "seed": seed, "uuid": uuid}
|
2019-09-13 12:33:19 +00:00
|
|
|
r = requests.get("{}/vm/status".format(config('UCLOUD_API_SERVER')), json=data)
|
2019-08-12 12:56:19 +00:00
|
|
|
print(load_dump_pretty(r.content))
|
|
|
|
|
2019-09-13 12:33:19 +00:00
|
|
|
|
2019-08-12 12:56:19 +00:00
|
|
|
@vm.command("migrate")
|
|
|
|
@click.option("--name", envvar="OTP_NAME", required=True)
|
|
|
|
@click.option("--realm", envvar="OTP_REALM", required=True)
|
|
|
|
@click.option("--seed", envvar="OTP_SEED", required=True)
|
|
|
|
@click.option("--uuid", required=True)
|
|
|
|
@click.option("--destination", required=True)
|
|
|
|
def vm_migration(name, realm, seed, uuid, destination):
|
|
|
|
otp = OTPCredentials(name, realm, seed)
|
|
|
|
data = {
|
|
|
|
**otp.get_json(),
|
|
|
|
"uuid": uuid,
|
|
|
|
"destination": destination
|
|
|
|
}
|
2019-09-13 12:33:19 +00:00
|
|
|
r = requests.post("{}/vm/migrate".format(config('UCLOUD_API_SERVER')), json=data)
|
|
|
|
print(load_dump_pretty(r.content))
|