2nd commit

This commit is contained in:
ahmadbilalkhalid 2020-01-10 00:33:35 +05:00
parent e5dd5e45c6
commit f1bb1ee3ca
2 changed files with 64 additions and 63 deletions

View file

@ -33,16 +33,16 @@ arg_parser = argparse.ArgumentParser('api', add_help=False)
arg_parser.add_argument('--port', '-p') arg_parser.add_argument('--port', '-p')
@app.errorhandler(Exception) # @app.errorhandler(Exception)
def handle_exception(e): # def handle_exception(e):
app.logger.error(e) # app.logger.error(e)
#
# pass through HTTP errors # # pass through HTTP errors
if isinstance(e, HTTPException): # if isinstance(e, HTTPException):
return e # return e
#
# now you're handling non-HTTP exceptions only # # now you're handling non-HTTP exceptions only
return {'message': 'Server Error'}, 500 # return {'message': 'Server Error'}, 500
class CreateVM(Resource): class CreateVM(Resource):
@ -63,22 +63,22 @@ class CreateVM(Resource):
'os-ssd': validator.specs['os-ssd'], 'os-ssd': validator.specs['os-ssd'],
'hdd': validator.specs['hdd'], 'hdd': validator.specs['hdd'],
} }
macs = [generate_mac() for _ in range(len(validator.network.value))] macs = [generate_mac() for _ in range(len(validator.network))]
tap_ids = [ tap_ids = [
counters.increment_etcd_counter(settings['etcd']['tap_counter']) counters.increment_etcd_counter(settings['etcd']['tap_counter'])
for _ in range(len(validator.network.value)) for _ in range(len(validator.network))
] ]
vm_entry = { vm_entry = {
'name': validator.vm_name.value, 'name': validator.vm_name,
'owner': validator.name.value, 'owner': validator.name,
'owner_realm': validator.realm.value, 'owner_realm': validator.realm,
'specs': specs, 'specs': specs,
'hostname': '', 'hostname': '',
'status': VMStatus.stopped, 'status': VMStatus.stopped,
'image_uuid': validator.image_uuid, 'image_uuid': validator.image_uuid,
'log': [], 'log': [],
'vnc_socket': '', 'vnc_socket': '',
'network': list(zip(validator.network.value, macs, tap_ids)), 'network': list(zip(validator.network, macs, tap_ids)),
'metadata': {'ssh-keys': []}, 'metadata': {'ssh-keys': []},
'in_migration': False, 'in_migration': False,
} }
@ -94,7 +94,7 @@ class CreateVM(Resource):
return make_return_message('VM Creation Queued') return make_return_message('VM Creation Queued')
class VmStatus(Resource): class GetVMStatus(Resource):
@staticmethod @staticmethod
def post(): def post():
data = request.json data = request.json
@ -104,13 +104,13 @@ class VmStatus(Resource):
except (ValidationException, Exception) as err: except (ValidationException, Exception) as err:
return make_return_message(err, 400) return make_return_message(err, 400)
else: else:
vm = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], data['uuid'])) vm = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], validator.uuid))
vm_value = vm.value.copy() vm_value = vm.value.copy()
vm_value['ip'] = [] vm_value['ip'] = []
for network_mac_and_tap in vm.network: for network_mac_and_tap in vm.network:
network_name, mac, tap = network_mac_and_tap network_name, mac, tap = network_mac_and_tap
network = shared.etcd_client.get( network = shared.etcd_client.get(
join_path(settings['etcd']['network_prefix'], data['name'], network_name), join_path(settings['etcd']['network_prefix'], validator.name, network_name),
value_in_json=True, value_in_json=True,
) )
ipv6_addr = (network.value.get('ipv6').split('::')[0] + '::') ipv6_addr = (network.value.get('ipv6').split('::')[0] + '::')
@ -131,7 +131,7 @@ class CreateImage(Resource):
else: else:
try: try:
file_entry = shared.etcd_client.get( file_entry = shared.etcd_client.get(
join_path(settings['etcd']['file_prefix'], data['uuid']), value_in_json=True join_path(settings['etcd']['file_prefix'], validator.uuid), value_in_json=True
) )
except KeyError: except KeyError:
# TODO: Add some message # TODO: Add some message
@ -146,7 +146,7 @@ class CreateImage(Resource):
'visibility': 'public', 'visibility': 'public',
} }
shared.etcd_client.put( shared.etcd_client.put(
join_path(settings['etcd']['image_prefix'], data['uuid']), join_path(settings['etcd']['image_prefix'], validator.uuid),
json.dumps(image_entry_json), json.dumps(image_entry_json),
) )
@ -174,8 +174,8 @@ class VMAction(Resource):
except ValidationException as err: except ValidationException as err:
return make_return_message(err, 400) return make_return_message(err, 400)
else: else:
vm_entry = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], data['uuid'])) vm_entry = shared.vm_pool.get(join_path(settings['etcd']['vm_prefix'], validator.uuid))
action = validator.action.value action = validator.action
if action == 'start': if action == 'start':
action = 'schedule' action = 'schedule'
@ -195,7 +195,7 @@ class VMAction(Resource):
r = RequestEntry.from_scratch( r = RequestEntry.from_scratch(
type='{}VM'.format(action.title()), type='{}VM'.format(action.title()),
uuid=data['uuid'], uuid=validator.uuid,
hostname=vm_entry.hostname, hostname=vm_entry.hostname,
request_prefix=settings['etcd']['request_prefix'], request_prefix=settings['etcd']['request_prefix'],
) )
@ -213,13 +213,13 @@ class VMMigration(Resource):
except ValidationException as err: except ValidationException as err:
return make_return_message(err), 400 return make_return_message(err), 400
else: else:
vm = shared.vm_pool.get(validator.uuid.value) vm = shared.vm_pool.get(validator.uuid)
r = RequestEntry.from_scratch( r = RequestEntry.from_scratch(
type=RequestType.InitVMMigration, type=RequestType.InitVMMigration,
uuid=vm.uuid, uuid=vm.uuid,
hostname=join_path( hostname=join_path(
settings['etcd']['host_prefix'], settings['etcd']['host_prefix'],
validator.destination.value, validator.destination,
), ),
request_prefix=settings['etcd']['request_prefix'], request_prefix=settings['etcd']['request_prefix'],
) )
@ -240,7 +240,7 @@ class ListUserVM(Resource):
else: else:
vms = shared.etcd_client.get_prefix(settings['etcd']['vm_prefix'], value_in_json=True) vms = shared.etcd_client.get_prefix(settings['etcd']['vm_prefix'], value_in_json=True)
return_vms = [] return_vms = []
user_vms = filter(lambda v: v.value['owner'] == validator.name.value, vms) user_vms = filter(lambda v: v.value['owner'] == validator.name, vms)
for vm in user_vms: for vm in user_vms:
return_vms.append( return_vms.append(
{ {
@ -267,7 +267,7 @@ class ListUserFiles(Resource):
else: else:
files = shared.etcd_client.get_prefix(settings['etcd']['file_prefix'], value_in_json=True) files = shared.etcd_client.get_prefix(settings['etcd']['file_prefix'], value_in_json=True)
return_files = [] return_files = []
user_files = [f for f in files if f.value['owner'] == data['name']] user_files = [f for f in files if f.value['owner'] == validator.name]
for file in user_files: for file in user_files:
file_uuid = file.key.split('/')[-1] file_uuid = file.key.split('/')[-1]
file = file.value file = file.value
@ -292,8 +292,8 @@ class CreateHost(Resource):
else: else:
host_key = join_path(settings['etcd']['host_prefix'], uuid4().hex) host_key = join_path(settings['etcd']['host_prefix'], uuid4().hex)
host_entry = { host_entry = {
'specs': validator.specs.value, 'specs': validator.specs,
'hostname': validator.hostname.value, 'hostname': validator.hostname,
'status': HostStatus.dead, 'status': HostStatus.dead,
'last_heartbeat': '', 'last_heartbeat': '',
} }
@ -325,9 +325,9 @@ class GetSSHKeys(Resource):
except ValidationException as err: except ValidationException as err:
return make_return_message(err, 400) return make_return_message(err, 400)
else: else:
etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm.value, etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm,
validator.name.value, 'key') validator.name, 'key')
if not validator.key_name.value: if not validator.key_name:
etcd_entry = shared.etcd_client.get_prefix(etcd_key, value_in_json=True) etcd_entry = shared.etcd_client.get_prefix(etcd_key, value_in_json=True)
keys = { keys = {
key.key.split('/')[-1]: key.value key.key.split('/')[-1]: key.value
@ -335,7 +335,7 @@ class GetSSHKeys(Resource):
} }
return {'keys': keys} return {'keys': keys}
else: else:
etcd_key = join_path(validator.key_name.value) etcd_key = join_path(validator.key_name)
try: try:
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True) etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
except KeyError: except KeyError:
@ -358,17 +358,17 @@ class AddSSHKey(Resource):
else: else:
# {user_prefix}/{realm}/{name}/key/{key_name} # {user_prefix}/{realm}/{name}/key/{key_name}
etcd_key = join_path( etcd_key = join_path(
settings['etcd']['user_prefix'], validator.realm.value, settings['etcd']['user_prefix'], validator.realm,
validator.name.value, 'key', validator.key_name.value validator.name, 'key', validator.key_name
) )
try: try:
shared.etcd_client.get(etcd_key, value_in_json=True) shared.etcd_client.get(etcd_key, value_in_json=True)
except KeyError: except KeyError:
# Key Not Found. It implies user' haven't added any key yet. # Key Not Found. It implies user' haven't added any key yet.
shared.etcd_client.put(etcd_key, validator.key.value, value_in_json=True) shared.etcd_client.put(etcd_key, validator.key, value_in_json=True)
return make_return_message('Key added successfully') return make_return_message('Key added successfully')
else: else:
return make_return_message('Key "{}" already exists'.format(validator.key_name.value)) return make_return_message('Key "{}" already exists'.format(validator.key_name))
class RemoveSSHKey(Resource): class RemoveSSHKey(Resource):
@ -382,12 +382,12 @@ class RemoveSSHKey(Resource):
return make_return_message(err, 400) return make_return_message(err, 400)
else: else:
# {user_prefix}/{realm}/{name}/key/{key_name} # {user_prefix}/{realm}/{name}/key/{key_name}
etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm.value, etcd_key = join_path(settings['etcd']['user_prefix'], validator.realm,
validator.name.value, 'key', validator.key_name.value) validator.name, 'key', validator.key_name)
try: try:
etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True) etcd_entry = shared.etcd_client.get(etcd_key, value_in_json=True)
except KeyError: except KeyError:
return make_return_message('No Key "{}" exists.'.format(validator.key_name.value)) return make_return_message('No Key "{}" exists.'.format(validator.key_name))
if etcd_entry: if etcd_entry:
shared.etcd_client.client.delete(etcd_key) shared.etcd_client.client.delete(etcd_key)
return {'message': 'Key successfully removed.'} return {'message': 'Key successfully removed.'}
@ -405,9 +405,9 @@ class CreateNetwork(Resource):
else: else:
network_entry = { network_entry = {
'id': counters.increment_etcd_counter(settings['etcd']['vxlan_counter']), 'id': counters.increment_etcd_counter(settings['etcd']['vxlan_counter']),
'type': validator.type.value, 'type': validator.type,
} }
if validator.user.value: if validator.user:
try: try:
nb = pynetbox.api(url=settings['netbox']['url'], token=settings['netbox']['token']) nb = pynetbox.api(url=settings['netbox']['url'], token=settings['netbox']['token'])
nb_prefix = nb.ipam.prefixes.get(prefix=settings['network']['prefix']) nb_prefix = nb.ipam.prefixes.get(prefix=settings['network']['prefix'])
@ -415,8 +415,8 @@ class CreateNetwork(Resource):
data={ data={
'prefix_length': int(settings['network']['prefix_length']), 'prefix_length': int(settings['network']['prefix_length']),
'description': '{}\'s network "{}"'.format( 'description': '{}\'s network "{}"'.format(
validator.name.value, validator.name,
validator.network_name.value validator.network_name
), ),
'is_pool': True, 'is_pool': True,
} }
@ -429,8 +429,8 @@ class CreateNetwork(Resource):
else: else:
network_entry['ipv6'] = 'fd00::/64' network_entry['ipv6'] = 'fd00::/64'
network_key = join_path(settings['etcd']['network_prefix'], validator.name.value, network_key = join_path(settings['etcd']['network_prefix'], validator.name,
validator.network_name.value) validator.network_name)
shared.etcd_client.put(network_key, network_entry, value_in_json=True) shared.etcd_client.put(network_key, network_entry, value_in_json=True)
return make_return_message('Network successfully added.') return make_return_message('Network successfully added.')
@ -445,7 +445,7 @@ class ListUserNetwork(Resource):
except ValidationException as err: except ValidationException as err:
return make_return_message(err, 400) return make_return_message(err, 400)
else: else:
prefix = join_path(settings['etcd']['network_prefix'], data['name']) prefix = join_path(settings['etcd']['network_prefix'], validator.name)
networks = shared.etcd_client.get_prefix(prefix, value_in_json=True) networks = shared.etcd_client.get_prefix(prefix, value_in_json=True)
user_networks = [] user_networks = []
for net in networks: for net in networks:
@ -455,7 +455,7 @@ class ListUserNetwork(Resource):
api.add_resource(CreateVM, '/vm/create') api.add_resource(CreateVM, '/vm/create')
api.add_resource(VmStatus, '/vm/status') api.add_resource(GetVMStatus, '/vm/status')
api.add_resource(VMAction, '/vm/action') api.add_resource(VMAction, '/vm/action')
api.add_resource(VMMigration, '/vm/migrate') api.add_resource(VMMigration, '/vm/migrate')

View file

@ -30,9 +30,9 @@ class Field:
def is_valid(self): def is_valid(self):
if not isinstance(self.value, self.type): if not isinstance(self.value, self.type):
raise ValidationException("Incorrect Type for '{}' field".format(self.name)) raise ValidationException("Incorrect Type for '{}' field".format(self.name))
else:
for validator in self.validators: for validator in self.validators:
validator() validator()
def __repr__(self): def __repr__(self):
return self.name return self.name
@ -52,8 +52,7 @@ class VmUUIDField(Field):
class BaseSchema: class BaseSchema:
def __init__(self, data): def __init__(self):
print(data)
self.fields = [getattr(self, field) for field in dir(self) if isinstance(getattr(self, field), Field)] self.fields = [getattr(self, field) for field in dir(self) if isinstance(getattr(self, field), Field)]
def validation(self): def validation(self):
@ -69,6 +68,9 @@ class BaseSchema:
self.validation() self.validation()
for field in self.fields:
setattr(self, field.name, field.value)
def get(dictionary: dict, key: str, return_default=False, default=None): def get(dictionary: dict, key: str, return_default=False, default=None):
if dictionary is None: if dictionary is None:
@ -88,7 +90,7 @@ class OTPSchema(BaseSchema):
self.name = Field('name', str, get(data, 'name')) self.name = Field('name', str, get(data, 'name'))
self.realm = Field('realm', str, get(data, 'realm')) self.realm = Field('realm', str, get(data, 'realm'))
self.token = Field('token', str, get(data, 'token')) self.token = Field('token', str, get(data, 'token'))
super().__init__(data=data) super().__init__()
def validation(self): def validation(self):
if check_otp(self.name.value, self.realm.value, self.token.value) != 200: if check_otp(self.name.value, self.realm.value, self.token.value) != 200:
@ -101,8 +103,7 @@ class CreateImageSchema(BaseSchema):
self.name = Field('name', str, get(data, 'name')) self.name = Field('name', str, get(data, 'name'))
self.image_store = Field('image_store', str, get(data, 'image_store'), self.image_store = Field('image_store', str, get(data, 'image_store'),
validators=[self.image_store_name_validation]) validators=[self.image_store_name_validation])
super().__init__()
super().__init__(data)
def file_uuid_validation(self): def file_uuid_validation(self):
try: try:
@ -123,7 +124,7 @@ class CreateHostSchema(OTPSchema):
self.specs = Field('specs', dict, get(data, 'specs'), validators=[self.specs_validation]) self.specs = Field('specs', dict, get(data, 'specs'), validators=[self.specs_validation])
self.hostname = Field('hostname', str, get(data, 'hostname')) self.hostname = Field('hostname', str, get(data, 'hostname'))
super().__init__(data=data) super().__init__(data)
def specs_validation(self): def specs_validation(self):
allowed_base = 10 allowed_base = 10
@ -191,7 +192,7 @@ class CreateVMSchema(OTPSchema):
def image_validation(self): def image_validation(self):
try: try:
image_uuid = helper.resolve_image_name(self.image.value) image_uuid = helper.resolve_image_name(self.image.value)
except Exception as e: except Exception:
raise ValidationException('No image of name \'{}\' found'.format(self.image.value)) raise ValidationException('No image of name \'{}\' found'.format(self.image.value))
else: else:
self.image_uuid = image_uuid self.image_uuid = image_uuid
@ -214,7 +215,7 @@ class CreateVMSchema(OTPSchema):
raise ValidationException('Network with name {} does not exists'.format(net)) raise ValidationException('Network with name {} does not exists'.format(net))
def specs_validation(self): def specs_validation(self):
ALLOWED_BASE = 10 allowed_base = 10
try: try:
_cpu = get(self.specs.value, 'cpu') _cpu = get(self.specs.value, 'cpu')
@ -228,10 +229,10 @@ class CreateVMSchema(OTPSchema):
parsed_ram = bitmath.parse_string_unsafe(_ram) parsed_ram = bitmath.parse_string_unsafe(_ram)
parsed_os_ssd = bitmath.parse_string_unsafe(_os_ssd) parsed_os_ssd = bitmath.parse_string_unsafe(_os_ssd)
if parsed_ram.base != ALLOWED_BASE: if parsed_ram.base != allowed_base:
raise ValidationException('Your specified RAM is not in correct units') raise ValidationException('Your specified RAM is not in correct units')
if parsed_os_ssd.base != ALLOWED_BASE: if parsed_os_ssd.base != allowed_base:
raise ValidationException('Your specified OS-SSD is not in correct units') raise ValidationException('Your specified OS-SSD is not in correct units')
if int(_cpu) < 1: if int(_cpu) < 1:
@ -246,7 +247,7 @@ class CreateVMSchema(OTPSchema):
parsed_hdd = [] parsed_hdd = []
for hdd in _hdd: for hdd in _hdd:
_parsed_hdd = bitmath.parse_string_unsafe(hdd) _parsed_hdd = bitmath.parse_string_unsafe(hdd)
if _parsed_hdd.base != ALLOWED_BASE: if _parsed_hdd.base != allowed_base:
raise ValidationException('Your specified HDD is not in correct units') raise ValidationException('Your specified HDD is not in correct units')
else: else:
parsed_hdd.append(str(_parsed_hdd)) parsed_hdd.append(str(_parsed_hdd))