Exemplo n.º 1
0
    def _egg_path(self, egg_name):
        log.debug("_egg_path")
        if not CACHE_DIR:
            raise ServerError("CACHE_DIR is %s'%s'" % (type(CACHE_DIR), CACHE_DIR))
        elif not egg_name:
            raise ServerError("egg_name for path is %s'%s'" % (type(egg_name), egg_name))

        return CACHE_DIR + "/" + egg_name
Exemplo n.º 2
0
    def _egg_remotepath(self, egg_name):
        log.debug("_egg_remotepath" + str(egg_name))
        if not REPO_BASE:
            raise ServerError("REPO_BASE is %s'%s'" % (type(REPO_BASE), REPO_BASE))
        elif not egg_name:
            raise ServerError("egg_name for remotepath is %s'%s'" % (type(egg_name), egg_name))

        return REPO_BASE + "/" + egg_name
 def _check_response(response):
     validate_is_instance(response,dict,"Malformed response from ElasticSearch (%s)" % response, ServerError)
     if response.has_key('error'):
         raise ServerError("ElasticSearch error: %s" % response['error'])
     if response.has_key('ok') and not response['ok']:
         # Cant determine a better exception to throw, add a new one perhapse?
         raise NotFound("ElasticSearch Response: %s" % response)
Exemplo n.º 4
0
    def _make_management_call(self, url, method="get", data=None):
        """
        Makes a call to the Rabbit HTTP management API using the passed in HTTP method.
        """
        log.debug("Calling rabbit API management (%s): %s", method, url)

        meth = getattr(requests, method)

        try:
            from putil.rabbitmq.rabbit_util import RabbitManagementUtil
            mgmt_cfg = RabbitManagementUtil.get_mgmt_config(CFG)

            with gevent.timeout.Timeout(10):
                r = meth(url,
                         auth=(mgmt_cfg["username"], mgmt_cfg["password"]),
                         data=data)
            r.raise_for_status()

            if not r.content == "":
                content = json.loads(r.content)
            else:
                content = None

        except gevent.timeout.Timeout as ex:
            raise Timeout(str(ex))
        except requests.exceptions.Timeout as ex:
            raise Timeout(str(ex))
        except (requests.exceptions.ConnectionError, socket.error) as ex:
            raise ServiceUnavailable(str(ex))
        except requests.exceptions.RequestException as ex:
            # the generic base exception all requests' exceptions inherit from, raise our
            # general server error too.
            raise ServerError(str(ex))

        return content
Exemplo n.º 5
0
 def rr_call(*args, **kwargs):
     try:
         return fn(*args, **kwargs)
     except TypeError as ex:
         # HACK HACK
         # catch error in couchdb client, log and reraise as ServerError
         log.exception("CouchDB access error")
         raise ServerError(ex)
Exemplo n.º 6
0
    def __init__(self, input_callback=None, ip_address=None):
        log.debug("TcpServer.__init__(): IP address = %s" % ip_address)

        # save callback if specified
        if not input_callback:
            log.warning("TcpServer.__init__(): callback not specified")
            raise ServerError("TcpServer.__init__(): callback not specified")
        self.parent_input_callback = input_callback

        # save ip address if specified
        if not ip_address:
            log.warning("TcpServer.__init__(): IP address not specified")
            raise ServerError("TcpServer.__init__(): IP address not specified")
        self.ip_address = ip_address

        # search for available port
        self.port = self.PORT_RANGE_LOWER
        # create a TCP socket
        self.server_socket = gevent.socket.socket()
        self.server_socket.allow_reuse_address = True
        while True:
            try:
                log.debug("trying to bind to port %s on %s" %
                          (str(self.port), self.ip_address))
                self.server_socket.bind((self.ip_address, self.port))
                break
            except Exception as ex:
                log.debug("exception caught for socket bind:" + str(ex))
                self.port = self.port + 1
                if self.port > self.PORT_RANGE_UPPER:
                    log.warning(
                        "TcpServer.__init__(): no available ports for server")
                    raise ServerError(
                        "TcpServer.__init__(): no available ports")
                    return

        # create token
        self.token = str(uuid.uuid4()).upper()

        log.debug("TcpServer.__init__(): starting server greenlet")
        self.server = gevent.spawn(self._server_greenlet)
Exemplo n.º 7
0
    def _get_dsa_client(self, instrument_device, dsa_instance):
        """
        Launch the agent and return a client
        """
        fake_process = FakeProcess()
        fake_process.container = self.container

        clients = DataAcquisitionManagementServiceDependentClients(
            fake_process)
        config_builder = ExternalDatasetAgentConfigurationBuilder(clients)

        try:
            config_builder.set_agent_instance_object(dsa_instance)
            self.agent_config = config_builder.prepare()
        except Exception as e:
            log.error('failed to launch: %s', e, exc_info=True)
            raise ServerError('failed to launch')

        self._dsa_pid = self.dams.start_external_dataset_agent_instance(
            dsa_instance._id)
        log.debug("_get_dsa_client CFG")
        return ResourceAgentClient(instrument_device._id,
                                   process=FakeProcess())

        dispatcher = ProcessDispatcherServiceClient()
        launcher = AgentLauncher(dispatcher)

        log.debug("Launching agent process!")

        self._dsa_pid = launcher.launch(
            self.agent_config,
            config_builder._get_process_definition()._id)
        if not self._dsa_pid:
            raise ServerError(
                "Launched external dataset agent instance but no process_id")
        config_builder.record_launch_parameters(self.agent_config)

        launcher.await_launch(10.0)
        return ResourceAgentClient(instrument_device._id,
                                   process=FakeProcess())
Exemplo n.º 8
0
    def add_endpoint(self, endpoint):
        """
        Adds a managed listening endpoint to this service/process.

        The service/process must be running inside of an IonProcessThread, or this
        method will raise an error.

        A managed listening endpoint will report failures up to the process, then to
        the container's process manager.
        """
        if self._process is None:
            raise ServerError("No attached IonProcessThread")

        self._process.add_endpoint(endpoint)
Exemplo n.º 9
0
    def remove_endpoint(self, endpoint):
        """
        Removes an endpoint from being managed by this service/process.

        The service/process must be running inside of an IonProcessThread, or this
        method will raise an error. It will also raise an error if the endpoint is
        not currently managed.

        Errors raised in the endpoint will no longer be reported to the process or
        process manager.
        """
        if self._process is None:
            raise ServerError("No attached IonProcessThread")

        self._process.remove_endpoint(endpoint)
    def __init__(self,
                 direct_access_type=None,
                 input_callback=None,
                 ip_address=None,
                 session_timeout=None,
                 inactivity_timeout=None):
        log.debug("DirectAccessServer.__init__()")

        if not direct_access_type:
            log.warning(
                "DirectAccessServer.__init__(): direct access type not specified"
            )
            raise ServerError(
                "DirectAccessServer.__init__(): direct access type not specified"
            )

        if not input_callback:
            log.warning(
                "DirectAccessServer.__init__(): callback not specified")
            raise ServerError(
                "DirectAccessServer.__init__(): callback not specified")

        if not ip_address:
            log.warning(
                "DirectAccessServer.__init__(): IP address not specified")
            raise ServerError(
                "DirectAccessServer.__init__(): IP address not specified")

        if not session_timeout:
            log.warning(
                "DirectAccessServer.__init__(): session timeout not specified")
            raise ServerError(
                "DirectAccessServer.__init__(): session timeout not specified")

        if not inactivity_timeout:
            log.warning(
                "DirectAccessServer.__init__(): inactivity timeout not specified"
            )
            raise ServerError(
                "DirectAccessServer.__init__(): inactivity timeout not specified"
            )

        # start the correct server based on direct_access_type
        if direct_access_type == DirectAccessTypes.telnet:
            self.server = TelnetServer(input_callback, ip_address)
        elif direct_access_type == DirectAccessTypes.vsp:
            self.server = SerialServer(input_callback, ip_address)
        else:
            raise ServerError(
                "DirectAccessServer.__init__(): Unsupported direct access type"
            )

        log.debug("DirectAccessServer.__init__(): starting timer greenlet")
        self.timer = gevent.spawn(self._timer_greenlet,
                                  session_timeout=session_timeout,
                                  inactivity_timeout=inactivity_timeout)
Exemplo n.º 11
0
 def _perform(self, op, store, *args, **kwargs):
     """ re-invoke the calling operation with a pooled DB connection """
     pool = self._pool[store]
     connection = pool.check_out()
     if not connection:
         raise ServerError('failed to get a connection for store: ' + store)
     try:
         return op(store, *args, _connection=connection, **kwargs)
     except IonException:
         raise
     except:
         # should have been rethrown as application exception by store, find and fix these occurrences...
         log.error('unexpected exception from datastore', exc_info=True)
         raise
     finally:
         pool.check_in(connection)
Exemplo n.º 12
0
        def command_loop():
            while True:
                try:
                    cmd = self._queue.pop(0)

                except IndexError:
                    # No command available, sleep for a while.
                    gevent.sleep(.1)
                    continue

                if self._id == 'fake_id':
                    log.debug('Processing fake command.')
                    worktime = cmd.kwargs.get('worktime', None)
                    if worktime:
                        worktime = random.uniform(0, worktime)
                        gevent.sleep(worktime)
                    payload = cmd.kwargs.get('payload', None)
                    result = payload or 'fake_result'
                else:
                    cmdstr = cmd.command
                    args = cmd.args
                    kwargs = cmd.kwargs

                    try:
                        log.debug('Remote endpoint attempting command: %s',
                                  cmdstr)
                        func = getattr(self._client, cmdstr)
                        result = func(*args, **kwargs)
                        log.debug('Remote endpoint command %s got result %s',
                                  cmdstr, str(result))

                    except AttributeError, TypeError:
                        # The command does not exist.
                        errstr = 'Unable to call remote command %s.' % cmdstr
                        log.error(errstr)
                        result = BadRequest(errstr)

                    except IonException as ex:
                        # populate result with error.
                        log.error(str(ex))
                        result = ex

                    except Exception as ex:
                        # populate result with error.
                        log.error(str(ex))
                        result = ServerError(str(ex))
Exemplo n.º 13
0
    def _make_management_call(self,
                              url,
                              use_ems=True,
                              method="get",
                              data=None):
        """
        Makes a call to the Rabbit HTTP management API using the passed in HTTP method.
        """
        log.debug("Calling rabbit API management (%s): %s", method, url)

        if use_ems and self._ems_available():
            log.debug("Directing call to EMS")
            content = self._ems_client.call_management(
                url, method, headers=self._build_security_headers())
        else:
            meth = getattr(requests, method)

            try:
                username = CFG.get_safe(
                    "container.exchange.management.username", "guest")
                password = CFG.get_safe(
                    "container.exchange.management.password", "guest")

                with gevent.timeout.Timeout(10):
                    r = meth(url, auth=(username, password), data=data)
                r.raise_for_status()

                if not r.content == "":
                    content = json.loads(r.content)
                else:
                    content = None

            except gevent.timeout.Timeout as ex:
                raise Timeout(str(ex))
            except requests.exceptions.Timeout as ex:
                raise Timeout(str(ex))
            except (requests.exceptions.ConnectionError, socket.error) as ex:
                raise ServiceUnavailable(str(ex))
            except requests.exceptions.RequestException as ex:
                # the generic base exception all requests' exceptions inherit from, raise our
                # general server error too.
                raise ServerError(str(ex))

        return content
Exemplo n.º 14
0
    def message_received(self, msg, headers):
        assert self._routing_obj, "How did I get created without a routing object?"

        log.debug(
            "RPCResponseEndpointUnit.message_received\n\tmsg: %s\n\theaders: %s",
            msg, headers)

        cmd_arg_obj = msg
        cmd_op = headers.get('op', None)

        # transform cmd_arg_obj into a dict
        if hasattr(cmd_arg_obj, '__dict__'):
            cmd_arg_obj = cmd_arg_obj.__dict__
        elif isinstance(cmd_arg_obj, dict):
            pass
        else:
            raise BadRequest(
                "Unknown message type, cannot convert into kwarg dict: %s" %
                str(type(cmd_arg_obj)))

        # op name must exist!
        if not hasattr(self._routing_obj, cmd_op):
            raise BadRequest("Unknown op name: %s" % cmd_op)

        ro_meth = getattr(self._routing_obj, cmd_op)

        result = None
        response_headers = {}
        try:
            ######
            ###### THIS IS WHERE THE SERVICE OPERATION IS CALLED ######
            ######
            result = self._make_routing_call(ro_meth, cmd_arg_obj)
            response_headers = {'status_code': 200, 'error_message': ''}
            ######

        except TypeError as ex:
            log.exception(
                "TypeError while attempting to call routing object's method")
            response_headers = self._create_error_response(
                ServerError(ex.message))

        return result, response_headers
Exemplo n.º 15
0
 def _get_port_from_file(self, filename):
     """
     Read the driver port from a status file.  The driver process writes two status files containing the port
     number for events and commands.
     @param filename path to the port file
     @return port port number read from the file
     @raise ServerError if file not read w/in 10sec
     """
     maxWait=10          # try for up to 10sec
     waitInterval=0.5    # repeating every 1/2 sec
     log.debug("about to read port from file %s" % filename)
     for n in xrange(int(maxWait/waitInterval)):
         try:
             with open(filename, 'r') as f:
                 port = int(f.read().strip())
                 log.debug("read port %d from file %s" % (port, filename))
                 return port
         except: pass
         time.sleep(waitInterval)
     raise ServerError('process PID file was not found: ' + filename)
Exemplo n.º 16
0
 def check_out(self):
     """ check a connection out of the pool or create one if necessary """
     self._lock.acquire()
     try:
         if self._close:
             raise ServerError('system shutting down')
         if len(self._unused):
             out = self._unused[0]
             del self._unused[0]
         else:
             count = len(self._used)
             if count>=self._max:
                 raise BadRequest('already have max connections for ' + self._name)
             elif count>self._expected:
                 log.warn('exceeding expected number of DB connections for ' + self._name + ': ' + str(count))
             out = self._create_object(self._name)
         self._used.append(out)
         return out
     finally:
         self._lock.release()
Exemplo n.º 17
0
 def _raise_exception(self, code, message):
     if str(code) in exception_map:
         raise exception_map[str(code)](message)
     else:
         log.debug("Raising ServerError")
         raise ServerError(message)
Exemplo n.º 18
0
 def _wrap_error(self, op, id, ex):
     return ServerError('CouchDB %s of object %s failed with error %s(%s)' %
                        (op, id, type(ex).__name__, str(ex)))