def generate_ssl_key_files(self):
        ssl_dir = os.path.join(get_test_dir(), "ssl")
        ensure_dir(ssl_dir)

        ssl_cmd = [which("openssl"), "req", "-newkey","rsa:2048", "-new", "-x509", "-days", "1", "-nodes", "-out",
                   "test-mongodb-cert.crt", "-keyout", "test-mongodb-cert.key", "-subj",
                   "/C=US/ST=CA/L=SF/O=mongoctl/CN=test"

                   ]

        call_command(ssl_cmd, cwd=ssl_dir)

        # create the .pem file

        crt_file = os.path.join(ssl_dir, "test-mongodb-cert.crt")
        key_file = os.path.join(ssl_dir, "test-mongodb-cert.key")
        pem_file = os.path.join(ssl_dir, "test-mongodb.pem")
        with open(pem_file, 'w') as outfile:
            with open(crt_file) as infile1:
                outfile.write(infile1.read())

            with open(key_file) as infile2:
                outfile.write(infile2.read())

        for server_id in self.get_my_test_servers():
            server = repository.lookup_server(server_id)
            server.set_cmd_option("sslPEMKeyFile", pem_file)
            server.set_cmd_option("sslCAFile", pem_file)
Exemple #2
0
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)
Exemple #3
0
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)
Exemple #4
0
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
Exemple #5
0
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)
Exemple #6
0
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)
Exemple #7
0
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
Exemple #9
0
def show_server_command(parsed_options):
    server = repository.lookup_server(parsed_options.server)
    if server is None:
        raise MongoctlException("Could not find server '%s'." %
                                parsed_options.server)
    log_info("Configuration for server '%s':" % parsed_options.server)
    print server
Exemple #10
0
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)
Exemple #11
0
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))
Exemple #12
0
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))
Exemple #13
0
def tail_log_command(parsed_options):
    server = repository.lookup_server(parsed_options.server)
    server.validate_local_op("tail-log")
    log_path = server.get_log_file_path()
    # check if log file exists
    if os.path.exists(log_path):
        log_tailer = tail_server_log(server)
        log_tailer.communicate()
    else:
        log_info("Log file '%s' does not exist." % log_path)
Exemple #14
0
def tail_log_command(parsed_options):
    server = repository.lookup_server(parsed_options.server)
    server.validate_local_op("tail-log")
    log_path = server.get_log_file_path()
    # check if log file exists
    if os.path.exists(log_path):
        log_tailer = tail_server_log(server)
        log_tailer.communicate()
    else:
        log_info("Log file '%s' does not exist." % log_path)
    def get_server(self):
        server_doc = self.get_property("server")
        if not server_doc:
            return

        if self._server is None:
            if server_doc is not None:
                if type(server_doc) is DBRef:
                    self._server = repository.lookup_server(server_doc.id)

        return self._server
Exemple #16
0
    def get_server(self):
        server_doc = self.get_property("server")
        if not server_doc:
            return

        if self._server is None:
            if server_doc is not None:
                if type(server_doc) is DBRef:
                    self._server = repository.lookup_server(server_doc.id)

        return self._server
    def get_server(self):

        server_doc = self.get_property("server")
        host = self.get_property("host")

        if self._server is None:
            if server_doc is not None:
                if type(server_doc) is DBRef:
                    self._server = repository.lookup_server(server_doc.id)
            elif host is not None:
                self._server = repository.build_server_from_address(host)

        return self._server
    def get_server(self):

        server_doc = self.get_property("server")
        host = self.get_property("host")

        if self._server is None:
            if server_doc is not None:
                if type(server_doc) is DBRef:
                    self._server = repository.lookup_server(server_doc.id)
            elif host is not None:
                self._server = repository.build_server_from_address(host)

        return self._server
Exemple #19
0
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)
Exemple #20
0
    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")
Exemple #21
0
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)
Exemple #22
0
    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)
Exemple #24
0
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)
Exemple #25
0
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)
Exemple #26
0
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)
Exemple #27
0
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
Exemple #28
0
 def assert_server_offline(self, server_id):
     server = repository.lookup_server(server_id)
     self.assertTrue(not server.is_online())