2019-07-01 18:53:38 +00:00
|
|
|
# TODO
|
|
|
|
# convert etcd3 usage to etcd3_wrapper
|
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
import etcd3
|
|
|
|
import json
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
from helper import check_otp
|
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-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
etcd_client = etcd3.client(host=config("ETCD_HOST"), port=int(config("ETCD_PORT")))
|
2019-07-01 18:53:38 +00:00
|
|
|
client = Etcd3Wrapper()
|
2019-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
createvm_argparser = reqparse.RequestParser()
|
|
|
|
createvm_argparser.add_argument("name", type=str, required=True)
|
|
|
|
createvm_argparser.add_argument("realm", type=str, required=True)
|
|
|
|
createvm_argparser.add_argument("seed", type=str, required=True)
|
|
|
|
createvm_argparser.add_argument("specs", type=dict, required=True)
|
|
|
|
|
2019-06-26 07:33:29 +00:00
|
|
|
deletevm_argparser = reqparse.RequestParser()
|
|
|
|
deletevm_argparser.add_argument("name", type=str, required=True)
|
|
|
|
deletevm_argparser.add_argument("realm", type=str, required=True)
|
|
|
|
deletevm_argparser.add_argument("seed", type=str, required=True)
|
|
|
|
deletevm_argparser.add_argument("vmid", type=str, required=True)
|
|
|
|
|
2019-06-25 10:07:23 +00:00
|
|
|
vmstatus_argparser = reqparse.RequestParser()
|
|
|
|
vmstatus_argparser.add_argument("id", type=str, required=True)
|
|
|
|
|
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-06-24 10:46:06 +00:00
|
|
|
|
|
|
|
class CreateVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
createvm_args = createvm_argparser.parse_args()
|
|
|
|
name, realm, seed, specs = createvm_args.name, createvm_args.realm,\
|
|
|
|
createvm_args.seed, createvm_args.specs
|
|
|
|
|
|
|
|
if check_otp(name, realm, seed) == 200:
|
|
|
|
# User is good
|
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
vm_entry = {"owner": name,
|
|
|
|
"specs": specs,
|
2019-06-25 10:07:23 +00:00
|
|
|
"hostname": "",
|
2019-06-25 10:18:32 +00:00
|
|
|
"status": "REQUESTED_NEW"}
|
2019-06-24 12:43:48 +00:00
|
|
|
|
2019-06-24 17:14:45 +00:00
|
|
|
etcd_client.put(f"/v1/vm/{uuid4().hex}", json.dumps(vm_entry))
|
2019-06-24 10:46:06 +00:00
|
|
|
|
2019-06-26 07:33:29 +00:00
|
|
|
return {"message": "VM Creation Queued"}, 200
|
|
|
|
else:
|
|
|
|
return {"message": "Invalid Credentials"}, 400
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteVM(Resource):
|
|
|
|
def post(self):
|
|
|
|
deletevm_args = deletevm_argparser.parse_args()
|
|
|
|
name, realm, seed, vmid = deletevm_args.name, deletevm_args.realm,\
|
|
|
|
deletevm_args.seed, deletevm_args.vmid
|
|
|
|
|
|
|
|
if check_otp(name, realm, seed) == 200:
|
|
|
|
# User is good
|
|
|
|
|
|
|
|
vmentry_etcd = etcd_client.get(f"/v1/vm/{vmid}")[0]
|
|
|
|
if vmentry_etcd:
|
|
|
|
vmentry_etcd = json.loads(vmentry_etcd)
|
|
|
|
vmentry_etcd["status"] = "REQUEST_DELETE"
|
|
|
|
|
|
|
|
etcd_client.put(f"/v1/vm/{vmid}", json.dumps(vmentry_etcd))
|
|
|
|
|
|
|
|
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()
|
|
|
|
r = etcd_client.get(f"/v1/vm/{args.id}")[0]
|
|
|
|
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-02 07:45:13 +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)
|
|
|
|
|
|
|
|
image_store = list(filter(lambda s: json.loads(s.value)["name"] == image_store_name, image_stores))
|
|
|
|
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,
|
|
|
|
"store_name": image_store_name
|
|
|
|
}
|
|
|
|
client.put(f"/v1/image/{image_file_uuid}", json.dumps(image_entry_json))
|
|
|
|
|
|
|
|
return {"Message": "Image successfully created"}
|
|
|
|
|
|
|
|
|
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-01 18:53:38 +00:00
|
|
|
api.add_resource(CreateImage, "/image/create")
|
|
|
|
|
2019-06-24 10:46:06 +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)
|