sshspawner/sshspawner.py
def load_state(self, state):
        """Restore state about spawned single-user server after a hub restart.

        Local processes only need the process id.
        """
        super(SSHSpawner, self).load_state(state)
        if state == {}:
            return

        self.pid = state.get("pid", 0)
        self.ssh_target = state.get("ssh_target", "")
        self.port = state.get("port", "")
        cmd = self.cmd[self.ssh_target]

        proc_found = False

        if self.pid == 0:
            self.log.debug("Stored PID is 0")

        for p in psutil.process_iter():
            if (
                f"{self.port}:127.0.0.1:{self.port}" in p.cmdline()
                and p.username() == self.user.name
            ):
                self.log.debug(
                    f"Found existing tunnel with correct port ({self.port}) and user ({self.user.name})"
                )
                proc_found = True
                if p.pid != self.pid and self.pid > 0:
                    self.log.warning(
                        "PID of found process is NOT the same as... updating"
                    )
                    self.pid = p.pid
                break

        if not proc_found:
            self.log.info(
                f"Tunnel was not found. Opening new tunnel on port: {self.port} to {self.ssh_target} for {self.user.name}"
            )
            opts = self.ssh_opts(
                persist=self.ssh_control_persist_time, known_hosts=self.known_hosts
            )

            check_server_exists = self.spawn_as_user(
                f"ssh {opts} {self.ssh_target} \"ps x | grep '{cmd[0]} --port={self.port}' | wc -l\" 2>/dev/null",
                timeout=None,
            )

            check_server_exists.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=15)
            self.log.debug(check_server_exists.before)
            try:
                if int(check_server_exists.before.split()[-1]) <= 2:
                    self.log.error(
                        f"Existing server on host {self.ssh_target}:{self.port} does not exist remotely for user {self.user.name}!"
                    )
                else:
                    self._started = True

                    self.resource_path = os.path.join(self.resource_path, self.ssh_target)

                    start_tunnel_child = self.spawn_as_user(
                        f"ssh {opts} -L {self.port}:127.0.0.1:{self.port} {self.ssh_target}",
                        timeout=None,
                    )
                    self.proc = start_tunnel_child.proc
                    self.pid = self.proc.pid
            except ValueError as e:
                self.log.error(
                    f"""Could not determine if server exists. ps x output: 
                    {check_server_exists.before}
                    Error: {e}
                    """
                )
Restore state about spawned single-user server after a hub restart.

        Local processes only need the process id.
        
Restore state about spawned single-user server after a hub restart.
Local processes only need the process id.
Restore state about spawned single - user server after a hub restart . Local processes only need the process id .
def load_state(self, state):
        super(SSHSpawner, self).load_state(state)
        if state == {}:
            return
        self.pid = state.get("pid", 0)
        self.ssh_target = state.get("ssh_target", "")
        self.port = state.get("port", "")
        cmd = self.cmd[self.ssh_target]
        proc_found = False
        if self.pid == 0:
            self.log.debug("Stored PID is 0")
        for p in psutil.process_iter():
            if (
                f"{self.port}:127.0.0.1:{self.port}" in p.cmdline()
                and p.username() == self.user.name
            ):
                self.log.debug(
                    f"Found existing tunnel with correct port ({self.port}) and user ({self.user.name})"
                )
                proc_found = True
                if p.pid != self.pid and self.pid > 0:
                    self.log.warning(
                        "PID of found process is NOT the same as... updating"
                    )
                    self.pid = p.pid
                break
        if not proc_found:
            self.log.info(
                f"Tunnel was not found. Opening new tunnel on port: {self.port} to {self.ssh_target} for {self.user.name}"
            )
            opts = self.ssh_opts(
                persist=self.ssh_control_persist_time, known_hosts=self.known_hosts
            )
            check_server_exists = self.spawn_as_user(
                f"ssh {opts} {self.ssh_target} \"ps x | grep '{cmd[0]} --port={self.port}' | wc -l\" 2>/dev/null",
                timeout=None,
            )
            check_server_exists.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=15)
            self.log.debug(check_server_exists.before)
            try:
                if int(check_server_exists.before.split()[-1]) <= 2:
                    self.log.error(
                        f"Existing server on host {self.ssh_target}:{self.port} does not exist remotely for user {self.user.name}!"
                    )
                else:
                    self._started = True
                    self.resource_path = os.path.join(self.resource_path, self.ssh_target)
                    start_tunnel_child = self.spawn_as_user(
                        f"ssh {opts} -L {self.port}:127.0.0.1:{self.port} {self.ssh_target}",
                        timeout=None,
                    )
                    self.proc = start_tunnel_child.proc
                    self.pid = self.proc.pid
            except ValueError as e:
                self.log.error(
                    f"""Could not determine if server exists. ps x output: 
                    {check_server_exists.before}
                    Error: {e}
                    """
                )
def load_state ( self , state ) : super ( SSHSpawner , self ) . load_state ( state ) if state == { } : return self . pid = state . get ( "pid" , 0 ) self . ssh_target = state . get ( "ssh_target" , "" ) self . port = state . get ( "port" , "" ) cmd = self . cmd [ self . ssh_target ] proc_found = False if self . pid == 0 : self . log . debug ( "Stored PID is 0" ) for p in psutil . process_iter ( ) : if ( f"{self.port}:127.0.0.1:{self.port}" in p . cmdline ( ) and p . username ( ) == self . user . name ) : self . log . debug ( f"Found existing tunnel with correct port ({self.port}) and user ({self.user.name})" ) proc_found = True if p . pid != self . pid and self . pid > 0 : self . log . warning ( "PID of found process is NOT the same as... updating" ) self . pid = p . pid break if not proc_found : self . log . info ( f"Tunnel was not found. Opening new tunnel on port: {self.port} to {self.ssh_target} for {self.user.name}" ) opts = self . ssh_opts ( persist = self . ssh_control_persist_time , known_hosts = self . known_hosts ) check_server_exists = self . spawn_as_user ( f"ssh {opts} {self.ssh_target} \"ps x | grep '{cmd[0]} --port={self.port}' | wc -l\" 2>/dev/null" , timeout = None , ) check_server_exists . expect ( [ pexpect . EOF , pexpect . TIMEOUT ] , timeout = 15 ) self . log . debug ( check_server_exists . before ) try : if int ( check_server_exists . before . split ( ) [ - 1 ] ) <= 2 : self . log . error ( f"Existing server on host {self.ssh_target}:{self.port} does not exist remotely for user {self.user.name}!" ) else : self . _started = True self . resource_path = os . path . join ( self . resource_path , self . ssh_target ) start_tunnel_child = self . spawn_as_user ( f"ssh {opts} -L {self.port}:127.0.0.1:{self.port} {self.ssh_target}" , timeout = None , ) self . proc = start_tunnel_child . proc self . pid = self . proc . pid except ValueError as e : self . log . error ( f"""Could not determine if server exists. ps x output: 
                    {check_server_exists.before}
                    Error: {e}
                    """ )
Restore state about spawned single-user server after a hub restart.
Restore state about spawned single - user server after a hub restart .
"""Restore state about spawned single-user server after a hub restart.

        Local processes only need the process id.
        """
self
state