Esempio n. 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.")
Esempio n. 2
0
def get_mongoctl_database():

    # if not using db then return
    if not has_db_repository():
        return

    global __mongoctl_db__

    if __mongoctl_db__ is not None:
        return __mongoctl_db__

    log_verbose("Connecting to mongoctl db...")
    try:
        client = _db_repo_connect()
        # issue a call to server to ensure it connects
        client.get_database("admin").command({"ping": 1})
        __mongoctl_db__ = client.get_default_database()
        return __mongoctl_db__
    except Exception, e:
        log_exception(e)
        __mongoctl_db__ = "OFFLINE"
        log_warning("\n*************\n"
                    "Will not be using database repository for configurations"
                    " at this time!"
                    "\nREASON: Could not establish a database"
                    " connection to mongoctl's database repository."
                    "\nCAUSE: %s."
                    "\n*************" % e)
Esempio n. 3
0
def get_mongoctl_database():

    # if not using db then return
    if not has_db_repository():
        return

    global __mongoctl_db__

    if __mongoctl_db__ is not None:
        return __mongoctl_db__

    log_verbose("Connecting to mongoctl db...")
    try:
        client = _db_repo_connect()
        # issue a call to server to ensure it connects
        client.get_database("admin").command({"ping": 1})
        __mongoctl_db__ = client.get_default_database()
        return __mongoctl_db__
    except Exception, e:
        log_exception(e)
        __mongoctl_db__ = "OFFLINE"
        log_warning("\n*************\n"
                    "Will not be using database repository for configurations"
                    " at this time!"
                    "\nREASON: Could not establish a database"
                    " connection to mongoctl's database repository."
                    "\nCAUSE: %s."
                    "\n*************" % e)
Esempio n. 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.")
Esempio n. 5
0
def validate_repositories():
    global __repos_validated__
    if not __repos_validated__:
        if not(has_file_repository() or has_db_repository() or has_commandline_servers_or_clusters()):
            log_verbose("******* WARNING!\nNo fileRepository or databaseRepository configured."
                        "\n*******")
        __repos_validated__ = True
Esempio n. 6
0
def get_mongoctl_database():

    # if not using db then return
    if not has_db_repository():
        return

    global __mongoctl_db__

    if __mongoctl_db__ is not None:
        return __mongoctl_db__

    log_verbose("Connecting to mongoctl db...")
    try:

        (conn, dbname) = _db_repo_connect()

        __mongoctl_db__ = conn[dbname]
        return __mongoctl_db__
    except Exception, e:
        log_exception(e)
        __mongoctl_db__ = "OFFLINE"
        log_warning("\n*************\n"
                    "Will not be using database repository for configurations"
                    " at this time!"
                    "\nREASON: Could not establish a database"
                    " connection to mongoctl's database repository."
                    "\nCAUSE: %s."
                    "\n*************" % e)
Esempio n. 7
0
def get_mongoctl_database():

    # if not using db then return
    if not has_db_repository():
        return

    global __mongoctl_db__

    if __mongoctl_db__ is not None:
        return __mongoctl_db__

    log_verbose("Connecting to mongoctl db...")
    try:

        (conn, dbname) = _db_repo_connect()

        __mongoctl_db__ = conn[dbname]
        return __mongoctl_db__
    except Exception, e:
        log_exception(e)
        __mongoctl_db__ = "OFFLINE"
        log_warning("\n*************\n"
                    "Will not be using database repository for configurations"
                    " at this time!"
                    "\nREASON: Could not establish a database"
                    " connection to mongoctl's database repository."
                    "\nCAUSE: %s."
                    "\n*************" % e)
Esempio n. 8
0
def validate_repositories():
    global __repos_validated__
    if not __repos_validated__:
        if not(has_file_repository() or has_db_repository() or has_commandline_servers_or_clusters()):
            log_verbose("******* WARNING!\nNo fileRepository or databaseRepository configured."
                        "\n*******")
        __repos_validated__ = True
Esempio n. 9
0
    def get_download_url(self, mongodb_version, mongodb_edition):
        template_args = get_template_args(mongodb_version, mongodb_edition)
        platform_spec = template_args["platform_spec"]
        os_dist_name = template_args["os_dist_name"]
        os_dist_version_no_dots = template_args["os_dist_version_no_dots"]
        os_name = template_args["os_name"]

        version_info = make_version_info(mongodb_version)
        if mongodb_edition == MongoDBEdition.COMMUNITY:
            archive_name = "mongodb-%s-%s.tgz" % (platform_spec,
                                                  mongodb_version)
            domain = "fastdl.mongodb.org"
        # community ssl from mongodb.org are supported only for version >= 3.0
        elif mongodb_edition == MongoDBEdition.COMMUNITY_SSL:
            if version_info >= VERSION_3_0:
                # Platforms that lack OS dist data (eg OS X) apparently don't
                # need it
                dist_bits = ""
                # Ones that have it, need it injected, i.e.
                # 'mongodb-linux-x86_64-debian71-3.2.9.tgz
                if os_dist_name is not None:
                    dist_bits = "-{0}{1}".format(
                        os_dist_name,
                        os_dist_version_no_dots
                    )
                archive_name = "mongodb-{0}{1}-{2}.tgz".format(
                    platform_spec,
                    dist_bits,
                    mongodb_version
                )
                domain = "fastdl.mongodb.org"
            else:
                return None
        elif mongodb_edition == MongoDBEdition.ENTERPRISE:
            if version_info and version_info >= VERSION_2_6_1:
                domain = "downloads.10gen.com"
                rel_name = "enterprise"
            else:
                rel_name = "subscription"
                domain = "downloads.mongodb.com"

            archive_name = ("mongodb-%s-%s-%s%s-%s.tgz" %
                            (platform_spec, rel_name, os_dist_name,
                             os_dist_version_no_dots,
                             mongodb_version))

        else:
            raise MongoctlException("Unsupported edition '%s'" %
                                    mongodb_edition)

        url = "http://%s/%s/%s" % (domain, os_name, archive_name)
        log_verbose("Download url for MongoDB %s (%s edition) OS %s, dist '%s': %s" %
                    (mongodb_version, mongodb_edition, os_name, os_dist_name, url))

        return url
Esempio n. 10
0
def should_seed_db_users(server, dbname):
    log_verbose("See if we should seed users for database '%s'" % dbname)
    try:
        connection = server.get_db_connection()
        if connection[dbname]['system.users'].find_one():
            return False
        else:
            return True
    except Exception, e:
        log_exception(e)
        return False
Esempio n. 11
0
def should_seed_db_users(server, dbname):
    log_verbose("See if we should seed users for database '%s'" % dbname)
    try:
        connection = server.get_db_connection()
        if connection[dbname]['system.users'].find_one():
            return False
        else:
            return True
    except Exception, e:
        log_exception(e)
        return False
Esempio n. 12
0
def should_seed_users(server):
    log_verbose("See if we should seed users for server '%s'" % server.id)
    try:
        connection = server.get_db_connection()
        dbnames = connection.database_names()
        for dbname in dbnames:
            if connection[dbname]['system.users'].find_one():
                return False
        return True
    except Exception, e:
        log_exception(e)
        return False
Esempio n. 13
0
def prepend_global_admin_user(other_users, server):
    """
    When making lists of administrative users -- e.g., seeding a new server --
    it's useful to put the credentials supplied on the command line at the head
    of the queue.
    """
    cred0 = get_global_login_user(server, "admin")
    if cred0 and cred0["username"] and cred0["password"]:
        log_verbose("Seeding : CRED0 to the front of the line!")
        return [cred0] + other_users if other_users else [cred0]
    else:
        return other_users
Esempio n. 14
0
def prepend_global_admin_user(other_users, server):
    """
    When making lists of administrative users -- e.g., seeding a new server --
    it's useful to put the credentials supplied on the command line at the head
    of the queue.
    """
    cred0 = get_global_login_user(server, "admin")
    if cred0 and cred0["username"] and cred0["password"]:
        log_verbose("Seeding : CRED0 to the front of the line!")
        return [cred0] + other_users if other_users else [cred0]
    else:
        return other_users
Esempio n. 15
0
def should_seed_users(server):
    log_verbose("See if we should seed users for server '%s'" %
                server.id)
    try:
        connection = server.get_db_connection()
        dbnames = connection.database_names()
        for dbname in dbnames:
            if connection[dbname]['system.users'].find_one():
                return False
        return True
    except Exception, e:
        log_exception(e)
        return False
Esempio n. 16
0
def should_seed_db_users(server, dbname):
    log_verbose("See if we should seed users for database '%s'" % dbname)
    try:
        connection = server.get_db_connection()
        if connection[dbname]['system.users'].find_one():
            return False
        else:
            return True
    except Exception, e:
        log_exception(e)
        if is_auth_error(e) and dbname == "admin" and server.try_on_auth_failures():
            return True
        return False
Esempio n. 17
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))
Esempio n. 18
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))
Esempio n. 19
0
    def get_download_url(self, mongodb_version, mongodb_edition):
        template_args = get_template_args(mongodb_version, mongodb_edition)
        platform_spec = template_args["platform_spec"]
        os_dist_name = template_args["os_dist_name"]
        os_dist_version_no_dots = template_args["os_dist_version_no_dots"]
        os_name = template_args["os_name"]

        version_info = make_version_info(mongodb_version)
        if mongodb_edition == MongoDBEdition.COMMUNITY:
            archive_name = "mongodb-%s-%s.tgz" % (platform_spec,
                                                  mongodb_version)
            domain = "fastdl.mongodb.org"
        # community ssl from mongodb.org are supported only for version >= 3.0
        elif mongodb_edition == MongoDBEdition.COMMUNITY_SSL:
            if version_info >= VERSION_3_0:
                # Platforms that lack OS dist data (eg OS X) apparently don't
                # need it
                dist_bits = ""
                # Ones that have it, need it injected, i.e.
                # 'mongodb-linux-x86_64-debian71-3.2.9.tgz
                if os_dist_name is not None:
                    dist_bits = "-{0}{1}".format(os_dist_name,
                                                 os_dist_version_no_dots)
                archive_name = "mongodb-{0}{1}-{2}.tgz".format(
                    platform_spec, dist_bits, mongodb_version)
                domain = "fastdl.mongodb.org"
            else:
                return None
        elif mongodb_edition == MongoDBEdition.ENTERPRISE:
            if version_info and version_info >= VERSION_2_6_1:
                domain = "downloads.10gen.com"
                rel_name = "enterprise"
            else:
                rel_name = "subscription"
                domain = "downloads.mongodb.com"

            archive_name = ("mongodb-%s-%s-%s%s-%s.tgz" %
                            (platform_spec, rel_name, os_dist_name,
                             os_dist_version_no_dots, mongodb_version))

        else:
            raise MongoctlException("Unsupported edition '%s'" %
                                    mongodb_edition)

        url = "http://%s/%s/%s" % (domain, os_name, archive_name)
        log_verbose(
            "Download url for MongoDB %s (%s edition) OS %s, dist '%s': %s" %
            (mongodb_version, mongodb_edition, os_name, os_dist_name, url))

        return url
Esempio n. 20
0
def setup_db_users(server, db, db_users):
    count_new_users = 0
    for user in db_users :
        username = user['username']
        log_verbose("adding user '%s' to db '%s'" % (username, db.name))
        password = user.get('password')
        if not password:
            password = read_seed_password(db.name, username)

        _mongo_add_user(server, db, username, password)
        # if there is no login user for this db then set it to this new one
        db_login_user = server.get_login_user(db.name)
        if not db_login_user:
            server.set_login_user(db.name, username, password)
            # inc new users
        count_new_users += 1

    return count_new_users
Esempio n. 21
0
def setup_db_users(server, db, db_users):
    count_new_users = 0
    for user in db_users:
        username = user['username']
        log_verbose("adding user '%s' to db '%s'" % (username, db.name))
        password = user.get('password')
        if not password:
            password = read_seed_password(db.name, username)

        _mongo_add_user(server, db, username, password)
        # if there is no login user for this db then set it to this new one
        db_login_user = server.get_login_user(db.name)
        if not db_login_user:
            server.set_login_user(db.name, username, password)
            # inc new users
        count_new_users += 1

    return count_new_users
Esempio n. 22
0
    def get_download_url(self, mongodb_version, mongodb_edition):
        template_args = get_template_args(mongodb_version, mongodb_edition)
        platform_spec = template_args["platform_spec"]
        os_dist_name = template_args["os_dist_name"]
        os_dist_version_no_dots = template_args["os_dist_version_no_dots"]
        os_name = template_args["os_name"]

        version_info = make_version_info(mongodb_version)
        if mongodb_edition == MongoDBEdition.COMMUNITY:
            archive_name = "mongodb-%s-%s.tgz" % (platform_spec,
                                                  mongodb_version)
            domain = "fastdl.mongodb.org"
        # community ssl from mongodb.org are supported only for version >= 3.0
        elif mongodb_edition == MongoDBEdition.COMMUNITY_SSL:
            if version_info >= VERSION_3_0 and os_dist_name is not None:
                archive_name = "mongodb-%s-%s%s-%s.tgz" % (platform_spec, os_dist_name,
                                                           os_dist_version_no_dots, mongodb_version)
                domain = "fastdl.mongodb.org"
            else:
                return None
        elif mongodb_edition == MongoDBEdition.ENTERPRISE:
            if version_info and version_info >= VERSION_2_6_1:
                domain = "downloads.10gen.com"
                rel_name = "enterprise"
            else:
                rel_name = "subscription"
                domain = "downloads.mongodb.com"

            archive_name = ("mongodb-%s-%s-%s%s-%s.tgz" %
                            (platform_spec, rel_name, os_dist_name,
                             os_dist_version_no_dots,
                             mongodb_version))

        else:
            raise MongoctlException("Unsupported edition '%s'" %
                                    mongodb_edition)

        url = "http://%s/%s/%s" % (domain, os_name, archive_name)
        log_verbose("Download url for MongoDB %s (%s edition) OS %s, dist '%s': %s" %
                    (mongodb_version, mongodb_edition, os_name, os_dist_name, url))

        return url
Esempio n. 23
0
def setup_server_admin_users(server):

    admin_users = server.get_admin_users()
    if server.is_auth():
        admin_users = prepend_global_admin_user(admin_users, server)

    if admin_users is None or len(admin_users) < 1:
        log_verbose("No users configured for admin DB...")
        return 0

    root_user_added = setup_root_admin_user(server, admin_users)
    if not root_user_added:
        log_verbose("Not seeding users for database 'admin'")
        return 0

    log_verbose("Checking setup for other admin users...")
    count_new_users = 1
    try:
        admin_db = server.get_db("admin")
        count_new_users += setup_db_users(server, admin_db, admin_users[1:])
        return count_new_users
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up admin users on server '%s'."
            "\n Cause: %s" % (server.id, e))
Esempio n. 24
0
def setup_server_admin_users(server):

    admin_users = server.get_admin_users()
    if server.is_auth():
        admin_users = prepend_global_admin_user(admin_users, server)

    if admin_users is None or len(admin_users) < 1:
        log_verbose("No users configured for admin DB...")
        return 0

    root_user_added = setup_root_admin_user(server, admin_users)
    if not root_user_added:
        log_verbose("Not seeding users for database 'admin'")
        return 0

    log_verbose("Checking setup for other admin users...")
    count_new_users = 1
    try:
        admin_db = server.get_db("admin")
        count_new_users += setup_db_users(server, admin_db, admin_users[1:])
        return count_new_users
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up admin users on server '%s'."
            "\n Cause: %s" % (server.id, e))
Esempio n. 25
0
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)
        return 0

    db = server.get_db(dbname)

    try:
        any_new_user_added = setup_db_users(server, db, db_users)
        if not any_new_user_added:
            log_verbose("No new users added for database '%s'" % dbname)
        return any_new_user_added
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up users for '%s'" " database on server '%s'." "\n Cause: %s" % (dbname, server.id, e)
        )
Esempio n. 26
0
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)
        return 0

    db = server.get_db(dbname)

    try:
        any_new_user_added = setup_db_users(server, db, db_users)
        if not any_new_user_added:
            log_verbose("No new users added for database '%s'" % dbname)
        return any_new_user_added
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up users for '%s'" \
            " database on server '%s'."
            "\n Cause: %s" % (dbname, server.id, e))
Esempio n. 27
0
def setup_server_admin_users(server):

    if not should_seed_db_users(server, "admin"):
        log_verbose("Not seeding users for database 'admin'")
        return 0

    admin_users = server.get_admin_users()
    if server.is_auth():
        admin_users = prepend_global_admin_user(admin_users, server)

    if (admin_users is None or len(admin_users) < 1):
        log_verbose("No users configured for admin DB...")
        return 0

    log_verbose("Checking setup for admin users...")
    count_new_users = 0
    try:
        admin_db = server.get_db("admin")

        # potentially create the 1st admin user
        count_new_users += setup_db_users(server, admin_db, admin_users[0:1])

        # the 1st-time init case:
        # BEFORE adding 1st admin user, auth. is not possible --
        #       only localhost cxn gets a magic pass.
        # AFTER adding 1st admin user, authentication is required;
        #      so, to be sure we now have authenticated cxn, re-pull admin db:
        admin_db = server.get_db("admin")

        # create the rest of the users
        count_new_users += setup_db_users(server, admin_db, admin_users[1:])
        return count_new_users
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up admin users on server '%s'."
            "\n Cause: %s" % (server.id, e))
Esempio n. 28
0
def setup_server_admin_users(server):

    if not should_seed_db_users(server, "admin"):
        log_verbose("Not seeding users for database 'admin'")
        return 0

    admin_users = server.get_admin_users()
    if server.is_auth():
        admin_users = prepend_global_admin_user(admin_users, server)

    if (admin_users is None or len(admin_users) < 1):
        log_verbose("No users configured for admin DB...")
        return 0

    log_verbose("Checking setup for admin users...")
    count_new_users = 0
    try:
        admin_db = server.get_db("admin")

        # potentially create the 1st admin user
        count_new_users += setup_db_users(server, admin_db, admin_users[0:1])

        # the 1st-time init case:
        # BEFORE adding 1st admin user, auth. is not possible --
        #       only localhost cxn gets a magic pass.
        # AFTER adding 1st admin user, authentication is required;
        #      so, to be sure we now have authenticated cxn, re-pull admin db:
        admin_db = server.get_db("admin")

        # create the rest of the users
        count_new_users += setup_db_users(server, admin_db, admin_users[1:])
        return count_new_users
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up admin users on server '%s'."
            "\n Cause: %s" % (server.id, e))
Esempio n. 29
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)
Esempio n. 30
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)
Esempio n. 31
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)
Esempio n. 32
0
def setup_cluster_users(cluster, primary_server):
    log_verbose("Setting up cluster '%s' users using primary server '%s'" %
                (cluster.id, primary_server.id))
    return setup_server_users(primary_server)
Esempio n. 33
0

###############################################################################
def setup_server_local_users(server):

    seed_local_users = False
    try:
        local_db = server.get_db("local", retry=False)
        if not local_db['system.users'].find_one():
            seed_local_users = True
    except Exception, e:
        log_exception(e)
        pass

    if not seed_local_users:
        log_verbose("Not seeding users for database 'local'")
        return 0

    try:
        local_users = server.get_db_seed_users("local")
        if server.is_auth():
            local_users = prepend_global_admin_user(local_users, server)

        if local_users:
            return setup_db_users(server, local_db, local_users)
        else:
            return 0
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up local users on server '%s'."
Esempio n. 34
0
def setup_cluster_users(cluster, primary_server):
    log_verbose("Setting up cluster '%s' users using primary server '%s'" %
                (cluster.id, primary_server.id))
    return setup_server_users(primary_server)
Esempio n. 35
0
            "\n Cause: %s" % (server.id, e))

###############################################################################
def setup_server_local_users(server):

    seed_local_users = False
    try:
        local_db = server.get_db("local", retry=False)
        if not local_db['system.users'].find_one():
            seed_local_users = True
    except Exception, e:
        log_exception(e)
        pass

    if not seed_local_users:
        log_verbose("Not seeding users for database 'local'")
        return 0

    try:
        local_users = server.get_db_seed_users("local")
        if server.is_auth():
            local_users = prepend_global_admin_user(local_users, server)

        if local_users:
            return setup_db_users(server, local_db, local_users)
        else:
            return 0
    except Exception, e:
        log_exception(e)
        raise MongoctlException(
            "Error while setting up local users on server '%s'."
Esempio n. 36
0
def should_seed_db_users(server, dbname):
    log_verbose("See if we should seed users for database '%s'" % dbname)
    return not list_database_users(server, server.get_db(dbname))
Esempio n. 37
0
def should_seed_db_users(server, dbname):
    log_verbose("See if we should seed users for database '%s'" % dbname)
    return not list_database_users(server, server.get_db(dbname))