api data redesign part2. add db query function
This commit is contained in:
parent
add7529a2e
commit
49e70c2449
3 changed files with 47 additions and 26 deletions
16
grid.py
16
grid.py
|
@ -20,16 +20,11 @@ logger = ioconfig.logger
|
||||||
config = ioconfig.parser
|
config = ioconfig.parser
|
||||||
|
|
||||||
def query(json):
|
def query(json):
|
||||||
if json['type'] == 'deploy' or json['type'] == 'router':
|
|
||||||
data = read(json)
|
data = read(json)
|
||||||
return data['slave'], data['type'], data['phyid'], data['hostname'], data['clientemail']
|
if json['type'] == 'kvm' or json['type'] == 'lxc':
|
||||||
|
return data['slave'], data['phyid'], data['hostname'], data['clientemail']
|
||||||
if json['type'] == 'bridge':
|
if json['type'] == 'vmbr':
|
||||||
data = read(json)
|
return data['slave'], data['phyid'], data['clientemail']
|
||||||
return data['slave'], data['type'], data['phyid'], data['clientemail']
|
|
||||||
|
|
||||||
def create(data):
|
|
||||||
write(data)
|
|
||||||
|
|
||||||
def read(data):
|
def read(data):
|
||||||
""" open a metadata file """
|
""" open a metadata file """
|
||||||
|
@ -45,7 +40,7 @@ def read(data):
|
||||||
pass
|
pass
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def write(data):
|
def create(json):
|
||||||
""" create new metadata file """
|
""" create new metadata file """
|
||||||
try:
|
try:
|
||||||
dbfile = 'db/{}.{}.json'.format(data['type'], data['unit_id'])
|
dbfile = 'db/{}.{}.json'.format(data['type'], data['unit_id'])
|
||||||
|
@ -53,6 +48,7 @@ def write(data):
|
||||||
json.dump(data, dbf)
|
json.dump(data, dbf)
|
||||||
dbf.close()
|
dbf.close()
|
||||||
logger.info('grid> {} --> {}'.format(data, dbfile))
|
logger.info('grid> {} --> {}'.format(data, dbfile))
|
||||||
|
return data
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.critical('grid> {}'.format(e))
|
logger.critical('grid> {}'.format(e))
|
||||||
pass
|
pass
|
||||||
|
|
41
plugin.py
41
plugin.py
|
@ -83,7 +83,7 @@ def create(json):
|
||||||
'slave': slave_name,
|
'slave': slave_name,
|
||||||
'phyid': phy_id
|
'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':
|
if json['type'] == 'router':
|
||||||
create_result = proxobject.nodes(real_slave_name).lxc.post(vmid=int(phy_id),
|
create_result = proxobject.nodes(real_slave_name).lxc.post(vmid=int(phy_id),
|
||||||
|
@ -107,9 +107,10 @@ def create(json):
|
||||||
'slave': slave_name,
|
'slave': slave_name,
|
||||||
'phyid': phy_id
|
'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':
|
if json['type'] == 'bridge':
|
||||||
#TODO: CREATE BRIDGE
|
|
||||||
data = { 'unit_id': int(unit_id),
|
data = { 'unit_id': int(unit_id),
|
||||||
'type': 'vmbr',
|
'type': 'vmbr',
|
||||||
'clientid': json['clientid'],
|
'clientid': json['clientid'],
|
||||||
|
@ -118,16 +119,18 @@ def create(json):
|
||||||
'slave': slave_name,
|
'slave': slave_name,
|
||||||
'phyid': phy_id
|
'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
|
time.sleep(7) #wait few seconds for the slave to prepare the machine for initial run
|
||||||
|
|
||||||
grid.create(data)
|
grid.create(data)
|
||||||
response = { 'status': 'CREATED', 'unit_id': unit_id, 'hostname': vm_name, 'password': vm_pass, 'slave': real_slave_name }
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def remove(json):
|
def remove(json):
|
||||||
""" terminate an unit """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
ioconfig.logger.info('%s[%s]> deleting %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
|
ioconfig.logger.info('%s[%s]> deleting %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
|
||||||
if unit_type == 'kvm':
|
if unit_type == 'kvm':
|
||||||
|
@ -136,12 +139,17 @@ def remove(json):
|
||||||
if unit_type == 'lxc':
|
if unit_type == 'lxc':
|
||||||
result = proxobject.nodes(slave_name).lxc(phy_id).delete()
|
result = proxobject.nodes(slave_name).lxc(phy_id).delete()
|
||||||
grid.delete(json)
|
grid.delete(json)
|
||||||
response = { 'status':'DELETED'}
|
response = { 'status':'{}_deleted'.format(unit_type) }
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def query(json):
|
||||||
|
""" return the db info of an unit """
|
||||||
|
return grid.query(json)
|
||||||
|
|
||||||
def status(json):
|
def status(json):
|
||||||
""" returns the status of an unit """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
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):
|
def start(json):
|
||||||
""" starts a machine """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
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):
|
def shutdown(json):
|
||||||
""" acpi shutdown the machine.. """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
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):
|
def stop(json):
|
||||||
""" poweroff the machine.. """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
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):
|
def suspend(json):
|
||||||
""" suspend machine """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
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):
|
def resume(json):
|
||||||
""" resume machine """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
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):
|
def vmrrd(json):
|
||||||
""" retrieve rrd graphs (PNG) """
|
""" 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 = auth(slave_name)
|
||||||
proxobject.cluster.status.get()[0]['name']
|
proxobject.cluster.status.get()[0]['name']
|
||||||
|
|
||||||
|
@ -255,7 +269,8 @@ def vmrrd(json):
|
||||||
|
|
||||||
def vmvnc(json):
|
def vmvnc(json):
|
||||||
""" invoke vnc ticket """
|
""" 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)
|
proxobject = auth(slave_name)
|
||||||
#slave_name = proxobject.cluster.status.get()[0]['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))
|
ioconfig.logger.info('%s[%s]> invoking vnc ticket for %s %s (%s)' % (vm_owner, slave_name, unit_type, phy_id, vm_host))
|
||||||
|
|
|
@ -41,6 +41,8 @@ def selector(fn, req):
|
||||||
body = plugin.remove(json)
|
body = plugin.remove(json)
|
||||||
elif fn == 'status':
|
elif fn == 'status':
|
||||||
body = plugin.status(json)
|
body = plugin.status(json)
|
||||||
|
elif fn == 'query':
|
||||||
|
body = grid.query(json)
|
||||||
|
|
||||||
elif fn == 'start':
|
elif fn == 'start':
|
||||||
body = plugin.start(json)
|
body = plugin.start(json)
|
||||||
|
@ -63,7 +65,7 @@ def selector(fn, req):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
else:
|
else:
|
||||||
logger.info('grid> {}'.format(fn))
|
#logger.info('grid> {}'.format(fn))
|
||||||
status = falcon.HTTP_202
|
status = falcon.HTTP_202
|
||||||
|
|
||||||
return status, body
|
return status, body
|
||||||
|
@ -148,6 +150,13 @@ class StatusUnit(object):
|
||||||
resp.status, response = selector('status', req)
|
resp.status, response = selector('status', req)
|
||||||
req.context['result'] = response
|
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):
|
class SuspendUnit(object):
|
||||||
@falcon.before(max_body(64 * 1024))
|
@falcon.before(max_body(64 * 1024))
|
||||||
def on_post(self, req, resp):
|
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('/create', CreateUnit())
|
||||||
api.add_route('/remove', RemoveUnit())
|
api.add_route('/remove', RemoveUnit())
|
||||||
api.add_route('/status', StatusUnit())
|
api.add_route('/status', StatusUnit())
|
||||||
|
api.add_route('/query', QueryUnit())
|
||||||
|
|
||||||
api.add_route('/start', StartUnit())
|
api.add_route('/start', StartUnit())
|
||||||
api.add_route('/suspend', SuspendUnit())
|
api.add_route('/suspend', SuspendUnit())
|
||||||
|
|
Loading…
Reference in a new issue