def is_online(self): try: self.new_db_connection() return True except Exception, e: log_exception(e) return False
def is_online(self): try: ping(self.get_mongo_client()) return True except (OperationFailure, AutoReconnect), ofe: log_exception(ofe) return "refused" not in str(ofe)
def is_local(self): try: server_host = self.get_host_address() return server_host is None or is_host_local(server_host) except Exception, e: log_exception(e) log_error("Unable to resolve address '%s' for server '%s'." " Cause: %s" % (self.get_host_address(), self.id, e))
def get_rs_status(self): try: rs_status_cmd = SON([('replSetGetStatus', 1)]) rs_status = self.db_command(rs_status_cmd, 'admin') return rs_status except (Exception,RuntimeError), e: log_debug("Cannot get rs status from server '%s'. cause: %s" % (self.id, e)) log_exception(e) return None
def is_online(self): log_debug("(BEGIN) is_online() for %s" % self.id) start_date = datetime.datetime.now() result = False try: self.new_default_mongo_client() result = True except (OperationFailure, AutoReconnect), ofe: log_exception(ofe) result = "refused" not in str(ofe)
def get_rs_config(self): try: return self.get_db("local")["system.replset"].find_one() except (Exception, RuntimeError), e: log_exception(e) if type(e) == MongoctlException: raise e else: log_verbose("Cannot get rs config from server '%s'. " "cause: %s" % (self.id, e)) return None
def has_connectivity_on(self, address): try: log_verbose("Checking if server '%s' is accessible on " "address '%s'" % (self.id, address)) self.make_db_connection(address) return True except Exception, e: log_exception(e) log_verbose("Check failed for server '%s' is accessible on " "address '%s': %s" % (self.id, address, e)) return False
def get_rs_status(self): try: rs_status_cmd = SON([('replSetGetStatus', 1)]) rs_status = self.db_command(rs_status_cmd, 'admin') return rs_status except (Exception, RuntimeError), e: log_debug("Cannot get rs status from server '%s'. cause: %s" % (self.id, e)) log_exception(e) return None
def make_db_connection(address): try: return Connection(address, socketTimeoutMS=CONN_TIMEOUT, connectTimeoutMS=CONN_TIMEOUT) except Exception, e: log_exception(e) error_msg = "Cannot connect to '%s'. Cause: %s" % \ (address, e) raise MongoctlException(error_msg, cause=e)
def make_db_connection(self, address): try: return Connection(address, socketTimeoutMS=CONN_TIMEOUT, connectTimeoutMS=CONN_TIMEOUT) except Exception, e: log_exception(e) error_msg = "Cannot connect to '%s'. Cause: %s" % \ (address, e) raise MongoctlException(error_msg,cause=e)
def get_rs_config(self): try: return self.get_db('local')['system.replset'].find_one() except (Exception, RuntimeError), e: log_exception(e) if type(e) == MongoctlException: raise e else: log_verbose("Cannot get rs config from server '%s'. " "cause: %s" % (self.id, e)) return None
def has_connectivity_on(self, address): try: log_verbose("Checking if server '%s' is accessible on " "address '%s'" % (self.id, address)) mongo_utils.mongo_client(address) return True except Exception, e: log_exception(e) log_verbose("Check failed for server '%s' is accessible on " "address '%s': %s" % (self.id, address, e)) return False
def prefer_use_ssl(self): if self.get_client_ssl_mode() != ClientSslMode.PREFER: return False log_debug("prefer_use_ssl() Checking if we prefer ssl for '%s'" % self.id) try: self.make_ssl_db_connection(self.get_connection_address()) return True except Exception, e: if not "SSL handshake failed" in str(e): log_exception(e) return None
def has_connectivity_on(self, address): try: log_verbose("Checking if server '%s' is accessible on " "address '%s'" % (self.id, address)) ping(pymongo.MongoClient(address, **DEFAULT_CLIENT_OPTIONS)) return True except Exception, e: log_exception(e) log_verbose("Check failed for server '%s' is accessible on " "address '%s': %s" % (self.id, address, e)) return False
def timeout_maybe_db_command(self, cmd, dbname): try: result = self.db_command(cmd, dbname) return result except Exception, e: log_exception(e) if "timed out" in str(e): log_warning("Command %s is taking a while to complete. " "This is not necessarily bad. " % document_pretty_string(cmd)) else: raise
def prefer_use_ssl(self): if self.get_client_ssl_mode() != ClientSslMode.PREFER: return False log_debug("prefer_use_ssl() Checking if we prefer ssl for '%s'" % self.id) try: self.new_ssl_test_mongo_client() return True except (OperationFailure, AutoReconnect), ofe: log_exception(ofe) return True
def prefer_use_ssl(self): if self.get_client_ssl_mode() != ClientSslMode.PREFER: return False log_debug("prefer_use_ssl() Checking if we prefer ssl for '%s'" % self.id) try: ping(self.new_mongo_client(ssl=True, **DEFAULT_CLIENT_OPTIONS)) return True except (OperationFailure, AutoReconnect), ofe: log_exception(ofe) return True
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 get_replication_info(self): try: ol = self.get_db("local")["oplog.rs"] first_op = ol.find(sort=[("$natural", 1)], limit=1).next() last_op = ol.find(sort=[("$natural", -1)], limit=1).next() first_ts = first_op["ts"] last_ts = last_op["ts"] return { "timeDiff": last_ts.time - first_ts.time, "timeDiffHours": (last_ts.time - first_ts.time) / 3600 } except Exception, ex: log_exception("Error during get_replication_info()")
def get_rs_config(self): try: return self.get_db('local')['system.replset'].find_one() except (Exception,RuntimeError), e: log_debug("Error whille trying to read rs config from " "server '%s': %s" % (self.id, e)) log_exception(e) if type(e) == MongoctlException: raise e else: log_verbose("Cannot get rs config from server '%s'. " "cause: %s" % (self.id, e)) return None
def get_member_rs_status(self): rs_status = self.get_rs_status() if rs_status: try: for member in rs_status['members']: if 'self' in member and member['self']: return member except (Exception, RuntimeError), e: log_debug("Cannot get member rs status from server '%s'." " cause: %s" % (self.id, e)) log_exception(e) return None
def get_member_rs_status(self): rs_status = self.get_rs_status() if rs_status: try: for member in rs_status['members']: if 'self' in member and member['self']: return member except (Exception,RuntimeError), e: log_debug("Cannot get member rs status from server '%s'." " cause: %s" % (self.id, e)) log_exception(e) return None
def needs_to_auth(self, dbname): """ Determines if the server needs to authenticate to the database. NOTE: we stopped depending on is_auth() since its only a configuration and may not be accurate """ log_debug("Checking if server '%s' needs to auth on db '%s'...." % (self.id, dbname)) try: conn = self.new_db_connection() db = conn[dbname] db.collection_names() result = False except (RuntimeError, Exception), e: log_exception(e) result = "authorized" in str(e)
def get_server_build_info(self): """ issues a buildinfo command """ if self.is_online(): try: return self.get_mongo_client().server_info() except OperationFailure, ofe: log_exception(ofe) if "there are no users authenticated" in str(ofe): # this is a pymongo 3.6.1 regression where the buildinfo command fails on non authenticated client # fall-back to an authenticated client admin_db = self.get_db("admin", no_auth=False) return admin_db.command("buildinfo") except Exception, e: log_exception(e)
def needs_to_auth(self, dbname): """ Determines if the server needs to authenticate to the database. NOTE: we stopped depending on is_auth() since its only a configuration and may not be accurate """ log_debug("Checking if server '%s' needs to auth on db '%s'...." % (self.id, dbname)) try: client = self.get_mongo_client() db = client.get_database(dbname) db.collection_names() result = False except (RuntimeError,Exception), e: log_exception(e) result = "authorized" in str(e)
def get_rs_config(self): rs_conf = None try: if self.version_greater_than_3_0(): rs_conf = self.db_command(SON([('replSetGetConfig', 1)]), "admin")["config"] else: rs_conf = self.get_db('local')['system.replset'].find_one() except (Exception,RuntimeError), e: log_debug("Error whille trying to read rs config from " "server '%s': %s" % (self.id, e)) log_exception(e) if type(e) == MongoctlException: raise e else: log_verbose("Cannot get rs config from server '%s'. " "cause: %s" % (self.id, e))
def needs_to_auth(self, dbname): """ Determines if the server needs to authenticate to the database. NOTE: we stopped depending on is_auth() since its only a configuration and may not be accurate """ log_debug("Checking if server '%s' needs to auth on db '%s'...." % (self.id, dbname)) try: client = self.get_mongo_client() db = client.get_database(dbname) db.collection_names() result = False except (RuntimeError,Exception), e: log_exception(e) # updated for to handle auth failures from mongodb 3.6 result = "authorized" in str(e) or "there are no users authenticated" in str(e)
def get_status(self, admin=False): status = {} ## check if the server is online try: self.new_default_mongo_client() status['connection'] = True # grab status summary if it was specified + if i am not an arbiter if admin: server_summary = self.get_server_status_summary() status["serverStatusSummary"] = server_summary except (RuntimeError, Exception), e: log_exception(e) status['connection'] = False status['error'] = "%s" % e if "timed out" in status['error'].lower(): status['timedOut'] = True
def get_status(self, admin=False): status = {} ## check if the server is online try: ping(self.get_mongo_client()) status['connection'] = True # grab status summary if it was specified + if i am not an arbiter if admin: server_summary = self.get_server_status_summary() status["serverStatusSummary"] = server_summary except (RuntimeError, Exception), e: log_exception(e) status['connection'] = False status['error'] = "%s" % e if "timed out" in status['error']: status['timedOut'] = True
def get_status(self, admin=False): status = {} ## check if the server is online try: self.get_db_connection() status["connection"] = True # grab status summary if it was specified + if i am not an arbiter if admin: server_summary = self.get_server_status_summary() status["serverStatusSummary"] = server_summary except (RuntimeError, Exception), e: log_exception(e) self.sever_db_connection() # better luck next time! status["connection"] = False status["error"] = "%s" % e if "timed out" in status["error"]: status["timedOut"] = True
def get_status(self, admin=False): status = {} ## check if the server is online try: self.get_db_connection() status['connection'] = True # grab status summary if it was specified + if i am not an arbiter if admin: server_summary = self.get_server_status_summary() status["serverStatusSummary"] = server_summary except (RuntimeError, Exception), e: log_exception(e) self.sever_db_connection() # better luck next time! status['connection'] = False status['error'] = "%s" % e if "timed out" in status['error']: status['timedOut'] = True
def get_status(self, admin=False): status = {} ## check if the server is online try: conn = self.get_db_connection() if conn is None: raise Exception("Cannot get server status") status['connection'] = True # grab status summary if it was specified + if i am not an arbiter if admin: server_summary = self.get_server_status_summary() status["serverStatusSummary"] = server_summary except (RuntimeError, Exception), e: log_exception(e) status['connection'] = False status['error'] = "%s" % e if "timed out" in status['error']: status['timedOut'] = True
def make_db_connection(address): """ Connect to a given database @param address: in the form host:port @return: pymongo connection """ if address is None: traceback.print_stack() print "db connection address: %s" % address try: tokens = address.split(":") host = tokens[0] port = tokens[1] return MongoClient(host=host, port=int(port), socketTimeoutMS=CONN_TIMEOUT, connectTimeoutMS=CONN_TIMEOUT) except Exception, e: log_exception(e) error_msg = "Cannot connect to '%s'. Cause: %s" % \ (address, e) raise MongoctlException(error_msg, cause=e)
def make_db_connection(self, address): try: kwargs = { "socketTimeoutMS": CONN_TIMEOUT, "connectTimeoutMS": CONN_TIMEOUT } use_ssl = self.use_ssl() if use_ssl: kwargs["ssl"] = True if use_ssl and self.ssl_key_file(): kwargs["ssl_keyfile"] = self.ssl_key_file() if use_ssl and self.ssl_cert_file(): kwargs["ssl_certfile"] = self.ssl_cert_file() return Connection(address, **kwargs) except Exception, e: log_exception(e) error_msg = "Cannot connect to '%s'. Cause: %s" % \ (address, e) raise MongoctlException(error_msg,cause=e)
def make_db_connection(self, address): try: kwargs = { "socketTimeoutMS": CONN_TIMEOUT, "connectTimeoutMS": CONN_TIMEOUT } use_ssl = self.use_ssl() if use_ssl: kwargs["ssl"] = True if use_ssl and self.ssl_key_file(): kwargs["ssl_keyfile"] = self.ssl_key_file() if use_ssl and self.ssl_cert_file(): kwargs["ssl_certfile"] = self.ssl_cert_file() return Connection(address, **kwargs) except Exception, e: log_exception(e) error_msg = "Cannot connect to '%s'. Cause: %s" % \ (address, e) raise MongoctlException(error_msg, cause=e)
def is_online(self): try: if self.get_db_connection() is not None: return True except Exception, e: log_exception(e)
def read_current_mongo_version(self): if self.is_online(): try: return self.get_mongo_client().server_info()['version'] except Exception, e: log_exception(e)
else None) return username, password ########################################################################### def is_online(self): log_debug("(BEGIN) is_online() for %s" % self.id) start_date = datetime.datetime.now() result = False try: self.new_default_mongo_client() result = True except (OperationFailure, AutoReconnect), ofe: log_exception(ofe) result = "refused" not in str(ofe) except ConnectionFailure, cfe: log_exception(cfe) result = "connection closed" in str(cfe) duration = timedelta_total_seconds(datetime.datetime.now() - start_date) log_debug("(BEGIN) is_online() for %s finished in %s seconds" % (self.id, duration)) return result ########################################################################### def can_function(self): status = self.get_status() if status['connection']: if 'error' not in status: return True else: log_verbose("Error while connecting to server '%s': %s " % (self.id, status['error']))
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))
if login_user: username = login_user["username"] password = (login_user["password"] if "password" in login_user else None) return username, password ########################################################################### def is_online(self): try: ping(self.get_mongo_client()) return True except (OperationFailure, AutoReconnect), ofe: log_exception(ofe) return "refused" not in str(ofe) except ConnectionFailure, cfe: log_exception(cfe) return "connection closed" in str(cfe) ########################################################################### def can_function(self): status = self.get_status() if status['connection']: if 'error' not in status: return True else: log_verbose("Error while connecting to server '%s': %s " % (self.id, status['error'])) ########################################################################### def is_online_locally(self): return self.is_use_local() and self.is_online()
elif client_ssl_mode == ClientSslMode.ALLOW: try: # attempt an ssl connection return self.make_plain_db_connection(address) except Exception, e: return self.make_ssl_db_connection(address) else: ## PREFER try: # attempt an ssl connection return self.make_ssl_db_connection(address) except Exception, e: return self.make_plain_db_connection(address) except Exception, e: log_exception(e) error_msg = "Cannot connect to '%s'. Cause: %s" % \ (address, e) raise MongoctlException(error_msg,cause=e) ########################################################################### def make_ssl_db_connection(self, address): kwargs = { "ssl": True } if self.ssl_key_file(): kwargs["ssl_keyfile"] = self.ssl_key_file() kwargs["ssl_certfile"] = self.ssl_cert_file()