what s mess :D

This commit is contained in:
deflax 2016-04-10 01:53:15 +03:00
parent c635db1af3
commit 9052ef4669
2 changed files with 79 additions and 174 deletions

View file

@ -78,7 +78,7 @@ def validate(clientemail, password):
except: except:
ioconfig.logger.warning('clients> {} was not found in the database!'.format(clientemail)) ioconfig.logger.warning('clients> {} was not found in the database!'.format(clientemail))
#log bad ips here... #log bad ips here...
return False return {}
#2. check the password #2. check the password
encpass = clientsdb[c_id]['encpasswd'] encpass = clientsdb[c_id]['encpasswd']

View file

@ -27,6 +27,54 @@ def welcome():
logger.info('###################################') logger.info('###################################')
def selector(fn, req, vmid=0):
""" TRY to exec commands """
json = req.context['doc']
apipass = json['apikey']
if apipass != config.get('general', 'apipass'):
status = falcon.HTTP_404
body = '404 Not Found'
logger.error('grid> access denied. bad api key!')
fn = '404'
try:
if fn == 'validate':
clientemail = json['clientemail']
passwd = json['password']
#logger.info('grid> access requested for {} with {}'.format(clientemail, passwd))
body = clientsdb.validate(clientemail, passwd)
elif fn == 'create':
body = urllib.parse.urlencode(plugin.vmcreate(req.params))
elif fn == 'status':
body = urllib.parse.urlencode(plugin.vmstatus(vmid))
elif fn == 'delete':
body = urllib.parse.urlencode(plugin.vmdelete(vmid))
elif fn == 'suspend':
body = urllib.parse.urlencode(plugin.vmsuspend(vmid))
elif fn == 'resume':
body = urllib.parse.urlencode(plugin.vmresume(vmid))
elif fn == 'start':
body = urllib.parse.urlencode(plugin.vmstart(vmid))
elif fn == 'shutdown':
body = urllib.parse.urlencode(plugin.vmshutdown(vmid))
elif fn == 'stop':
body = urllib.parse.urlencode(plugin.vmstop(vmid))
elif fn == 'vnc':
body = urllib.parse.urlencode(plugin.vmvnc(vmid))
except:
logger.critical('grid> {} malfunction!'.format(fn))
body = '793 Zombie Apocalypse'
status = falcon.HTTP_404
else:
#logger.info('{}> 200 OK'.format(fn))
status = falcon.HTTP_202
return status, body
class RequireJSON(object): class RequireJSON(object):
def process_request(self, req, resp): def process_request(self, req, resp):
if not req.client_accepts_json: if not req.client_accepts_json:
@ -86,246 +134,100 @@ def max_body(limit):
return hook return hook
#API methods
class ValidateResource(object): class ValidateResource(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):
""" get clientemail and password and compare it with the client db and returns a list of managed object IDs """ """ get clientemail and password and compare it with the client db and returns a list of managed object IDs """
json = req.context['doc'] resp.status, response = selector('validate', req)
apipass = json['apikey']
if apipass != config.get('general', 'apipass'):
resp.status = falcon.HTTP_404
logger.error('grid> access denied. bad api key!')
return None
clientemail = json['clientemail']
passwd = json['password']
logger.info('grid> access requested for {} with {}'.format(clientemail, passwd))
response = clientsdb.validate(clientemail, passwd)
resp.status = falcon.HTTP_202
req.context['result'] = response req.context['result'] = response
class ClusterResource(object): class ClusterResource(object):
def on_get(self, req, resp): #def on_get(self, req, resp):
"""TEST ONLY. List cluster nodes. TEST ONLY""" # """ TEST . List cluster nodes . TEST """
json = req.context['doc'] # logger.info('grid> cache status')
apipass = json['apikey'] # response = grid.sync(False)
if apipass != config.get('general', 'apipass'): # response = 'poke'
resp.status = falcon.HTTP_404 # req.context['result'] = response
logger.error('grid> access denied. bad api key!')
return None
logger.info('grid> cache status')
response = grid.sync(False)
resp.status = falcon.HTTP_202
req.context['result'] = response
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp): def on_post(self, req, resp):
"""Create a cluster node, returns array of: status, vmid, pass, ipv4, """ """Create a cluster node, returns array of: status, vmid, pass, ipv4, """
logger.info('grid> create ' + str(req.params)) logger.info('grid> create ' + str(req.params))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('create', req)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_200
try:
resp.body = urllib.parse.urlencode(plugin.vmcreate(req.params))
except:
logger.error('grid> create function cancelled')
raise
resp.status = falcon.HTTP_403
response = 'CREATE ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class StatusResource(object): class StatusResource(object):
def on_get(self, req, resp, vmid): @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid):
""" check vm status """ """ check vm status """
logger.info('grid> status ' + str(vmid)) logger.info('grid> status ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('status', req, vmid)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_200
try:
resp.body = urllib.parse.urlencode(plugin.vmstatus(vmid))
except:
logger.error('grid> status error')
raise
resp.status = falcon.HTTP_403
response = 'STATUS ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class DeleteResource(object): class DeleteResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" delete machine completely""" """ delete machine completely"""
logger.info('grid> delete ' + str(vmid)) logger.info('grid> delete ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('delete', req, vmid)
if apicheck_stat: req.context['result'] = response
try:
resp.body = urllib.parse.urlencode(plugin.vmdelete(vmid))
except:
logger.error('grid> delete error')
raise
resp.status = falcon.HTTP_403
response = 'DELETE ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class ArchivateResource(object): class ArchivateResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Temporary suspend the instance """ """ Temporary suspend the instance """
logger.info('grid> suspend ' + str(vmid)) logger.info('grid> suspend ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('suspend', req, vmid)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_202
try:
resp.body = urllib.parse.urlencode(plugin.vmsuspend(vmid))
except:
logger.error('grid> pause error')
raise
resp.status = falcon.HTTP_403
response = 'PAUSE ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class UnArchiveResource(object): class UnArchiveResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Unuspend the instance """ """ Unuspend the instance """
logger.info('grid> resume ' + str(vmid)) logger.info('grid> resume ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('resume', req, vmid)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_202
try:
resp.body = urllib.parse.urlencode(plugin.vmresume(vmid))
except:
logger.error('grid> resume error')
raise
resp.status = falcon.HTTP_403
response = 'RESUME ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class StartResource(object): class StartResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Start the instance """ """ Start the instance """
logger.info('grid> start ' + str(vmid)) logger.info('grid> start ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('start', req, vmid)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_202
try:
resp.body = urllib.parse.urlencode(plugin.vmstart(vmid))
except:
logger.error('grid> start error')
#raise
resp.status = falcon.HTTP_403
response = 'START ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class ShutdownResource(object): class ShutdownResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" ACPI Shutdown the instance """ """ ACPI Shutdown the instance """
logger.info('grid> shutdown ' + str(vmid)) logger.info('grid> shutdown ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('shutdown', req, vmid)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_202
try:
resp.body = urllib.parse.urlencode(plugin.vmshutdown(vmid))
#TODO: Try few times and then return proper status message
except:
logger.error('grid> shutdown error')
#raise
resp.status = falcon.HTTP_403
response = 'SHUTDOWN ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class StopResource(object): class StopResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Stop the instance """ """ Stop the instance """
logger.info('grid> stop ' + str(vmid)) logger.info('grid> stop ' + str(vmid))
apicheck_stat, apicheck_resp = apicheck(req.params) resp.status, response = selector('stop', req, vmid)
if apicheck_stat: req.context['result'] = response
resp.status = falcon.HTTP_202
try:
resp.body = urllib.parse.urlencode(plugin.vmstop(vmid))
except:
logger.error('grid> stop error')
#raise
resp.status = falcon.HTTP_403
response = 'STOP ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
class VNCResource(object): class VNCResource(object):
@falcon.before(max_body(64 * 1024)) @falcon.before(max_body(64 * 1024))
def on_post(self, req, resp, vmid): def on_post(self, req, resp, vmid):
""" Create a VNC link to the instance """ """ Create a VNC link to the instance """
apicheck_stat, apicheck_resp = apicheck(req.params)
logger.info('grid> vnc ' + str(vmid)) logger.info('grid> vnc ' + str(vmid))
if apicheck_stat: resp.status, response = selector('vnc', req, vmid)
try: req.context['result'] = response
resp.status = falcon.HTTP_202
resp.body = urllib.parse.urlencode(plugin.vmvnc(vmid))
except:
logger.error('grid> vnc error')
raise
resp.status = falcon.HTTP_403
response = 'VNC ERR'
resp.body = response
else:
resp.status = falcon.HTTP_403
resp.body = apicheck_resp
if __name__ == '__main__': if __name__ == '__main__':
sys.exit("invoke proxmaster via uwsgi. thanks. bye. o/") sys.exit("invoke proxmaster via uwsgi. thanks. bye. o/")
#setup routes
wsgi_app = api = application = falcon.API(middleware=[ wsgi_app = api = application = falcon.API(middleware=[
RequireJSON(), RequireJSON(),
JSONTranslator(), JSONTranslator(),
]) ])
#display motd
welcome()
#logger.info('grid> sync')
#grid.sync()
# setup routes # setup routes
res_validate = ValidateResource() res_validate = ValidateResource()
api.add_route('/validate', res_validate) api.add_route('/validate', res_validate)
@ -357,3 +259,6 @@ api.add_route('/instance/stop/{vmid}', res_stop)
res_vnc = VNCResource() res_vnc = VNCResource()
api.add_route('/instance/vnc/{vmid}', res_vnc) api.add_route('/instance/vnc/{vmid}', res_vnc)
#display motd
welcome()