2019-09-22 18:51:33 +00:00
|
|
|
# TODO: realm should be part of user's name. Because, there can be multiple
|
|
|
|
# name but with different realms. For Example, nico exists in both
|
|
|
|
# ungleich-admin and ungleich-twitter realm. So, to differentiate between
|
|
|
|
# them, we need to make realm part of user's name
|
2019-06-24 17:14:45 +00:00
|
|
|
import json
|
2019-08-11 17:01:27 +00:00
|
|
|
import subprocess
|
2019-09-07 10:38:58 +00:00
|
|
|
import os
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
from uuid import uuid4
|
2019-08-01 10:04:40 +00:00
|
|
|
|
2019-09-12 15:55:25 +00:00
|
|
|
from flask import Flask, request
|
|
|
|
from flask_restful import Resource, Api
|
2019-09-22 18:51:33 +00:00
|
|
|
from ucloud_common.vm import VMStatus
|
|
|
|
from ucloud_common.request import RequestEntry, RequestType
|
2019-09-12 15:55:25 +00:00
|
|
|
|
|
|
|
from config import etcd_client as client
|
2019-09-22 18:51:33 +00:00
|
|
|
from config import (
|
|
|
|
WITHOUT_CEPH,
|
|
|
|
VM_PREFIX,
|
|
|
|
HOST_PREFIX,
|
|
|
|
FILE_PREFIX,
|
|
|
|
IMAGE_PREFIX,
|
|
|
|
logging,
|
|
|
|
REQUEST_POOL,
|
|
|
|
VM_POOL,
|
|
|
|
HOST_POOL,
|
|
|
|
)
|
|
|
|
from schemas import (
|
|
|
|
CreateVMSchema,
|
|
|
|
VMStatusSchema,
|
|
|
|
CreateImageSchema,
|
|
|
|
VmActionSchema,
|
|
|
|
OTPSchema,
|
|
|
|
CreateHostSchema,
|
|
|
|
VmMigrationSchema,
|
2019-09-26 12:54:05 +00:00
|
|
|
AddSSHSchema,
|
|
|
|
RemoveSSHSchema
|
2019-09-22 18:51:33 +00:00
|
|
|
)
|
2019-08-01 10:04:40 +00:00
|
|
|
|
2019-06-24 10:46:06 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
|
|
|
|
|
|
|
|
|
|
|
class CreateVM(Resource):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def post():
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = CreateVMSchema(data)
|
|
|
|
if validator.is_valid():
|
2019-08-11 17:01:27 +00:00
|
|
|
vm_uuid = uuid4().hex
|
2019-09-12 15:55:25 +00:00
|
|
|
vm_key = os.path.join(VM_PREFIX, vm_uuid)
|
2019-08-01 10:04:40 +00:00
|
|
|
vm_entry = {
|
2019-09-22 18:51:33 +00:00
|
|
|
"name": data["vm_name"],
|
2019-08-01 10:04:40 +00:00
|
|
|
"owner": data["name"],
|
|
|
|
"specs": data["specs"],
|
|
|
|
"hostname": "",
|
2019-08-11 17:01:27 +00:00
|
|
|
"status": "",
|
2019-08-01 10:04:40 +00:00
|
|
|
"image_uuid": data["image_uuid"],
|
2019-09-03 16:01:40 +00:00
|
|
|
"log": [],
|
2019-09-17 06:49:40 +00:00
|
|
|
"storage_attachment": [],
|
2019-09-22 18:51:33 +00:00
|
|
|
"vnc_socket": "",
|
2019-08-01 10:04:40 +00:00
|
|
|
}
|
|
|
|
client.put(vm_key, vm_entry, value_in_json=True)
|
2019-08-11 17:01:27 +00:00
|
|
|
|
|
|
|
# Create ScheduleVM Request
|
2019-09-22 18:51:33 +00:00
|
|
|
r = RequestEntry.from_scratch(type=RequestType.ScheduleVM, uuid=vm_uuid)
|
2019-09-12 15:55:25 +00:00
|
|
|
REQUEST_POOL.put(r)
|
2019-08-11 17:01:27 +00:00
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
return {"message": "VM Creation Queued"}, 200
|
2019-09-12 15:55:25 +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):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def get():
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = VMStatusSchema(data)
|
|
|
|
if validator.is_valid():
|
2019-09-22 18:51:33 +00:00
|
|
|
vm = VM_POOL.get(os.path.join(VM_PREFIX, data["uuid"]))
|
2019-08-01 10:04:40 +00:00
|
|
|
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):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def post():
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = CreateImageSchema(data)
|
|
|
|
if validator.is_valid():
|
2019-09-22 18:51:33 +00:00
|
|
|
file_entry = client.get(os.path.join(FILE_PREFIX, data["uuid"]))
|
2019-08-01 10:04:40 +00:00
|
|
|
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",
|
|
|
|
}
|
2019-09-22 18:51:33 +00:00
|
|
|
client.put(
|
|
|
|
os.path.join(IMAGE_PREFIX, data["uuid"]), json.dumps(image_entry_json)
|
|
|
|
)
|
2019-08-01 10:04:40 +00:00
|
|
|
|
2019-08-11 17:01:27 +00:00
|
|
|
return {"message": "Image successfully created"}
|
2019-09-12 15:55:25 +00:00
|
|
|
return validator.get_errors(), 400
|
2019-07-01 18:53:38 +00:00
|
|
|
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
class ListPublicImages(Resource):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def get():
|
2019-09-12 15:55:25 +00:00
|
|
|
images = client.get_prefix(IMAGE_PREFIX)
|
2019-07-03 12:47:24 +00:00
|
|
|
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-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def post():
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = VmActionSchema(data)
|
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-09-22 18:51:33 +00:00
|
|
|
vm_entry = VM_POOL.get(os.path.join(VM_PREFIX, data["uuid"]))
|
2019-08-11 17:01:27 +00:00
|
|
|
action = data["action"]
|
|
|
|
|
2019-08-01 10:04:40 +00:00
|
|
|
if action == "start":
|
2019-08-11 17:01:27 +00:00
|
|
|
vm_entry.status = VMStatus.requested_start
|
2019-09-12 15:55:25 +00:00
|
|
|
VM_POOL.put(vm_entry)
|
2019-08-11 17:01:27 +00:00
|
|
|
action = "schedule"
|
|
|
|
|
|
|
|
if action == "delete" and vm_entry.hostname == "":
|
|
|
|
try:
|
2019-09-22 18:51:33 +00:00
|
|
|
path_without_protocol = vm_entry.path[vm_entry.path.find(":") + 1 :]
|
2019-09-07 10:38:58 +00:00
|
|
|
|
|
|
|
if WITHOUT_CEPH:
|
2019-09-22 18:51:33 +00:00
|
|
|
command_to_delete = [
|
|
|
|
"rm",
|
|
|
|
os.path.join("/var/vm", vm_entry.uuid),
|
|
|
|
]
|
2019-09-07 10:38:58 +00:00
|
|
|
else:
|
|
|
|
command_to_delete = ["rbd", "rm", path_without_protocol]
|
|
|
|
|
|
|
|
subprocess.check_output(command_to_delete, stderr=subprocess.PIPE)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
if "No such file" in e.stderr.decode("utf-8"):
|
2019-08-11 17:01:27 +00:00
|
|
|
client.client.delete(vm_entry.key)
|
|
|
|
return {"message": "VM successfully deleted"}
|
|
|
|
else:
|
2019-09-07 10:38:58 +00:00
|
|
|
logging.exception(e)
|
2019-08-11 17:01:27 +00:00
|
|
|
return {"message": "Some error occurred while deleting VM"}
|
2019-09-07 10:38:58 +00:00
|
|
|
else:
|
|
|
|
client.client.delete(vm_entry.key)
|
|
|
|
return {"message": "VM successfully deleted"}
|
2019-08-11 17:01:27 +00:00
|
|
|
|
2019-09-22 18:51:33 +00:00
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type="{}VM".format(action.title()),
|
|
|
|
uuid=data["uuid"],
|
|
|
|
hostname=vm_entry.hostname,
|
|
|
|
)
|
2019-09-12 15:55:25 +00:00
|
|
|
REQUEST_POOL.put(r)
|
|
|
|
return {"message": "VM {} Queued".format(action.title())}, 200
|
2019-08-11 17:01:27 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class VMMigration(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
|
|
|
validator = VmMigrationSchema(data)
|
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-09-22 18:51:33 +00:00
|
|
|
vm = VM_POOL.get(data["uuid"])
|
|
|
|
|
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type=RequestType.ScheduleVM,
|
|
|
|
uuid=vm.uuid,
|
|
|
|
destination=os.path.join(HOST_PREFIX, data["destination"]),
|
|
|
|
migration=True,
|
|
|
|
)
|
2019-09-12 15:55:25 +00:00
|
|
|
REQUEST_POOL.put(r)
|
|
|
|
return {"message": "VM Migration Initialization Queued"}, 200
|
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):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def get():
|
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-09-12 15:55:25 +00:00
|
|
|
vms = client.get_prefix(VM_PREFIX, value_in_json=True)
|
2019-09-22 18:51:33 +00:00
|
|
|
return_vms = []
|
|
|
|
user_vms = filter(lambda v: v.value["owner"] == data["name"], vms)
|
|
|
|
for vm in user_vms:
|
|
|
|
return_vms.append(
|
|
|
|
{
|
|
|
|
"name": vm.value["name"],
|
|
|
|
"vm_uuid": vm.key.split("/")[-1],
|
|
|
|
"specs": vm.value["specs"],
|
|
|
|
"status": vm.value["status"],
|
|
|
|
"hostname": vm.value["hostname"],
|
|
|
|
"vnc_socket": None
|
|
|
|
if vm.value.get("vnc_socket", None) == None
|
|
|
|
else vm.value["vnc_socket"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if return_vms:
|
2019-07-03 12:47:24 +00:00
|
|
|
return {"message": return_vms}, 200
|
2019-09-22 18:51:33 +00:00
|
|
|
return {"message": "No VM found"}, 404
|
|
|
|
|
2019-07-03 12:47:24 +00:00
|
|
|
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):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def get():
|
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-09-12 15:55:25 +00:00
|
|
|
files = client.get_prefix(FILE_PREFIX, value_in_json=True)
|
|
|
|
return_files = []
|
|
|
|
user_files = list(filter(lambda f: f.value["owner"] == data["name"], files))
|
|
|
|
for file in user_files:
|
|
|
|
return_files.append(
|
|
|
|
{
|
|
|
|
"filename": file.value["filename"],
|
|
|
|
"uuid": file.key.split("/")[-1],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return {"message": return_files}, 200
|
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-18 12:10:17 +00:00
|
|
|
|
|
|
|
class CreateHost(Resource):
|
2019-08-11 17:01:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def post():
|
2019-08-01 10:04:40 +00:00
|
|
|
data = request.json
|
|
|
|
validator = CreateHostSchema(data)
|
|
|
|
if validator.is_valid():
|
2019-09-12 15:55:25 +00:00
|
|
|
host_key = os.path.join(HOST_PREFIX, uuid4().hex)
|
2019-07-18 12:10:17 +00:00
|
|
|
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
|
2019-09-03 16:01:40 +00:00
|
|
|
|
|
|
|
return validator.get_errors(), 400
|
2019-07-18 12:10:17 +00:00
|
|
|
|
|
|
|
|
2019-08-11 17:01:27 +00:00
|
|
|
class ListHost(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
2019-09-12 15:55:25 +00:00
|
|
|
hosts = HOST_POOL.hosts
|
2019-09-22 18:51:33 +00:00
|
|
|
r = {
|
|
|
|
host.key: {
|
|
|
|
"status": host.status,
|
|
|
|
"specs": host.specs,
|
|
|
|
"hostname": host.hostname,
|
|
|
|
}
|
|
|
|
for host in hosts
|
|
|
|
}
|
2019-08-11 17:01:27 +00:00
|
|
|
return r, 200
|
|
|
|
|
|
|
|
|
2019-09-26 12:54:05 +00:00
|
|
|
class AddSSHKey(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
|
|
|
validator = AddSSHSchema(data)
|
|
|
|
if validator.is_valid():
|
|
|
|
etcd_key = "/v1/user/{}/keys".format(data["name"])
|
|
|
|
etcd_entry = client.get(etcd_key, value_in_json=True)
|
|
|
|
if etcd_entry:
|
|
|
|
if data["key_name"] not in etcd_entry.value:
|
|
|
|
etcd_entry.value[data["key_name"]] = data["key"]
|
|
|
|
client.put(etcd_entry.key, etcd_entry.value, value_in_json=True)
|
|
|
|
else:
|
|
|
|
return {"message": "Key with name '{}' already exists".format(data["key_name"])}
|
|
|
|
else:
|
|
|
|
# Key Not Found. It implies user' haven't added any key yet.
|
|
|
|
key_entry = {data["key_name"]: data["key"]}
|
|
|
|
client.put(etcd_key, key_entry, value_in_json=True)
|
|
|
|
|
|
|
|
return {"message": "Key added successfully"}
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class RemoveSSHKey(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
|
|
|
validator = RemoveSSHSchema(data)
|
|
|
|
if validator.is_valid():
|
|
|
|
etcd_key = "/v1/user/{}/keys".format(data["name"])
|
|
|
|
etcd_entry = client.get(etcd_key, value_in_json=True)
|
|
|
|
if etcd_entry:
|
|
|
|
try:
|
|
|
|
del etcd_entry.value[data["key_name"]]
|
|
|
|
except KeyError:
|
|
|
|
return {"message": "No such key exists."}
|
|
|
|
else:
|
|
|
|
client.put(etcd_entry.key, etcd_entry.value, value_in_json=True)
|
|
|
|
return {"message": "Key successfully removed."}
|
|
|
|
else:
|
|
|
|
return {"message": "No Key Exists at all."}
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
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-08-11 17:01:27 +00:00
|
|
|
api.add_resource(VMMigration, "/vm/migrate")
|
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-09-26 12:54:05 +00:00
|
|
|
api.add_resource(AddSSHKey, "/user/add-ssh")
|
|
|
|
api.add_resource(RemoveSSHKey, "/user/remove-ssh")
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-07-18 12:10:17 +00:00
|
|
|
api.add_resource(CreateHost, "/host/create")
|
2019-08-11 17:01:27 +00:00
|
|
|
api.add_resource(ListHost, "/host/list")
|
2019-07-18 12:10:17 +00:00
|
|
|
|
2019-06-26 07:33:29 +00:00
|
|
|
if __name__ == "__main__":
|
2019-06-30 16:20:02 +00:00
|
|
|
app.run(host="::", debug=True)
|