def Shutdown()

in automation/tinc/main/ext/qautils/gppylib/operations/gpMigratorUtil.py [0:0]


    def Shutdown(self):
        '''
        Stops the specified database
        '''
        if not self.dbup:
            return
        [env, utility] = self.dbup

        dir = env['MASTER_DATA_DIRECTORY']
        if not os.path.exists('%s/postmaster.pid' % dir):
            logger.warn(
                     'Shutdown skipped - %s/postmaster.pid not found' % dir)
            return

        if (env == self.oldenv):
            logger.info('Shutting down old Greenplum postmaster')
        else:
            logger.info('Shutting down new Greenplum postmaster')

        locked = os.path.join(env['MASTER_DATA_DIRECTORY'],
                              'pg_hba.conf'+LOCKEXT)

        try:

            # Note on arguments to gpstop:
            #   This code has gone back and forth on -s vs -f for shutdown:
            #
            #   -f aborts active connections.  This is a good thing.  If 
            #      a user or script snuck in a connection before we were able
            #      to establish the lockdown then we want to abort that 
            #      connection otherwise it will cause the upgrade to fail and 
            #      that is bad.
            #
            #      Prior versions would sometimes issue a kill for fast 
            #      shutdown, this is a problem since we need the database 
            #      shutdown cleanly with no pending xlog transactions.
            #      Because of that we switched to -s.
            # 
            #   -s causes problems because it is a stop that will fail if a 
            #      session is connected and we want to abort active sessions.
            #      Because of that we switched back to -f.
            #
            #   The belief is currently that the current version of gpstop
            #   should be good with passing -f.  To help safeguard this belief
            #   there is a check when we set the catalog version to ensure 
            #   that the database shutdown cleanly.
            #   
            # If this needs to be changed again please read the above,
            # consider what happens if you try to upgrade with an active 
            # connection open to the database, and procede cautiously.

            if utility:  cmd = 'gpstop -a -f -m'
            else:        cmd = 'gpstop -a -f'

            cmd += ' -l %s' % self.logdir

            locked = os.path.join(env['MASTER_DATA_DIRECTORY'],
                                  'pg_hba.conf'+LOCKEXT)
            try:
                if os.path.exists(locked):
                    env['PGUSER'] = MIGRATIONUSER
                    logger.debug('handling locked shutdown')
                logger.debug('shutting down with env: %s' % str(env))
                logger.debug('shutting down with command: %s' % cmd)

                pid = subprocess.Popen(cmd, preexec_fn=os.setpgrp,
                                       env=env, shell=True,
                                       stdout=self.logfile, 
                                       stderr=self.logfile,
                                       close_fds=True)
            finally:
                if os.path.exists(locked):
                    del env['PGUSER']
            self.dbup = None

            # Ignore interrupt requests until shutdown is done
            error = None
            retcode = None
            while retcode == None:
                try: 
                    retcode = pid.wait();

                except KeyboardInterrupt, e:
                    if not self.interrupted:
                        logger.fatal('***************************************')
                        logger.fatal('SIGINT-: Upgrade Aborted')
                        logger.fatal('***************************************')
                        logger.info( 'Performing clean abort')
                        self.interrupted = True
                        error = e
                    else:
                        logger.info( 'SIGINT-: Still processing shutdown')

            if retcode < 0:
                raise UpgradeError("Shutdown terminated by signal");
            today = date.today().strftime('%Y%m%d')
            logname = os.path.join(self.logdir, 'gpstop_%s.log' % today)
            if retcode == 1:
                logger.warn('***************************************')
                logger.warn('Warnings generated stopping cluster')
                logger.warn('Check %s for detailed warnings' % logname)
                logger.warn('***************************************')
            if retcode > 1:
                logger.fatal('***************************************')
                logger.fatal('Shutdown failed with error code %d' % retcode)
                logger.fatal('Check %s for detailed error' % logname)
                logger.fatal('***************************************')
                raise UpgradeError('Shutdown failed')

            # If we recieved an interrupt, resignal it now that the startup is done
            if error: 
                raise error

        except OSError, e:
            logger.fatal(str(e))
            raise UpgradeError('Shutdown failed')

        self.CheckDown();