def parse(): """ Parse command options and generate config. """ conf = Config() parser = argparse.ArgumentParser(description='Check data consistency including data and indexes.') parser.add_argument('--from', nargs='?', required=True, help='the source must be a mongod instance of replica-set') parser.add_argument('--src-authdb', nargs='?', required=False, default='admin', help="authentication database, default is 'admin'") parser.add_argument('--src-username', nargs='?', required=False, help='src username') parser.add_argument('--src-password', nargs='?', required=False, help='src password') parser.add_argument('--to', nargs='?', required=True, help='the destionation should be a mongos or mongod instance') parser.add_argument('--dst-authdb', nargs='?', required=False, default='admin', help="authentication database, default is 'admin'") parser.add_argument('--dst-username', nargs='?', required=False, help='dst username') parser.add_argument('--dst-password', nargs='?', required=False, help='dst password') parser.add_argument('--dbs', nargs='+', required=False, help='databases to check') parser.add_argument('--src-db', nargs='?', required=False, help="src database to check, work with '--dst-db', conflict with '--dbs'") parser.add_argument('--dst-db', nargs='?', required=False, help="dst database to check, work with '--src-db', conflict with '--dbs'") args = vars(parser.parse_args()) if args['from'] is not None: conf.src_hostportstr = args['from'] conf.src_host, conf.src_port = parse_hostportstr(conf.src_hostportstr) if args['src_authdb'] is not None: conf.src_authdb = args['src_authdb'] if args['src_username'] is not None: conf.src_username = args['src_username'] if args['src_password'] is not None: conf.src_password = args['src_password'] if args['to'] is not None: conf.dst_hostportstr = args['to'] conf.dst_host, conf.dst_port = parse_hostportstr(conf.dst_hostportstr) if args['dst_authdb'] is not None: conf.dst_authdb = args['dst_authdb'] if args['dst_username'] is not None: conf.dst_username = args['dst_username'] if args['dst_password'] is not None: conf.dst_password = args['dst_password'] if args['dbs'] is not None: conf.dbs = args['dbs'] if args['src_db'] is not None: conf.src_db = args['src_db'] if args['dst_db'] is not None: conf.dst_db = args['dst_db'] if conf.dbs and (conf.src_db or conf.dst_db): print "Terminated, conflict command options found" sys.exit(1) if conf.src_db and not conf.dst_db: print "Terminated, require command option '--dst-db'" sys.exit(1) if conf.dst_db and not conf.src_db: print "Terminated, require command option '--src-db'" sys.exit(1) if conf.src_db and conf.dst_db and conf.src_db == conf.dst_db: print 'Terminated, src_db is same as dst_db' sys.exit(1) return conf
def connect(self): """ Connect to server. """ try: if isinstance(self._conf.hosts, str): if("mongodb+srv" in self._conf.hosts): host = self._conf.hosts port = None self._mc = mongo_utils.connect(host, port, authdb=self._conf.authdb, username=self._conf.username, password=self._conf.password) return True else: host, port = mongo_utils.parse_hostportstr(self._conf.hosts) self._mc = mongo_utils.connect(host, port, authdb=self._conf.authdb, username=self._conf.username, password=self._conf.password) self._mc.admin.command('ismaster') return True elif isinstance(self._conf.hosts, list): # TODO return False except Exception as e: log.error('connect failed: %s' % e) return False
def connect(self): """ Connect to server. """ try: if isinstance(self._conf.hosts, unicode): host, port = mongo_utils.parse_hostportstr(self._conf.hosts) self._mc = mongo_utils.connect(host, port, ssl=self._conf.ssl, authdb=self._conf.authdb, username=self._conf.username, password=self._conf.password) self._mc.admin.command('ismaster') return True else: log.error('hosts contains something unsupported %r' % self._conf.hosts) except Exception as e: log.error('connect failed: %s' % e) return False
def connect(self): """ Connect to server. """ try: if isinstance(self._conf.hosts, unicode): host, port = mongo_utils.parse_hostportstr(self._conf.hosts) self._mc = mongo_utils.connect(host, port, authdb=self._conf.authdb, username=self._conf.username, password=self._conf.password) self._mc.admin.command('ismaster') return True elif isinstance(self._conf.__hosts, list): # TODO return False except Exception as e: log.error('connect failed: %s' % e) return False
def parse(): """ Parse command options and generate config. """ conf = Config() parser = argparse.ArgumentParser( description='Check data consistency including data and indexes.') parser.add_argument( '--from', nargs='?', required=True, help='the source must be a mongod instance of replica-set') parser.add_argument('--src-authdb', nargs='?', required=False, default='admin', help="authentication database, default is 'admin'") parser.add_argument('--src-username', nargs='?', required=False, help='src username') parser.add_argument('--src-password', nargs='?', required=False, help='src password') parser.add_argument( '--to', nargs='?', required=True, help='the destionation should be a mongos or mongod instance') parser.add_argument('--dst-authdb', nargs='?', required=False, default='admin', help="authentication database, default is 'admin'") parser.add_argument('--dst-username', nargs='?', required=False, help='dst username') parser.add_argument('--dst-password', nargs='?', required=False, help='dst password') parser.add_argument('--dbs', nargs='+', required=False, help='databases to check') parser.add_argument( '--src-db', nargs='?', required=False, help= "src database to check, work with '--dst-db', conflict with '--dbs'" ) parser.add_argument( '--dst-db', nargs='?', required=False, help= "dst database to check, work with '--src-db', conflict with '--dbs'" ) args = vars(parser.parse_args()) if args['from'] is not None: conf.src_hostportstr = args['from'] conf.src_host, conf.src_port = parse_hostportstr( conf.src_hostportstr) if args['src_authdb'] is not None: conf.src_authdb = args['src_authdb'] if args['src_username'] is not None: conf.src_username = args['src_username'] if args['src_password'] is not None: conf.src_password = args['src_password'] if args['to'] is not None: conf.dst_hostportstr = args['to'] conf.dst_host, conf.dst_port = parse_hostportstr( conf.dst_hostportstr) if args['dst_authdb'] is not None: conf.dst_authdb = args['dst_authdb'] if args['dst_username'] is not None: conf.dst_username = args['dst_username'] if args['dst_password'] is not None: conf.dst_password = args['dst_password'] if args['dbs'] is not None: conf.dbs = args['dbs'] if args['src_db'] is not None: conf.src_db = args['src_db'] if args['dst_db'] is not None: conf.dst_db = args['dst_db'] if conf.dbs and (conf.src_db or conf.dst_db): print "Terminated, conflict command options found" sys.exit(1) if conf.src_db and not conf.dst_db: print "Terminated, require command option '--dst-db'" sys.exit(1) if conf.dst_db and not conf.src_db: print "Terminated, require command option '--src-db'" sys.exit(1) if conf.src_db and conf.dst_db and conf.src_db == conf.dst_db: print 'Terminated, src_db is same as dst_db' sys.exit(1) return conf