Ejemplo n.º 1
0
        def hidden_decorator(self):
            try:
                # update stats
                SolUtils.AppStats().increment('JSON_NUM_REQ')

                # authorize the client
                siteName = self._authorizeClient(
                    token=bottle.request.headers.get('X-REALMS-Token'), )

                # abort if not authorized
                if not siteName:
                    return bottle.HTTPResponse(
                        body=json.dumps({'error': 'Unauthorized'}),
                        status=401,
                        headers={'Content-Type': 'application/json'},
                    )

                # abort if not valid JSON payload
                if bottle.request.json is not None:
                    try:
                        json.dumps(bottle.request.json)
                    except ValueError:
                        return bottle.HTTPResponse(
                            body=json.dumps({'error': 'Malformed JSON body'}),
                            status=400,
                            headers={'Content-Type': 'application/json'},
                        )

                # retrieve the return value
                returnVal = func(self, siteName=siteName)

                # send back answer
                return bottle.HTTPResponse(
                    status=200,
                    headers={'Content-Type': 'application/json'},
                    body=json.dumps(returnVal),
                )

            except SolExceptions.UnauthorizedError:
                return bottle.HTTPResponse(
                    status=401,
                    headers={'Content-Type': 'application/json'},
                    body=json.dumps({'error': 'Unauthorized'}),
                )
            except Exception as err:

                crashMsg = SolUtils.logCrash(err, SolUtils.AppStats())

                return bottle.HTTPResponse(
                    status=500,
                    headers={'Content-Type': 'application/json'},
                    body=json.dumps(crashMsg),
                )
Ejemplo n.º 2
0
 def run(self, handler):
     from cheroot.wsgi import Server as WSGIServer
     from cheroot.ssl.pyopenssl import pyOpenSSLAdapter
     server = WSGIServer((self.host, self.port), handler)
     server.ssl_adapter = pyOpenSSLAdapter(
         certificate=SolUtils.AppConfig().get('solserver_certificate'),
         private_key=SolUtils.AppConfig().get('solserver_private_key'),
     )
     try:
         server.start()
         log.info("Server started")
     finally:
         server.stop()
Ejemplo n.º 3
0
 def _clihandle_stats(params):
     stats = SolUtils.AppStats().get()
     output = []
     for k in sorted(stats.keys()):
         output += ['{0:<30}: {1}'.format(k, stats[k])]
     output = '\n'.join(output)
     print output
Ejemplo n.º 4
0
    def __init__(self):

        # local variables
        self.sites = []
        self.influxClient = influxdb.client.InfluxDBClient(
            host=SolUtils.AppConfig().get('influxdb_host'),
            port=SolUtils.AppConfig().get('influxdb_port'),
            database=SolUtils.AppConfig().get('influxdb_database'),
        )
        self.actions = []

        # initialize web server
        self.web = bottle.Bottle()
        # interaction with SolManager
        self.web.route(
            path='/api/v1/o.json',
            method='PUT',
            callback=self._webhandle_o_PUT,
        )
        self.web.route(
            path='/api/v1/getactions/',
            method='GET',
            callback=self._webhandle_getactions_GET,
        )
        # interaction with administrator
        self.web.route(
            path='/api/v1/echo.json',
            method='POST',
            callback=self._webhandle_echo_POST,
        )
        self.web.route(
            path='/api/v1/status.json',
            method='GET',
            callback=self._webhandle_status_GET,
        )
        self.web.route(
            path='/api/v1/setaction/',
            method='POST',
            callback=self._webhandle_setactions_POST,
        )

        # start the thread
        threading.Thread.__init__(self)
        self.name = 'JsonApiThread'
        self.daemon = True
        self.start()
Ejemplo n.º 5
0
    def run(self):
        try:
            self.web.run(
                host='0.0.0.0',
                port=SolUtils.AppConfig().get('solserver_tcpport'),
                server=self.HTTPSServer,
                quiet=True,
                debug=False,
            )

        except bottle.BottleException:
            raise

        except Exception as err:
            SolUtils.logCrash(err, SolUtils.AppStats(), threadName=self.name)

        log.info("JsonApiThread started")
Ejemplo n.º 6
0
    def __init__(self):

        # init Singletons -- must be first init
        SolUtils.AppConfig(config_file=CONFIGFILE)
        SolUtils.AppStats(stats_file=STATSFILE, stats_list=ALLSTATS)

        # API thread
        self.jsonApiThread = JsonApiThread()

        # CLI interface
        self.cli = DustCli.DustCli("SolServer", self._clihandle_quit)
        self.cli.registerCommand(
            name='stats',
            alias='s',
            description='print the stats',
            params=[],
            callback=self._clihandle_stats,
        )
Ejemplo n.º 7
0
 def _webhandle_status_GET(self, siteName=None):
     return {
         'version solserver': solserver_version.VERSION,
         'version Sol': list(sol.version()),
         'uptime computer': self._exec_cmd('uptime'),
         'utc': int(time.time()),
         'date': time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()),
         #'last reboot': self._exec_cmd('last reboot'),  # TODO not working anymore
         'stats': SolUtils.AppStats().get()
     }
Ejemplo n.º 8
0
    def _webhandle_o_PUT(self, siteName=None):

        # abort if not  JSON payload
        if bottle.request.json is None:
            return bottle.HTTPResponse(
                body=json.dumps({'error': 'JSON body is required'}),
                status=400,
                headers={'Content-Type': 'application/json'},
            )

        # http->bin
        try:
            sol_binl = sol.http_to_bin(bottle.request.json)
        except:
            return bottle.HTTPResponse(
                body=json.dumps({'error': 'Malformed JSON body contents'}),
                status=400,
                headers={'Content-Type': 'application/json'},
            )

        # bin->json->influxdb format, then write to put database
        sol_influxdbl = []
        for sol_bin in sol_binl:

            # convert bin->json
            sol_json = sol.bin_to_json(sol_bin)

            # convert json->influxdb
            tags = self._get_tags(siteName, sol_json["mac"])
            sol_influxdbl += [sol.json_to_influxdb(sol_json, tags)]

        # write to database
        try:
            self.influxClient.write_points(sol_influxdbl)
        except:
            SolUtils.AppStats().increment('NUM_OBJECTS_DB_FAIL')
            raise
        else:
            SolUtils.AppStats().increment('NUM_OBJECTS_DB_OK')
Ejemplo n.º 9
0
    def _authorizeClient(self, token=None):

        assert token

        siteName = None
        searchAfterReload = False
        while True:
            for site in self.sites:
                if site['token'] == token:
                    siteName = site["name"]
                    break
            if (not siteName) and (searchAfterReload is False):
                with open('solserver.sites', 'r') as f:
                    self.sites = json.load(f)["sites"]
                searchAfterReload = True
            else:
                break

        if not siteName:
            SolUtils.AppStats().increment('JSON_NUM_UNAUTHORIZED')

        return siteName