2019-10-25 06:42:40 +00:00
|
|
|
import json
|
2019-11-25 06:52:36 +00:00
|
|
|
import pynetbox
|
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
from uuid import uuid4
|
2019-11-25 06:52:36 +00:00
|
|
|
from os.path import join as join_path
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
from flask import Flask, request
|
|
|
|
from flask_restful import Resource, Api
|
|
|
|
|
2019-12-03 10:40:41 +00:00
|
|
|
from ucloud.common import counters
|
|
|
|
from ucloud.common.vm import VMStatus
|
|
|
|
from ucloud.common.request import RequestEntry, RequestType
|
2019-12-08 12:29:24 +00:00
|
|
|
from ucloud.config import (etcd_client, request_pool, vm_pool, host_pool, config, image_storage_handler)
|
2019-11-18 17:39:57 +00:00
|
|
|
from . import schemas
|
|
|
|
from .helper import generate_mac, mac2ipv6
|
2019-12-03 11:49:10 +00:00
|
|
|
from . import logger
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
|
|
|
|
|
|
|
|
|
|
|
class CreateVM(Resource):
|
2019-11-25 06:52:36 +00:00
|
|
|
"""API Request to Handle Creation of VM"""
|
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.CreateVMSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
|
|
|
vm_uuid = uuid4().hex
|
2019-12-08 13:15:36 +00:00
|
|
|
vm_key = join_path(config['etcd']["VM_PREFIX"], vm_uuid)
|
2019-10-25 06:42:40 +00:00
|
|
|
specs = {
|
2019-11-18 17:39:57 +00:00
|
|
|
"cpu": validator.specs["cpu"],
|
|
|
|
"ram": validator.specs["ram"],
|
|
|
|
"os-ssd": validator.specs["os-ssd"],
|
|
|
|
"hdd": validator.specs["hdd"],
|
2019-10-25 06:42:40 +00:00
|
|
|
}
|
2019-12-03 10:40:41 +00:00
|
|
|
macs = [generate_mac() for _ in range(len(data["network"]))]
|
2019-11-28 08:04:53 +00:00
|
|
|
tap_ids = [counters.increment_etcd_counter(etcd_client, "/v1/counter/tap")
|
2019-12-03 10:40:41 +00:00
|
|
|
for _ in range(len(data["network"]))]
|
2019-10-25 06:42:40 +00:00
|
|
|
vm_entry = {
|
|
|
|
"name": data["vm_name"],
|
|
|
|
"owner": data["name"],
|
|
|
|
"owner_realm": data["realm"],
|
|
|
|
"specs": specs,
|
|
|
|
"hostname": "",
|
2019-11-27 07:12:29 +00:00
|
|
|
"status": VMStatus.stopped,
|
2019-11-02 15:42:24 +00:00
|
|
|
"image_uuid": validator.image_uuid,
|
2019-10-25 06:42:40 +00:00
|
|
|
"log": [],
|
|
|
|
"vnc_socket": "",
|
2019-11-28 08:04:53 +00:00
|
|
|
"network": list(zip(data["network"], macs, tap_ids)),
|
2019-11-18 17:39:57 +00:00
|
|
|
"metadata": {"ssh-keys": []},
|
2019-10-25 06:42:40 +00:00
|
|
|
}
|
|
|
|
etcd_client.put(vm_key, vm_entry, value_in_json=True)
|
|
|
|
|
|
|
|
# Create ScheduleVM Request
|
2019-11-18 17:39:57 +00:00
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type=RequestType.ScheduleVM, uuid=vm_uuid,
|
2019-12-08 13:15:36 +00:00
|
|
|
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
|
|
|
request_pool.put(r)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
return {"message": "VM Creation Queued"}, 200
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class VmStatus(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.VMStatusSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
vm = vm_pool.get(
|
2019-12-08 13:15:36 +00:00
|
|
|
join_path(config['etcd']["VM_PREFIX"], data["uuid"])
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-11-02 15:42:24 +00:00
|
|
|
vm_value = vm.value.copy()
|
2019-11-15 16:11:45 +00:00
|
|
|
vm_value["ip"] = []
|
2019-11-28 08:04:53 +00:00
|
|
|
for network_mac_and_tap in vm.network:
|
|
|
|
network_name, mac, tap = network_mac_and_tap
|
2019-11-18 17:39:57 +00:00
|
|
|
network = etcd_client.get(
|
2019-11-25 06:52:36 +00:00
|
|
|
join_path(
|
2019-12-08 13:15:36 +00:00
|
|
|
config['etcd']["NETWORK_PREFIX"],
|
2019-11-18 17:39:57 +00:00
|
|
|
data["name"],
|
|
|
|
network_name,
|
|
|
|
),
|
|
|
|
value_in_json=True,
|
|
|
|
)
|
2019-11-15 16:11:45 +00:00
|
|
|
ipv6_addr = network.value.get("ipv6").split("::")[0] + "::"
|
|
|
|
vm_value["ip"].append(mac2ipv6(mac, ipv6_addr))
|
2019-11-02 15:42:24 +00:00
|
|
|
vm.value = vm_value
|
|
|
|
return vm.value
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class CreateImage(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.CreateImageSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
file_entry = etcd_client.get(
|
2019-12-08 13:15:36 +00:00
|
|
|
join_path(config['etcd']["FILE_PREFIX"], data["uuid"])
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-10-25 06:42: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",
|
|
|
|
}
|
|
|
|
etcd_client.put(
|
2019-12-08 13:15:36 +00:00
|
|
|
join_path(config['etcd']["IMAGE_PREFIX"], data["uuid"]),
|
2019-11-18 17:39:57 +00:00
|
|
|
json.dumps(image_entry_json),
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
|
|
|
|
2019-11-12 10:26:10 +00:00
|
|
|
return {"message": "Image queued for creation."}
|
2019-10-25 06:42:40 +00:00
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListPublicImages(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
2019-11-18 17:39:57 +00:00
|
|
|
images = etcd_client.get_prefix(
|
2019-12-08 13:15:36 +00:00
|
|
|
config['etcd']["IMAGE_PREFIX"], value_in_json=True
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-11-25 06:52:36 +00:00
|
|
|
r = {
|
|
|
|
"images": []
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
for image in images:
|
2019-11-18 17:39:57 +00:00
|
|
|
image_key = "{}:{}".format(
|
|
|
|
image.value["store_name"], image.value["name"]
|
|
|
|
)
|
|
|
|
r["images"].append(
|
|
|
|
{"name": image_key, "status": image.value["status"]}
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
return r, 200
|
|
|
|
|
|
|
|
|
|
|
|
class VMAction(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.VmActionSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
vm_entry = vm_pool.get(
|
2019-12-08 13:15:36 +00:00
|
|
|
join_path(config['etcd']["VM_PREFIX"], data["uuid"])
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
action = data["action"]
|
|
|
|
|
|
|
|
if action == "start":
|
|
|
|
action = "schedule"
|
|
|
|
|
|
|
|
if action == "delete" and vm_entry.hostname == "":
|
2019-11-25 06:52:36 +00:00
|
|
|
if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
|
|
|
|
r_status = image_storage_handler.delete_vm_image(vm_entry.uuid)
|
|
|
|
if r_status:
|
2019-10-25 06:42:40 +00:00
|
|
|
etcd_client.client.delete(vm_entry.key)
|
|
|
|
return {"message": "VM successfully deleted"}
|
|
|
|
else:
|
2019-11-25 06:52:36 +00:00
|
|
|
logger.error("Some Error Occurred while deleting VM")
|
|
|
|
return {"message": "VM deletion unsuccessfull"}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
etcd_client.client.delete(vm_entry.key)
|
|
|
|
return {"message": "VM successfully deleted"}
|
|
|
|
|
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type="{}VM".format(action.title()),
|
|
|
|
uuid=data["uuid"],
|
|
|
|
hostname=vm_entry.hostname,
|
2019-12-08 12:29:24 +00:00
|
|
|
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
2019-11-18 17:39:57 +00:00
|
|
|
request_pool.put(r)
|
2019-10-25 06:42:40 +00:00
|
|
|
return {"message": "VM {} Queued".format(action.title())}, 200
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class VMMigration(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.VmMigrationSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
vm = vm_pool.get(data["uuid"])
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type=RequestType.ScheduleVM,
|
|
|
|
uuid=vm.uuid,
|
2019-11-25 06:52:36 +00:00
|
|
|
destination=join_path(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['etcd']["HOST_PREFIX"], validator.destination.value
|
2019-11-18 17:39:57 +00:00
|
|
|
),
|
2019-10-25 06:42:40 +00:00
|
|
|
migration=True,
|
2019-12-08 12:29:24 +00:00
|
|
|
request_prefix=config['etcd']["REQUEST_PREFIX"]
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
2019-11-18 17:39:57 +00:00
|
|
|
request_pool.put(r)
|
2019-10-25 06:42:40 +00:00
|
|
|
return {"message": "VM Migration Initialization Queued"}, 200
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListUserVM(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.OTPSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
vms = etcd_client.get_prefix(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['etcd']["VM_PREFIX"], value_in_json=True
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +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"],
|
2019-11-11 18:42:57 +00:00
|
|
|
# "mac": vm.value["mac"],
|
2019-10-25 06:42:40 +00:00
|
|
|
"vnc_socket": None
|
|
|
|
if vm.value.get("vnc_socket", None) is None
|
|
|
|
else vm.value["vnc_socket"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if return_vms:
|
|
|
|
return {"message": return_vms}, 200
|
|
|
|
return {"message": "No VM found"}, 404
|
|
|
|
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListUserFiles(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.OTPSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
files = etcd_client.get_prefix(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['etcd']["FILE_PREFIX"], value_in_json=True
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
return_files = []
|
2019-11-18 17:39:57 +00:00
|
|
|
user_files = list(
|
|
|
|
filter(lambda f: f.value["owner"] == data["name"], files)
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class CreateHost(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.CreateHostSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
2019-12-08 12:29:24 +00:00
|
|
|
host_key = join_path(config['etcd']["HOST_PREFIX"], uuid4().hex)
|
2019-10-25 06:42:40 +00:00
|
|
|
host_entry = {
|
|
|
|
"specs": data["specs"],
|
|
|
|
"hostname": data["hostname"],
|
|
|
|
"status": "DEAD",
|
|
|
|
"last_heartbeat": "",
|
|
|
|
}
|
|
|
|
etcd_client.put(host_key, host_entry, value_in_json=True)
|
|
|
|
|
|
|
|
return {"message": "Host Created"}, 200
|
|
|
|
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListHost(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
2019-11-18 17:39:57 +00:00
|
|
|
hosts = host_pool.hosts
|
2019-10-25 06:42:40 +00:00
|
|
|
r = {
|
|
|
|
host.key: {
|
|
|
|
"status": host.status,
|
|
|
|
"specs": host.specs,
|
|
|
|
"hostname": host.hostname,
|
|
|
|
}
|
|
|
|
for host in hosts
|
|
|
|
}
|
|
|
|
return r, 200
|
|
|
|
|
|
|
|
|
|
|
|
class GetSSHKeys(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.GetSSHSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
|
|
|
if not validator.key_name.value:
|
|
|
|
|
|
|
|
# {user_prefix}/{realm}/{name}/key/
|
2019-11-25 06:52:36 +00:00
|
|
|
etcd_key = join_path(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['etcd']['USER_PREFIX'],
|
2019-11-18 17:39:57 +00:00
|
|
|
data["realm"],
|
|
|
|
data["name"],
|
|
|
|
"key",
|
|
|
|
)
|
|
|
|
etcd_entry = etcd_client.get_prefix(
|
|
|
|
etcd_key, value_in_json=True
|
|
|
|
)
|
|
|
|
|
|
|
|
keys = {
|
|
|
|
key.key.split("/")[-1]: key.value for key in etcd_entry
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
return {"keys": keys}
|
|
|
|
else:
|
|
|
|
|
|
|
|
# {user_prefix}/{realm}/{name}/key/{key_name}
|
2019-11-25 06:52:36 +00:00
|
|
|
etcd_key = join_path(
|
2019-12-08 13:15:36 +00:00
|
|
|
config['etcd']['USER_PREFIX'],
|
2019-11-18 17:39:57 +00:00
|
|
|
data["realm"],
|
|
|
|
data["name"],
|
|
|
|
"key",
|
|
|
|
data["key_name"],
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
if etcd_entry:
|
2019-11-18 17:39:57 +00:00
|
|
|
return {
|
|
|
|
"keys": {
|
|
|
|
etcd_entry.key.split("/")[-1]: etcd_entry.value
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
return {"keys": {}}
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class AddSSHKey(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.AddSSHSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
# {user_prefix}/{realm}/{name}/key/{key_name}
|
2019-11-25 06:52:36 +00:00
|
|
|
etcd_key = join_path(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['etcd']["USER_PREFIX"],
|
2019-11-18 17:39:57 +00:00
|
|
|
data["realm"],
|
|
|
|
data["name"],
|
|
|
|
"key",
|
|
|
|
data["key_name"],
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
|
|
|
|
if etcd_entry:
|
2019-11-18 17:39:57 +00:00
|
|
|
return {
|
|
|
|
"message": "Key with name '{}' already exists".format(
|
|
|
|
data["key_name"]
|
|
|
|
)
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
# Key Not Found. It implies user' haven't added any key yet.
|
|
|
|
etcd_client.put(etcd_key, data["key"], value_in_json=True)
|
|
|
|
return {"message": "Key added successfully"}
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class RemoveSSHKey(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
2019-11-02 15:42:24 +00:00
|
|
|
validator = schemas.RemoveSSHSchema(data)
|
2019-10-25 06:42:40 +00:00
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
# {user_prefix}/{realm}/{name}/key/{key_name}
|
2019-11-25 06:52:36 +00:00
|
|
|
etcd_key = join_path(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['etcd']["USER_PREFIX"],
|
2019-11-18 17:39:57 +00:00
|
|
|
data["realm"],
|
|
|
|
data["name"],
|
|
|
|
"key",
|
|
|
|
data["key_name"],
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
|
|
|
|
if etcd_entry:
|
|
|
|
etcd_client.client.delete(etcd_key)
|
|
|
|
return {"message": "Key successfully removed."}
|
|
|
|
else:
|
2019-11-18 17:39:57 +00:00
|
|
|
return {
|
|
|
|
"message": "No Key with name '{}' Exists at all.".format(
|
|
|
|
data["key_name"]
|
|
|
|
)
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
2019-11-11 18:42:57 +00:00
|
|
|
|
|
|
|
class CreateNetwork(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def post():
|
|
|
|
data = request.json
|
|
|
|
validator = schemas.CreateNetwork(data)
|
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-11-11 18:42:57 +00:00
|
|
|
network_entry = {
|
2019-11-18 17:39:57 +00:00
|
|
|
"id": counters.increment_etcd_counter(
|
|
|
|
etcd_client, "/v1/counter/vxlan"
|
|
|
|
),
|
2019-11-15 16:11:45 +00:00
|
|
|
"type": data["type"],
|
2019-11-11 18:42:57 +00:00
|
|
|
}
|
2019-11-15 16:11:45 +00:00
|
|
|
if validator.user.value:
|
2019-11-18 17:39:57 +00:00
|
|
|
nb = pynetbox.api(
|
2019-12-08 12:29:24 +00:00
|
|
|
url=config['netbox']["NETBOX_URL"],
|
|
|
|
token=config['netbox']["NETBOX_TOKEN"],
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
|
|
|
nb_prefix = nb.ipam.prefixes.get(
|
2019-12-08 12:29:24 +00:00
|
|
|
prefix=config['network']["PREFIX"]
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-11-15 16:11:45 +00:00
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
prefix = nb_prefix.available_prefixes.create(
|
|
|
|
data={
|
2019-12-08 13:15:36 +00:00
|
|
|
"prefix_length": config['network']["PREFIX_LENGTH"],
|
2019-11-18 17:39:57 +00:00
|
|
|
"description": '{}\'s network "{}"'.format(
|
|
|
|
data["name"], data["network_name"]
|
|
|
|
),
|
|
|
|
"is_pool": True,
|
2019-11-15 16:11:45 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
network_entry["ipv6"] = prefix["prefix"]
|
|
|
|
else:
|
|
|
|
network_entry["ipv6"] = "fd00::/64"
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
network_key = join_path(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['network']["NETWORK_PREFIX"],
|
2019-11-18 17:39:57 +00:00
|
|
|
data["name"],
|
|
|
|
data["network_name"],
|
|
|
|
)
|
2019-11-11 18:42:57 +00:00
|
|
|
etcd_client.put(network_key, network_entry, value_in_json=True)
|
|
|
|
return {"message": "Network successfully added."}
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListUserNetwork(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
data = request.json
|
|
|
|
validator = schemas.OTPSchema(data)
|
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-25 06:52:36 +00:00
|
|
|
prefix = join_path(
|
2019-12-08 12:29:24 +00:00
|
|
|
config['network']["NETWORK_PREFIX"], data["name"]
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-11-11 18:42:57 +00:00
|
|
|
networks = etcd_client.get_prefix(prefix, value_in_json=True)
|
|
|
|
user_networks = []
|
|
|
|
for net in networks:
|
|
|
|
net.value["name"] = net.key.split("/")[-1]
|
|
|
|
user_networks.append(net.value)
|
|
|
|
return {"networks": user_networks}, 200
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
api.add_resource(CreateVM, "/vm/create")
|
|
|
|
api.add_resource(VmStatus, "/vm/status")
|
|
|
|
|
|
|
|
api.add_resource(VMAction, "/vm/action")
|
|
|
|
api.add_resource(VMMigration, "/vm/migrate")
|
|
|
|
|
|
|
|
api.add_resource(CreateImage, "/image/create")
|
|
|
|
api.add_resource(ListPublicImages, "/image/list-public")
|
|
|
|
|
|
|
|
api.add_resource(ListUserVM, "/user/vms")
|
|
|
|
api.add_resource(ListUserFiles, "/user/files")
|
2019-11-11 18:42:57 +00:00
|
|
|
api.add_resource(ListUserNetwork, "/user/networks")
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
api.add_resource(AddSSHKey, "/user/add-ssh")
|
|
|
|
api.add_resource(RemoveSSHKey, "/user/remove-ssh")
|
|
|
|
api.add_resource(GetSSHKeys, "/user/get-ssh")
|
|
|
|
|
|
|
|
api.add_resource(CreateHost, "/host/create")
|
|
|
|
api.add_resource(ListHost, "/host/list")
|
|
|
|
|
2019-11-11 18:42:57 +00:00
|
|
|
api.add_resource(CreateNetwork, "/network/create")
|
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
def main():
|
2019-12-08 12:29:24 +00:00
|
|
|
image_stores = list(etcd_client.get_prefix(config['etcd']['IMAGE_STORE_PREFIX'], value_in_json=True))
|
2019-11-25 06:52:36 +00:00
|
|
|
if len(image_stores) == 0:
|
|
|
|
data = {
|
|
|
|
"is_public": True,
|
|
|
|
"type": "ceph",
|
|
|
|
"name": "images",
|
|
|
|
"description": "first ever public image-store",
|
|
|
|
"attributes": {"list": [], "key": [], "pool": "images"},
|
|
|
|
}
|
|
|
|
|
2019-12-08 12:29:24 +00:00
|
|
|
etcd_client.put(join_path(config['etcd']['IMAGE_STORE_PREFIX'], uuid4().hex), json.dumps(data))
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
app.run(host="::", debug=True)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|