Ejemplo n.º 1
0
    def stop_daemon(self, host_id):
        """
        Stops a running daemon.

        :param host_id: the hash id of the host
        :type host_id: string
        """
        main_deferred = Deferred()
        host = self.get_host(host_id)
        if not host:
            main_deferred.callback((False, _("Daemon doesn't exist")))
            return main_deferred

        try:
            def on_connect(connected, c):
                if not connected:
                    main_deferred.callback((False, _("Daemon not running")))
                    return
                c.daemon.shutdown()
                main_deferred.callback((True, ))

            def on_connect_failed(reason):
                main_deferred.callback((False, reason))

            host, port, user, password = host[1:5]
            c = Client()
            d = c.connect(host, port, user, password)
            d.addCallback(on_connect, c)
            d.addErrback(on_connect_failed)
        except:
            main_deferred.callback((False, "An error occurred"))
        return main_deferred
Ejemplo n.º 2
0
    def stop_daemon(self, host_id):
        """
        Stops a running daemon.

        :param host_id: the hash id of the host
        :type host_id: string
        """
        main_deferred = Deferred()
        host = self._get_host(host_id)
        if not host:
            main_deferred.callback((False, _('Daemon does not exist')))
            return main_deferred

        try:

            def on_connect(connected, c):
                if not connected:
                    main_deferred.callback((False, _('Daemon not running')))
                    return
                c.daemon.shutdown()
                main_deferred.callback((True, ))

            def on_connect_failed(reason):
                main_deferred.callback((False, reason))

            host, port, user, password = host[1:5]
            c = Client()
            d = c.connect(host, port, user, password)
            d.addCallback(on_connect, c)
            d.addErrback(on_connect_failed)
        except Exception:
            main_deferred.callback((False, 'An error occurred'))
        return main_deferred
Ejemplo n.º 3
0
    def get_host_status(self, host_id):
        """Gets the current status (online/offline) of the host

        Args:
            host_id (str): The host id to check status of.

        Returns:
            tuple: A tuple of strings (host_id, status, version).

        """
        status_offline = (host_id, 'Offline', '')

        def on_connect(result, c, host_id):
            """Successfully connected to a daemon"""
            def on_info(info, c):
                c.disconnect()
                return host_id, 'Online', info

            def on_info_fail(reason, c):
                c.disconnect()
                return status_offline

            return c.daemon.info().addCallback(on_info,
                                               c).addErrback(on_info_fail, c)

        def on_connect_failed(reason, host_id):
            """Connection to daemon failed"""
            log.debug('Host status failed for %s: %s', host_id, reason)
            return status_offline

        try:
            host_id, host, port, user = self.get_host_info(host_id)
        except ValueError:
            log.warning('Problem getting host_id info from hostlist')
            return status_offline

        try:
            ip = gethostbyname(host)
        except gaierror as ex:
            log.error('Error resolving host %s to ip: %s', host, ex.args[1])
            return status_offline

        host_conn_info = (ip, port, 'localclient'
                          if not user and host in LOCALHOST else user)
        if client.connected() and host_conn_info == client.connection_info():
            # Currently connected to host_id daemon.
            def on_info(info, host_id):
                log.debug('Client connected, query info: %s', info)
                return host_id, 'Connected', info

            return client.daemon.info().addCallback(on_info, host_id)
        else:
            # Attempt to connect to daemon with host_id details.
            c = Client()
            d = c.connect(host, port, skip_authentication=True)
            d.addCallback(on_connect, c, host_id)
            d.addErrback(on_connect_failed, host_id)
            return d
Ejemplo n.º 4
0
 def on_button_stop_daemon_clicked(self, clicked):
     host = self.selectedItem()
     if host.is_connected():
         yield client.daemon.shutdown()
     else:
         c = Client()
         yield c.connect(host.host, host.port, host.username, host.password)
         yield c.daemon.shutdown()
     host.update()
Ejemplo n.º 5
0
 def on_button_stop_daemon_clicked(self, clicked):
     host = self.selectedItem()
     if host.is_connected():
         yield client.daemon.shutdown()
     else:
         c = Client()
         yield c.connect(host.host, host.port, host.username, host.password)
         yield c.daemon.shutdown()
     host.update()
Ejemplo n.º 6
0
def started_deluge_client(tmpdir):
    # Set Up
    standalone_client = Client()

    if len(component._ComponentRegistry.components) != 0:
        warnings.warn("The component._ComponentRegistry.components"
                      " is not empty on test setup.\n"
                      "This is probably caused by another test"
                      " that did not clean up after finishing!:"
                      f" {component._ComponentRegistry.components}")

    configmanager.set_config_dir(tmpdir)
    standalone_client.start_standalone()

    pytest_twisted.blockon(component.start())
    yield standalone_client

    # Tear Down
    pytest_twisted.blockon(standalone_client.disconnect())
    pytest_twisted.blockon(component.shutdown())

    # There can be KeyErrors after pytest run about RPCServer
    # This errors are happening because of the next clear()
    #
    # It is so because plugins objects (that are based on CorePluginBase)
    # are still stored inside RPCServer's factory.methods
    # When RPCServer object is destroyed, it's dicts are cleared as well
    # That's when CorePluginBase.__del__ is called
    # And it tries to obtain RPCServer object from a list of components,
    # but this object already excluded from the list, so it fails to
    # deregister itself from RPCServer's dicts.
    #
    # Moreover, calling RPCServer's deregister_object is of no use, because:
    # def deregister_object(self, obj):
    #     """
    #     Deregisters an objects exported rpc methods.
    #
    #     :param obj: the object that was previously registered
    #
    #     """
    # >       for key, value in self.factory.methods.items():
    #             if value.__self__ == obj:
    #                 del self.factory.methods[key]
    # E       RuntimeError: dictionary changed size during iteration

    component._ComponentRegistry.components.clear()
    component._ComponentRegistry.dependents.clear()
Ejemplo n.º 7
0
    def on_button_startdaemon_clicked(self, widget):
        log.debug('on_button_startdaemon_clicked')
        if not self.liststore.iter_n_children(None):
            # There is nothing in the list, so lets create a localhost entry
            try:
                self.hostlist.add_default_host()
            except ValueError as ex:
                log.error('Error adding default host: %s', ex)
            else:
                self.start_daemon(DEFAULT_PORT, get_config_dir())
            finally:
                return

        paths = self.treeview.get_selection().get_selected_rows()[1]
        if len(paths):
            __, host, port, user, password, status, __ = self.liststore[
                paths[0]]
        else:
            return

        if host not in LOCALHOST:
            return

        def on_daemon_status_change(result):
            """Daemon start/stop callback"""
            reactor.callLater(0.7, self._update_host_status)

        if status in ('Online', 'Connected'):
            # Button will stop the daemon if status is online or connected.
            def on_connect(d, c):
                """Client callback to call daemon shutdown"""
                c.daemon.shutdown().addCallback(on_daemon_status_change)

            if client.connected() and (host, port,
                                       user) == client.connection_info():
                client.daemon.shutdown().addCallback(on_daemon_status_change)
            elif user and password:
                c = Client()
                c.connect(host, port, user,
                          password).addCallback(on_connect, c)
        else:
            # Otherwise button will start the daemon.
            self.start_daemon(port, get_config_dir())
Ejemplo n.º 8
0
 def update(self):
     self.setText(0, u"%s@%s:%d" % (self.username, self.host, self.port))
     if self.is_connected():
         self.version = yield client.daemon.info()
         self.setIcon(0, self._icon_connected)
         self.setText(1, self.version)
     else:
         c = Client()
         try:
             yield c.connect(self.host, self.port, self.username, self.password)
             self.version = yield c.daemon.info()
             self.setIcon(0, self._icon_alive)
         except Exception:
             log.debug("Connection failed", exc_info=True)
             self.version = ""
             self.setIcon(0, self._icon_dead)
         finally:
             if c.connected():
                 c.disconnect()
         self.setText(1, self.version)
Ejemplo n.º 9
0
    def on_button_startdaemon_clicked(self, widget):
        log.debug('on_button_startdaemon_clicked')
        if not self.liststore.iter_n_children(None):
            # There is nothing in the list, so lets create a localhost entry
            try:
                self.hostlist.add_default_host()
            except ValueError as ex:
                log.error('Error adding default host: %s', ex)
            else:
                self.start_daemon(DEFAULT_PORT, get_config_dir())
            finally:
                return

        paths = self.treeview.get_selection().get_selected_rows()[1]
        if len(paths):
            __, host, port, user, password, status, __ = self.liststore[paths[0]]
        else:
            return

        if host not in LOCALHOST:
            return

        def on_daemon_status_change(result):
            """Daemon start/stop callback"""
            reactor.callLater(0.7, self._update_host_status)

        if status in ('Online', 'Connected'):
            # Button will stop the daemon if status is online or connected.
            def on_connect(d, c):
                """Client callback to call daemon shutdown"""
                c.daemon.shutdown().addCallback(on_daemon_status_change)

            if client.connected() and (host, port, user) == client.connection_info():
                client.daemon.shutdown().addCallback(on_daemon_status_change)
            elif user and password:
                c = Client()
                c.connect(host, port, user, password).addCallback(on_connect, c)
        else:
            # Otherwise button will start the daemon.
            self.start_daemon(port, get_config_dir())
Ejemplo n.º 10
0
 def update(self):
     self.setText(0, u"%s@%s:%d" % (self.username, self.host, self.port))
     if self.is_connected():
         self.version = yield client.daemon.info()
         self.setIcon(0, self._icon_connected)
         self.setText(1, self.version)
     else:
         c = Client()
         try:
             yield c.connect(self.host, self.port, self.username,
                             self.password)
             self.version = yield c.daemon.info()
             self.setIcon(0, self._icon_alive)
         except Exception:
             log.debug("Connection failed", exc_info=True)
             self.version = ""
             self.setIcon(0, self._icon_dead)
         finally:
             if c.connected():
                 c.disconnect()
         self.setText(1, self.version)
Ejemplo n.º 11
0
            return c.daemon.info(
                ).addCallback(on_info, c
                ).addErrback(on_info_fail, c)

        def on_connect_failed(reason, host_id):
            return response(_("Offline"))

        if client.connected() and (host, port, "localclient" if not
                                   user and host in ("127.0.0.1", "localhost") else
                                   user)  == client.connection_info():
            def on_info(info):
                return response(_("Connected"), info)

            return client.daemon.info().addCallback(on_info)
        else:
            c = Client()
            return c.connect(host, port, user, password
                ).addCallback(on_connect, c, host_id
                ).addErrback(on_connect_failed, host_id)

    @export(AUTH_LEVEL_ADMIN)
    def start_daemon(self, port):
        """
    Starts a local daemon.
    """
        client.start_daemon(port, get_config_dir())

    @export(AUTH_LEVEL_ADMIN)
    def stop_daemon(self, host_id):
        """
        Stops a running daemon.
Ejemplo n.º 12
0
            return c.daemon.info().addCallback(on_info,
                                               c).addErrback(on_info_fail, c)

        def on_connect_failed(reason, host_id):
            return response(_("Offline"))

        if client.connected() and (host, port, "localclient" if not user
                                   and host in ("127.0.0.1", "localhost") else
                                   user) == client.connection_info():

            def on_info(info):
                return response(_("Connected"), info)

            return client.daemon.info().addCallback(on_info)
        else:
            c = Client()
            return c.connect(host, port, user, password).addCallback(
                on_connect, c, host_id).addErrback(on_connect_failed, host_id)

    @export
    def start_daemon(self, port):
        """
    Starts a local daemon.
    """
        client.start_daemon(port, get_config_dir())

    @export
    def stop_daemon(self, host_id):
        """
        Stops a running daemon.