def find_all_executables(executable_name): # create a list of all available executables found and then return the best # match if applicable executables_found = [] ####### Look in $PATH path_executable = which(executable_name) if path_executable is not None: add_to_executables_found(executables_found, path_executable) #### Look in $MONGO_HOME if set mongo_home = os.getenv(MONGO_HOME_ENV_VAR) if mongo_home is not None: mongo_home = resolve_path(mongo_home) mongo_home_exe = get_mongo_home_exe(mongo_home, executable_name) add_to_executables_found(executables_found, mongo_home_exe) # Look in mongod_installs_dir if set mongo_installs_dir = config.get_mongodb_installs_dir() if mongo_installs_dir is not None: if os.path.exists(mongo_installs_dir): for mongo_installation in os.listdir(mongo_installs_dir): child_mongo_home = os.path.join(mongo_installs_dir, mongo_installation) child_mongo_exe = get_mongo_home_exe(child_mongo_home, executable_name) add_to_executables_found(executables_found, child_mongo_exe) return get_exe_version_tuples(executables_found)
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 download(url): log_info("Downloading %s..." % url) if which("curl"): download_cmd = ['curl', '-O'] if not is_interactive_mode(): download_cmd.append('-Ss') elif which("wget"): download_cmd = ['wget'] else: msg = ("Cannot download file.You need to have 'curl' or 'wget" "' command in your path in order to proceed.") raise MongoctlException(msg) download_cmd.append(url) call_command(download_cmd)
def extract_archive(archive_name): log_info("Extracting %s..." % archive_name) if not which("tar"): msg = ("Cannot extract archive.You need to have 'tar' command in your" " path in order to proceed.") raise MongoctlException(msg) tar_cmd = ['tar', 'xvf', archive_name] call_command(tar_cmd)
def push_mongodb(repo_name, mongodb_version, mongodb_edition=None, access_key=None, secret_key=None): """ :param repo_name: :param mongodb_version: :param mongodb_edition: :return: """ mongodb_edition = mongodb_edition or MongoDBEdition.COMMUNITY repo = get_binary_repository(repo_name) if access_key and isinstance(repo, S3MongoDBBinaryRepository): repo.access_key = access_key repo.secret_key = secret_key repo.validate() version_info = make_version_info(mongodb_version, mongodb_edition) mongodb_install_dir = get_mongo_installation(version_info) if not mongodb_install_dir: raise MongoctlException("No mongodb installation found for '%s'" % version_info) mongodb_install_home = os.path.dirname(mongodb_install_dir) target_archive_name = repo.get_archive_name(mongodb_version, mongodb_edition) target_archive_path = os.path.join(mongodb_install_home, target_archive_name) mongodb_install_dir_name = os.path.basename(mongodb_install_dir) log_info("Taring MongoDB at '%s'" % mongodb_install_dir_name) tar_exe = which("tar") tar_cmd = [tar_exe, "-cvzf", target_archive_name, mongodb_install_dir_name] call_command(tar_cmd, cwd=mongodb_install_home) log_info("Uploading tar to repo") repo.upload_file(mongodb_version, mongodb_edition, target_archive_path) # cleanup log_info("Cleanup") try: os.remove(target_archive_path) except Exception, e: log_error(str(e)) log_exception(e)
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 get_numactl_exe(): return which("numactl")