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)
def mk_server_dir(server): # ensure the dbpath dir exists server_dir = server.get_root_path() log_verbose("Ensuring that server's root path '%s' exists..." % server_dir) if ensure_dir(server.get_root_path()): log_verbose("server root path %s already exists!" % server_dir) return True else: log_verbose("server root path '%s' created successfully" % server_dir) return False
def mk_server_home_dir(server): # ensure server home dir exists if it has one server_dir = server.get_server_home() if not server_dir: return log_verbose("Ensuring that server's home dir '%s' exists..." % server_dir) if ensure_dir(server_dir): log_verbose("server home dir %s already exists!" % server_dir) else: log_verbose("server home dir '%s' created successfully" % server_dir)
def install_mongodb(mongodb_version=None, mongodb_edition=None, from_source=False, build_threads=1, build_tmp_dir=None, include_only=None): if mongodb_version is None: mongodb_version = fetch_latest_stable_version() log_info("Installing latest stable MongoDB version '%s'..." % mongodb_version) version_info = make_version_info(mongodb_version, mongodb_edition) mongo_installation = get_mongo_installation(version_info) mongodb_edition = version_info.edition if mongo_installation is not None: # no-op log_info("You already have MongoDB %s installed ('%s'). " "Nothing to do." % (version_info, mongo_installation)) return mongo_installation target_dir = get_install_target_dir(mongodb_version, mongodb_edition) if os.path.exists(target_dir): raise MongoctlException("Target directory '%s' already exists" % target_dir) if mongodb_edition not in MongoDBEdition.ALL: raise MongoctlException("Unknown edition '%s'. Please select from %s" % (mongodb_edition, MongoDBEdition.ALL)) if from_source: install_from_source(mongodb_version, mongodb_edition, build_threads=build_threads, build_tmp_dir=build_tmp_dir) return bits = platform.architecture()[0].replace("bit", "") os_name = platform.system().lower() if os_name == 'darwin' and platform.mac_ver(): os_name = "osx" mongodb_installs_dir = config.get_mongodb_installs_dir() if not mongodb_installs_dir: raise MongoctlException("No mongoDBInstallationsDirectory configured" " in mongoctl.config") # ensure the mongo installs dir ensure_dir(mongodb_installs_dir) platform_spec = get_validate_platform_spec(os_name, bits) log_verbose("INSTALL_MONGODB: OS='%s' , BITS='%s' , VERSION='%s', " "PLATFORM_SPEC='%s'" % (os_name, bits, version_info, platform_spec)) # XXX LOOK OUT! Two processes installing same version simultaneously => BAD. # TODO: mutex to protect the following try: ## download the url archive_path = download_mongodb_binary(mongodb_version, mongodb_edition) archive_name = os.path.basename(archive_path) mongo_dir_name = extract_archive(archive_name) # apply include_only if specified if include_only: apply_include_only(mongo_dir_name, include_only) log_info("Deleting archive %s" % archive_name) os.remove(archive_name) target_dir_name = os.path.basename(target_dir) os.rename(mongo_dir_name, target_dir_name) # move target to mongodb install dir (Unless target is already there! # i.e current working dir == mongodb_installs_dir if os.getcwd() != mongodb_installs_dir: log_info("Moving extracted folder to %s" % mongodb_installs_dir) shutil.move(target_dir_name, mongodb_installs_dir) install_dir = os.path.join(mongodb_installs_dir, mongo_dir_name) # install validation validate_mongodb_install(target_dir) log_info("MongoDB %s installed successfully!" % version_info) return install_dir except Exception, e: log_exception(e) msg = "Failed to install MongoDB '%s'. Cause: %s" % (version_info, e) raise MongoctlException(msg)
def install_from_source(mongodb_version, mongodb_edition, build_threads=None, build_tmp_dir=None): """ :param version: :param ssl: :param repo_name: The repo to use to generate archive name :return: """ version_info = make_version_info(mongodb_version, mongodb_edition) if build_tmp_dir: ensure_dir(build_tmp_dir) os.chdir(build_tmp_dir) allowed_build_editions = [MongoDBEdition.COMMUNITY, MongoDBEdition.COMMUNITY_SSL] if mongodb_edition not in allowed_build_editions: raise MongoctlException("build is only allowed for %s editions" % allowed_build_editions) log_info("Installing MongoDB '%s %s' from source" % (mongodb_version, mongodb_edition)) source_archive_name = "r%s.tar.gz" % mongodb_version target_dir = get_install_target_dir(mongodb_version, mongodb_edition) source_url = ("https://github.com/mongodb/mongo/archive/%s" % source_archive_name) response = urllib.urlopen(source_url) if response.getcode() != 200: msg = ("Unable to find a mongodb release for version '%s' in MongoDB" " github repo. See https://github.com/mongodb/mongo/releases " "for possible releases (response code '%s'). " % (mongodb_version, response.getcode())) raise MongoctlException(msg) log_info("Downloading MongoDB '%s' source from github '%s' ..." % (mongodb_version, source_url)) download_url(source_url) log_info("Extract source archive ...") source_dir = extract_archive(source_archive_name) log_info("Building with scons!") scons_exe = which("scons") if not scons_exe: raise MongoctlException("scons command not found in your path") scons_cmd = [scons_exe, "core", "tools", "install"] if build_threads: scons_cmd.extend(["-j", str(build_threads)]) scons_cmd.append("--prefix=%s" % target_dir) if mongodb_edition == MongoDBEdition.COMMUNITY_SSL: validate_openssl() scons_cmd.append("--ssl") log_info("Running scons command: %s" % " ".join(scons_cmd)) call_command(scons_cmd, cwd=source_dir) # cleanup log_info("Cleanup") try: os.remove(source_archive_name) shutil.rmtree(source_dir) except Exception, e: log_error(str(e)) log_exception(e)
def do_mongo_dump(host=None, port=None, dbpath=None, database=None, username=None, password=None, version_info=None, dump_options=None, ssl=False): # create dump command with host and port dump_cmd = [get_mongo_dump_executable(version_info)] # ssl options if ssl: dump_cmd.append("--ssl") if host: dump_cmd.extend(["--host", host]) if port: dump_cmd.extend(["--port", str(port)]) # dbpath if dbpath: dump_cmd.extend(["--dbpath", dbpath]) # database if database: dump_cmd.extend(["-d", database]) # username and password if username: dump_cmd.extend(["-u", username, "-p"]) if password: dump_cmd.append(password) # ignore authenticationDatabase option is version_info is less than 2.4.0 if (dump_options and "authenticationDatabase" in dump_options and version_info and version_info < MongoDBVersionInfo("2.4.0")): dump_options.pop("authenticationDatabase", None) # ignore dumpDbUsersAndRoles option is version_info is less than 2.6.0 if (dump_options and "dumpDbUsersAndRoles" in dump_options and version_info and version_info < MongoDBVersionInfo("2.6.0")): dump_options.pop("dumpDbUsersAndRoles", None) # append shell options if dump_options: dump_cmd.extend(options_to_command_args(dump_options)) # ensure destination dir if specified if dump_options and "out" in dump_options: ensure_dir(dump_options["out"]) cmd_display = dump_cmd[:] # mask user/password if username: cmd_display[cmd_display.index("-u") + 1] = "****" if password: cmd_display[cmd_display.index("-p") + 1] = "****" log_info("Executing command: \n%s" % " ".join(cmd_display)) call_command(dump_cmd, bubble_exit_code=True)
def do_install_mongodb(os_name, bits, version_info): mongodb_installs_dir = config.get_mongodb_installs_dir() if not mongodb_installs_dir: raise MongoctlException("No mongoDBInstallationsDirectory configured" " in mongoctl.config") platform_spec = get_validate_platform_spec(os_name, bits) log_verbose("INSTALL_MONGODB: OS='%s' , BITS='%s' , VERSION='%s', " "PLATFORM_SPEC='%s'" % (os_name, bits, version_info, platform_spec)) os_dist_name, os_dist_version = get_os_dist_info() if os_dist_name: dist_info = "(%s %s)" % (os_dist_name, os_dist_version) else: dist_info = "" log_info("Running install for %s %sbit %s to " "mongoDBInstallationsDirectory (%s)..." % (os_name, bits, dist_info, mongodb_installs_dir)) mongo_installation = get_mongo_installation(version_info) if mongo_installation is not None: # no-op log_info("You already have MongoDB %s installed ('%s'). " "Nothing to do." % (version_info, mongo_installation)) return mongo_installation url = get_download_url(os_name, platform_spec, os_dist_name, os_dist_version, version_info) archive_name = url.split("/")[-1] # Validate if the version exists response = urllib.urlopen(url) if response.getcode() != 200: msg = ("Unable to download from url '%s' (response code '%s'). " "It could be that version '%s' you specified does not exist." " Please double check the version you provide" % (url, response.getcode(), version_info)) raise MongoctlException(msg) mongo_dir_name = archive_name.replace(".tgz", "") install_dir = os.path.join(mongodb_installs_dir, mongo_dir_name) ensure_dir(mongodb_installs_dir) # XXX LOOK OUT! Two processes installing same version simultaneously => BAD. # TODO: mutex to protect the following if not dir_exists(install_dir): try: ## download the url download(url) extract_archive(archive_name) log_info("Moving extracted folder to %s" % mongodb_installs_dir) shutil.move(mongo_dir_name, mongodb_installs_dir) os.remove(archive_name) log_info("Deleting archive %s" % archive_name) log_info("MongoDB %s installed successfully!" % version_info) return install_dir except Exception, e: log_exception(e) log_error("Failed to install MongoDB '%s'. Cause: %s" % (version_info, e))
def do_install_mongodb(os_name, bits, version): if version is None: version = fetch_latest_stable_version() log_info("Installing latest stable MongoDB version '%s'..." % version) # validate version string elif not is_valid_version(version): raise MongoctlException("Invalid version '%s'. Please provide a" " valid MongoDB version." % version) mongodb_installs_dir = config.get_mongodb_installs_dir() if not mongodb_installs_dir: raise MongoctlException("No mongoDBInstallationsDirectory configured" " in mongoctl.config") platform_spec = get_validate_platform_spec(os_name, bits) log_info("Running install for %s %sbit to " "mongoDBInstallationsDirectory (%s)..." % (os_name, bits, mongodb_installs_dir)) mongo_installation = get_mongo_installation(version) if mongo_installation is not None: # no-op log_info("You already have MongoDB %s installed ('%s'). " "Nothing to do." % (version, mongo_installation)) return mongo_installation archive_name = "mongodb-%s-%s.tgz" % (platform_spec, version) url = "http://fastdl.mongodb.org/%s/%s" % (os_name, archive_name) # Validate if the version exists response = urllib.urlopen(url) if response.getcode() != 200: msg = ("Unable to download from url '%s' (response code '%s'). " "It could be that version '%s' you specified does not exist." " Please double check the version you provide" % (url, response.getcode(), version)) raise MongoctlException(msg) mongo_dir_name = "mongodb-%s-%s" % (platform_spec, version) install_dir = os.path.join(mongodb_installs_dir, mongo_dir_name) ensure_dir(mongodb_installs_dir) # XXX LOOK OUT! Two processes installing same version simultaneously => BAD. # TODO: mutex to protect the following if not dir_exists(install_dir): try: ## download the url download(url) extract_archive(archive_name) log_info("Moving extracted folder to %s" % mongodb_installs_dir) shutil.move(mongo_dir_name, mongodb_installs_dir) os.remove(archive_name) log_info("Deleting archive %s" % archive_name) log_info("MongoDB %s installed successfully!" % version) return install_dir except Exception, e: log_exception(e) log_error("Failed to install MongoDB '%s'. Cause: %s" % (version, e))