2019-10-25 06:42:40 +00:00
|
|
|
import json
|
2019-11-25 06:52:36 +00:00
|
|
|
import pynetbox
|
2019-12-23 07:58:04 +00:00
|
|
|
import logging
|
2020-01-03 13:38:59 +00:00
|
|
|
import argparse
|
2019-11-25 06:52:36 +00:00
|
|
|
|
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-23 07:58:04 +00:00
|
|
|
from werkzeug.exceptions import HTTPException
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-08 19:40:05 +00:00
|
|
|
from uncloud.common.shared import shared
|
|
|
|
|
2019-12-31 10:30:02 +00:00
|
|
|
from uncloud.common import counters
|
|
|
|
from uncloud.common.vm import VMStatus
|
|
|
|
from uncloud.common.request import RequestEntry, RequestType
|
2020-01-06 07:25:59 +00:00
|
|
|
from uncloud.common.settings import settings
|
2019-11-18 17:39:57 +00:00
|
|
|
from . import schemas
|
|
|
|
from .helper import generate_mac, mac2ipv6
|
2019-12-31 12:13:19 +00:00
|
|
|
from uncloud import UncloudException
|
2019-12-23 07:58:04 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
2019-12-23 07:58:04 +00:00
|
|
|
app.logger.handlers.clear()
|
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
arg_parser = argparse.ArgumentParser('api', add_help=False)
|
|
|
|
arg_parser.add_argument('--port', '-p')
|
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
|
|
|
|
@app.errorhandler(Exception)
|
|
|
|
def handle_exception(e):
|
|
|
|
app.logger.error(e)
|
|
|
|
# pass through HTTP errors
|
|
|
|
if isinstance(e, HTTPException):
|
|
|
|
return e
|
|
|
|
|
|
|
|
# now you're handling non-HTTP exceptions only
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'Server Error'}, 500
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CreateVM(Resource):
|
2020-01-05 14:46:38 +00:00
|
|
|
"""API Request to Handle Creation of VM"""
|
2019-11-25 06:52:36 +00:00
|
|
|
|
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
|
2020-01-03 13:38:59 +00:00
|
|
|
vm_key = join_path(settings['etcd']['vm_prefix'], vm_uuid)
|
2019-10-25 06:42:40 +00:00
|
|
|
specs = {
|
2020-01-03 13:38:59 +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
|
|
|
}
|
2020-01-03 13:38:59 +00:00
|
|
|
macs = [generate_mac() for _ in range(len(data['network']))]
|
2019-12-30 09:35:07 +00:00
|
|
|
tap_ids = [
|
|
|
|
counters.increment_etcd_counter(
|
2020-01-07 17:18:13 +00:00
|
|
|
shared.etcd_client, settings['etcd']['tap_counter']
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
for _ in range(len(data['network']))
|
2019-12-30 09:35:07 +00:00
|
|
|
]
|
2019-10-25 06:42:40 +00:00
|
|
|
vm_entry = {
|
2020-01-03 13:38:59 +00:00
|
|
|
'name': data['vm_name'],
|
|
|
|
'owner': data['name'],
|
|
|
|
'owner_realm': data['realm'],
|
|
|
|
'specs': specs,
|
|
|
|
'hostname': '',
|
|
|
|
'status': VMStatus.stopped,
|
|
|
|
'image_uuid': validator.image_uuid,
|
|
|
|
'log': [],
|
|
|
|
'vnc_socket': '',
|
|
|
|
'network': list(zip(data['network'], macs, tap_ids)),
|
|
|
|
'metadata': {'ssh-keys': []},
|
|
|
|
'in_migration': False,
|
2019-10-25 06:42:40 +00:00
|
|
|
}
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.etcd_client.put(vm_key, vm_entry, value_in_json=True)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
# Create ScheduleVM Request
|
2019-11-18 17:39:57 +00:00
|
|
|
r = RequestEntry.from_scratch(
|
2019-12-30 09:35:07 +00:00
|
|
|
type=RequestType.ScheduleVM,
|
|
|
|
uuid=vm_uuid,
|
2020-01-03 13:38:59 +00:00
|
|
|
request_prefix=settings['etcd']['request_prefix'],
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.request_pool.put(r)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'VM Creation Queued'}, 200
|
2019-10-25 06:42:40 +00:00
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class VmStatus(Resource):
|
|
|
|
@staticmethod
|
2019-12-31 15:33:55 +00:00
|
|
|
def post():
|
2019-10-25 06:42:40 +00:00
|
|
|
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-12-22 07:26:48 +00:00
|
|
|
vm = shared.vm_pool.get(
|
2020-01-03 13:38:59 +00:00
|
|
|
join_path(settings['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()
|
2020-01-03 13:38:59 +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-12-22 07:26:48 +00:00
|
|
|
network = shared.etcd_client.get(
|
2019-11-25 06:52:36 +00:00
|
|
|
join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['network_prefix'],
|
|
|
|
data['name'],
|
2019-11-18 17:39:57 +00:00
|
|
|
network_name,
|
|
|
|
),
|
|
|
|
value_in_json=True,
|
|
|
|
)
|
2019-12-30 09:35:07 +00:00
|
|
|
ipv6_addr = (
|
2020-01-03 13:38:59 +00:00
|
|
|
network.value.get('ipv6').split('::')[0] + '::'
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
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-12-22 07:26:48 +00:00
|
|
|
file_entry = shared.etcd_client.get(
|
2020-01-03 13:38:59 +00:00
|
|
|
join_path(settings['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 = {
|
2020-01-03 13:38:59 +00:00
|
|
|
'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-10-25 06:42:40 +00:00
|
|
|
}
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.etcd_client.put(
|
2019-12-30 09:35:07 +00:00
|
|
|
join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['image_prefix'], data['uuid']
|
2019-12-30 09:35:07 +00:00
|
|
|
),
|
2019-11-18 17:39:57 +00:00
|
|
|
json.dumps(image_entry_json),
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
|
|
|
|
2020-01-03 13:38:59 +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-12-22 07:26:48 +00:00
|
|
|
images = shared.etcd_client.get_prefix(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['image_prefix'], value_in_json=True
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
r = {'images': []}
|
2019-10-25 06:42:40 +00:00
|
|
|
for image in images:
|
2020-01-03 13:38:59 +00:00
|
|
|
image_key = '{}:{}'.format(
|
|
|
|
image.value['store_name'], image.value['name']
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
r['images'].append(
|
|
|
|
{'name': image_key, 'status': image.value['status']}
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
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-12-22 07:26:48 +00:00
|
|
|
vm_entry = shared.vm_pool.get(
|
2020-01-03 13:38:59 +00:00
|
|
|
join_path(settings['etcd']['vm_prefix'], data['uuid'])
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
action = data['action']
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
if action == 'start':
|
|
|
|
action = 'schedule'
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
if action == 'delete' and vm_entry.hostname == '':
|
2019-12-30 09:35:07 +00:00
|
|
|
if shared.storage_handler.is_vm_image_exists(
|
|
|
|
vm_entry.uuid
|
|
|
|
):
|
|
|
|
r_status = shared.storage_handler.delete_vm_image(
|
|
|
|
vm_entry.uuid
|
|
|
|
)
|
2019-11-25 06:52:36 +00:00
|
|
|
if r_status:
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.etcd_client.client.delete(vm_entry.key)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'VM successfully deleted'}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-12-30 09:35:07 +00:00
|
|
|
logger.error(
|
2020-01-03 13:38:59 +00:00
|
|
|
'Some Error Occurred while deleting VM'
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'VM deletion unsuccessfull'}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.etcd_client.client.delete(vm_entry.key)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'VM successfully deleted'}
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
r = RequestEntry.from_scratch(
|
2020-01-03 13:38:59 +00:00
|
|
|
type='{}VM'.format(action.title()),
|
|
|
|
uuid=data['uuid'],
|
2019-10-25 06:42:40 +00:00
|
|
|
hostname=vm_entry.hostname,
|
2020-01-03 13:38:59 +00:00
|
|
|
request_prefix=settings['etcd']['request_prefix'],
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.request_pool.put(r)
|
2019-12-30 09:35:07 +00:00
|
|
|
return (
|
2020-01-03 13:38:59 +00:00
|
|
|
{'message': 'VM {} Queued'.format(action.title())},
|
2019-12-30 09:35:07 +00:00
|
|
|
200,
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
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():
|
2020-01-03 13:38:59 +00:00
|
|
|
vm = shared.vm_pool.get(data['uuid'])
|
2019-12-30 09:35:07 +00:00
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type=RequestType.InitVMMigration,
|
|
|
|
uuid=vm.uuid,
|
|
|
|
hostname=join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['host_prefix'],
|
2019-12-30 09:35:07 +00:00
|
|
|
validator.destination.value,
|
|
|
|
),
|
2020-01-03 13:38:59 +00:00
|
|
|
request_prefix=settings['etcd']['request_prefix'],
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.request_pool.put(r)
|
2019-12-30 09:35:07 +00:00
|
|
|
return (
|
2020-01-03 13:38:59 +00:00
|
|
|
{'message': 'VM Migration Initialization Queued'},
|
2019-12-30 09:35:07 +00:00
|
|
|
200,
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListUserVM(Resource):
|
|
|
|
@staticmethod
|
2019-12-31 15:33:55 +00:00
|
|
|
def post():
|
2019-10-25 06:42:40 +00:00
|
|
|
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-12-22 07:26:48 +00:00
|
|
|
vms = shared.etcd_client.get_prefix(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['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 = []
|
2019-12-30 09:35:07 +00:00
|
|
|
user_vms = filter(
|
2020-01-03 13:38:59 +00:00
|
|
|
lambda v: v.value['owner'] == data['name'], vms
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
for vm in user_vms:
|
|
|
|
return_vms.append(
|
|
|
|
{
|
2020-01-03 13:38:59 +00:00
|
|
|
'name': vm.value['name'],
|
|
|
|
'vm_uuid': vm.key.split('/')[-1],
|
|
|
|
'specs': vm.value['specs'],
|
|
|
|
'status': vm.value['status'],
|
|
|
|
'hostname': vm.value['hostname'],
|
|
|
|
'vnc_socket': vm.value.get('vnc_socket', None),
|
2019-10-25 06:42:40 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if return_vms:
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': return_vms}, 200
|
|
|
|
return {'message': 'No VM found'}, 404
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListUserFiles(Resource):
|
|
|
|
@staticmethod
|
2019-12-31 15:33:55 +00:00
|
|
|
def post():
|
2019-10-25 06:42:40 +00:00
|
|
|
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-12-22 07:26:48 +00:00
|
|
|
files = shared.etcd_client.get_prefix(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['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 = []
|
2020-01-07 13:27:22 +00:00
|
|
|
user_files = [f for f in files if f.value['owner'] == data['name']]
|
2019-10-25 06:42:40 +00:00
|
|
|
for file in user_files:
|
2020-01-07 13:27:22 +00:00
|
|
|
file_uuid = file.key.split('/')[-1]
|
|
|
|
file = file.value
|
|
|
|
file['uuid'] = file_uuid
|
|
|
|
|
|
|
|
file.pop('sha512sum', None)
|
|
|
|
file.pop('owner', None)
|
|
|
|
|
|
|
|
return_files.append(file)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': return_files}, 200
|
2019-10-25 06:42:40 +00:00
|
|
|
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-30 09:35:07 +00:00
|
|
|
host_key = join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['host_prefix'], uuid4().hex
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
host_entry = {
|
2020-01-03 13:38:59 +00:00
|
|
|
'specs': data['specs'],
|
|
|
|
'hostname': data['hostname'],
|
|
|
|
'status': 'DEAD',
|
|
|
|
'last_heartbeat': '',
|
2019-10-25 06:42:40 +00:00
|
|
|
}
|
2019-12-30 09:35:07 +00:00
|
|
|
shared.etcd_client.put(
|
|
|
|
host_key, host_entry, value_in_json=True
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'Host Created'}, 200
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListHost(Resource):
|
|
|
|
@staticmethod
|
|
|
|
def get():
|
2019-12-22 07:26:48 +00:00
|
|
|
hosts = shared.host_pool.hosts
|
2019-10-25 06:42:40 +00:00
|
|
|
r = {
|
|
|
|
host.key: {
|
2020-01-03 13:38:59 +00:00
|
|
|
'status': host.status,
|
|
|
|
'specs': host.specs,
|
|
|
|
'hostname': host.hostname,
|
2019-10-25 06:42:40 +00:00
|
|
|
}
|
|
|
|
for host in hosts
|
|
|
|
}
|
|
|
|
return r, 200
|
|
|
|
|
|
|
|
|
|
|
|
class GetSSHKeys(Resource):
|
|
|
|
@staticmethod
|
2019-12-31 15:33:55 +00:00
|
|
|
def post():
|
2019-10-25 06:42:40 +00:00
|
|
|
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(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['user_prefix'],
|
|
|
|
data['realm'],
|
|
|
|
data['name'],
|
|
|
|
'key',
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-12-22 07:26:48 +00:00
|
|
|
etcd_entry = shared.etcd_client.get_prefix(
|
2019-11-18 17:39:57 +00:00
|
|
|
etcd_key, value_in_json=True
|
|
|
|
)
|
|
|
|
|
|
|
|
keys = {
|
2020-01-03 13:38:59 +00:00
|
|
|
key.key.split('/')[-1]: key.value
|
2019-12-30 09:35:07 +00:00
|
|
|
for key in etcd_entry
|
2019-11-18 17:39:57 +00:00
|
|
|
}
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'keys': keys}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
|
|
|
|
# {user_prefix}/{realm}/{name}/key/{key_name}
|
2019-11-25 06:52:36 +00:00
|
|
|
etcd_key = join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['user_prefix'],
|
|
|
|
data['realm'],
|
|
|
|
data['name'],
|
|
|
|
'key',
|
|
|
|
data['key_name'],
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-12-30 09:35:07 +00:00
|
|
|
etcd_entry = shared.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 {
|
2020-01-03 13:38:59 +00:00
|
|
|
'keys': {
|
|
|
|
etcd_entry.key.split('/')[
|
2019-12-30 09:35:07 +00:00
|
|
|
-1
|
|
|
|
]: etcd_entry.value
|
2019-11-18 17:39:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'keys': {}}
|
2019-10-25 06:42:40 +00:00
|
|
|
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(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['user_prefix'],
|
|
|
|
data['realm'],
|
|
|
|
data['name'],
|
|
|
|
'key',
|
|
|
|
data['key_name'],
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-12-30 09:35:07 +00:00
|
|
|
etcd_entry = shared.etcd_client.get(
|
|
|
|
etcd_key, value_in_json=True
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
if etcd_entry:
|
2019-11-18 17:39:57 +00:00
|
|
|
return {
|
2020-01-03 13:38:59 +00:00
|
|
|
'message': 'Key with name "{}" already exists'.format(
|
|
|
|
data['key_name']
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
|
|
|
}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
# Key Not Found. It implies user' haven't added any key yet.
|
2019-12-30 09:35:07 +00:00
|
|
|
shared.etcd_client.put(
|
2020-01-03 13:38:59 +00:00
|
|
|
etcd_key, data['key'], value_in_json=True
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'Key added successfully'}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class RemoveSSHKey(Resource):
|
|
|
|
@staticmethod
|
2019-12-31 15:33:55 +00:00
|
|
|
def post():
|
2019-10-25 06:42:40 +00:00
|
|
|
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(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['user_prefix'],
|
|
|
|
data['realm'],
|
|
|
|
data['name'],
|
|
|
|
'key',
|
|
|
|
data['key_name'],
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-12-30 09:35:07 +00:00
|
|
|
etcd_entry = shared.etcd_client.get(
|
|
|
|
etcd_key, value_in_json=True
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
if etcd_entry:
|
2019-12-22 07:26:48 +00:00
|
|
|
shared.etcd_client.client.delete(etcd_key)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'Key successfully removed.'}
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-11-18 17:39:57 +00:00
|
|
|
return {
|
2020-01-03 13:38:59 +00:00
|
|
|
'message': 'No Key with name "{}" Exists at all.'.format(
|
|
|
|
data['key_name']
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
|
|
|
}
|
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 = {
|
2020-01-03 13:38:59 +00:00
|
|
|
'id': counters.increment_etcd_counter(
|
2020-01-07 17:18:13 +00:00
|
|
|
shared.etcd_client, settings['etcd']['vxlan_counter']
|
2019-11-18 17:39:57 +00:00
|
|
|
),
|
2020-01-03 13:38:59 +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-12-14 15:23:31 +00:00
|
|
|
try:
|
|
|
|
nb = pynetbox.api(
|
2020-01-03 13:38:59 +00:00
|
|
|
url=settings['netbox']['url'],
|
|
|
|
token=settings['netbox']['token'],
|
2019-12-14 15:23:31 +00:00
|
|
|
)
|
|
|
|
nb_prefix = nb.ipam.prefixes.get(
|
2020-01-03 13:38:59 +00:00
|
|
|
prefix=settings['network']['prefix']
|
2019-12-14 15:23:31 +00:00
|
|
|
)
|
|
|
|
prefix = nb_prefix.available_prefixes.create(
|
|
|
|
data={
|
2020-01-03 13:38:59 +00:00
|
|
|
'prefix_length': int(
|
|
|
|
settings['network']['prefix_length']
|
2019-12-30 09:35:07 +00:00
|
|
|
),
|
2020-01-03 13:38:59 +00:00
|
|
|
'description': '{}\'s network "{}"'.format(
|
|
|
|
data['name'], data['network_name']
|
2019-12-14 15:23:31 +00:00
|
|
|
),
|
2020-01-03 13:38:59 +00:00
|
|
|
'is_pool': True,
|
2019-12-14 15:23:31 +00:00
|
|
|
}
|
|
|
|
)
|
2019-12-23 07:58:04 +00:00
|
|
|
except Exception as err:
|
|
|
|
app.logger.error(err)
|
2019-12-30 09:35:07 +00:00
|
|
|
return {
|
2020-01-03 13:38:59 +00:00
|
|
|
'message': 'Error occured while creating network.'
|
2019-12-30 09:35:07 +00:00
|
|
|
}
|
2019-12-14 15:23:31 +00:00
|
|
|
else:
|
2020-01-03 13:38:59 +00:00
|
|
|
network_entry['ipv6'] = prefix['prefix']
|
2019-11-15 16:11:45 +00:00
|
|
|
else:
|
2020-01-03 13:38:59 +00:00
|
|
|
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(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['network_prefix'],
|
|
|
|
data['name'],
|
|
|
|
data['network_name'],
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
|
|
|
shared.etcd_client.put(
|
|
|
|
network_key, network_entry, value_in_json=True
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'message': 'Network successfully added.'}
|
2019-11-11 18:42:57 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
|
|
|
class ListUserNetwork(Resource):
|
|
|
|
@staticmethod
|
2019-12-31 15:33:55 +00:00
|
|
|
def post():
|
2019-11-11 18:42:57 +00:00
|
|
|
data = request.json
|
|
|
|
validator = schemas.OTPSchema(data)
|
|
|
|
|
|
|
|
if validator.is_valid():
|
2019-11-25 06:52:36 +00:00
|
|
|
prefix = join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['network_prefix'], data['name']
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
|
|
|
networks = shared.etcd_client.get_prefix(
|
|
|
|
prefix, value_in_json=True
|
2019-11-18 17:39:57 +00:00
|
|
|
)
|
2019-11-11 18:42:57 +00:00
|
|
|
user_networks = []
|
|
|
|
for net in networks:
|
2020-01-03 13:38:59 +00:00
|
|
|
net.value['name'] = net.key.split('/')[-1]
|
2019-11-11 18:42:57 +00:00
|
|
|
user_networks.append(net.value)
|
2020-01-03 13:38:59 +00:00
|
|
|
return {'networks': user_networks}, 200
|
2019-11-11 18:42:57 +00:00
|
|
|
else:
|
|
|
|
return validator.get_errors(), 400
|
|
|
|
|
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(CreateVM, '/vm/create')
|
|
|
|
api.add_resource(VmStatus, '/vm/status')
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(VMAction, '/vm/action')
|
|
|
|
api.add_resource(VMMigration, '/vm/migrate')
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(CreateImage, '/image/create')
|
|
|
|
api.add_resource(ListPublicImages, '/image/list-public')
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(ListUserVM, '/user/vms')
|
|
|
|
api.add_resource(ListUserFiles, '/user/files')
|
|
|
|
api.add_resource(ListUserNetwork, '/user/networks')
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(AddSSHKey, '/user/add-ssh')
|
|
|
|
api.add_resource(RemoveSSHKey, '/user/remove-ssh')
|
|
|
|
api.add_resource(GetSSHKeys, '/user/get-ssh')
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(CreateHost, '/host/create')
|
|
|
|
api.add_resource(ListHost, '/host/list')
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
api.add_resource(CreateNetwork, '/network/create')
|
2019-11-11 18:42:57 +00:00
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-12-31 12:13:19 +00:00
|
|
|
def main(debug=False, port=None):
|
|
|
|
try:
|
|
|
|
image_stores = list(
|
|
|
|
shared.etcd_client.get_prefix(
|
2020-01-03 13:38:59 +00:00
|
|
|
settings['etcd']['image_store_prefix'], value_in_json=True
|
2019-12-31 12:13:19 +00:00
|
|
|
)
|
2019-12-30 09:35:07 +00:00
|
|
|
)
|
2019-12-31 12:13:19 +00:00
|
|
|
except KeyError:
|
|
|
|
image_stores = False
|
|
|
|
|
|
|
|
# Do not inject default values that might be very wrong
|
|
|
|
# fail when required, not before
|
|
|
|
#
|
|
|
|
# if not image_stores:
|
|
|
|
# data = {
|
2020-01-03 13:38:59 +00:00
|
|
|
# 'is_public': True,
|
|
|
|
# 'type': 'ceph',
|
|
|
|
# 'name': 'images',
|
|
|
|
# 'description': 'first ever public image-store',
|
|
|
|
# 'attributes': {'list': [], 'key': [], 'pool': 'images'},
|
2019-12-31 12:13:19 +00:00
|
|
|
# }
|
|
|
|
|
|
|
|
# shared.etcd_client.put(
|
|
|
|
# join_path(
|
2020-01-03 13:38:59 +00:00
|
|
|
# settings['etcd']['image_store_prefix'], uuid4().hex
|
2019-12-31 12:13:19 +00:00
|
|
|
# ),
|
|
|
|
# json.dumps(data),
|
|
|
|
# )
|
|
|
|
|
|
|
|
try:
|
2020-01-03 13:38:59 +00:00
|
|
|
app.run(host='::',
|
2019-12-31 13:06:51 +00:00
|
|
|
port=port,
|
|
|
|
debug=debug)
|
2019-12-31 12:13:19 +00:00
|
|
|
except OSError as e:
|
2020-01-03 13:38:59 +00:00
|
|
|
raise UncloudException('Failed to start Flask: {}'.format(e))
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
if __name__ == '__main__':
|
2019-11-18 17:39:57 +00:00
|
|
|
main()
|