def mongo_exe_version(mongo_exe): mongod_path = os.path.join(os.path.dirname(mongo_exe), "mongod") try: re_expr = "v?((([0-9]+)\.([0-9]+)\.([0-9]+))([^, ]*))" vers_spew = execute_command([mongod_path, "--version"]) # only take first line of spew vers_spew_line = vers_spew.split('\n')[0] vers_grep = re.findall(re_expr, vers_spew_line) help_spew = execute_command([mongod_path, "--help"]) full_version = vers_grep[-1][0] if "subscription" in vers_spew or "enterprise" in vers_spew: edition = MongoDBEdition.ENTERPRISE elif "SSL" in help_spew: edition = MongoDBEdition.COMMUNITY_SSL else: edition = MongoDBEdition.COMMUNITY result = make_version_info(full_version, edition=edition) if result is not None: return result else: raise MongoctlException("Cannot parse mongo version from the" " output of '%s --version'" % mongod_path) except Exception, e: log_exception(e) raise MongoctlException("Unable to get mongo version of '%s'." " Cause: %s" % (mongod_path, e))
def validate_mongodb_install(install_dir): log_info("Verifying mongodb installation %s" % install_dir) mongod_exe = os.path.join(install_dir, "bin", "mongod") cmd = [mongod_exe, "--version"] try: execute_command(cmd) log_info("Validation passed!") except CalledProcessError, cpe: log_exception(cpe) raise MongoctlException("MongoDB installation failed. Validation command %s failed with error: %s" % (" ".join(cmd), cpe.output))
def tail_server_log(server): try: logpath = server.get_log_file_path() # touch log file to make sure it exists log_verbose("Touching log file '%s'" % logpath) execute_command(["touch", logpath]) tail_cmd = ["tail", "-f", logpath] log_verbose("Executing command: %s" % (" ".join(tail_cmd))) return create_subprocess(tail_cmd) except Exception, e: log_exception(e) log_error("Unable to tail server log file. Cause: %s" % e) return None
def ensure_mongo_home_not_used(mongo_installation): output = execute_command([ "ps", "-eaf" ]) if mongo_installation in output: msg = ("ERROR: Cannot uninstall '%s' because its currently being used. " "Please terminate all running processes then try again." % mongo_installation) raise MongoctlException(msg)
def mongo_exe_version(mongo_exe): try: re_expr = "v?((([0-9]+)\.([0-9]+)\.([0-9]+))([^, ]*))" vers_spew = execute_command([mongo_exe, "--version"]) # only take first line of spew vers_spew = vers_spew.split('\n')[0] vers_grep = re.findall(re_expr, vers_spew) full_version = vers_grep[-1][0] result = version_obj(full_version) if result is not None: return result else: raise MongoctlException("Cannot parse mongo version from the" " output of '%s --version'" % mongo_exe) except Exception, e: log_exception(e) raise MongoctlException("Unable to get mongo version of '%s'." " Cause: %s" % (mongo_exe, e))
def exec_assert_cmd(self, cmd, exit_code=0): print "++++++++++ Testing command : %s" % cmd try: output = execute_command(cmd, shell=True, cwd=get_mongoctl_module_dir()) print output return output except Exception, e: print("Error while executing test command '%s'. Cause: %s " % (cmd, e)) if isinstance(e, CalledProcessError): print "#### Command output ####" print e.output print "###################" print "================= STACK TRACE ================" traceback.print_exc() print "Failing..." self.fail()