<s> import logging <EOL> import simplejson <EOL> from kingpin . kazoo_utils import KazooClientManager <EOL> from kingpin . config_utils import ZKBaseConfigManager <EOL> log = logging . getLogger ( __name__ ) <EOL> ZK_DOWNLOAD_DATA_PREFIX = None <EOL> METACONFIG_ZK_PATH_FORMAT = '<STR_LIT>' <EOL> DEPENDENCY_ZK_PATH_FORMAT = '<STR_LIT>' <EOL> ZK_DOWNLOAD_DATA_SERVERSET_FORMAT = '<STR_LIT>' <EOL> ZK_DOWNLOAD_DATA_CONFIGV2_FORMAT = '<STR_LIT>' <EOL> class MetaConfigManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , zk_hosts , aws_keyfile , s3_bucket , s3_endpoint = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . zk_hosts = zk_hosts <EOL> self . aws_keyfile = aws_keyfile <EOL> self . s3_bucket = s3_bucket <EOL> self . s3_endpoint = s3_endpoint <EOL> self . _kazoo_client ( ) . ensure_path ( "<STR_LIT>" ) <EOL> self . _kazoo_client ( ) . ensure_path ( "<STR_LIT>" ) <EOL> def _kazoo_client ( self ) : <EOL> kazoo_client = KazooClientManager ( self . zk_hosts , <EOL> start_timeout = <NUM_LIT> , <EOL> session_timeout = <NUM_LIT> ) . get_client ( ) <EOL> if not kazoo_client : <EOL> KazooClientManager ( self . zk_hosts ) . _reconnect ( ) <EOL> kazoo_client = KazooClientManager ( self . zk_hosts ) . get_client ( ) <EOL> return kazoo_client <EOL> def _construct_absolute_dependency_leaf_node ( self , source , destination ) : <EOL> destination_zk_path = DEPENDENCY_ZK_PATH_FORMAT . format ( destination ) <EOL> return "<STR_LIT>" . format ( destination_zk_path , source ) <EOL> def get_metaconfig_content ( self , metaconfig_name ) : <EOL> """<STR_LIT>""" <EOL> metaconfig_zk_path = METACONFIG_ZK_PATH_FORMAT . format ( metaconfig_name ) <EOL> if not self . _kazoo_client ( ) . exists ( metaconfig_zk_path ) : <EOL> return "<STR_LIT>" <EOL> return ZKBaseConfigManager ( self . zk_hosts , metaconfig_zk_path , <EOL> self . aws_keyfile , self . s3_bucket , <EOL> s3_endpoint = self . s3_endpoint ) . get_data ( ) <EOL> def add_to_dependency ( self , destination_dependency , new_member ) : <EOL> """<STR_LIT>""" <EOL> destination_zk_path = DEPENDENCY_ZK_PATH_FORMAT . format ( destination_dependency ) <EOL> self . _kazoo_client ( ) . ensure_path ( destination_zk_path ) <EOL> if new_member . endswith ( "<STR_LIT>" ) : <EOL> source_zk_path = DEPENDENCY_ZK_PATH_FORMAT . format ( new_member ) <EOL> else : <EOL> source_zk_path = METACONFIG_ZK_PATH_FORMAT . format ( new_member ) <EOL> if not self . _kazoo_client ( ) . exists ( source_zk_path ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> znode_path_added_to_dependency = self . _construct_absolute_dependency_leaf_node ( new_member , <EOL> destination_dependency ) <EOL> if self . _kazoo_client ( ) . exists ( znode_path_added_to_dependency ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> try : <EOL> returned_path = self . _kazoo_client ( ) . create ( znode_path_added_to_dependency ) <EOL> if returned_path == znode_path_added_to_dependency : <EOL> return <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" , znode_path_added_to_dependency , returned_path ) <EOL> except Exception as e : <EOL> raise Exception ( "<STR_LIT>" , e ) <EOL> def create_dependency ( self , dependency_name ) : <EOL> """<STR_LIT>""" <EOL> if not dependency_name . endswith ( "<STR_LIT>" ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> zk_path = DEPENDENCY_ZK_PATH_FORMAT . format ( dependency_name ) <EOL> self . _kazoo_client ( ) . ensure_path ( zk_path ) <EOL> def dependency_exists ( self , dependency_name ) : <EOL> """<STR_LIT>""" <EOL> dependency_zk_path = DEPENDENCY_ZK_PATH_FORMAT . format ( dependency_name ) <EOL> if self . _kazoo_client ( ) . exists ( dependency_zk_path ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def is_member_of_dependency ( self , dependency_name , metaconfig_name ) : <EOL> """<STR_LIT>""" <EOL> znode_path_added_to_dependency = self . _construct_absolute_dependency_leaf_node ( metaconfig_name , dependency_name ) <EOL> if self . _kazoo_client ( ) . exists ( znode_path_added_to_dependency ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def remove_dependency ( self , destination_dependency , member_to_remove ) : <EOL> """<STR_LIT>""" <EOL> destination_zk_path = DEPENDENCY_ZK_PATH_FORMAT . format ( destination_dependency ) <EOL> if not self . _kazoo_client ( ) . exists ( destination_zk_path ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> znode_path_deleted_from_dependency = self . _construct_absolute_dependency_leaf_node ( <EOL> member_to_remove , destination_dependency ) <EOL> if not self . _kazoo_client ( ) . exists ( znode_path_deleted_from_dependency ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> try : <EOL> self . _kazoo_client ( ) . delete ( znode_path_deleted_from_dependency ) <EOL> except Exception as e : <EOL> raise Exception ( "<STR_LIT>" , e ) <EOL> def get_dependency_members ( self , dependency_name ) : <EOL> """<STR_LIT>""" <EOL> dependency_path = DEPENDENCY_ZK_PATH_FORMAT . format ( dependency_name ) <EOL> return self . _kazoo_client ( ) . get_children ( dependency_path ) <EOL> def update_metaconfig ( self , name , content ) : <EOL> """<STR_LIT>""" <EOL> metaconfig_zk_path = METACONFIG_ZK_PATH_FORMAT . format ( name ) <EOL> try : <EOL> return ZKBaseConfigManager ( self . zk_hosts , metaconfig_zk_path , <EOL> self . aws_keyfile , self . s3_bucket , <EOL> s3_endpoint = self . s3_endpoint ) . update_zk ( None , content , force_update = True ) <EOL> except Exception as e : <EOL> raise Exception ( "<STR_LIT>" , e ) <EOL> def metaconfig_exists ( self , name ) : <EOL> """<STR_LIT>""" <EOL> metaconfig_zk_path = METACONFIG_ZK_PATH_FORMAT . format ( name ) <EOL> if self . _kazoo_client ( ) . exists ( metaconfig_zk_path ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def create_default_metaconfig_for_config ( self , name , config_zk_path ) : <EOL> """<STR_LIT>""" <EOL> self . _kazoo_client ( ) . ensure_path ( config_zk_path ) <EOL> download_command = self . construct_zk_download_data_command_for_config ( config_zk_path ) <EOL> metaconfig_content = self . construct_metaconfig_with_single_section ( <EOL> name , config_zk_path , download_command ) <EOL> try : <EOL> self . update_metaconfig ( name , metaconfig_content ) <EOL> except : <EOL> self . remove_metaconfig ( name ) <EOL> raise <EOL> return metaconfig_content <EOL> def create_default_metaconfig_for_serverset ( self , name , serverset_zk_path ) : <EOL> """<STR_LIT>""" <EOL> self . _kazoo_client ( ) . ensure_path ( serverset_zk_path ) <EOL> download_command = self . construct_zk_download_data_command_for_serverset ( serverset_zk_path ) <EOL> metaconfig_content = self . construct_metaconfig_with_single_section ( <EOL> name , serverset_zk_path , download_command , is_serverset = True ) <EOL> self . update_metaconfig ( name , metaconfig_content ) <EOL> return metaconfig_content <EOL> def update_metaconfig_with_dict ( self , name , metaconfig_content_dict ) : <EOL> """<STR_LIT>""" <EOL> content = simplejson . dumps ( metaconfig_content_dict ) <EOL> return self . create_metaconfig ( name , content ) <EOL> def _construct_single_metaconfig_section ( self , config_section_name , zk_path , <EOL> command , max_wait_in_secs = <NUM_LIT:0> , <EOL> is_serverset = False ) : <EOL> """<STR_LIT>""" <EOL> content_dict = { "<STR_LIT>" : config_section_name , <EOL> "<STR_LIT>" : zk_path , <EOL> "<STR_LIT>" : max_wait_in_secs , <EOL> "<STR_LIT>" : command } <EOL> content_dict [ '<STR_LIT:type>' ] = "<STR_LIT>" if is_serverset else '<STR_LIT>' <EOL> return content_dict <EOL> def construct_metaconfig_with_single_section ( self , config_section_name , zk_path , command , <EOL> max_wait_in_secs = <NUM_LIT:0> , is_serverset = False ) : <EOL> """<STR_LIT>""" <EOL> metaconfig = [ self . _construct_single_metaconfig_section ( <EOL> config_section_name , zk_path , command , max_wait_in_secs = max_wait_in_secs , <EOL> is_serverset = is_serverset ) ] <EOL> return simplejson . dumps ( metaconfig ) <EOL> def construct_zk_download_data_command_for_serverset ( self , serverset_zk_path , serverset_file_name = None ) : <EOL> if not serverset_zk_path . startswith ( "<STR_LIT>" ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not serverset_file_name : <EOL> serverset_file_name = "<STR_LIT:.>" . join ( serverset_zk_path . split ( "<STR_LIT:/>" ) [ <NUM_LIT:1> : ] ) <EOL> return ZK_DOWNLOAD_DATA_SERVERSET_FORMAT . format ( serverset_file_name , serverset_zk_path ) <EOL> def construct_zk_download_data_command_for_config ( self , config_zk_path , <EOL> config_local_filename = None , <EOL> s3_path = None ) : <EOL> if not config_zk_path . startswith ( "<STR_LIT>" ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not config_local_filename : <EOL> config_local_filename = "<STR_LIT:.>" . join ( config_zk_path . split ( "<STR_LIT:/>" ) [ <NUM_LIT:1> : ] ) <EOL> if not s3_path : <EOL> s3_path = "<STR_LIT>" . format ( config_zk_path ) <EOL> return ZK_DOWNLOAD_DATA_CONFIGV2_FORMAT . format ( config_local_filename , s3_path , config_zk_path ) <EOL> def remove_metaconfig ( self , metaconfig_name ) : <EOL> metaconfig_path = METACONFIG_ZK_PATH_FORMAT . format ( metaconfig_name ) <EOL> metaconfig_lock_path = metaconfig_path + "<STR_LIT>" <EOL> transaction = self . _kazoo_client ( ) . transaction ( ) <EOL> transaction . delete ( metaconfig_path ) <EOL> transaction . delete ( metaconfig_lock_path ) <EOL> transaction . commit ( ) <EOL> def get_all_metaconfigs ( self ) : <EOL> parent_node = "<STR_LIT>" <EOL> children = self . _kazoo_client ( ) . get_children ( parent_node ) <EOL> return [ child for child in children if not child . endswith ( "<STR_LIT>" ) ] <EOL> def get_all_dependencies ( self ) : <EOL> parent_node = "<STR_LIT>" <EOL> children = self . _kazoo_client ( ) . get_children ( parent_node ) <EOL> return [ child for child in children if not child . endswith ( "<STR_LIT>" ) ] </s>
