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-07-03 12:47:24 +00:00
|
|
|
from helper import check_otp, add_otp_args, add_vmid_args
|
2019-06-24 10:46:06 +00:00
|
|
|
from flask import Flask
|
|
|
|
from flask_restful import Resource, Api, reqparse
|
|
|
|
from decouple import config
|
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-07-11 08:34:21 +00:00
|
|
|
from specs_parser import SpecsParser
|
2019-07-18 12:10:17 +00:00
|
|
|
from functools import wraps
|
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-06-24 10:46:06 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
# CreateVM argparser
|
2019-06-24 10:46:06 +00:00
|
|
|
createvm_argparser = reqparse.RequestParser()
|
|
|
|
createvm_argparser.add_argument("specs", type=dict, required=True)
|
2019-07-03 12:47:24 +00:00
|
|
|
createvm_argparser.add_argument("image_uuid", type=str, required=True)
|
|
|
|
add_otp_args(createvm_argparser)
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
# CreateImage argparser
|
2019-07-01 18:53:38 +00:00
|
|
|
createimage_argparser = reqparse.RequestParser()
|
|
|
|
createimage_argparser.add_argument("uuid", type=str, required=True)
|
|
|
|
createimage_argparser.add_argument("name", type=str, required=True)
|
|
|
|
createimage_argparser.add_argument("image_store", type=str, required=True)
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
# DeleteVM argparser
|
|
|
|
deletevm_argparser = reqparse.RequestParser()
|
|
|
|
add_vmid_args(add_otp_args(deletevm_argparser))
|
|
|
|
|
|
|
|
# VMStatus argparser
|
|
|
|
vmstatus_argparser = reqparse.RequestParser()
|
|
|
|
add_vmid_args(vmstatus_argparser)
|
|
|
|
|
|
|
|
# StartVM argparser
|
|
|
|
startvm_argparser = reqparse.RequestParser()
|
|
|
|
add_vmid_args(add_otp_args(startvm_argparser))
|
|
|
|
|
|
|
|
# UserVM argparser
|
|
|
|
uservm_argparser = reqparse.RequestParser()
|
|
|
|
add_otp_args(uservm_argparser)
|
|
|
|
|
2019-07-11 08:34:21 +00:00
|
|
|
# Specs parser
|
|
|
|
specs_parser = SpecsParser(exceptional_devices=["cpu"])
|
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
# AddHost argparser
|
|
|
|
addhost_argparser = reqparse.RequestParser()
|
|
|
|
addhost_argparser.add_argument("hostname", type=str, required=True)
|
|
|
|
addhost_argparser.add_argument("specs", type=dict, required=True)
|
|
|
|
add_otp_args(addhost_argparser)
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
|
|
|
|
def is_image_valid(image_uuid):
|
|
|
|
images = client.get_prefix("/v1/image/")
|
|
|
|
return image_uuid in [i.key.split("/")[-1] for i in images]
|
|
|
|
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
class CreateVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
createvm_args = createvm_argparser.parse_args()
|
2019-07-18 12:10:17 +00:00
|
|
|
name, realm, token, specs, image_uuid = (
|
|
|
|
createvm_args.name,
|
|
|
|
createvm_args.realm,
|
|
|
|
createvm_args.token,
|
|
|
|
createvm_args.specs,
|
|
|
|
createvm_args.image_uuid,
|
|
|
|
)
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
if check_otp(name, realm, token) == 200:
|
2019-06-24 10:46:06 +00:00
|
|
|
# User is good
|
2019-07-03 12:47:24 +00:00
|
|
|
if is_image_valid(image_uuid):
|
2019-07-11 08:34:21 +00:00
|
|
|
if not specs_parser.transform_specs(specs):
|
2019-07-18 12:10:17 +00:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
"message": f"""Invalid unit - Please use following units {specs_parser.get_allowed_units()}"""
|
|
|
|
},
|
|
|
|
400,
|
|
|
|
)
|
2019-07-11 08:34:21 +00:00
|
|
|
|
|
|
|
print(specs)
|
2019-07-03 12:47:24 +00:00
|
|
|
vm_key = f"/v1/vm/{uuid4().hex}"
|
2019-07-18 12:10:17 +00:00
|
|
|
vm_entry = {
|
|
|
|
"owner": name,
|
|
|
|
"specs": specs,
|
|
|
|
"hostname": "",
|
|
|
|
"status": "REQUESTED_NEW",
|
|
|
|
"image_uuid": image_uuid,
|
|
|
|
}
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
client.put(vm_key, vm_entry, value_in_json=True)
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
return {"message": "VM Creation Queued"}, 200
|
|
|
|
else:
|
|
|
|
return {"message": "Image uuid not valid"}
|
2019-06-26 07:33:29 +00:00
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
deletevm_args = deletevm_argparser.parse_args()
|
2019-07-18 12:10:17 +00:00
|
|
|
name, realm, token, vmid = (
|
|
|
|
deletevm_args.name,
|
|
|
|
deletevm_args.realm,
|
|
|
|
deletevm_args.token,
|
|
|
|
deletevm_args.vmid,
|
|
|
|
)
|
2019-06-26 07:33:29 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
if check_otp(name, realm, token) == 200:
|
2019-06-26 07:33:29 +00:00
|
|
|
# User is good
|
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
vmentry_etcd = client.get(f"/v1/vm/{vmid}").value
|
2019-06-26 07:33:29 +00:00
|
|
|
if vmentry_etcd:
|
|
|
|
vmentry_etcd = json.loads(vmentry_etcd)
|
2019-07-03 12:47:24 +00:00
|
|
|
vmentry_etcd["status"] = "REQUESTED_DELETE"
|
2019-06-26 07:33:29 +00:00
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
client.put(f"/v1/vm/{vmid}", vmentry_etcd,
|
|
|
|
value_in_json=True)
|
2019-06-26 07:33:29 +00:00
|
|
|
|
|
|
|
return {"message": "VM Deletion Queued"}, 200
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid VM ID"}
|
2019-06-24 10:46:06 +00:00
|
|
|
else:
|
2019-06-26 07:33:29 +00:00
|
|
|
return {"message": "Invalid Credentials"}, 400
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
|
2019-06-25 10:07:23 +00:00
|
|
|
class VmStatus(Resource):
|
|
|
|
def get(self):
|
|
|
|
args = vmstatus_argparser.parse_args()
|
2019-07-18 12:10:17 +00:00
|
|
|
r = client.get(f"/v1/vm/{args.vmid}").value
|
2019-07-03 12:47:24 +00:00
|
|
|
print(r)
|
2019-06-25 10:07:23 +00:00
|
|
|
if r:
|
|
|
|
r = dict(json.loads(r.decode("utf-8")))
|
|
|
|
return r
|
|
|
|
return {"Message": "Not Found"}
|
|
|
|
|
|
|
|
|
2019-07-01 18:53:38 +00:00
|
|
|
class CreateImage(Resource):
|
|
|
|
def post(self):
|
|
|
|
image_stores = list(client.get_prefix("/v1/image_store/"))
|
|
|
|
args = createimage_argparser.parse_args()
|
|
|
|
image_file_uuid = args.uuid
|
|
|
|
image_store_name = args.image_store
|
|
|
|
|
|
|
|
file_entry = client.get(f"/v1/files/{image_file_uuid}")
|
|
|
|
if file_entry is None:
|
2019-07-18 12:10:17 +00:00
|
|
|
return (
|
|
|
|
{"Message": f"Image File with uuid '{image_file_uuid}' Not Found"},
|
|
|
|
400,
|
|
|
|
)
|
2019-07-01 18:53:38 +00:00
|
|
|
|
|
|
|
file_entry_value = json.loads(file_entry.value)
|
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
image_store = list(
|
|
|
|
filter(
|
|
|
|
lambda s: json.loads(s.value)["name"] == image_store_name, image_stores
|
|
|
|
)
|
|
|
|
)
|
2019-07-01 18:53:38 +00:00
|
|
|
if not image_store:
|
|
|
|
return {"Message": f"Store '{image_store_name}' does not exists"}, 400
|
|
|
|
|
|
|
|
image_store = image_store[0]
|
|
|
|
image_entry_json = {
|
|
|
|
"status": "TO_BE_CREATED",
|
|
|
|
"owner": file_entry_value["owner"],
|
|
|
|
"filename": file_entry_value["filename"],
|
|
|
|
"name": args.name,
|
2019-07-03 12:47:24 +00:00
|
|
|
"store_name": image_store_name,
|
2019-07-18 12:10:17 +00:00
|
|
|
"visibility": "public",
|
2019-07-01 18:53:38 +00:00
|
|
|
}
|
|
|
|
client.put(f"/v1/image/{image_file_uuid}", json.dumps(image_entry_json))
|
|
|
|
|
|
|
|
return {"Message": "Image successfully created"}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class StartVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
args = startvm_argparser.parse_args()
|
|
|
|
name, realm, token, vm_uuid = args.name, args.realm, args.token, args.vmid
|
|
|
|
|
|
|
|
if check_otp(name, realm, token) == 200:
|
|
|
|
vm = client.get(f"/v1/vm/{vm_uuid}", value_in_json=True)
|
2019-07-18 12:10:17 +00:00
|
|
|
if vm.value["owner"] != name:
|
|
|
|
return {"message": "Invalid User"}
|
2019-07-03 12:47:24 +00:00
|
|
|
if vm:
|
|
|
|
vm.value["status"] = "REQUESTED_START"
|
|
|
|
client.put(vm.key, json.dumps(vm.value))
|
|
|
|
return {"message": f"VM Start Queued"}
|
|
|
|
else:
|
|
|
|
return {"message": "No such VM found"}
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
|
|
|
class SuspendVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
args = startvm_argparser.parse_args()
|
|
|
|
name, realm, token, vm_uuid = args.name, args.realm, args.token, args.vmid
|
|
|
|
|
|
|
|
if check_otp(name, realm, token) == 200:
|
|
|
|
vm = client.get(f"/v1/vm/{vm_uuid}", value_in_json=True)
|
2019-07-18 12:10:17 +00:00
|
|
|
if vm.value["owner"] != name:
|
|
|
|
return {"message": "Invalid User"}
|
2019-07-03 12:47:24 +00:00
|
|
|
if vm:
|
|
|
|
vm.value["status"] = "REQUESTED_SUSPEND"
|
|
|
|
client.put(vm.key, json.dumps(vm.value))
|
|
|
|
return {"message": f"VM Suspension Queued"}
|
|
|
|
else:
|
|
|
|
return {"message": "No such VM found"}
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
|
|
|
class ResumeVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
args = startvm_argparser.parse_args()
|
|
|
|
name, realm, token, vm_uuid = args.name, args.realm, args.token, args.vmid
|
|
|
|
|
|
|
|
if check_otp(name, realm, token) == 200:
|
|
|
|
vm = client.get(f"/v1/vm/{vm_uuid}", value_in_json=True)
|
2019-07-18 12:10:17 +00:00
|
|
|
if vm.value["owner"] != name:
|
|
|
|
return {"message": "Invalid User"}
|
2019-07-03 12:47:24 +00:00
|
|
|
if vm:
|
|
|
|
vm.value["status"] = "REQUESTED_RESUME"
|
|
|
|
client.put(vm.key, json.dumps(vm.value))
|
|
|
|
return {"message": f"VM Resume Queued"}
|
|
|
|
else:
|
|
|
|
return {"message": "No such VM found"}
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
2019-07-11 08:34:21 +00:00
|
|
|
class ShutdownVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
args = startvm_argparser.parse_args()
|
|
|
|
name, realm, token, vm_uuid = args.name, args.realm, args.token, args.vmid
|
|
|
|
|
|
|
|
if check_otp(name, realm, token) == 200:
|
|
|
|
vm = client.get(f"/v1/vm/{vm_uuid}", value_in_json=True)
|
2019-07-18 12:10:17 +00:00
|
|
|
if vm.value["owner"] != name:
|
|
|
|
return {"message": "Invalid User"}
|
|
|
|
|
2019-07-11 08:34:21 +00:00
|
|
|
if vm:
|
|
|
|
vm.value["status"] = "REQUESTED_SHUTDOWN"
|
|
|
|
client.put(vm.key, json.dumps(vm.value))
|
|
|
|
return {"message": f"VM Shutdown Queued"}
|
|
|
|
else:
|
|
|
|
return {"message": "No such VM found"}
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
class ListUserVM(Resource):
|
|
|
|
def get(self):
|
|
|
|
args = uservm_argparser.parse_args()
|
|
|
|
name, realm, token = args.name, args.realm, args.token
|
|
|
|
|
|
|
|
if check_otp(name, realm, token) == 200:
|
|
|
|
vms = client.get_prefix(f"/v1/vm/", value_in_json=True)
|
|
|
|
if vms:
|
|
|
|
return_vms = []
|
|
|
|
user_vms = list(filter(lambda v: v.value["owner"] == name, vms))
|
|
|
|
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:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
2019-07-11 08:34:21 +00:00
|
|
|
class ListUserFiles(Resource):
|
|
|
|
def get(self):
|
|
|
|
args = uservm_argparser.parse_args()
|
|
|
|
name, realm, token = args.name, args.realm, args.token
|
|
|
|
|
|
|
|
if check_otp(name, realm, token) == 200:
|
|
|
|
files = client.get_prefix(f"/v1/files/", value_in_json=True)
|
|
|
|
if files:
|
|
|
|
return_files = []
|
|
|
|
user_files = list(filter(lambda f: f.value["owner"] == name, files))
|
|
|
|
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:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
|
|
|
|
class CreateHost(Resource):
|
|
|
|
def post(self):
|
|
|
|
args = addhost_argparser.parse_args()
|
|
|
|
name, realm, token, specs, hostname = (
|
|
|
|
args.name,
|
|
|
|
args.realm,
|
|
|
|
args.token,
|
|
|
|
args.specs,
|
|
|
|
args.hostname,
|
|
|
|
)
|
|
|
|
|
|
|
|
if realm == "ungleich-admin" and check_otp(name, realm, token) == 200:
|
|
|
|
# User is good
|
|
|
|
if not specs_parser.transform_specs(specs):
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
"message": f"""Invalid unit - Please use following units {specs_parser.get_allowed_units()}"""
|
|
|
|
},
|
|
|
|
400,
|
|
|
|
)
|
|
|
|
|
|
|
|
print(specs)
|
|
|
|
host_key = f"/v1/host/{uuid4().hex}"
|
|
|
|
host_entry = {
|
|
|
|
"specs": specs,
|
|
|
|
"hostname": hostname,
|
|
|
|
"status": "DEAD",
|
|
|
|
"last_heart_beat": "",
|
|
|
|
}
|
|
|
|
client.put(host_key, host_entry, value_in_json=True)
|
|
|
|
|
|
|
|
return {"message": "Host Created"}, 200
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials/Insufficient Permission"}, 400
|
|
|
|
|
|
|
|
|
2019-06-26 07:33:29 +00:00
|
|
|
api.add_resource(CreateVM, "/vm/create")
|
|
|
|
api.add_resource(DeleteVM, "/vm/delete")
|
|
|
|
api.add_resource(VmStatus, "/vm/status")
|
2019-07-11 08:34:21 +00:00
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
api.add_resource(StartVM, "/vm/start")
|
|
|
|
api.add_resource(SuspendVM, "/vm/suspend")
|
|
|
|
api.add_resource(ResumeVM, "/vm/resume")
|
2019-07-11 08:34:21 +00:00
|
|
|
api.add_resource(ShutdownVM, "/vm/shutdown")
|
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)
|