def __cleanup_previous_runs(self):
        if (not yes_no(
                'The next step will kill all mongodb processes and wipe out the data path.\n'
                + 'Proceed (yes/no)? ')):
            raise KeyboardInterrupt('User disallowed cleanup of the data path')

        # Iterate through all processes and kill mongod and mongos
        for process in psutil.process_iter():
            try:
                processExecutable = os.path.basename(process.exe())
            except psutil.NoSuchProcess:
                pass
            except psutil.AccessDenied:
                pass
            else:
                if (processExecutable
                        in [exe_name('mongod'),
                            exe_name('mongos')]):
                    process.kill()
                    process.wait()

        # Remove the output directories
        try:
            shutil.rmtree(self.introspectRoot)
        except FileNotFoundError:
            pass

        try:
            shutil.rmtree(self.clusterRoot)
        except FileNotFoundError:
            pass
    def __init__(self, args):
        self.binarypath = args.binarypath
        self.dir = args.dir
        self.configdump = args.configdumpdir[0]

        self.mongoRestoreBinary = os.path.join(self.binarypath, exe_name('mongorestore'))

        self.clusterIntrospectMongoDPort = 19000
        self.clusterStartingPort = 20000

        self.mongoRestoreNumInsertionWorkers = 16

        print('Running cluster import with binaries located at: ', self.binarypath)
        print('Data directory root at: ', self.dir)
        print('Config dump directory at: ', self.configdump)

        if (not self.__cleanup_previous_runs()):
            raise FileExistsError('Unable to cleanup any previous runs.')

        os.makedirs(self.dir)

        # Make it unbuffered so the output of the subprocesses shows up immediately in the file
        kOutputLogFileBufSize = 256
        self._outputLogFile = open(
            os.path.join(self.dir, 'reconstruct.log'), 'w', kOutputLogFileBufSize)
    def __init__(self, args):
        self.binarypath = args.binarypath
        self.introspectRoot = os.path.join(args.dir, 'introspect')
        self.clusterRoot = os.path.join(args.dir, 'cluster')
        self.configdump = args.configdumpdir[0]
        self.numShards = args.numshards

        self.mongoRestoreBinary = os.path.join(self.binarypath,
                                               exe_name('mongorestore'))

        self.clusterIntrospectMongoDPort = 19000
        self.clusterStartingPort = 20000

        self.mongoRestoreNumInsertionWorkers = 16

        print('Running cluster import with binaries located at:',
              self.binarypath)
        print('Config dump directory at:', self.configdump)
        print('Data directory root at:', args.dir)
        print('Introspect directory at:', self.introspectRoot)
        print('Cluster directory at:', self.clusterRoot)

        self.__cleanup_previous_runs()

        os.makedirs(self.introspectRoot)
        os.makedirs(self.clusterRoot)

        # Make it unbuffered so the output of the subprocesses shows up immediately in the file
        kOutputLogFileBufSize = 256
        self._outputLogFile = open(os.path.join(args.dir, 'reconstruct.log'),
                                   'w', kOutputLogFileBufSize)
    def __cleanup_previous_runs(self):
        if (not yes_no('The next step will kill all mongodb processes and wipe out the data path.\n'
                       + 'Proceed (yes/no)? ')):
            return False

        # Iterate through all processes and kill mongod and mongos
        for process in psutil.process_iter():
            try:
                processExecutable = os.path.basename(process.exe())
            except psutil.NoSuchProcess:
                pass
            except psutil.AccessDenied:
                pass
            else:
                if (processExecutable in [exe_name('mongod'), exe_name('mongos')]):
                    process.kill()
                    process.wait()

        # Remove the output directory
        shutil.rmtree(self.dir)
        return True