api data redesign part2. add db query function

This commit is contained in:
deflax 2017-10-24 18:47:37 +03:00
parent add7529a2e
commit 49e70c2449
3 changed files with 47 additions and 26 deletions

20
grid.py
View file

@ -20,16 +20,11 @@ logger = ioconfig.logger
config = ioconfig.parser
def query(json):
if json['type'] == 'deploy' or json['type'] == 'router':
data = read(json)
return data['slave'], data['type'], data['phyid'], data['hostname'], data['clientemail']
if json['type'] == 'bridge':
data = read(json)
return data['slave'], data['type'], data['phyid'], data['clientemail']
def create(data):
write(data)
data = read(json)
if json['type'] == 'kvm' or json['type'] == 'lxc':
return data['slave'], data['phyid'], data['hostname'], data['clientemail']
if json['type'] == 'vmbr':
return data['slave'], data['phyid'], data['clientemail']
def read(data):
""" open a metadata file """
@ -45,7 +40,7 @@ def read(data):
pass
return None
def write(data):
def create(json):
""" create new metadata file """
try:
dbfile = 'db/{}.{}.json'.format(data['type'], data['unit_id'])
@ -53,10 +48,11 @@ def write(data):
json.dump(data, dbf)
dbf.close()
logger.info('grid> {} --> {}'.format(data, dbfile))
return data
except Exception as e:
logger.critical('grid> {}'.format(e))
pass
return None
return None
def delete(unit_type, unit_id):
""" remove metadata file """

View file

@ -83,7 +83,7 @@ def create(json):
'slave': slave_name,
'phyid': phy_id
}
response = { 'status': 'deploy_created', 'unit_id': unit_id, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
if json['type'] == 'router':
create_result = proxobject.nodes(real_slave_name).lxc.post(vmid=int(phy_id),
@ -107,9 +107,10 @@ def create(json):
'slave': slave_name,
'phyid': phy_id
}
response = { 'status': 'router_created', 'unit_id': unit_id, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
if json['type'] == 'bridge':
#TODO: CREATE BRIDGE
data = { 'unit_id': int(unit_id),
'type': 'vmbr',
'clientid': json['clientid'],
@ -118,16 +119,18 @@ def create(json):
'slave': slave_name,
'phyid': phy_id
}
#TODO: CREATE BRIDGE
response = { 'status': 'bridge_created', 'unit_id': unit_id, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run
grid.create(data)
response = { 'status': 'CREATED', 'unit_id': unit_id, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
return response
def remove(json):
""" terminate an unit """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
ioconfig.logger.info('%s[%s]> deleting %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
if unit_type == 'kvm':
@ -136,12 +139,17 @@ def remove(json):
if unit_type == 'lxc':
result = proxobject.nodes(slave_name).lxc(phy_id).delete()
grid.delete(json)
response = { 'status':'DELETED'}
response = { 'status':'{}_deleted'.format(unit_type) }
return response
def query(json):
""" return the db info of an unit """
return grid.query(json)
def status(json):
""" returns the status of an unit """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> status of %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
@ -153,7 +161,8 @@ def status(json):
def start(json):
""" starts a machine """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> starting %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
@ -167,7 +176,8 @@ def start(json):
def shutdown(json):
""" acpi shutdown the machine.. """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> acpi shutdown %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
@ -183,7 +193,8 @@ def shutdown(json):
def stop(json):
""" poweroff the machine.. """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> power off %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
@ -199,7 +210,8 @@ def stop(json):
def suspend(json):
""" suspend machine """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> suspending %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
@ -213,7 +225,8 @@ def suspend(json):
def resume(json):
""" resume machine """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> resuming %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
@ -227,7 +240,8 @@ def resume(json):
def vmrrd(json):
""" retrieve rrd graphs (PNG) """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
proxobject.cluster.status.get()[0]['name']
@ -255,7 +269,8 @@ def vmrrd(json):
def vmvnc(json):
""" invoke vnc ticket """
slave_name, unit_type, phy_id, vm_host, vm_owner = grid.query(json)
unit_type = json['type']
slave_name, phy_id, vm_host, vm_owner = grid.query(json)
proxobject = auth(slave_name)
#slave_name = proxobject.cluster.status.get()[0]['name']
ioconfig.logger.info('%s[%s]> invoking vnc ticket for %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))

View file

@ -41,6 +41,8 @@ def selector(fn, req):
body = plugin.remove(json)
elif fn == 'status':
body = plugin.status(json)
elif fn == 'query':
body = grid.query(json)
elif fn == 'start':
body = plugin.start(json)
@ -63,7 +65,7 @@ def selector(fn, req):
raise
else:
logger.info('grid> {}'.format(fn))
#logger.info('grid> {}'.format(fn))
status = falcon.HTTP_202
return status, body
@ -148,6 +150,13 @@ class StatusUnit(object):
resp.status, response = selector('status', req)
req.context['result'] = response
class QueryUnit(object):
@falcon.before(max_body(64 * 1024))
def on_post(self, req, resp):
""" query unit info """
resp.status, response = selector('query', req)
req.context['result'] = response
class SuspendUnit(object):
@falcon.before(max_body(64 * 1024))
def on_post(self, req, resp):
@ -209,6 +218,7 @@ wsgi_app = api = application = falcon.API(middleware=[
api.add_route('/create', CreateUnit())
api.add_route('/remove', RemoveUnit())
api.add_route('/status', StatusUnit())
api.add_route('/query', QueryUnit())
api.add_route('/start', StartUnit())
api.add_route('/suspend', SuspendUnit())