<s> __author__ = '<STR_LIT>' <EOL> import server <EOL> from mongoctl . utils import resolve_path <EOL> from mongoctl . mongoctl_logging import log_verbose , log_debug , log_exception , log_warning <EOL> from bson . son import SON <EOL> from mongoctl . errors import MongoctlException <EOL> from replicaset_cluster import ReplicaSetCluster , get_member_repl_lag <EOL> from sharded_cluster import ShardedCluster <EOL> DEFAULT_DBPATH = '<STR_LIT>' <EOL> LOCK_FILE_NAME = "<STR_LIT>" <EOL> class MongodServer ( server . Server ) : <EOL> def __init__ ( self , server_doc ) : <EOL> super ( MongodServer , self ) . __init__ ( server_doc ) <EOL> def get_db_path ( self ) : <EOL> dbpath = self . get_cmd_option ( "<STR_LIT>" ) <EOL> if not dbpath : <EOL> dbpath = super ( MongodServer , self ) . get_server_home ( ) <EOL> if not dbpath : <EOL> dbpath = DEFAULT_DBPATH <EOL> return resolve_path ( dbpath ) <EOL> def get_server_home ( self ) : <EOL> """<STR_LIT>""" <EOL> home_dir = super ( MongodServer , self ) . get_server_home ( ) <EOL> if not home_dir : <EOL> home_dir = self . get_db_path ( ) <EOL> return home_dir <EOL> def export_cmd_options ( self , options_override = None , standalone = False ) : <EOL> """<STR_LIT>""" <EOL> cmd_options = super ( MongodServer , self ) . export_cmd_options ( <EOL> options_override = options_override ) <EOL> cmd_options [ '<STR_LIT>' ] = self . get_db_path ( ) <EOL> if '<STR_LIT>' in cmd_options : <EOL> cmd_options [ '<STR_LIT>' ] = resolve_path ( cmd_options [ '<STR_LIT>' ] ) <EOL> repl_cluster = self . get_replicaset_cluster ( ) <EOL> if repl_cluster is not None : <EOL> if "<STR_LIT>" not in cmd_options : <EOL> cmd_options [ "<STR_LIT>" ] = repl_cluster . id <EOL> if standalone : <EOL> if "<STR_LIT>" in cmd_options : <EOL> del cmd_options [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in cmd_options : <EOL> del cmd_options [ "<STR_LIT>" ] <EOL> if self . is_config_server ( ) : <EOL> cmd_options [ "<STR_LIT>" ] = True <EOL> if self . is_shard_server ( ) : <EOL> cmd_options [ "<STR_LIT>" ] = True <EOL> return cmd_options <EOL> def get_seed_users ( self ) : <EOL> """<STR_LIT>""" <EOL> seed_users = super ( MongodServer , self ) . get_seed_users ( ) <EOL> if seed_users and self . is_config_server ( ) : <EOL> for dbname in seed_users . keys ( ) : <EOL> if dbname not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> del seed_users [ dbname ] <EOL> return seed_users <EOL> def get_lock_file_path ( self ) : <EOL> return self . get_default_file_path ( LOCK_FILE_NAME ) <EOL> def is_master ( self ) : <EOL> return self . get_cmd_option ( "<STR_LIT>" ) <EOL> def is_slave ( self ) : <EOL> return self . get_cmd_option ( "<STR_LIT>" ) <EOL> def set_auth ( self , auth ) : <EOL> self . set_cmd_option ( "<STR_LIT>" , auth ) <EOL> def is_administrable ( self ) : <EOL> return not self . is_arbiter_server ( ) and self . can_function ( ) <EOL> def get_status ( self , admin = False ) : <EOL> status = super ( MongodServer , self ) . get_status ( admin = admin ) <EOL> if "<STR_LIT:error>" not in status and admin : <EOL> rs_summary = self . get_rs_status_summary ( ) <EOL> if rs_summary : <EOL> status [ "<STR_LIT>" ] = rs_summary <EOL> return status <EOL> def get_rs_status_summary ( self ) : <EOL> if self . is_replicaset_member ( ) : <EOL> member_rs_status = self . get_member_rs_status ( ) <EOL> if member_rs_status : <EOL> return { <EOL> "<STR_LIT:name>" : member_rs_status [ '<STR_LIT:name>' ] , <EOL> "<STR_LIT>" : member_rs_status [ '<STR_LIT>' ] <EOL> } <EOL> def is_cluster_connection_member ( self ) : <EOL> return not self . is_arbiter_server ( ) <EOL> def is_arbiter_server ( self ) : <EOL> cluster = self . get_cluster ( ) <EOL> return ( isinstance ( cluster , ReplicaSetCluster ) and <EOL> cluster . get_member_for ( self ) . is_arbiter ( ) ) <EOL> def is_replicaset_member ( self ) : <EOL> cluster = self . get_cluster ( ) <EOL> return isinstance ( cluster , ReplicaSetCluster ) <EOL> def get_replicaset_cluster ( self ) : <EOL> cluster = self . get_cluster ( ) <EOL> if isinstance ( cluster , ReplicaSetCluster ) : <EOL> return cluster <EOL> def is_config_server ( self ) : <EOL> cluster = self . get_cluster ( ) <EOL> return ( ( isinstance ( cluster , ShardedCluster ) and <EOL> cluster . has_config_server ( self ) ) or <EOL> self . get_cmd_option ( "<STR_LIT>" ) ) <EOL> def is_shard_server ( self ) : <EOL> cluster = self . get_cluster ( ) <EOL> if isinstance ( cluster , ShardedCluster ) : <EOL> return cluster . has_shard ( self ) <EOL> elif isinstance ( cluster , ReplicaSetCluster ) : <EOL> return cluster . is_shard_member ( ) <EOL> def command_needs_auth ( self , dbname , cmd ) : <EOL> if "<STR_LIT>" in cmd or "<STR_LIT>" in cmd : <EOL> return False <EOL> if '<STR_LIT>' in cmd and self . is_arbiter_server ( ) : <EOL> return False <EOL> return super ( MongodServer , self ) . command_needs_auth ( dbname , cmd ) <EOL> def get_mongo_uri_template ( self , db = None ) : <EOL> if not db : <EOL> if self . is_auth ( ) : <EOL> db = "<STR_LIT>" <EOL> else : <EOL> db = "<STR_LIT>" <EOL> else : <EOL> db = "<STR_LIT:/>" + db <EOL> creds = "<STR_LIT>" if self . is_auth ( ) else "<STR_LIT>" <EOL> return "<STR_LIT>" % ( creds , self . get_address_display ( ) , db ) <EOL> def get_rs_status ( self ) : <EOL> try : <EOL> rs_status_cmd = SON ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) ] ) <EOL> rs_status = self . db_command ( rs_status_cmd , '<STR_LIT>' ) <EOL> return rs_status <EOL> except ( Exception , RuntimeError ) , e : <EOL> log_debug ( "<STR_LIT>" % <EOL> ( self . id , e ) ) <EOL> log_exception ( e ) <EOL> return None <EOL> def get_member_rs_status ( self ) : <EOL> rs_status = self . get_rs_status ( ) <EOL> if rs_status : <EOL> try : <EOL> for member in rs_status [ '<STR_LIT>' ] : <EOL> if '<STR_LIT>' in member and member [ '<STR_LIT>' ] : <EOL> return member <EOL> except ( Exception , RuntimeError ) , e : <EOL> log_debug ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . id , e ) ) <EOL> log_exception ( e ) <EOL> return None <EOL> def is_primary ( self ) : <EOL> master_result = self . is_master_command ( ) <EOL> if master_result : <EOL> return master_result . get ( "<STR_LIT>" ) <EOL> def is_secondary ( self ) : <EOL> master_result = self . is_master_command ( ) <EOL> if master_result : <EOL> return master_result . get ( "<STR_LIT>" ) <EOL> def is_master_command ( self ) : <EOL> try : <EOL> if self . is_online ( ) : <EOL> result = self . db_command ( { "<STR_LIT>" : <NUM_LIT:1> } , "<STR_LIT>" ) <EOL> return result <EOL> except ( Exception , RuntimeError ) , e : <EOL> log_verbose ( "<STR_LIT>" % <EOL> ( self . id , e ) ) <EOL> def has_joined_replica ( self ) : <EOL> master_result = self . is_master_command ( ) <EOL> if master_result : <EOL> return ( master_result . get ( "<STR_LIT>" ) is not None or <EOL> master_result . get ( "<STR_LIT>" ) or <EOL> master_result . get ( "<STR_LIT>" ) or <EOL> master_result . get ( "<STR_LIT>" ) ) <EOL> def get_repl_lag ( self , master_status ) : <EOL> """<STR_LIT>""" <EOL> member_status = self . get_member_rs_status ( ) <EOL> if not member_status : <EOL> raise MongoctlException ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> self . id ) <EOL> return get_member_repl_lag ( member_status , master_status ) </s>
