proxmaster/proxmaster.py

232 lines
6.8 KiB
Python
Raw Normal View History

2016-02-15 05:30:43 -05:00
#. -*- coding: utf-8 -
# import site packages
import logging
import falcon
import sys
import json
#import local packages
import ioconfig
import grid
import plugin
config = ioconfig.parser
logger = ioconfig.logger
2016-04-01 21:29:22 -04:00
2016-02-15 05:30:43 -05:00
def welcome():
"""displays motd in log as welcome message"""
2017-07-30 19:15:40 -04:00
logger.info('# proxmaster ][ (c) 2015-2017 deflax.net #')
2016-02-15 05:30:43 -05:00
2016-04-01 21:29:22 -04:00
2017-10-19 11:55:09 -04:00
def selector(fn, req):
2016-04-09 23:37:32 -04:00
""" try to exec commands """
2016-04-09 18:53:15 -04:00
json = req.context['doc']
2016-10-17 05:10:41 -04:00
#print(json)
2016-04-09 18:53:15 -04:00
apipass = json['apikey']
if apipass != config.get('general', 'apipass'):
2016-04-11 07:54:34 -04:00
status = falcon.HTTP_403
body = falcon.HTTP_403
2016-04-09 18:53:15 -04:00
logger.error('grid> access denied. bad api key!')
2016-04-11 07:54:34 -04:00
return status, body
2016-04-09 18:53:15 -04:00
try:
2017-10-19 11:55:09 -04:00
if fn == 'create':
body = plugin.create(json)
elif fn == 'remove':
body = plugin.remove(json)
elif fn == 'status':
body = plugin.status(json)
elif fn == 'query':
2017-11-05 15:21:06 -05:00
body = grid.read(json)
2017-10-19 11:55:09 -04:00
elif fn == 'start':
body = plugin.start(json)
elif fn == 'shutdown':
body = plugin.shutdown(json)
elif fn == 'stop':
body = plugin.stop(json)
elif fn == 'suspend':
body = plugin.suspend(json)
elif fn == 'resume':
body = plugin.resume(json)
2016-04-09 23:37:32 -04:00
2016-06-26 11:09:22 -04:00
elif fn == 'vmrrd':
2017-10-19 11:55:09 -04:00
body = plugin.vmrrd(json)
2016-06-25 11:27:45 -04:00
elif fn == 'vmvnc':
2017-10-19 11:55:09 -04:00
body = plugin.vmvnc(json)
2016-04-09 18:53:15 -04:00
except:
2016-05-22 18:55:56 -04:00
logger.critical('grid> {} error'.format(fn))
status = falcon.HTTP_404
2016-04-09 23:37:32 -04:00
raise
2016-04-09 18:53:15 -04:00
else:
#logger.info('grid> {}'.format(fn))
2016-04-09 18:53:15 -04:00
status = falcon.HTTP_202
return status, body
2016-04-08 10:48:18 -04:00
class RequireJSON(object):
def process_request(self, req, resp):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT'):
if 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
class JSONTranslator(object):
def process_request(self, req, resp):
# req.stream corresponds to the WSGI wsgi.input environ variable,
# and allows you to read bytes from the request body.
#
# See also: PEP 3333
if req.content_length in (None, 0):
# Nothing to do
return
body = req.stream.read()
if not body:
raise falcon.HTTPBadRequest('Empty request body',
'A valid JSON document is required.')
try:
req.context['doc'] = json.loads(body.decode('utf-8'))
except (ValueError, UnicodeDecodeError):
2016-04-09 23:37:32 -04:00
raise falcon.HTTPError(falcon.HTTP_400,
2016-04-08 10:48:18 -04:00
'Malformed JSON',
'Could not decode the request body. The '
'JSON was incorrect or not encoded as '
'UTF-8.')
def process_response(self, req, resp, resource):
if 'result' not in req.context:
return
resp.body = json.dumps(req.context['result'])
def max_body(limit):
def hook(req, resp, resource, params):
length = req.content_length
if length is not None and length > limit:
msg = ('The size of the request is too large. The body must not '
'exceed ' + str(limit) + ' bytes in length.')
raise falcon.HTTPRequestEntityTooLarge(
'Request body is too large', msg)
return hook
2017-10-19 11:55:09 -04:00
class CreateUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2016-02-15 05:30:43 -05:00
def on_post(self, req, resp):
2017-10-19 11:55:09 -04:00
""" creates an unit """
resp.status, response = selector('create', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-04-01 21:29:22 -04:00
2017-10-19 11:55:09 -04:00
class RemoveUnit(object):
2016-04-09 18:53:15 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
""" removes unit completely"""
resp.status, response = selector('remove', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-03-07 12:25:13 -05:00
2017-10-19 11:55:09 -04:00
class StatusUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
""" checks unit status """
resp.status, response = selector('status', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-04-01 21:29:22 -04:00
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
2017-10-19 11:55:09 -04:00
class SuspendUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-02-15 05:30:43 -05:00
""" Temporary suspend the instance """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('suspend', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-04-01 21:29:22 -04:00
2017-10-19 11:55:09 -04:00
class ResumeUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-02-15 05:30:43 -05:00
""" Unuspend the instance """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('resume', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-04-01 21:29:22 -04:00
2017-10-19 11:55:09 -04:00
class StartUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-02-15 05:30:43 -05:00
""" Start the instance """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('start', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-02-15 05:30:43 -05:00
2017-10-19 11:55:09 -04:00
class ShutdownUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-02-15 05:30:43 -05:00
""" ACPI Shutdown the instance """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('shutdown', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-02-15 05:30:43 -05:00
2017-10-19 11:55:09 -04:00
class StopUnit(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-02-15 05:30:43 -05:00
""" Stop the instance """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('stop', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-02-15 05:30:43 -05:00
2017-10-19 11:55:09 -04:00
class RRDVM(object):
2016-06-26 11:09:22 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-06-26 11:09:22 -04:00
""" Generate rrd pngs """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('vmrrd', req)
2016-06-26 11:54:27 -04:00
req.context['result'] = response
2016-06-26 11:09:22 -04:00
2017-10-19 11:55:09 -04:00
class VNCVM(object):
2016-04-08 21:10:07 -04:00
@falcon.before(max_body(64 * 1024))
2017-10-19 11:55:09 -04:00
def on_post(self, req, resp):
2016-02-15 05:30:43 -05:00
""" Create a VNC link to the instance """
2017-10-19 11:55:09 -04:00
resp.status, response = selector('vmvnc', req)
2016-04-09 18:53:15 -04:00
req.context['result'] = response
2016-02-15 05:30:43 -05:00
if __name__ == '__main__':
sys.exit("invoke proxmaster via uwsgi. thanks. bye. o/")
2016-04-08 10:48:18 -04:00
wsgi_app = api = application = falcon.API(middleware=[
RequireJSON(),
JSONTranslator(),
])
2016-02-15 05:30:43 -05:00
# setup routes
2017-10-19 11:55:09 -04:00
api.add_route('/create', CreateUnit())
api.add_route('/remove', RemoveUnit())
api.add_route('/status', StatusUnit())
api.add_route('/query', QueryUnit())
2017-10-19 11:55:09 -04:00
api.add_route('/start', StartUnit())
api.add_route('/suspend', SuspendUnit())
api.add_route('/resume', ResumeUnit())
api.add_route('/shutdown', ShutdownUnit())
api.add_route('/stop', StopUnit())
api.add_route('/vmrrd', RRDVM())
api.add_route('/vmvnc', VNCVM())
2016-02-15 05:30:43 -05:00
2016-04-09 18:53:15 -04:00
#display motd
welcome()