<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import pymongo <EOL> import time <EOL> import urllib2 <EOL> from optparse import OptionParser <EOL> import registrar <EOL> import updater <EOL> LEVELS = { '<STR_LIT>' : logging . DEBUG , <EOL> '<STR_LIT:info>' : logging . INFO , <EOL> '<STR_LIT>' : logging . WARNING , <EOL> '<STR_LIT:error>' : logging . ERROR , <EOL> '<STR_LIT>' : logging . CRITICAL } <EOL> INFO_URL = '<STR_LIT>' <EOL> def get_mongo_info ( ) : <EOL> """<STR_LIT>""" <EOL> server = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> port = os . environ . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> return ( server , port ) <EOL> def db_connect ( host , port ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( '<STR_LIT>' % ( host , port ) ) <EOL> return pymongo . Connection ( host , port ) <EOL> def db_disconnect ( conn ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( '<STR_LIT>' ) <EOL> conn . disconnect ( ) <EOL> def get_aws_data ( offline = False ) : <EOL> """<STR_LIT>""" <EOL> instance_id = None <EOL> local_ip = None <EOL> if offline : <EOL> return ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> while instance_id is None or local_ip is None : <EOL> instance_id = urllib2 . urlopen ( INFO_URL % '<STR_LIT>' ) . read ( ) <EOL> logging . debug ( '<STR_LIT>' % instance_id ) <EOL> local_ip = urllib2 . urlopen ( INFO_URL % '<STR_LIT>' ) . read ( ) <EOL> logging . debug ( '<STR_LIT>' % local_ip ) <EOL> return ( instance_id , local_ip ) <EOL> def hostname_exists ( db , hostname ) : <EOL> """<STR_LIT>""" <EOL> results = db . servers . find ( { '<STR_LIT>' : hostname } ) . count ( ) <EOL> if results > <NUM_LIT:0> : <EOL> logging . info ( '<STR_LIT>' % hostname ) <EOL> return True <EOL> else : <EOL> logging . info ( '<STR_LIT>' % hostname ) <EOL> return False <EOL> def get_hostname ( db , inst_id ) : <EOL> """<STR_LIT>""" <EOL> result = db . servers . find_one ( { '<STR_LIT>' : inst_id } ) <EOL> if result : <EOL> return result [ '<STR_LIT>' ] <EOL> else : <EOL> return None <EOL> def get_master ( db , service ) : <EOL> """<STR_LIT>""" <EOL> master_id = None <EOL> res = list ( db . servers . find ( <EOL> { '<STR_LIT>' : '<STR_LIT>' % service } ) ) <EOL> if len ( res ) > <NUM_LIT:1> : <EOL> logging . error ( '<STR_LIT>' % service ) <EOL> return None <EOL> if res : <EOL> master_id = res [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> return master_id <EOL> def check_master ( db , service , inst_id ) : <EOL> """<STR_LIT>""" <EOL> cur_master = get_master ( db , service ) <EOL> return cur_master == inst_id <EOL> def main ( ) : <EOL> usage = '<STR_LIT>' <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = '<STR_LIT>' ) <EOL> parser . add_option ( <EOL> '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , default = None , <EOL> help = '<STR_LIT>' ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> parser . error ( '<STR_LIT>' ) <EOL> level = LEVELS . get ( options . loglevel , logging . NOTSET ) <EOL> logging . basicConfig ( level = level ) <EOL> mserver , mport = get_mongo_info ( ) <EOL> if options . server : <EOL> mserver = options . server <EOL> conn = db_connect ( mserver , mport ) <EOL> db = conn . aerostat <EOL> if options . register or options . change_master : <EOL> reg = registrar . Registrar ( ) <EOL> reg . do_registrar ( db , options . dry_run , <EOL> options . change_master , options . offline ) <EOL> elif options . update_configs : <EOL> conf_db = conn . configs <EOL> config = configurer . Configurer ( ) <EOL> config . do_update ( conf_db , config_names = options . configs . split ( ) ) <EOL> else : <EOL> update = updater . Updater ( ) <EOL> if not options . daemon : <EOL> update . do_update ( db , options . dry_run , options . legacy ) <EOL> else : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> update . do_update ( db , options . dry_run , options . legacy ) <EOL> except pymongo . errors . AutoReconnect : <EOL> logging . error ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( mserver , str ( mport ) ) ) <EOL> time . sleep ( <NUM_LIT> ) <EOL> db_disconnect ( conn ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
