<s> import logging <EOL> import re <EOL> import os <EOL> import socket <EOL> import subprocess <EOL> import yaml <EOL> import pystache <EOL> import requests <EOL> def write_certificates ( environment ) : <EOL> """<STR_LIT>""" <EOL> ssl_keys = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> if set ( ssl_keys ) <= set ( environment ) : <EOL> for k in ssl_keys : <EOL> with open ( environment [ k + '<STR_LIT>' ] , '<STR_LIT:w>' ) as f : <EOL> f . write ( environment [ k ] ) <EOL> else : <EOL> openssl_cmd = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> environment [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' , <EOL> environment [ '<STR_LIT>' ] , <EOL> ] <EOL> logging . info ( '<STR_LIT>' ) <EOL> p = subprocess . Popen ( openssl_cmd , shell = False , stdout = subprocess . PIPE , stderr = subprocess . STDOUT ) <EOL> output , _ = p . communicate ( ) <EOL> logging . debug ( output ) <EOL> os . chmod ( environment [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> def deep_update ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if type ( a ) is dict and type ( b ) is dict : <EOL> for key in b : <EOL> if key in a : <EOL> a [ key ] = deep_update ( a [ key ] , b [ key ] ) <EOL> else : <EOL> a [ key ] = b [ key ] <EOL> return a <EOL> if type ( a ) is list and type ( b ) is list : <EOL> return a + [ i for i in b if i not in a ] <EOL> return a if a is not None else b <EOL> TEMPLATE = '''<STR_LIT>''' <EOL> def get_instance_meta_data ( key ) : <EOL> try : <EOL> result = requests . get ( '<STR_LIT>' . format ( key ) , timeout = <NUM_LIT:2> ) <EOL> if result . status_code != <NUM_LIT:200> : <EOL> raise Exception ( '<STR_LIT>' . format ( result . status_code , result . text ) ) <EOL> return result . text <EOL> except Exception , e : <EOL> logging . debug ( '<STR_LIT>' . format ( key , e ) ) <EOL> def get_placeholders ( ) : <EOL> placeholders = dict ( os . environ ) <EOL> placeholders . setdefault ( '<STR_LIT>' , os . path . expanduser ( '<STR_LIT>' ) ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> placeholders . setdefault ( '<STR_LIT>' , os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> placeholders . setdefault ( '<STR_LIT>' , <NUM_LIT> ) <EOL> placeholders . setdefault ( '<STR_LIT>' , <NUM_LIT:30> ) <EOL> placeholders . setdefault ( '<STR_LIT>' , os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> placeholders . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> placeholders . setdefault ( '<STR_LIT>' , { } ) <EOL> placeholders [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , { } ) <EOL> os_memory_mb = os . sysconf ( '<STR_LIT>' ) * os . sysconf ( '<STR_LIT>' ) / <NUM_LIT> / <NUM_LIT> <EOL> placeholders [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' . format ( int ( os_memory_mb / <NUM_LIT:4> ) ) <EOL> placeholders [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = max ( <NUM_LIT:100> , int ( os_memory_mb / <NUM_LIT:30> ) ) <EOL> placeholders [ '<STR_LIT>' ] = dict ( ) <EOL> placeholders [ '<STR_LIT>' ] [ '<STR_LIT>' ] = get_instance_meta_data ( '<STR_LIT>' ) or socket . gethostbyname ( socket . gethostname ( ) ) <EOL> placeholders [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] = re . sub ( r'<STR_LIT>' , '<STR_LIT:_>' , get_instance_meta_data ( '<STR_LIT>' ) <EOL> or socket . gethostname ( ) ) <EOL> return placeholders <EOL> def write_configuration ( config , filename ) : <EOL> with open ( filename , '<STR_LIT:w>' ) as f : <EOL> f . write ( yaml . dump ( config , default_flow_style = False , width = <NUM_LIT> ) ) <EOL> def pystache_render ( * args , ** kwargs ) : <EOL> render = pystache . Renderer ( missing_tags = '<STR_LIT:strict>' ) <EOL> return render . render ( * args , ** kwargs ) <EOL> def get_dcs_config ( config , placeholders ) : <EOL> defaults = yaml . load ( '''<STR_LIT>''' . format ( ** config ) ) <EOL> config = { } <EOL> if '<STR_LIT>' in placeholders : <EOL> config = { '<STR_LIT>' : defaults [ '<STR_LIT>' ] } <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = yaml . load ( placeholders [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT>' in placeholders and '<STR_LIT>' in placeholders : <EOL> config = { '<STR_LIT>' : defaults [ '<STR_LIT>' ] } <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:port>' : placeholders [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : yaml . load ( placeholders [ '<STR_LIT>' ] ) } <EOL> elif '<STR_LIT>' in placeholders : <EOL> config = { '<STR_LIT>' : defaults [ '<STR_LIT>' ] } <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT:host>' ] = placeholders [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in placeholders : <EOL> config = { '<STR_LIT>' : defaults [ '<STR_LIT>' ] } <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] = placeholders [ '<STR_LIT>' ] <EOL> else : <EOL> pass <EOL> return config <EOL> def write_wale_command_environment ( placeholders ) : <EOL> az = get_instance_meta_data ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> if not os . path . exists ( placeholders [ '<STR_LIT>' ] ) : <EOL> os . makedirs ( placeholders [ '<STR_LIT>' ] ) <EOL> with open ( os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' . format ( ** placeholders ) ) <EOL> with open ( os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' . format ( az [ : - <NUM_LIT:1> ] ) ) <EOL> def write_crontab ( placeholders , path ) : <EOL> lines = [ '<STR_LIT>' . format ( path ) ] <EOL> lines += [ '<STR_LIT>' . format ( ** placeholders ) ] <EOL> lines += yaml . load ( placeholders [ '<STR_LIT>' ] ) <EOL> lines += [ '<STR_LIT>' ] <EOL> c = subprocess . Popen ( [ '<STR_LIT>' ] , stdin = subprocess . PIPE ) <EOL> c . communicate ( input = '<STR_LIT:\n>' . join ( lines ) . encode ( ) ) <EOL> def configure_patronictl ( patroni_configfile , patronictl_configfile ) : <EOL> if not os . path . exists ( os . path . dirname ( patronictl_configfile ) ) : <EOL> os . makedirs ( os . path . dirname ( patronictl_configfile ) ) <EOL> if not os . path . exists ( patronictl_configfile ) : <EOL> os . symlink ( patroni_configfile , patronictl_configfile ) <EOL> def main ( ) : <EOL> debug = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) in [ '<STR_LIT:1>' , '<STR_LIT:true>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> logging . basicConfig ( format = '<STR_LIT>' , level = ( '<STR_LIT>' <EOL> if debug else '<STR_LIT>' ) ) <EOL> if debug : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> os . execlpe ( '<STR_LIT>' , '<STR_LIT>' , dict ( os . environ ) ) <EOL> placeholders = get_placeholders ( ) <EOL> write_certificates ( placeholders ) <EOL> config = yaml . load ( pystache_render ( TEMPLATE , placeholders ) ) <EOL> config . update ( get_dcs_config ( config , placeholders ) ) <EOL> user_config = yaml . load ( os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) or { } <EOL> if not isinstance ( user_config , dict ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( type ( user_config ) ) ) <EOL> config = deep_update ( user_config , config ) <EOL> if not any ( [ '<STR_LIT>' in i for i in config [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] ) : <EOL> rep_hba = '<STR_LIT>' . format ( config [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:username>' ] ) <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , rep_hba ) <EOL> patroni_configfile = os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> write_configuration ( config , patroni_configfile ) <EOL> configure_patronictl ( patroni_configfile , os . path . join ( placeholders [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> write_wale_command_environment ( placeholders ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> write_crontab ( placeholders , os . environ . get ( '<STR_LIT>' ) ) <EOL> subprocess . call ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , placeholders [ '<STR_LIT>' ] , placeholders [ '<STR_LIT>' ] ] , <EOL> env = { '<STR_LIT>' : os . environ [ '<STR_LIT>' ] , '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> os . execlpe ( '<STR_LIT>' , '<STR_LIT>' , patroni_configfile , { '<STR_LIT>' : os . environ . get ( '<STR_LIT>' ) } ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
