Source code for pacifica.policy.root

# -*- coding: utf-8 -*-
"""CherryPy root object class."""
from __future__ import print_function
import sys
from time import sleep
from json import dumps
import requests
import cherrypy
from import UploaderPolicy
from import StatusPolicy
from import IngestPolicy
from import EventsPolicy
from import ReportingPolicy
from pacifica.policy.config import get_config
from pacifica.policy.globals import METADATA_CONNECT_ATTEMPTS, METADATA_WAIT

[docs]def error_page_default(**kwargs): """The default error page should always enforce json.""" cherrypy.response.headers['Content-Type'] = 'application/json' json_str = dumps({ 'postdata':'UTF-8'), 'status': kwargs['status'], 'message': kwargs['message'], 'traceback': kwargs['traceback'], 'version': kwargs['version'] }, sort_keys=True, indent=4) print(json_str, file=sys.stderr) return json_str
# pylint: disable=too-few-public-methods
[docs]class Root(object): """ CherryPy root object class. not exposed by default the base objects are exposed """ exposed = True
[docs] def __init__(self): """Create the local objects we need.""" self.uploader = UploaderPolicy() self.status = StatusPolicy() self.reporting = ReportingPolicy() self.ingest = IngestPolicy() = EventsPolicy()
[docs] @staticmethod # pylint: disable=invalid-name def GET(): """Return happy message about functioning service.""" return {'message': 'Pacifica Policy Up and Running'}
# pylint: enable=invalid-name
[docs] @classmethod def try_meta_connect(cls, attempts=0): """Try to connect to the metadata service see if its there.""" try: ret = requests.get(get_config().get('metadata', 'status_url')) if ret.status_code != 200: raise Exception( 'try_meta_connect: {0}\n'.format(ret.status_code) ) # pylint: disable=broad-except except Exception: # pylint: enable=broad-except if attempts < METADATA_CONNECT_ATTEMPTS: sleep(METADATA_WAIT) attempts += 1 cls.try_meta_connect(attempts) else: raise Exception
# pylint: enable=too-few-public-methods