def open_mongo_shell_to(db_address, username=None, password=None, shell_options=None, js_files=None): if is_mongo_uri(db_address): open_mongo_shell_to_uri(db_address, username, password, shell_options, js_files) return # db_address is an id string id_path = db_address.split("/") id = id_path[0] database = id_path[1] if len(id_path) == 2 else None server = repository.lookup_server(id) if server: open_mongo_shell_to_server(server, database, username, password, shell_options, js_files) return # Maybe cluster? cluster = repository.lookup_cluster(id) if cluster: open_mongo_shell_to_cluster(cluster, database, username, password, shell_options, js_files) return # Unknown destination raise MongoctlException("Unknown db address '%s'" % db_address)
def remove_shard_command(parsed_options): shard_id = parsed_options.shardId # determine if the shard is a replicaset cluster or a server shard = repository.lookup_cluster(shard_id) if not shard: shard = repository.lookup_server(shard_id) if not shard: raise MongoctlException("Unknown shard '%s'" % shard_id) sharded_cluster = repository.lookup_cluster_by_shard(shard) if not sharded_cluster: raise MongoctlException("'%s' is not a shard" % shard_id) dest = getattr(parsed_options, "unshardedDataDestination") synchronized = getattr(parsed_options, "synchronized") if parsed_options.dryRun: dry_run_remove_shard(shard, sharded_cluster) else: sharded_cluster.remove_shard(shard, unsharded_data_dest_id=dest, synchronized=synchronized)
def mongo_dump_db_address(db_address, username=None, password=None, use_best_secondary=False, max_repl_lag=None, dump_options=None): if is_mongo_uri(db_address): mongo_dump_uri(uri=db_address, username=username, password=password, use_best_secondary=use_best_secondary, dump_options=dump_options) return # db_address is an id string id_path = db_address.split("/") id = id_path[0] database = id_path[1] if len(id_path) == 2 else None server = repository.lookup_server(id) if server: mongo_dump_server(server, database=database, username=username, password=password, dump_options=dump_options) return else: cluster = repository.lookup_cluster(id) if cluster: mongo_dump_cluster(cluster, database=database, username=username, password=password, use_best_secondary=use_best_secondary, max_repl_lag=max_repl_lag, dump_options=dump_options) return # Unknown destination raise MongoctlException("Unknown db address '%s'" % db_address)
def status_command(parsed_options): # we need to print status json to stdout so that its seperate from all # other messages that are printed on stderr. This is so scripts can read # status json and parse it if it needs id = parsed_options.id server = repository.lookup_server(id) if server: # log_info("Status for server '%s':" % id) print "Server Info:" print server status = server.get_status(admin=True) else: cluster = repository.lookup_cluster(id) if cluster: # log_info("Status for cluster '%s':" % id) status = cluster.get_status() else: raise MongoctlException("Cannot find a server or a cluster with" " id '%s'" % id) status_str = document_pretty_string(status) # if mongoctl.is_service() is False: # stdout_log(status_str) return status_str
def open_mongo_shell_to(db_address, username=None, password=None, shell_options={}, js_files=[]): if is_mongo_uri(db_address): open_mongo_shell_to_uri(db_address, username, password, shell_options, js_files) return # db_address is an id string id_path = db_address.split("/") id = id_path[0] database = id_path[1] if len(id_path) == 2 else None server = repository.lookup_server(id) if server: open_mongo_shell_to_server(server, database, username, password, shell_options, js_files) return # Maybe cluster? cluster = repository.lookup_cluster(id) if cluster: open_mongo_shell_to_cluster(cluster, database, username, password, shell_options, js_files) return # Unknown destination raise MongoctlException("Unknown db address '%s'" % db_address)
def mongo_restore_db_address(db_address, source, username=None, password=None, restore_options=None): if is_mongo_uri(db_address): mongo_restore_uri(db_address, source, username, password, restore_options) return # db_address is an id string id_path = db_address.split("/") id = id_path[0] database = id_path[1] if len(id_path) == 2 else None server = repository.lookup_server(id) if server: mongo_restore_server(server, source, database=database, username=username, password=password, restore_options=restore_options) return else: cluster = repository.lookup_cluster(id) if cluster: mongo_restore_cluster(cluster, source, database=database, username=username, password=password, restore_options=restore_options) return raise MongoctlException("Unknown db address '%s'" % db_address)
def print_uri_command(parsed_options): id = parsed_options.id db = parsed_options.db # check if the id is a server id print "in print_uri_command:" print "\t " + str(id) uri = "" server = repository.lookup_server(id) if server: print "\t As a server" uri = server.get_mongo_uri_template(db=db) print "\t " + str(uri) else: print "\t As a cluster" cluster = repository.lookup_cluster(id) if cluster: uri = cluster.get_mongo_uri_template(db=db) else: print "\t Raise an error?" raise MongoctlException("Cannot find a server or a cluster with" " id '%s'" % id) if mongoctl.is_service() is False: print uri return uri
def show_cluster_command(parsed_options): cluster = repository.lookup_cluster(parsed_options.cluster) if cluster is None: raise MongoctlException("Could not find cluster '%s'." % parsed_options.cluster) log_info("Configuration for cluster '%s':" % parsed_options.cluster) print cluster
def is_server_or_cluster_db_address(value): """ checks if the specified value is in the form of [server or cluster id][/database] """ # check if value is an id string id_path = value.split("/") id = id_path[0] return len(id_path) <= 2 and (repository.lookup_server(id) or repository.lookup_cluster(id))
def get_cluster(self): cluster_doc = self.get_property("cluster") if not cluster_doc: return if self._cluster is None: if cluster_doc is not None: if type(cluster_doc) is DBRef: self._cluster = repository.lookup_cluster(cluster_doc.id) return self._cluster
def print_uri_command(parsed_options): id = parsed_options.id db = parsed_options.db # check if the id is a server id server = repository.lookup_server(id) if server: print server.get_mongo_uri_template(db=db) else: cluster = repository.lookup_cluster(id) if cluster: print cluster.get_mongo_uri_template(db=db) else: raise MongoctlException("Cannot find a server or a cluster with" " id '%s'." % id)
def _resolve_config_servers(self): config_refs = self.get_property("configServers") # check if its a config replica if isinstance(config_refs, DBRef): config_cluster_id = config_refs.id return repository.lookup_cluster(config_cluster_id) elif isinstance(config_refs, list): config_servers = [] for ref in config_refs: config_servers.append(repository.lookup_server(ref["server"].id)) return config_servers else: raise Exception("Invalid configServers value")
def _resolve_config_servers(self): config_refs = self.get_property("configServers") # check if its a config replica if isinstance(config_refs, DBRef): config_cluster_id = config_refs.id return repository.lookup_cluster(config_cluster_id) elif isinstance(config_refs, list): config_servers = [] for ref in config_refs: config_servers.append( repository.lookup_server(ref["server"].id)) return config_servers else: raise Exception("Invalid configServers value")
def _do_add_shard_command(shard_id, dry_run=False): # determine if the shard is a replicaset cluster or a server shard = repository.lookup_cluster(shard_id) if not shard: shard = repository.lookup_server(shard_id) if not shard: raise MongoctlException("Unknown shard '%s'" % shard_id) # We obtain which cluster this shard belongs to sharded_cluster = repository.lookup_cluster_by_shard(shard) if not sharded_cluster: raise MongoctlException("'%s' is not a shard" % shard_id) if dry_run: dry_run_add_shard(shard, sharded_cluster) else: sharded_cluster.add_shard(shard)
def add_shard_command(parsed_options): shard_id = parsed_options.shardId # determine if the shard is a replicaset cluster or a server shard = repository.lookup_cluster(shard_id) if not shard: shard = repository.lookup_server(shard_id) if not shard: raise MongoctlException("Unknown shard '%s'" % shard_id) sharded_cluster = repository.lookup_cluster_by_shard(shard) if not sharded_cluster: raise MongoctlException("'%s' is not a shard" % shard_id) if parsed_options.dryRun: dry_run_add_shard(shard, sharded_cluster) else: add_shard(shard, sharded_cluster)
def mongo_restore_db_address(db_address, source, username=None, password=None, parsed_options=None): if is_mongo_uri(db_address): mongo_restore_uri(db_address, source, username, password, parsed_options=parsed_options) return # db_address is an id string id_path = db_address.split("/") id = id_path[0] database = id_path[1] if len(id_path) == 2 else None server = repository.lookup_server(id) if server: mongo_restore_server(server, source, database=database, username=username, password=password, parsed_options=parsed_options) return else: cluster = repository.lookup_cluster(id) if cluster: mongo_restore_cluster(cluster, source, database=database, username=username, password=password, parsed_options=parsed_options) return raise MongoctlException("Unknown db address '%s'" % db_address)
def status_command(parsed_options): # we need to print status json to stdout so that its seperate from all # other messages that are printed on stderr. This is so scripts can read # status json and parse it if it needs id = parsed_options.id server = repository.lookup_server(id) if server: log_info("Status for server '%s':" % id) status = server.get_status(admin=True) else: cluster = repository.lookup_cluster(id) if cluster: log_info("Status for cluster '%s':" % id) status = cluster.get_status() else: raise MongoctlException("Cannot find a server or a cluster with" " id '%s'" % id) status_str = document_pretty_string(status) stdout_log(status_str) return status