예제 #1
0
파일: util.py 프로젝트: Natim/circus-web
        def client_or_redirect(*fargs, **fkwargs):
            if ensure_client:
                client = get_client()
                session = get_session()

                if client is None:
                    session = get_session()
                    if session.get('endpoint', None) is not None:
                        # XXX we need to pass SSH too here
                        connect_to_circus(session['endpoint'])
                    else:
                        return redirect('/connect')

            return func(*fargs, **fkwargs)
예제 #2
0
        def client_or_redirect(*fargs, **fkwargs):
            if ensure_client:
                client = get_client()
                session = get_session()

                if client is None:
                    session = get_session()
                    if session.get('endpoint', None) is not None:
                        # XXX we need to pass SSH too here
                        connect_to_circus(session['endpoint'])
                    else:
                        return redirect(url('connect'))

            return func(*fargs, **fkwargs)
예제 #3
0
파일: util.py 프로젝트: Natim/circus-web
def render_template(template, **data):
    """Finds the given template and renders it with the given data.

    Also adds some data that can be useful to the template, even if not
    explicitely asked so.

    :param template: the template to render
    :param **data: the kwargs that will be passed when rendering the template
    """
    tmpl = TMPLS.get_template(template)
    client = get_client()

    # send the last message stored in the session in addition, in the "message"
    # attribute.
    server = '%s://%s' % (request.urlparts.scheme, request.urlparts.netloc)

    return tmpl.render(client=client, version=__version__,
                       session=get_session(), SERVER=server, **data)
예제 #4
0
파일: util.py 프로젝트: Natim/circus-web
def run_command(func, message, redirect_url, redirect_on_error=None,
                args=None, kwargs=None):

    func = getattr(get_client(), func)

    if redirect_on_error is None:
        redirect_on_error = redirect_url
    args = args or ()
    kwargs = kwargs or {}

    try:
        logger.debug('Running %r' % func)
        res = func(*args, **kwargs)
        logger.debug('Result : %r' % res)

        if res['status'] != 'ok':
            message = "An error happened: %s" % res['reason']
    except CallError, e:
        message = "An error happened: %s" % e
        redirect_url = redirect_on_error
예제 #5
0
def render_template(template, **data):
    """Finds the given template and renders it with the given data.

    Also adds some data that can be useful to the template, even if not
    explicitely asked so.

    :param template: the template to render
    :param **data: the kwargs that will be passed when rendering the template
    """
    tmpl = TMPLS.get_template(template)
    client = get_client()

    # send the last message stored in the session in addition, in the "message"
    # attribute.
    server = '%s://%s' % (request.urlparts.scheme, request.urlparts.netloc)

    return tmpl.render(client=client,
                       version=__version__,
                       session=get_session(),
                       SERVER=server,
                       **data)
예제 #6
0
def run_command(func,
                message,
                redirect_url,
                redirect_on_error=None,
                args=None,
                kwargs=None):

    func = getattr(get_client(), func)

    if redirect_on_error is None:
        redirect_on_error = redirect_url
    args = args or ()
    kwargs = kwargs or {}

    try:
        logger.debug('Running %r' % func)
        res = func(*args, **kwargs)
        logger.debug('Result : %r' % res)

        if res['status'] != 'ok':
            message = "An error happened: %s" % res['reason']
    except CallError, e:
        message = "An error happened: %s" % e
        redirect_url = redirect_on_error
예제 #7
0
    def on_get_stats(self, msg):
        """This method is the one way to start a conversation with the socket.
        When sending a message here, the parameters are packt into the msg
        dictionary, which contains:

            - "streams", a list of streams that the client want to be notified
              about.
            - "get_processes", if it wants to include the subprocesses managed
              by this watcher or not (optional, defaults to False)

        The server sends back to the client some messages, on different
        channels:

            - "stats-<watchername>" sends memory and cpu info for the
              aggregation of stats.
            - stats-<watchername>-pids sends the list of pids for this watcher
            - "stats-<watchername>-<pid>" sends the information about
              specific pids for the different watchers (works only if
              "get_processes" is set to True when calling this method)
            - "socket-stats" send the aggregation information about
               sockets.
            - "socket-stats-<fd>" sends information about a particular fd.
        """

        # unpack the params
        streams = msg.get('watchers', [])
        streams_with_pids = msg.get('watchersWithPids', [])

        # if we want to supervise the processes of a watcher, then send the
        # list of pids trough a socket. If we asked about sockets, do the same
        # with their fds
        client = get_client()
        for watcher in streams_with_pids:
            if watcher == "sockets":
                fds = [s['fd'] for s in client.get_sockets()]
                self.send_data('socket-stats-fds', fds=fds)
            else:
                pids = [int(pid) for pid in client.get_pids(watcher)]
                channel = 'stats-{watcher}-pids'.format(watcher=watcher)
                self.send_data(channel, pids=pids)

        # Get the channels that are interesting to us and send back information
        # there when we got them.
        stats = StatsClient(endpoint=client.stats_endpoint)
        for watcher, pid, stat in stats:
            if not self._running:
                return

            if watcher == 'sockets':
                # if we get information about sockets and we explicitely
                # requested them, send back the information.
                if 'sockets' in streams_with_pids and 'fd' in stat:
                    self.send_data('socket-stats-{fd}'.format(fd=stat['fd']),
                                   **stat)
                elif 'sockets' in streams and 'addresses' in stat:
                    self.send_data('socket-stats', reads=stat['reads'],
                                   adresses=stat['addresses'])
            else:
                available_watchers = streams + streams_with_pids + ['circus']
                # these are not sockets but normal watchers
                if watcher in available_watchers:
                    if (watcher == 'circus'
                            and stat.get('name', None) in available_watchers):
                        self.send_data(
                            'stats-{watcher}'.format(watcher=stat['name']),
                            mem=stat['mem'], cpu=stat['cpu'], age=stat['age'])
                    else:
                        if pid is None:  # means that it's the aggregation
                            self.send_data(
                                'stats-{watcher}'.format(watcher=watcher),
                                mem=stat['mem'], cpu=stat['cpu'],
                                age=stat['age'])
                        else:
                            if watcher in streams_with_pids:
                                self.send_data(
                                    'stats-{watcher}-{pid}'.format(
                                        watcher=watcher, pid=pid),
                                    mem=stat['mem'],
                                    cpu=stat['cpu'],
                                    age=stat['age'])
예제 #8
0
    def on_get_stats(self, msg):
        """This method is the one way to start a conversation with the socket.
        When sending a message here, the parameters are packt into the msg
        dictionary, which contains:

            - "streams", a list of streams that the client want to be notified
              about.
            - "get_processes", if it wants to include the subprocesses managed
              by this watcher or not (optional, defaults to False)

        The server sends back to the client some messages, on different
        channels:

            - "stats-<watchername>" sends memory and cpu info for the
              aggregation of stats.
            - stats-<watchername>-pids sends the list of pids for this watcher
            - "stats-<watchername>-<pid>" sends the information about
              specific pids for the different watchers (works only if
              "get_processes" is set to True when calling this method)
            - "socket-stats" send the aggregation information about
               sockets.
            - "socket-stats-<fd>" sends information about a particular fd.
        """

        # unpack the params
        streams = msg.get('watchers', [])
        streams_with_pids = msg.get('watchersWithPids', [])

        # if we want to supervise the processes of a watcher, then send the
        # list of pids trough a socket. If we asked about sockets, do the same
        # with their fds
        client = get_client()
        for watcher in streams_with_pids:
            if watcher == "sockets":
                fds = [s['fd'] for s in client.get_sockets()]
                self.send_data('socket-stats-fds', fds=fds)
            else:
                pids = [int(pid) for pid in client.get_pids(watcher)]
                channel = 'stats-{watcher}-pids'.format(watcher=watcher)
                self.send_data(channel, pids=pids)

        # Get the channels that are interesting to us and send back information
        # there when we got them.
        stats = StatsClient(endpoint=client.stats_endpoint)
        for watcher, pid, stat in stats:
            if not self._running:
                return

            if watcher == 'sockets':
                # if we get information about sockets and we explicitely
                # requested them, send back the information.
                if 'sockets' in streams_with_pids and 'fd' in stat:
                    self.send_data('socket-stats-{fd}'.format(fd=stat['fd']),
                                   **stat)
                elif 'sockets' in streams and 'addresses' in stat:
                    self.send_data('socket-stats',
                                   reads=stat['reads'],
                                   adresses=stat['addresses'])
            else:
                available_watchers = streams + streams_with_pids + ['circus']
                # these are not sockets but normal watchers
                if watcher in available_watchers:
                    if (watcher == 'circus'
                            and stat.get('name', None) in available_watchers):
                        self.send_data(
                            'stats-{watcher}'.format(watcher=stat['name']),
                            mem=stat['mem'],
                            cpu=stat['cpu'],
                            age=stat['age'])
                    else:
                        if pid is None:  # means that it's the aggregation
                            self.send_data(
                                'stats-{watcher}'.format(watcher=watcher),
                                mem=stat['mem'],
                                cpu=stat['cpu'],
                                age=stat['age'])
                        else:
                            if watcher in streams_with_pids:
                                self.send_data('stats-{watcher}-{pid}'.format(
                                    watcher=watcher, pid=pid),
                                               mem=stat['mem'],
                                               cpu=stat['cpu'],
                                               age=stat['age'])