示例#1
0
    def end(self):

        sys.stdout.flush()  ## needed ?
        sys.stdin.flush()  ## needed ?

        import logging
        verbose = logging.root.manager.disable <= logging.DEBUG
        del logging

        if self.session: self.session.verbose = verbose
        if self.tunnel: self.tunnel.verbose = verbose

        if self.__pid:
            try:
                if self.session:  # and verbose :
                    command = 'kill -s SIGUSR1 %d ' % self.__pid
                    self.session(command=command)
                    self.session.launch()
                    r = self.session.response()
                    if r:
                        logger.info('%s : %s : %s' %
                                    (self.remote_host, command, r))

                logger.debug('Kill remote process %s:%s' %
                             (self.remote_host, self.__pid))
                PC.kill(self.__pid, self.remote_host)
            except OSError:
                pass
        self.__pid = None

        ## remove unnesessary files
        if self.session:
            commands = []
            if self.__environment:
                commands.append('rm -f %s' % self.__environment)
            if self.__script: commands.append('rm -f %s' % self.__script)
            if self.__tmpdir:
                commands.append('rmdir --ignore-fail-on-non-empty %s' %
                                self.__tmpdir)
            if commands:
                command = ' ; '.join(commands)
                self.session(command=command)
                self.session.launch()
                r = self.session.response()
                if r: logger.verbose("Response: %s" % r)

        if self.session:
            logger.debug('Kill session      %s' % self.session)
            self.session.kill()

        if self.tunnel:
            logger.debug('Disconnect tunnel %s' % self.tunnel)
            self.tunnel.disconnect()

        self.__tunnel = None
        self.__session = None
示例#2
0
    def __exit__(self):
        """Context manager:  EXIT
        - kill server and SSH session
        - disconnect SSH tunnel
        """

        if self.pid:
            import pathos.core as PC
            PC.kill(self.pid, self.hostname)
            self.pid = None
        if self.session: self.session.kill()
        if self.tunnel: self.tunnel.disconnect()
        if self.input: self.input.close()
        self.session = None
        self.tunnel = None
        self.input = None
示例#3
0
    def end   ( self ) :

        sys.stdout.flush () ## needed ?
        sys.stdin .flush () ## needed ? 
        
        ## import logging
        ## verbose = logging.root.manager.disable <= logging.DEBUG 
        ## del logging        
        ## if self.session : self.session.verbose = verbose 
        ## if self.tunnel  : self.tunnel .verbose = verbose 

        ## import logging
        ## print 'DISABLE', logging.root.manager.disable 
        
        if self.pid :
            try  :
                if self.session :  # and verbose :
                    command = 'kill -s SIGUSR1 %d ' % self.pid 
                    self.session.verbose = False
                    self.session ( command = command )
                    self.session.launch   ()
                    r = self.session.response()
                    if r : self.logger.error (' Sending signal: %s : %s : %s' %  ( self.remote_host , command , r ) )
                self.logger.debug ( 'Killing remote process %s' % self.pid )
                PC.kill ( self.pid , self.remote_host )
            except OSError :
                self.logger.warning ( 'Failure to kill remote process %s' % self.pid )
                pass
        self.__pid = 0

        ## remove unnesessary files 
        if self.session :
            commands = []
            if self.__environment : commands.append ( 'rm -f %s' %  self.__environment )
            if self.__script      : commands.append ( 'rm -f %s' %  self.__script      )
            if self.__tmpdir      : commands.append ( 'rmdir --ignore-fail-on-non-empty %s' %  self.__tmpdir )
            ##
            if commands :
                command = ' ; '.join ( commands )
                try : 
                    self.logger.debug  ('Remove remote files using "%s"' % command )
                    self.session.verbose = False  
                    self.session ( command = command )
                    self.session.launch()
                    r = self.session.response () 
                    if r : self.logger.warning ( "Response for %s is %s" % ( command  , r ) )                 
                except :
                    self.logger.warning ( 'Failure to remove remote files "%s" ' % command )
                    
        if self.session :
            self.logger.debug ( 'kill SSH-session %s' )
            try: 
                self.session.verbose = False  
                self.session.kill      () 
            except :
                self.logger.warning ( 'Failure to kill remote session')
                pass 
            self.__session = None
            
        if self.tunnel  :
            self.logger.debug ( 'Disconnect SSH-tunnel %s -> %s' % ( self.local , self.remote ) )  
            try : 
                self.tunnel.verbose  = False
                self.tunnel.disconnect ()
            except :
                self.logger.warning ( 'Failure to disconnect tunnel')
                pass 
            self.__tunnel  = None
            
        self.__active  = False
        
        self.remove_tunnel ( self.stamp )