Refactoring, Removal of most global vars, config default path is ~/ucloud/
This commit is contained in:
parent
bc58a6ed9c
commit
04993e4106
23 changed files with 673 additions and 726 deletions
|
|
@ -10,10 +10,9 @@ from flask_restful import Resource, Api
|
|||
from ucloud.common import counters
|
||||
from ucloud.common.vm import VMStatus
|
||||
from ucloud.common.request import RequestEntry, RequestType
|
||||
from ucloud.config import (
|
||||
etcd_client, request_pool, vm_pool,
|
||||
host_pool, config, image_storage_handler
|
||||
)
|
||||
from ucloud.settings import settings
|
||||
from ucloud.shared import shared
|
||||
|
||||
from . import schemas
|
||||
from .helper import generate_mac, mac2ipv6
|
||||
from . import logger
|
||||
|
|
@ -31,7 +30,7 @@ class CreateVM(Resource):
|
|||
validator = schemas.CreateVMSchema(data)
|
||||
if validator.is_valid():
|
||||
vm_uuid = uuid4().hex
|
||||
vm_key = join_path(config['etcd']['vm_prefix'], vm_uuid)
|
||||
vm_key = join_path(settings['etcd']['vm_prefix'], vm_uuid)
|
||||
specs = {
|
||||
"cpu": validator.specs["cpu"],
|
||||
"ram": validator.specs["ram"],
|
||||
|
|
@ -39,7 +38,7 @@ class CreateVM(Resource):
|
|||
"hdd": validator.specs["hdd"],
|
||||
}
|
||||
macs = [generate_mac() for _ in range(len(data["network"]))]
|
||||
tap_ids = [counters.increment_etcd_counter(etcd_client, "/v1/counter/tap")
|
||||
tap_ids = [counters.increment_etcd_counter(shared.etcd_client, "/v1/counter/tap")
|
||||
for _ in range(len(data["network"]))]
|
||||
vm_entry = {
|
||||
"name": data["vm_name"],
|
||||
|
|
@ -54,14 +53,14 @@ class CreateVM(Resource):
|
|||
"network": list(zip(data["network"], macs, tap_ids)),
|
||||
"metadata": {"ssh-keys": []},
|
||||
}
|
||||
etcd_client.put(vm_key, vm_entry, value_in_json=True)
|
||||
shared.etcd_client.put(vm_key, vm_entry, value_in_json=True)
|
||||
|
||||
# Create ScheduleVM Request
|
||||
r = RequestEntry.from_scratch(
|
||||
type=RequestType.ScheduleVM, uuid=vm_uuid,
|
||||
request_prefix=config['etcd']['request_prefix']
|
||||
request_prefix=settings['etcd']['request_prefix']
|
||||
)
|
||||
request_pool.put(r)
|
||||
shared.request_pool.put(r)
|
||||
|
||||
return {"message": "VM Creation Queued"}, 200
|
||||
return validator.get_errors(), 400
|
||||
|
|
@ -73,16 +72,16 @@ class VmStatus(Resource):
|
|||
data = request.json
|
||||
validator = schemas.VMStatusSchema(data)
|
||||
if validator.is_valid():
|
||||
vm = vm_pool.get(
|
||||
join_path(config['etcd']['vm_prefix'], data["uuid"])
|
||||
vm = shared.vm_pool.get(
|
||||
join_path(settings['etcd']['vm_prefix'], data["uuid"])
|
||||
)
|
||||
vm_value = vm.value.copy()
|
||||
vm_value["ip"] = []
|
||||
for network_mac_and_tap in vm.network:
|
||||
network_name, mac, tap = network_mac_and_tap
|
||||
network = etcd_client.get(
|
||||
network = shared.etcd_client.get(
|
||||
join_path(
|
||||
config['etcd']['network_prefix'],
|
||||
settings['etcd']['network_prefix'],
|
||||
data["name"],
|
||||
network_name,
|
||||
),
|
||||
|
|
@ -102,8 +101,8 @@ class CreateImage(Resource):
|
|||
data = request.json
|
||||
validator = schemas.CreateImageSchema(data)
|
||||
if validator.is_valid():
|
||||
file_entry = etcd_client.get(
|
||||
join_path(config['etcd']['file_prefix'], data["uuid"])
|
||||
file_entry = shared.etcd_client.get(
|
||||
join_path(settings['etcd']['file_prefix'], data["uuid"])
|
||||
)
|
||||
file_entry_value = json.loads(file_entry.value)
|
||||
|
||||
|
|
@ -115,8 +114,8 @@ class CreateImage(Resource):
|
|||
"store_name": data["image_store"],
|
||||
"visibility": "public",
|
||||
}
|
||||
etcd_client.put(
|
||||
join_path(config['etcd']['image_prefix'], data["uuid"]),
|
||||
shared.etcd_client.put(
|
||||
join_path(settings['etcd']['image_prefix'], data["uuid"]),
|
||||
json.dumps(image_entry_json),
|
||||
)
|
||||
|
||||
|
|
@ -127,8 +126,8 @@ class CreateImage(Resource):
|
|||
class ListPublicImages(Resource):
|
||||
@staticmethod
|
||||
def get():
|
||||
images = etcd_client.get_prefix(
|
||||
config['etcd']['image_prefix'], value_in_json=True
|
||||
images = shared.etcd_client.get_prefix(
|
||||
settings['etcd']['image_prefix'], value_in_json=True
|
||||
)
|
||||
r = {
|
||||
"images": []
|
||||
|
|
@ -150,8 +149,8 @@ class VMAction(Resource):
|
|||
validator = schemas.VmActionSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
vm_entry = vm_pool.get(
|
||||
join_path(config['etcd']['vm_prefix'], data["uuid"])
|
||||
vm_entry = shared.vm_pool.get(
|
||||
join_path(settings['etcd']['vm_prefix'], data["uuid"])
|
||||
)
|
||||
action = data["action"]
|
||||
|
||||
|
|
@ -159,25 +158,25 @@ class VMAction(Resource):
|
|||
action = "schedule"
|
||||
|
||||
if action == "delete" and vm_entry.hostname == "":
|
||||
if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
|
||||
r_status = image_storage_handler.delete_vm_image(vm_entry.uuid)
|
||||
if shared.storage_handler.is_vm_image_exists(vm_entry.uuid):
|
||||
r_status = shared.storage_handler.delete_vm_image(vm_entry.uuid)
|
||||
if r_status:
|
||||
etcd_client.client.delete(vm_entry.key)
|
||||
shared.etcd_client.client.delete(vm_entry.key)
|
||||
return {"message": "VM successfully deleted"}
|
||||
else:
|
||||
logger.error("Some Error Occurred while deleting VM")
|
||||
return {"message": "VM deletion unsuccessfull"}
|
||||
else:
|
||||
etcd_client.client.delete(vm_entry.key)
|
||||
shared.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,
|
||||
request_prefix=config['etcd']['request_prefix']
|
||||
request_prefix=settings['etcd']['request_prefix']
|
||||
)
|
||||
request_pool.put(r)
|
||||
shared.request_pool.put(r)
|
||||
return {"message": "VM {} Queued".format(action.title())}, 200
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
|
@ -190,18 +189,18 @@ class VMMigration(Resource):
|
|||
validator = schemas.VmMigrationSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
vm = vm_pool.get(data["uuid"])
|
||||
vm = shared.vm_pool.get(data["uuid"])
|
||||
|
||||
r = RequestEntry.from_scratch(
|
||||
type=RequestType.ScheduleVM,
|
||||
uuid=vm.uuid,
|
||||
destination=join_path(
|
||||
config['etcd']['host_prefix'], validator.destination.value
|
||||
settings['etcd']['host_prefix'], validator.destination.value
|
||||
),
|
||||
migration=True,
|
||||
request_prefix=config['etcd']['request_prefix']
|
||||
request_prefix=settings['etcd']['request_prefix']
|
||||
)
|
||||
request_pool.put(r)
|
||||
shared.request_pool.put(r)
|
||||
return {"message": "VM Migration Initialization Queued"}, 200
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
|
@ -214,8 +213,8 @@ class ListUserVM(Resource):
|
|||
validator = schemas.OTPSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
vms = etcd_client.get_prefix(
|
||||
config['etcd']['vm_prefix'], value_in_json=True
|
||||
vms = shared.etcd_client.get_prefix(
|
||||
settings['etcd']['vm_prefix'], value_in_json=True
|
||||
)
|
||||
return_vms = []
|
||||
user_vms = filter(lambda v: v.value["owner"] == data["name"], vms)
|
||||
|
|
@ -227,7 +226,6 @@ class ListUserVM(Resource):
|
|||
"specs": vm.value["specs"],
|
||||
"status": vm.value["status"],
|
||||
"hostname": vm.value["hostname"],
|
||||
# "mac": vm.value["mac"],
|
||||
"vnc_socket": None
|
||||
if vm.value.get("vnc_socket", None) is None
|
||||
else vm.value["vnc_socket"],
|
||||
|
|
@ -248,8 +246,8 @@ class ListUserFiles(Resource):
|
|||
validator = schemas.OTPSchema(data)
|
||||
|
||||
if validator.is_valid():
|
||||
files = etcd_client.get_prefix(
|
||||
config['etcd']['file_prefix'], value_in_json=True
|
||||
files = shared.etcd_client.get_prefix(
|
||||
settings['etcd']['file_prefix'], value_in_json=True
|
||||
)
|
||||
return_files = []
|
||||
user_files = list(
|
||||
|
|
@ -273,14 +271,14 @@ class CreateHost(Resource):
|
|||
data = request.json
|
||||
validator = schemas.CreateHostSchema(data)
|
||||
if validator.is_valid():
|
||||
host_key = join_path(config['etcd']['host_prefix'], uuid4().hex)
|
||||
host_key = join_path(settings['etcd']['host_prefix'], uuid4().hex)
|
||||
host_entry = {
|
||||
"specs": data["specs"],
|
||||
"hostname": data["hostname"],
|
||||
"status": "DEAD",
|
||||
"last_heartbeat": "",
|
||||
}
|
||||
etcd_client.put(host_key, host_entry, value_in_json=True)
|
||||
shared.etcd_client.put(host_key, host_entry, value_in_json=True)
|
||||
|
||||
return {"message": "Host Created"}, 200
|
||||
|
||||
|
|
@ -290,7 +288,7 @@ class CreateHost(Resource):
|
|||
class ListHost(Resource):
|
||||
@staticmethod
|
||||
def get():
|
||||
hosts = host_pool.hosts
|
||||
hosts = shared.host_pool.hosts
|
||||
r = {
|
||||
host.key: {
|
||||
"status": host.status,
|
||||
|
|
@ -312,12 +310,12 @@ class GetSSHKeys(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/
|
||||
etcd_key = join_path(
|
||||
config['etcd']['user_prefix'],
|
||||
settings['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
)
|
||||
etcd_entry = etcd_client.get_prefix(
|
||||
etcd_entry = shared.etcd_client.get_prefix(
|
||||
etcd_key, value_in_json=True
|
||||
)
|
||||
|
||||
|
|
@ -329,13 +327,13 @@ class GetSSHKeys(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
config['etcd']['user_prefix'],
|
||||
settings['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
data["key_name"],
|
||||
)
|
||||
etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
|
||||
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
|
||||
|
||||
if etcd_entry:
|
||||
return {
|
||||
|
|
@ -358,13 +356,13 @@ class AddSSHKey(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
config['etcd']['user_prefix'],
|
||||
settings['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
data["key_name"],
|
||||
)
|
||||
etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
|
||||
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
|
||||
if etcd_entry:
|
||||
return {
|
||||
"message": "Key with name '{}' already exists".format(
|
||||
|
|
@ -373,7 +371,7 @@ class AddSSHKey(Resource):
|
|||
}
|
||||
else:
|
||||
# Key Not Found. It implies user' haven't added any key yet.
|
||||
etcd_client.put(etcd_key, data["key"], value_in_json=True)
|
||||
shared.etcd_client.put(etcd_key, data["key"], value_in_json=True)
|
||||
return {"message": "Key added successfully"}
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
|
@ -388,15 +386,15 @@ class RemoveSSHKey(Resource):
|
|||
|
||||
# {user_prefix}/{realm}/{name}/key/{key_name}
|
||||
etcd_key = join_path(
|
||||
config['etcd']['user_prefix'],
|
||||
settings['etcd']['user_prefix'],
|
||||
data["realm"],
|
||||
data["name"],
|
||||
"key",
|
||||
data["key_name"],
|
||||
)
|
||||
etcd_entry = etcd_client.get(etcd_key, value_in_json=True)
|
||||
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
|
||||
if etcd_entry:
|
||||
etcd_client.client.delete(etcd_key)
|
||||
shared.etcd_client.client.delete(etcd_key)
|
||||
return {"message": "Key successfully removed."}
|
||||
else:
|
||||
return {
|
||||
|
|
@ -418,22 +416,22 @@ class CreateNetwork(Resource):
|
|||
|
||||
network_entry = {
|
||||
"id": counters.increment_etcd_counter(
|
||||
etcd_client, "/v1/counter/vxlan"
|
||||
shared.etcd_client, "/v1/counter/vxlan"
|
||||
),
|
||||
"type": data["type"],
|
||||
}
|
||||
if validator.user.value:
|
||||
try:
|
||||
nb = pynetbox.api(
|
||||
url=config['netbox']['url'],
|
||||
token=config['netbox']['token'],
|
||||
url=settings['netbox']['url'],
|
||||
token=settings['netbox']['token'],
|
||||
)
|
||||
nb_prefix = nb.ipam.prefixes.get(
|
||||
prefix=config['network']['prefix']
|
||||
prefix=settings['network']['prefix']
|
||||
)
|
||||
prefix = nb_prefix.available_prefixes.create(
|
||||
data={
|
||||
"prefix_length": int(config['network']['prefix_length']),
|
||||
"prefix_length": int(settings['network']['prefix_length']),
|
||||
"description": '{}\'s network "{}"'.format(
|
||||
data["name"], data["network_name"]
|
||||
),
|
||||
|
|
@ -449,11 +447,11 @@ class CreateNetwork(Resource):
|
|||
network_entry["ipv6"] = "fd00::/64"
|
||||
|
||||
network_key = join_path(
|
||||
config['etcd']['network_prefix'],
|
||||
settings['etcd']['network_prefix'],
|
||||
data['name'],
|
||||
data['network_name'],
|
||||
)
|
||||
etcd_client.put(network_key, network_entry, value_in_json=True)
|
||||
shared.etcd_client.put(network_key, network_entry, value_in_json=True)
|
||||
return {"message": "Network successfully added."}
|
||||
else:
|
||||
return validator.get_errors(), 400
|
||||
|
|
@ -467,9 +465,9 @@ class ListUserNetwork(Resource):
|
|||
|
||||
if validator.is_valid():
|
||||
prefix = join_path(
|
||||
config['etcd']['network_prefix'], data["name"]
|
||||
settings['etcd']['network_prefix'], data["name"]
|
||||
)
|
||||
networks = etcd_client.get_prefix(prefix, value_in_json=True)
|
||||
networks = shared.etcd_client.get_prefix(prefix, value_in_json=True)
|
||||
user_networks = []
|
||||
for net in networks:
|
||||
net.value["name"] = net.key.split("/")[-1]
|
||||
|
|
@ -503,7 +501,7 @@ api.add_resource(CreateNetwork, "/network/create")
|
|||
|
||||
|
||||
def main():
|
||||
image_stores = list(etcd_client.get_prefix(config['etcd']['image_store_prefix'], value_in_json=True))
|
||||
image_stores = list(shared.etcd_client.get_prefix(settings['etcd']['image_store_prefix'], value_in_json=True))
|
||||
if len(image_stores) == 0:
|
||||
data = {
|
||||
"is_public": True,
|
||||
|
|
@ -513,7 +511,7 @@ def main():
|
|||
"attributes": {"list": [], "key": [], "pool": "images"},
|
||||
}
|
||||
|
||||
etcd_client.put(join_path(config['etcd']['image_store_prefix'], uuid4().hex), json.dumps(data))
|
||||
shared.etcd_client.put(join_path(settings['etcd']['image_store_prefix'], uuid4().hex), json.dumps(data))
|
||||
|
||||
app.run(host="::", debug=True)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue