a
This commit is contained in:
parent
3f406799db
commit
ec032ed0db
9 changed files with 544 additions and 103 deletions
93
main.py
93
main.py
|
|
@ -6,16 +6,21 @@ import json
|
|||
import subprocess
|
||||
import os
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import Flask, request
|
||||
from flask_restful import Resource, Api
|
||||
from uuid import uuid4
|
||||
from os.path import join
|
||||
from config import etcd_client as client
|
||||
from config import WITHOUT_CEPH, logging
|
||||
|
||||
from ucloud_common.vm import VmPool, VMStatus
|
||||
from ucloud_common.host import HostPool
|
||||
from ucloud_common.request import RequestEntry, RequestPool, RequestType
|
||||
from ucloud_common.request import (RequestEntry,
|
||||
RequestPool,
|
||||
RequestType)
|
||||
|
||||
from config import etcd_client as client
|
||||
from config import (WITHOUT_CEPH, VM_PREFIX,
|
||||
HOST_PREFIX, REQUEST_PREFIX,
|
||||
FILE_PREFIX, IMAGE_PREFIX,
|
||||
logging)
|
||||
from schemas import (CreateVMSchema, VMStatusSchema,
|
||||
CreateImageSchema, VmActionSchema,
|
||||
OTPSchema, CreateHostSchema,
|
||||
|
|
@ -24,9 +29,9 @@ from schemas import (CreateVMSchema, VMStatusSchema,
|
|||
app = Flask(__name__)
|
||||
api = Api(app)
|
||||
|
||||
vm_pool = VmPool(client, "/v1/vm")
|
||||
host_pool = HostPool(client, "/v1/host")
|
||||
request_pool = RequestPool(client, "/v1/request")
|
||||
VM_POOL = VmPool(client, VM_PREFIX)
|
||||
HOST_POOL = HostPool(client, HOST_PREFIX)
|
||||
REQUEST_POOL = RequestPool(client, REQUEST_PREFIX)
|
||||
|
||||
|
||||
class CreateVM(Resource):
|
||||
|
|
@ -35,10 +40,8 @@ class CreateVM(Resource):
|
|||
data = request.json
|
||||
validator = CreateVMSchema(data)
|
||||
if validator.is_valid():
|
||||
# Create VM Entry under /v1/vm/
|
||||
# TODO: !!!Generate Mac Address on creation of VM
|
||||
vm_uuid = uuid4().hex
|
||||
vm_key = f"/v1/vm/{vm_uuid}"
|
||||
vm_key = os.path.join(VM_PREFIX, vm_uuid)
|
||||
vm_entry = {
|
||||
"owner": data["name"],
|
||||
"specs": data["specs"],
|
||||
|
|
@ -53,11 +56,10 @@ class CreateVM(Resource):
|
|||
# Create ScheduleVM Request
|
||||
r = RequestEntry.from_scratch(type=RequestType.ScheduleVM,
|
||||
uuid=vm_uuid)
|
||||
request_pool.put(r)
|
||||
REQUEST_POOL.put(r)
|
||||
|
||||
return {"message": "VM Creation Queued"}, 200
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
return validator.get_errors(), 400
|
||||
|
||||
|
||||
class VmStatus(Resource):
|
||||
|
|
@ -66,7 +68,7 @@ class VmStatus(Resource):
|
|||
data = request.json
|
||||
validator = VMStatusSchema(data)
|
||||
if validator.is_valid():
|
||||
vm = vm_pool.get(f"/v1/vm/{data['uuid']}")
|
||||
vm = VM_POOL.get(os.path.join(VM_PREFIX, data['uuid']))
|
||||
return str(vm)
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
|
@ -78,7 +80,7 @@ class CreateImage(Resource):
|
|||
data = request.json
|
||||
validator = CreateImageSchema(data)
|
||||
if validator.is_valid():
|
||||
file_entry = client.get(f"/v1/file/{data['uuid']}")
|
||||
file_entry = client.get(os.path.join(FILE_PREFIX, data['uuid']))
|
||||
file_entry_value = json.loads(file_entry.value)
|
||||
|
||||
image_entry_json = {
|
||||
|
|
@ -89,21 +91,19 @@ class CreateImage(Resource):
|
|||
"store_name": data["image_store"],
|
||||
"visibility": "public",
|
||||
}
|
||||
client.put(f"/v1/image/{data['uuid']}", json.dumps(image_entry_json))
|
||||
client.put(os.path.join(IMAGE_PREFIX, data['uuid']), json.dumps(image_entry_json))
|
||||
|
||||
return {"message": "Image successfully created"}
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
return validator.get_errors(), 400
|
||||
|
||||
|
||||
class ListPublicImages(Resource):
|
||||
@staticmethod
|
||||
def get():
|
||||
images = client.get_prefix("/v1/image/")
|
||||
images = client.get_prefix(IMAGE_PREFIX)
|
||||
r = {}
|
||||
for image in images:
|
||||
r[image.key.split("/")[-1]] = json.loads(image.value)
|
||||
|
||||
return r, 200
|
||||
|
||||
|
||||
|
|
@ -114,12 +114,12 @@ class VMAction(Resource):
|
|||
validator = VmActionSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
vm_entry = vm_pool.get(f"/v1/vm/{data['uuid']}")
|
||||
vm_entry = VM_POOL.get(os.path.join(VM_PREFIX, data['uuid']))
|
||||
action = data["action"]
|
||||
|
||||
if action == "start":
|
||||
vm_entry.status = VMStatus.requested_start
|
||||
vm_pool.put(vm_entry)
|
||||
VM_POOL.put(vm_entry)
|
||||
action = "schedule"
|
||||
|
||||
if action == "delete" and vm_entry.hostname == "":
|
||||
|
|
@ -143,11 +143,11 @@ class VMAction(Resource):
|
|||
client.client.delete(vm_entry.key)
|
||||
return {"message": "VM successfully deleted"}
|
||||
|
||||
r = RequestEntry.from_scratch(type=f"{action.title()}VM",
|
||||
r = RequestEntry.from_scratch(type="{}VM".format(action.title()),
|
||||
uuid=data['uuid'],
|
||||
hostname=vm_entry.hostname)
|
||||
request_pool.put(r)
|
||||
return {"message": f"VM {action.title()} Queued"}, 200
|
||||
REQUEST_POOL.put(r)
|
||||
return {"message": "VM {} Queued".format(action.title())}, 200
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
||||
|
|
@ -159,14 +159,14 @@ class VMMigration(Resource):
|
|||
validator = VmMigrationSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
vm = vm_pool.get(data['uuid'])
|
||||
vm = VM_POOL.get(data['uuid'])
|
||||
|
||||
r = RequestEntry.from_scratch(type=RequestType.ScheduleVM,
|
||||
uuid=vm.uuid,
|
||||
destination=join("/v1/host", data["destination"]),
|
||||
destination=os.path.join(HOST_PREFIX, data["destination"]),
|
||||
migration=True)
|
||||
request_pool.put(r)
|
||||
return {"message": f"VM Migration Initialization Queued"}, 200
|
||||
REQUEST_POOL.put(r)
|
||||
return {"message": "VM Migration Initialization Queued"}, 200
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
||||
|
|
@ -178,7 +178,7 @@ class ListUserVM(Resource):
|
|||
validator = OTPSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
vms = client.get_prefix(f"/v1/vm/", value_in_json=True)
|
||||
vms = client.get_prefix(VM_PREFIX, value_in_json=True)
|
||||
if vms:
|
||||
return_vms = []
|
||||
user_vms = list(filter(lambda v: v.value["owner"] == data["name"], vms))
|
||||
|
|
@ -205,20 +205,17 @@ class ListUserFiles(Resource):
|
|||
validator = OTPSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
files = client.get_prefix(f"/v1/file/", value_in_json=True)
|
||||
if files:
|
||||
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
|
||||
else:
|
||||
return {"message": "No File found"}, 404
|
||||
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
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
||||
|
|
@ -229,7 +226,7 @@ class CreateHost(Resource):
|
|||
data = request.json
|
||||
validator = CreateHostSchema(data)
|
||||
if validator.is_valid():
|
||||
host_key = f"/v1/host/{uuid4().hex}"
|
||||
host_key = os.path.join(HOST_PREFIX, uuid4().hex)
|
||||
host_entry = {
|
||||
"specs": data["specs"],
|
||||
"hostname": data["hostname"],
|
||||
|
|
@ -246,7 +243,7 @@ class CreateHost(Resource):
|
|||
class ListHost(Resource):
|
||||
@staticmethod
|
||||
def get():
|
||||
hosts = host_pool.hosts
|
||||
hosts = HOST_POOL.hosts
|
||||
r = {host.key: {"status": host.status, "specs": host.specs, "hostname": host.hostname} for host in hosts}
|
||||
return r, 200
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue