2019-07-01 18:53:38 +00:00
|
|
|
# TODO
|
2019-07-18 12:10:17 +00:00
|
|
|
# 1. Allow user of realm ungleich-admin to perform any action on
|
|
|
|
# any user vm.
|
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
import json
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
from flask import Flask, request
|
|
|
|
from flask_restful import Resource, Api
|
2019-06-24 17:14:45 +00:00
|
|
|
from uuid import uuid4
|
2019-07-01 18:53:38 +00:00
|
|
|
from etcd3_wrapper import Etcd3Wrapper
|
2019-08-01 10:04:40 +00:00
|
|
|
|
|
|
|
from ucloud_common.vm import VmPool, VMStatus
|
|
|
|
from schemas import (CreateVMSchema, VMStatusSchema,
|
|
|
|
CreateImageSchema, VmActionSchema,
|
|
|
|
OTPSchema, CreateHostSchema)
|
|
|
|
|
2019-06-24 10:46:06 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
|
|
|
|
2019-07-01 18:53:38 +00:00
|
|
|
client = Etcd3Wrapper()
|
2019-08-01 10:04:40 +00:00
|
|
|
vm_pool = VmPool(client, "/v1/vm")
|
2019-07-03 12:47:24 +00:00
|
|
|
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
class CreateVM(Resource):
|
|
|
|
def post(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = CreateVMSchema(data)
|
|
|
|
if validator.is_valid():
|
|
|
|
vm_key = f"/v1/vm/{uuid4().hex}"
|
|
|
|
vm_entry = {
|
|
|
|
"owner": data["name"],
|
|
|
|
"specs": data["specs"],
|
|
|
|
"hostname": "",
|
|
|
|
"status": "REQUESTED_NEW",
|
|
|
|
"image_uuid": data["image_uuid"],
|
|
|
|
}
|
|
|
|
client.put(vm_key, vm_entry, value_in_json=True)
|
|
|
|
return {"message": "VM Creation Queued"}, 200
|
2019-06-24 10:46:06 +00:00
|
|
|
else:
|
2019-08-01 10:04:40 +00:00
|
|
|
return validator.get_errors(), 400
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
|
2019-06-25 10:07:23 +00:00
|
|
|
class VmStatus(Resource):
|
|
|
|
def get(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = VMStatusSchema(data)
|
|
|
|
if validator.is_valid():
|
|
|
|
vm = vm_pool.get(f"/v1/vm/{data['uuid']}")
|
|
|
|
return str(vm)
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
2019-06-25 10:07:23 +00:00
|
|
|
|
|
|
|
|
2019-07-01 18:53:38 +00:00
|
|
|
class CreateImage(Resource):
|
|
|
|
def post(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = CreateImageSchema(data)
|
|
|
|
if validator.is_valid():
|
|
|
|
file_entry = client.get(f"/v1/file/{data['uuid']}")
|
|
|
|
file_entry_value = json.loads(file_entry.value)
|
|
|
|
|
|
|
|
image_entry_json = {
|
|
|
|
"status": "TO_BE_CREATED",
|
|
|
|
"owner": file_entry_value["owner"],
|
|
|
|
"filename": file_entry_value["filename"],
|
|
|
|
"name": data["name"],
|
|
|
|
"store_name": data["image_store"],
|
|
|
|
"visibility": "public",
|
|
|
|
}
|
|
|
|
client.put(f"/v1/image/{data['uuid']}", json.dumps(image_entry_json))
|
|
|
|
|
|
|
|
return {"Message": "Image successfully created"}
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
2019-07-01 18:53:38 +00:00
|
|
|
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
class ListPublicImages(Resource):
|
|
|
|
def get(self):
|
|
|
|
images = client.get_prefix("/v1/image/")
|
|
|
|
r = {}
|
|
|
|
for image in images:
|
|
|
|
r[image.key.split("/")[-1]] = json.loads(image.value)
|
|
|
|
|
|
|
|
return r, 200
|
|
|
|
|
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
class VMAction(Resource):
|
2019-07-03 12:47:24 +00:00
|
|
|
def post(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = VmActionSchema(data)
|
|
|
|
action = data["action"]
|
|
|
|
|
|
|
|
if validator.is_valid():
|
|
|
|
vm = vm_pool.get(f"/v1/vm/{data['uuid']}")
|
|
|
|
if action == "start":
|
|
|
|
vm.status = VMStatus.requested_start
|
|
|
|
elif action == "suspend":
|
|
|
|
vm.status = VMStatus.requested_suspend
|
|
|
|
elif action == "resume":
|
|
|
|
vm.status = VMStatus.requested_resume
|
|
|
|
elif action == "shutdown":
|
|
|
|
vm.status = VMStatus.requested_shutdown
|
|
|
|
elif action == "delete":
|
|
|
|
vm.status = VMStatus.requested_delete
|
|
|
|
|
|
|
|
client.put(vm.key, json.dumps(vm.value))
|
|
|
|
return {"message": f"VM Start Queued"}
|
2019-07-11 08:34:21 +00:00
|
|
|
else:
|
2019-08-01 10:04:40 +00:00
|
|
|
return validator.get_errors(), 400
|
2019-07-11 08:34:21 +00:00
|
|
|
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
class ListUserVM(Resource):
|
|
|
|
def get(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = OTPSchema(data)
|
2019-07-03 12:47:24 +00:00
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
if validator.is_valid():
|
2019-07-03 12:47:24 +00:00
|
|
|
vms = client.get_prefix(f"/v1/vm/", value_in_json=True)
|
|
|
|
if vms:
|
|
|
|
return_vms = []
|
2019-08-01 10:04:40 +00:00
|
|
|
user_vms = list(filter(lambda v: v.value["owner"] == data["name"], vms))
|
2019-07-03 12:47:24 +00:00
|
|
|
for vm in user_vms:
|
2019-07-18 12:10:17 +00:00
|
|
|
return_vms.append(
|
|
|
|
{
|
|
|
|
"vm_uuid": vm.key.split("/")[-1],
|
|
|
|
"specs": vm.value["specs"],
|
|
|
|
"status": vm.value["status"],
|
|
|
|
}
|
|
|
|
)
|
2019-07-03 12:47:24 +00:00
|
|
|
return {"message": return_vms}, 200
|
|
|
|
else:
|
|
|
|
return {"message": "No VM found"}, 404
|
|
|
|
else:
|
2019-08-01 10:04:40 +00:00
|
|
|
return validator.get_errors(), 400
|
2019-07-03 12:47:24 +00:00
|
|
|
|
|
|
|
|
2019-07-11 08:34:21 +00:00
|
|
|
class ListUserFiles(Resource):
|
|
|
|
def get(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = OTPSchema(data)
|
2019-07-11 08:34:21 +00:00
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
if validator.is_valid():
|
2019-07-30 15:45:05 +00:00
|
|
|
files = client.get_prefix(f"/v1/file/", value_in_json=True)
|
2019-07-11 08:34:21 +00:00
|
|
|
if files:
|
|
|
|
return_files = []
|
2019-08-01 10:04:40 +00:00
|
|
|
user_files = list(filter(lambda f: f.value["owner"] == data["name"], files))
|
2019-07-11 08:34:21 +00:00
|
|
|
for file in user_files:
|
2019-07-18 12:10:17 +00:00
|
|
|
return_files.append(
|
|
|
|
{
|
|
|
|
"filename": file.value["filename"],
|
|
|
|
"uuid": file.key.split("/")[-1],
|
|
|
|
}
|
|
|
|
)
|
2019-07-11 08:34:21 +00:00
|
|
|
return {"message": return_files}, 200
|
|
|
|
else:
|
|
|
|
return {"message": "No File found"}, 404
|
|
|
|
else:
|
2019-08-01 10:04:40 +00:00
|
|
|
return validator.get_errors(), 400
|
2019-07-11 08:34:21 +00:00
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
|
|
|
|
class CreateHost(Resource):
|
|
|
|
def post(self):
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = CreateHostSchema(data)
|
|
|
|
if validator.is_valid():
|
2019-07-18 12:10:17 +00:00
|
|
|
host_key = f"/v1/host/{uuid4().hex}"
|
|
|
|
host_entry = {
|
2019-08-01 10:04:40 +00:00
|
|
|
"specs": data["specs"],
|
|
|
|
"hostname": data["hostname"],
|
2019-07-18 12:10:17 +00:00
|
|
|
"status": "DEAD",
|
2019-07-18 14:09:26 +00:00
|
|
|
"last_heartbeat": "",
|
2019-07-18 12:10:17 +00:00
|
|
|
}
|
|
|
|
client.put(host_key, host_entry, value_in_json=True)
|
|
|
|
|
|
|
|
return {"message": "Host Created"}, 200
|
|
|
|
else:
|
2019-08-01 10:04:40 +00:00
|
|
|
return validator.get_errors(), 400
|
2019-07-18 12:10:17 +00:00
|
|
|
|
|
|
|
|
2019-06-26 07:33:29 +00:00
|
|
|
api.add_resource(CreateVM, "/vm/create")
|
|
|
|
api.add_resource(VmStatus, "/vm/status")
|
2019-07-11 08:34:21 +00:00
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
api.add_resource(VMAction, "/vm/action")
|
2019-07-03 12:47:24 +00:00
|
|
|
|
2019-07-01 18:53:38 +00:00
|
|
|
api.add_resource(CreateImage, "/image/create")
|
2019-07-03 12:47:24 +00:00
|
|
|
api.add_resource(ListPublicImages, "/image/list-public")
|
2019-07-01 18:53:38 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
api.add_resource(ListUserVM, "/user/vms")
|
2019-07-11 08:34:21 +00:00
|
|
|
api.add_resource(ListUserFiles, "/user/files")
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
api.add_resource(CreateHost, "/host/create")
|
|
|
|
|
|
|
|
|
2019-06-26 07:33:29 +00:00
|
|
|
if __name__ == "__main__":
|
2019-06-30 16:20:02 +00:00
|
|
|
app.run(host="::", debug=True)
|