2019-07-03 11:26:33 +00:00
|
|
|
import argparse
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
from decouple import config
|
|
|
|
from pyotp import TOTP
|
|
|
|
|
|
|
|
def add_otp(parser):
|
|
|
|
parser.add_argument("--name", required=True)
|
|
|
|
parser.add_argument("--realm", required=True)
|
|
|
|
parser.add_argument("--seed", required=True)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def add_vmid(parser):
|
|
|
|
parser.add_argument("--vmid", required=True)
|
|
|
|
return parser
|
|
|
|
|
|
|
|
argparser = argparse.ArgumentParser()
|
|
|
|
subparser = argparser.add_subparsers(dest="command")
|
|
|
|
|
|
|
|
create_image_parser = subparser.add_parser("create-image-from-file")
|
|
|
|
create_image_parser.add_argument("--uuid", required=True)
|
|
|
|
create_image_parser.add_argument("--name", required=True)
|
|
|
|
create_image_parser.add_argument("--image-store",
|
|
|
|
dest="image_store_name",
|
|
|
|
required=True)
|
|
|
|
|
|
|
|
images_parser = subparser.add_parser("images")
|
|
|
|
images_parser.add_argument("--list",
|
|
|
|
choices=["public", "private"],
|
|
|
|
required=True)
|
|
|
|
|
2019-07-11 08:37:48 +00:00
|
|
|
user_parser = subparser.add_parser("user")
|
|
|
|
user_subparser = user_parser.add_subparsers(dest="usercommand")
|
|
|
|
|
|
|
|
user_list_vms_parser = user_subparser.add_parser("list")
|
|
|
|
user_list_vms_parser.add_argument("--files", action='store_true')
|
|
|
|
user_list_vms_parser.add_argument("--vms", action='store_true')
|
|
|
|
add_otp(user_list_vms_parser)
|
|
|
|
|
2019-07-03 11:26:33 +00:00
|
|
|
vm_parser = subparser.add_parser("vm")
|
|
|
|
vm_subparser = vm_parser.add_subparsers(dest="vmcommand")
|
|
|
|
|
|
|
|
vm_create_parser = vm_subparser.add_parser("create")
|
|
|
|
add_otp(vm_create_parser)
|
|
|
|
vm_create_parser.add_argument("--specs_file", required=True)
|
|
|
|
vm_create_parser.add_argument("--image_uuid", required=True)
|
|
|
|
|
|
|
|
|
|
|
|
vm_start_parser = vm_subparser.add_parser("start")
|
|
|
|
vm_suspend_parser = vm_subparser.add_parser("suspend")
|
|
|
|
vm_resume_parser = vm_subparser.add_parser("resume")
|
2019-07-11 08:37:48 +00:00
|
|
|
vm_shutdown_parser = vm_subparser.add_parser("shutdown")
|
2019-07-03 11:26:33 +00:00
|
|
|
vm_status_parser = vm_subparser.add_parser("status")
|
|
|
|
|
|
|
|
add_vmid(add_otp(vm_start_parser))
|
|
|
|
add_vmid(add_otp(vm_suspend_parser))
|
|
|
|
add_vmid(add_otp(vm_resume_parser))
|
2019-07-11 08:37:48 +00:00
|
|
|
add_vmid(add_otp(vm_shutdown_parser))
|
2019-07-03 11:26:33 +00:00
|
|
|
add_vmid(add_otp(vm_status_parser))
|
|
|
|
|
|
|
|
args = argparser.parse_args()
|
|
|
|
|
|
|
|
if args.command == "create-image-from-file":
|
|
|
|
name = args.name
|
|
|
|
uuid = args.uuid
|
|
|
|
image_store_name = args.image_store_name
|
|
|
|
data = {
|
|
|
|
"name": args.name,
|
|
|
|
"uuid": uuid,
|
|
|
|
"image_store": image_store_name
|
|
|
|
}
|
|
|
|
r = requests.post(f"{config('UCLOUD_API_SERVER')}/image/create", data)
|
|
|
|
print(r.content.decode("utf-8"))
|
|
|
|
|
|
|
|
elif args.command == "images":
|
|
|
|
if args.list == "public":
|
|
|
|
r = requests.get(f"{config('UCLOUD_API_SERVER')}/image/list-public")
|
|
|
|
print(json.loads(r.content))
|
|
|
|
|
|
|
|
elif args.command == "vm":
|
|
|
|
if args.vmcommand == "create":
|
|
|
|
with open(args.specs_file, "r") as specs_f:
|
|
|
|
specs = json.loads(specs_f.read())
|
|
|
|
data = {
|
|
|
|
"name": args.name,
|
|
|
|
"realm": args.realm,
|
|
|
|
"token": TOTP(args.seed).now(),
|
|
|
|
"specs": specs,
|
|
|
|
"image_uuid": args.image_uuid
|
|
|
|
}
|
|
|
|
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/create",
|
|
|
|
json=data)
|
|
|
|
print(json.loads(r.content))
|
2019-07-11 08:37:48 +00:00
|
|
|
elif args.vmcommand in ["start", "suspend", "resume",
|
|
|
|
"status", "shutdown"]:
|
2019-07-03 11:26:33 +00:00
|
|
|
data = {
|
|
|
|
"name": args.name,
|
|
|
|
"realm": args.realm,
|
|
|
|
"token": TOTP(args.seed).now(),
|
|
|
|
"vmid": args.vmid,
|
|
|
|
}
|
|
|
|
if args.vmcommand == "status":
|
|
|
|
r = requests.get(f"{config('UCLOUD_API_SERVER')}/vm/{args.vmcommand}", json=data)
|
|
|
|
else:
|
|
|
|
r = requests.post(f"{config('UCLOUD_API_SERVER')}/vm/{args.vmcommand}", json=data)
|
|
|
|
print(json.loads(r.content))
|
2019-07-11 08:37:48 +00:00
|
|
|
|
|
|
|
elif args.command == "user":
|
|
|
|
if args.usercommand == "list" and args.vms:
|
|
|
|
data = {
|
|
|
|
"name": args.name,
|
|
|
|
"realm": args.realm,
|
|
|
|
"token": TOTP(args.seed).now()
|
|
|
|
}
|
|
|
|
r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/vms",
|
|
|
|
json=data)
|
|
|
|
print(json.loads(r.content))
|
|
|
|
elif args.usercommand == "list" and args.files:
|
|
|
|
data = {
|
|
|
|
"name": args.name,
|
|
|
|
"realm": args.realm,
|
|
|
|
"token": TOTP(args.seed).now()
|
|
|
|
}
|
|
|
|
r = requests.get(f"{config('UCLOUD_API_SERVER')}/user/files",
|
|
|
|
json=data)
|
|
|
|
print(json.loads(r.content))
|