Пример #1
0
def setup_server_users(server):
    """
    Seeds all users returned by get_seed_users() IF there are no users seed yet
    i.e. system.users collection is empty
    """
    """if not should_seed_users(server):
        log_verbose("Not seeding users for server '%s'" % server.id)
        return"""

    log_info("Checking if there are any users that need to be added for "
             "server '%s'..." % server.id)

    seed_users = server.get_seed_users()

    count_new_users = 0

    # Note: If server member of a replica then don't setup admin
    # users because primary server will do that at replinit

    # Now create admin ones
    if not server.is_slave():
        count_new_users += setup_server_admin_users(server)

    for dbname, db_seed_users in seed_users.items():
        # create the admin ones last so we won't have an auth issue
        if dbname in ["admin", "local"]:
            continue
        count_new_users += setup_server_db_users(server, dbname, db_seed_users)

    if count_new_users > 0:
        log_info("Added %s users." % count_new_users)
    else:
        log_verbose("Did not add any new users.")
Пример #2
0
def download_mongodb_binary(mongodb_version,
                            mongodb_edition,
                            destination=None):
    destination = destination or os.getcwd()

    log_info("Looking for a download for MongoDB ('%s', '%s')" %
             (mongodb_version, mongodb_edition))

    for repo in get_registered_binary_repositories():
        log_verbose("Trying from '%s' binary repository..." % repo.name)
        if mongodb_edition in repo.supported_editions:
            try:
                if repo.file_exists(mongodb_version, mongodb_edition):
                    return repo.download_file(mongodb_version,
                                              mongodb_edition,
                                              destination=destination)
                else:
                    log_verbose(
                        "Repository '%s' doesnt have this version. Skipping..."
                        % repo.name)
                    continue
            except FileNotInRepoError, e:
                log_verbose("No mongodb binary (version: '%s', edition: '%s' )"
                            "found in repo '%s'" %
                            (mongodb_version, mongodb_edition, repo.name))
        else:
            log_verbose("Binary repository '%s' does not support edition '%s'."
                        " Supported editions %s" %
                        (repo.name, mongodb_edition, repo.supported_editions))
Пример #3
0
def download_mongodb_binary(mongodb_version, mongodb_edition,
                            destination=None):
    destination = destination or os.getcwd()

    log_info("Looking for a download for MongoDB ('%s', '%s')" %
             (mongodb_version, mongodb_edition))

    for repo in get_registered_binary_repositories():
        log_verbose("Trying from '%s' binary repository..." % repo.name)
        if mongodb_edition in repo.supported_editions:
            try:
                if repo.file_exists(mongodb_version, mongodb_edition):
                    return repo.download_file(mongodb_version, mongodb_edition,
                                              destination=destination)
                else:
                    log_verbose("Repository '%s' doesnt have this version. Skipping..." % repo.name)
                    continue
            except FileNotInRepoError, e:
                log_verbose("No mongodb binary (version: '%s', edition: '%s' )"
                            "found in repo '%s'" %
                            (mongodb_version, mongodb_edition, repo.name))
        else:
            log_verbose("Binary repository '%s' does not support edition '%s'."
                        " Supported editions %s" %
                        (repo.name, mongodb_edition, repo.supported_editions))
Пример #4
0
def setup_server_users(server):
    """
    Seeds all users returned by get_seed_users() IF there are no users seed yet
    i.e. system.users collection is empty
    """
    """if not should_seed_users(server):
        log_verbose("Not seeding users for server '%s'" % server.id)
        return"""

    log_info("Checking if there are any users that need to be added for "
             "server '%s'..." % server.id)

    seed_users = server.get_seed_users()

    count_new_users = 0

    # Note: If server member of a replica then don't setup admin
    # users because primary server will do that at replinit

    # Now create admin ones
    if not server.is_slave():
        count_new_users += setup_server_admin_users(server)

    for dbname, db_seed_users in seed_users.items():
        # create the admin ones last so we won't have an auth issue
        if dbname in ["admin", "local"]:
            continue
        count_new_users += setup_server_db_users(server, dbname, db_seed_users)


    if count_new_users > 0:
        log_info("Added %s users." % count_new_users)
    else:
        log_verbose("Did not add any new users.")
Пример #5
0
def setup_root_admin_user(server, admin_users):
    log_info("Setting up root admin user...")
    if not admin_users:
        log_info("No admin users passed/configured. NOOP")
        return

    admin_user = server.get_login_user("admin") or admin_users[0]

    admin_db = server.get_db("admin", no_auth=True)
    # try to authenticate with the admin user to see if it is already setup
    try:
        success = admin_db.authenticate(admin_user["username"], admin_user["password"])
    except OperationFailure, of:
        success = False
Пример #6
0
def setup_root_admin_user(server, admin_users):
    log_info("Setting up root admin user...")
    if not admin_users:
        log_info("No admin users passed/configured. NOOP")
        return

    admin_user = server.get_login_user("admin") or admin_users[0]

    admin_db = server.get_db("admin", no_auth=True)
    # try to authenticate with the admin user to see if it is already setup
    try:
        success = admin_db.authenticate(admin_user["username"],
                                        admin_user["password"])
    except OperationFailure, of:
        success = False
Пример #7
0
def validate_cluster(cluster):
    log_info("Validating cluster '%s'..." % cluster.id )

    errors = []

    if isinstance(cluster, replicaset_cluster_type()):
        errors.extend(validate_replicaset_cluster(cluster))
    elif isinstance(cluster, shardset_cluster_type()):
        errors.extend(validate_shardset_cluster(cluster))

    if len(errors) > 0:
        raise MongoctlException("Cluster %s configuration is not valid. "
                                "Please fix errors below and try again.\n%s" %
                                (cluster.id , "\n".join(errors)))

    return cluster
Пример #8
0
def validate_cluster(cluster):
    log_info("Validating cluster '%s'..." % cluster.id)

    errors = []

    if isinstance(cluster, replicaset_cluster_type()):
        errors.extend(validate_replicaset_cluster(cluster))
    elif isinstance(cluster, sharded_cluster_type()):
        errors.extend(validate_sharded_cluster(cluster))

    if len(errors) > 0:
        raise MongoctlException("Cluster %s configuration is not valid. "
                                "Please fix errors below and try again.\n%s" %
                                (cluster.id, "\n".join(errors)))

    return cluster
Пример #9
0
    def _upload_file_to_bucket(self, file_path, destination):

        file_name = os.path.basename(file_path)
        destination_path = os.path.join(destination, file_name)
        log_info("Uploading '%s' to s3 bucket '%s' to '%s'" %
                 (file_path, self.bucket_name, destination))

        file_obj = open(file_path)
        k = Key(self.bucket)
        k.key = destination_path
        # set meta data (has to be before setting content in
        # order for it to work)
        k.set_metadata("Content-Type", "application/x-compressed")

        k.set_contents_from_file(file_obj)

        log_info("Completed upload '%s' to s3 bucket '%s'!" %
                 (file_path, self.bucket_name))
Пример #10
0
    def _upload_file_to_bucket(self, file_path, destination):

        file_name = os.path.basename(file_path)
        destination_path = os.path.join(destination, file_name)
        log_info("Uploading '%s' to s3 bucket '%s' to '%s'" %
                (file_path, self.bucket_name, destination))

        file_obj = open(file_path)
        k = Key(self.bucket)
        k.key = destination_path
        # set meta data (has to be before setting content in
        # order for it to work)
        k.set_metadata("Content-Type", "application/x-compressed")

        k.set_contents_from_file(file_obj)

        log_info("Completed upload '%s' to s3 bucket '%s'!" %
                 (file_path, self.bucket_name))
Пример #11
0
    def _download_file_from_bucket(self, file_path, destination):

        key = self.bucket.get_key(file_path)
        file_name = os.path.basename(file_path)

        if not key:
            raise FileNotInRepoError("No such file '%s' in bucket '%s'" %
                                     (file_path, self.bucket_name))

        log_info("Downloading '%s' from s3 bucket '%s'" %
                    (file_path, self.bucket_name))

        destination_path = os.path.join(destination, file_name)
        file_obj = open(destination_path, mode="w")

        num_call_backs = key.size / 1000
        key.get_contents_to_file(file_obj, cb=_download_progress,
                                 num_cb=num_call_backs)

        print("Download completed successfully!!")

        return destination_path
Пример #12
0
    def _download_file_from_bucket(self, file_path, destination):

        key = self.bucket.get_key(file_path)
        file_name = os.path.basename(file_path)

        if not key:
            raise FileNotInRepoError("No such file '%s' in bucket '%s'" %
                                     (file_path, self.bucket_name))

        log_info("Downloading '%s' from s3 bucket '%s'" %
                 (file_path, self.bucket_name))

        destination_path = os.path.join(destination, file_name)
        file_obj = open(destination_path, mode="w")

        num_call_backs = key.size / 1000
        key.get_contents_to_file(file_obj,
                                 cb=_download_progress,
                                 num_cb=num_call_backs)

        print("Download completed successfully!!")

        return destination_path
Пример #13
0
        db.add_user(username, password, read_only)
    except OperationFailure, ofe:
        # This is a workaround for PYTHON-407. i.e. catching a harmless
        # error that is raised after adding the first
        if "login" in str(ofe):
            pass
        else:
            raise
    except AutoReconnect, ar:
        log_exception(ar)
        if num_tries < 3:
            log_warning("_mongo_add_user: Caught a AutoReconnect error. %s " %
                        ar)
            # check if the user/pass was saved successfully
            if db.authenticate(username, password):
                log_info("_mongo_add_user: user was added successfully. "
                         "no need to retry")
            else:
                log_warning("_mongo_add_user: re-trying ...")
                _mongo_add_user(server, db, username, password,
                                read_only=read_only, num_tries=num_tries+1)
        else:
            raise


###############################################################################
def setup_server_db_users(server, dbname, db_users):
    log_verbose("Checking if there are any users that needs to be added for "
                "database '%s'..." % dbname)

    if not should_seed_db_users(server, dbname):
        log_verbose("Not seeding users for database '%s'" % dbname)
Пример #14
0
def __do_execute(args):
    header = """
-------------------------------------------------------------------------------------------
  __ _  ___  ___  ___ ____  ____/ /_/ /
 /  ' \/ _ \/ _ \/ _ `/ _ \/ __/ __/ / 
/_/_/_/\___/_//_/\_, /\___/\__/\__/_/  
                /___/ 
-------------------------------------------------------------------------------------------
   """
    global parser

    if len(args) < 1:
        print(header)
        parser.print_help()
        return

    # Parse the arguments and call the function of the selected cmd
    parsed_args = parser.parse_args(args)

    # turn on verbose if specified
    if namespace_get_property(parsed_args, "mongoctlVerbose"):
        turn_logging_verbose_on()

    # set interactive mode
    non_interactive = namespace_get_property(parsed_args, 'noninteractive')
    non_interactive = False if non_interactive is None else non_interactive

    set_interactive_mode(not non_interactive)

    if not is_interactive_mode():
        log_verbose("Running with noninteractive mode")

    # set global prompt value
    yes_all = parsed_args.yesToEverything
    no_all = parsed_args.noToEverything

    if yes_all and no_all:
        raise MongoctlException("Cannot have --yes and --no at the same time. "
                                "Please choose either --yes or --no")
    elif yes_all:
        say_yes_to_everything()
    elif no_all:
        say_no_to_everything()

    # set conf root if specified
    if parsed_args.configRoot is not None:
        config._set_config_root(parsed_args.configRoot)
    elif os.getenv(CONF_ROOT_ENV_VAR) is not None:
        config._set_config_root(os.getenv(CONF_ROOT_ENV_VAR))

    # get the function to call from the parser framework
    command_function = parsed_args.func

    # parse global login if present
    username = namespace_get_property(parsed_args, "username")
    password = namespace_get_property(parsed_args, "password")
    server_id = namespace_get_property(parsed_args, SERVER_ID_PARAM)
    parse_global_login_user_arg(username, password, server_id)

    if server_id is not None:
        # check if assumeLocal was specified
        assume_local = namespace_get_property(parsed_args, "assumeLocal")
        if assume_local:
            objects.server.assume_local_server(server_id)
    # execute command
    log_info("")
    return command_function(parsed_args)
Пример #15
0
        db.add_user(username, password, read_only, **kwargs)
    except OperationFailure, ofe:
        # This is a workaround for PYTHON-407. i.e. catching a harmless
        # error that is raised after adding the first
        if "login" in str(ofe):
            pass
        else:
            raise
    except AutoReconnect, ar:
        log_exception(ar)
        if num_tries < 3:
            log_warning("_mongo_add_user: Caught a AutoReconnect error. %s " %
                        ar)
            # check if the user/pass was saved successfully
            if db.authenticate(username, password):
                log_info("_mongo_add_user: user was added successfully. "
                         "no need to retry")
            else:
                log_warning("_mongo_add_user: re-trying ...")
                _mongo_add_user(server, db, username, password,
                                read_only=read_only, num_tries=num_tries+1)
        else:
            raise


###############################################################################
def list_database_users(server, db):
    if server.supports_local_users():
        result = db['system.users'].find()
    else:
        result = server.get_db("admin")["system.users"].find({
            "db": db.name
Пример #16
0
        db.add_user(username, password, read_only)
    except OperationFailure, ofe:
        # This is a workaround for PYTHON-407. i.e. catching a harmless
        # error that is raised after adding the first
        if "login" in str(ofe):
            pass
        else:
            raise
    except AutoReconnect, ar:
        log_exception(ar)
        if num_tries < 3:
            log_warning("_mongo_add_user: Caught a AutoReconnect error. %s " %
                        ar)
            # check if the user/pass was saved successfully
            if db.authenticate(username, password):
                log_info("_mongo_add_user: user was added successfully. "
                         "no need to retry")
            else:
                log_warning("_mongo_add_user: re-trying ...")
                _mongo_add_user(server,
                                db,
                                username,
                                password,
                                read_only=read_only,
                                num_tries=num_tries + 1)
        else:
            raise


###############################################################################
def setup_server_db_users(server, dbname, db_users):
    log_verbose("Checking if there are any users that needs to be added for "
Пример #17
0
def do_main(args):
    init_mongoctl_signal_handler()

    header = """
-------------------------------------------------------------------------------------------
  __ _  ___  ___  ___ ____  ____/ /_/ /
 /  ' \/ _ \/ _ \/ _ `/ _ \/ __/ __/ / 
/_/_/_/\___/_//_/\_, /\___/\__/\__/_/  
                /___/ 
-------------------------------------------------------------------------------------------
   """

    # Parse options
    parser = get_mongoctl_cmd_parser()

    if len(args) < 1:
        print(header)
        parser.print_help()
        return

    # Parse the arguments and call the function of the selected cmd
    parsed_args = parser.parse_args(args)

    # turn on verbose if specified
    if namespace_get_property(parsed_args,"mongoctlVerbose"):
        turn_logging_verbose_on()

    # set interactive mode
    non_interactive = namespace_get_property(parsed_args,'noninteractive')
    non_interactive = False if non_interactive is None else non_interactive

    set_interactive_mode(not non_interactive)

    if not is_interactive_mode():
        log_verbose("Running with noninteractive mode")

    # set global prompt value
    yes_all = parsed_args.yesToEverything
    no_all = parsed_args.noToEverything

    if yes_all and no_all:
        raise MongoctlException("Cannot have --yes and --no at the same time. "
                                "Please choose either --yes or --no")
    elif yes_all:
        say_yes_to_everything()
    elif no_all:
        say_no_to_everything()

    # set the global SSL_OFF flag
    if parsed_args.clientSslMode:
        objects.server.set_client_ssl_mode(parsed_args.clientSslMode)

    # set the global USE_ALT_ADDRESS field
    if parsed_args.useAltAddress:
        use_alt_address = parsed_args.useAltAddress
        log_info("Using alternative address '%s'..." % use_alt_address)
        objects.server.USE_ALT_ADDRESS = use_alt_address

    # set conf root if specified
    if parsed_args.configRoot is not None:
        config.set_config_root(parsed_args.configRoot)
    elif os.getenv(CONF_ROOT_ENV_VAR) is not None:
        config.set_config_root(os.getenv(CONF_ROOT_ENV_VAR))

    # set cmd arg servers/clusters
    if parsed_args.servers or parsed_args.clusters:
        repository.set_commandline_servers_and_clusters(parsed_args.servers, parsed_args.clusters)

    # get the function to call from the parser framework
    command_function = parsed_args.func

    # parse global login if present
    username = namespace_get_property(parsed_args, "username")

    password = namespace_get_property(parsed_args, "password")
    server_id = namespace_get_property(parsed_args, SERVER_ID_PARAM)
    parse_global_login_user_arg(username, password, server_id)

    if server_id is not None:
        # check if assumeLocal was specified
        assume_local = namespace_get_property(parsed_args,"assumeLocal")
        if assume_local:
            objects.server.assume_local_server(server_id)
    # execute command
    log_info("")
    return command_function(parsed_args)
Пример #18
0
def do_main(args):
    init_mongoctl_signal_handler()

    header = """
-------------------------------------------------------------------------------------------
  __ _  ___  ___  ___ ____  ____/ /_/ /
 /  ' \/ _ \/ _ \/ _ `/ _ \/ __/ __/ / 
/_/_/_/\___/_//_/\_, /\___/\__/\__/_/  
                /___/ 
-------------------------------------------------------------------------------------------
   """

    # Parse options
    parser = get_mongoctl_cmd_parser()

    if len(args) < 1:
        print(header)
        parser.print_help()
        return

    # Parse the arguments and call the function of the selected cmd
    parsed_args = parser.parse_args(args)

    # turn on verbose if specified
    if namespace_get_property(parsed_args, "mongoctlVerbose"):
        turn_logging_verbose_on()

    # set interactive mode
    non_interactive = namespace_get_property(parsed_args, "noninteractive")
    non_interactive = False if non_interactive is None else non_interactive

    set_interactive_mode(not non_interactive)

    if not is_interactive_mode():
        log_verbose("Running with noninteractive mode")

    # set global prompt value
    yes_all = parsed_args.yesToEverything
    no_all = parsed_args.noToEverything

    if yes_all and no_all:
        raise MongoctlException("Cannot have --yes and --no at the same time. " "Please choose either --yes or --no")
    elif yes_all:
        say_yes_to_everything()
    elif no_all:
        say_no_to_everything()

    # set the global SSL_OFF flag
    if parsed_args.clientSslMode:
        objects.server.set_client_ssl_mode(parsed_args.clientSslMode)

    # set the global USE_ALT_ADDRESS field
    if parsed_args.useAltAddress:
        use_alt_address = parsed_args.useAltAddress
        log_info("Using alternative address '%s'..." % use_alt_address)
        objects.server.USE_ALT_ADDRESS = use_alt_address

    # set conf root if specified
    if parsed_args.configRoot is not None:
        config.set_config_root(parsed_args.configRoot)
    elif os.getenv(CONF_ROOT_ENV_VAR) is not None:
        config.set_config_root(os.getenv(CONF_ROOT_ENV_VAR))

    # set cmd arg servers/clusters
    if parsed_args.servers or parsed_args.clusters:
        repository.set_commandline_servers_and_clusters(parsed_args.servers, parsed_args.clusters)

    # get the function to call from the parser framework
    command_function = parsed_args.func

    # parse global login if present
    username = namespace_get_property(parsed_args, "username")

    password = namespace_get_property(parsed_args, "password")
    server_id = namespace_get_property(parsed_args, SERVER_ID_PARAM)
    parse_global_login_user_arg(username, password, server_id)

    if server_id is not None:
        # check if assumeLocal was specified
        assume_local = namespace_get_property(parsed_args, "assumeLocal")
        if assume_local:
            objects.server.assume_local_server(server_id)
    # execute command
    log_info("")
    return command_function(parsed_args)
Пример #19
0
        db.add_user(username, password, read_only, **kwargs)
    except OperationFailure, ofe:
        # This is a workaround for PYTHON-407. i.e. catching a harmless
        # error that is raised after adding the first
        if "login" in str(ofe):
            pass
        else:
            raise
    except AutoReconnect, ar:
        log_exception(ar)
        if num_tries < 3:
            log_warning("_mongo_add_user: Caught a AutoReconnect error. %s " %
                        ar)
            # check if the user/pass was saved successfully
            if db.authenticate(username, password):
                log_info("_mongo_add_user: user was added successfully. "
                         "no need to retry")
            else:
                log_warning("_mongo_add_user: re-trying ...")
                _mongo_add_user(server,
                                db,
                                username,
                                password,
                                read_only=read_only,
                                num_tries=num_tries + 1)
        else:
            raise


###############################################################################
def list_database_users(server, db):
    if server.supports_local_users():