<s> import yaml <EOL> import socket <EOL> import subprocess <EOL> MASTER_FILE = "<STR_LIT>" <EOL> DNS_IP_FILE = "<STR_LIT>" <EOL> def get_masters ( master_file = MASTER_FILE ) : <EOL> try : <EOL> f = open ( master_file , '<STR_LIT:r>' ) <EOL> mystr = f . read ( ) <EOL> mydict = yaml . load ( mystr ) <EOL> masters = mydict . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> raise <EOL> return masters <EOL> def lookup_ip ( dns_name ) : <EOL> try : <EOL> ipaddr = socket . gethostbyname ( dns_name ) <EOL> except : <EOL> raise <EOL> return ipaddr <EOL> def lookup_previous_ip_addrs ( dns_ip_file = DNS_IP_FILE ) : <EOL> previous = False <EOL> try : <EOL> f = open ( dns_ip_file , '<STR_LIT:r>' ) <EOL> previous = f . readlines ( ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" <EOL> return previous <EOL> def write_ip_addrs ( ip_addrs_list , dns_ip_file = DNS_IP_FILE ) : <EOL> f = open ( dns_ip_file , '<STR_LIT:w>' ) <EOL> for ip in ip_addrs_list : <EOL> f . write ( ip ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> f . close ( ) <EOL> return <EOL> def restart_service ( svc_name ) : <EOL> result = False <EOL> try : <EOL> output = subprocess . check_output ( [ "<STR_LIT>" , svc_name , "<STR_LIT>" ] ) <EOL> print "<STR_LIT>" % svc_name <EOL> except : <EOL> print "<STR_LIT>" % svc_name <EOL> raise <EOL> return <EOL> def main ( ) : <EOL> try : <EOL> result = lookup_previous_ip_addrs ( ) <EOL> masters = get_masters ( ) <EOL> ip_addrs = [ ] <EOL> if not result : <EOL> for m in masters : <EOL> ip = lookup_ip ( m ) <EOL> ip_addrs . append ( ip ) <EOL> write_ip_addrs ( ip_addrs , DNS_IP_FILE ) <EOL> else : <EOL> for m in masters : <EOL> ip = lookup_ip ( m ) <EOL> ip_addrs . append ( ip ) <EOL> previous_ip_addrs = lookup_previous_ip_addrs ( ) <EOL> for pip in previous_ip_addrs : <EOL> pip = pip . strip ( "<STR_LIT:\n>" ) <EOL> if pip not in ip_addrs : <EOL> write_ip_addrs ( ip_addrs , DNS_IP_FILE ) <EOL> restart_service ( '<STR_LIT>' ) <EOL> break <EOL> except : <EOL> raise <EOL> return <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> main ( ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" % e </s>
