<s> """<STR_LIT>""" <EOL> import os <EOL> import logging <EOL> import sys <EOL> import zipfile <EOL> from time import sleep <EOL> import requests <EOL> from pyk import toolkit <EOL> from pyk import util <EOL> PODS_UP_DELAY_IN_SEC = <NUM_LIT:5> <EOL> def _fmt_cmds ( cmds ) : <EOL> """<STR_LIT>""" <EOL> keys = sorted ( cmds . keys ( ) ) <EOL> fk = "<STR_LIT:\n>" <EOL> for k in keys : <EOL> fk += "<STR_LIT>" + k + "<STR_LIT>" <EOL> return fk <EOL> def _connect ( api_server , debug ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> pyk_client = toolkit . KubeHTTPClient ( kube_version = "<STR_LIT>" , api_server = api_server , debug = debug ) <EOL> pyk_client . execute_operation ( method = "<STR_LIT:GET>" , ops_path = "<STR_LIT>" ) <EOL> except : <EOL> print ( "<STR_LIT>" % ( api_server ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return pyk_client <EOL> def _visit ( dir_name , resource_name , cache_remotes = False ) : <EOL> """<STR_LIT>""" <EOL> flist = [ ] <EOL> logging . debug ( "<STR_LIT>" % dir_name ) <EOL> for _ , _ , file_names in os . walk ( dir_name ) : <EOL> for afile in file_names : <EOL> if not afile . endswith ( "<STR_LIT>" ) : <EOL> if afile . endswith ( "<STR_LIT>" ) : <EOL> logging . debug ( "<STR_LIT>" % ( resource_name , afile ) ) <EOL> flist . append ( afile ) <EOL> else : <EOL> logging . debug ( "<STR_LIT>" % ( afile ) ) <EOL> else : <EOL> remote_ref_file_name = os . path . join ( dir_name , afile ) <EOL> file_name = _download_remote ( remote_ref_file_name , do_cache = cache_remotes ) <EOL> logging . debug ( "<STR_LIT>" % ( resource_name , file_name ) ) <EOL> return flist <EOL> def _dump ( alist ) : <EOL> """<STR_LIT>""" <EOL> for litem in alist : <EOL> logging . info ( "<STR_LIT>" % litem ) <EOL> def _deploy ( pyk_client , namespace , here , dir_name , alist , resource_name , verbose ) : <EOL> """<STR_LIT>""" <EOL> for litem in alist : <EOL> file_name = os . path . join ( os . path . join ( here , dir_name ) , litem ) <EOL> if verbose : logging . info ( "<STR_LIT>" % ( resource_name , file_name ) ) <EOL> if resource_name == "<STR_LIT>" : <EOL> _ , res_path = pyk_client . create_svc ( manifest_filename = file_name , namespace = namespace ) <EOL> elif resource_name == "<STR_LIT>" : <EOL> _ , res_path = pyk_client . create_rc ( manifest_filename = file_name , namespace = namespace ) <EOL> if verbose : logging . info ( "<STR_LIT>" % ( res_path ) ) <EOL> _own_resource ( pyk_client , res_path , verbose ) <EOL> res = pyk_client . describe_resource ( res_path ) <EOL> logging . debug ( res . json ( ) ) <EOL> if resource_name == "<STR_LIT>" : <EOL> _own_pods_of_rc ( pyk_client , res , namespace , res_path , verbose ) <EOL> def _destroy ( pyk_client , namespace , here , dir_name , alist , resource_name , verbose ) : <EOL> """<STR_LIT>""" <EOL> for litem in alist : <EOL> file_name = os . path . join ( os . path . join ( here , dir_name ) , litem ) <EOL> if file_name . endswith ( "<STR_LIT>" ) : <EOL> file_name = _deref_remote ( file_name ) <EOL> if verbose : logging . info ( "<STR_LIT>" % ( resource_name , file_name ) ) <EOL> res_manifest , _ = util . load_yaml ( filename = file_name ) <EOL> res_name = res_manifest [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] <EOL> if resource_name == "<STR_LIT>" : <EOL> res_path = "<STR_LIT>" . join ( [ "<STR_LIT>" , namespace , "<STR_LIT>" , res_name ] ) <EOL> elif resource_name == "<STR_LIT>" : <EOL> res_path = "<STR_LIT>" . join ( [ "<STR_LIT>" , namespace , "<STR_LIT>" , res_name ] ) <EOL> res = pyk_client . describe_resource ( res_path ) <EOL> if res . status_code == <NUM_LIT> : <EOL> break <EOL> else : <EOL> resource = res . json ( ) <EOL> resource [ "<STR_LIT>" ] [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> if verbose : logging . info ( "<STR_LIT>" % ( res_path ) ) <EOL> pyk_client . execute_operation ( method = '<STR_LIT>' , ops_path = res_path , payload = util . serialize_tojson ( resource ) ) <EOL> else : return None <EOL> pyk_client . delete_resource ( resource_path = res_path ) <EOL> def _check_status ( pyk_client , resource_path ) : <EOL> """<STR_LIT>""" <EOL> res = pyk_client . describe_resource ( resource_path ) <EOL> logging . debug ( res . json ( ) ) <EOL> if res . status_code == <NUM_LIT:200> : return "<STR_LIT>" <EOL> else : return "<STR_LIT>" <EOL> def _own_resource ( pyk_client , resource_path , verbose ) : <EOL> """<STR_LIT>""" <EOL> res = pyk_client . describe_resource ( resource_path ) <EOL> resource = res . json ( ) <EOL> if "<STR_LIT>" in resource [ "<STR_LIT>" ] : <EOL> labels = resource [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> else : <EOL> labels = { } <EOL> labels [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> resource [ "<STR_LIT>" ] [ "<STR_LIT>" ] = labels <EOL> logging . debug ( "<STR_LIT>" % ( resource [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) ) <EOL> pyk_client . execute_operation ( method = '<STR_LIT>' , ops_path = resource_path , payload = util . serialize_tojson ( resource ) ) <EOL> def _own_pods_of_rc ( pyk_client , rc , namespace , rc_path , verbose ) : <EOL> """<STR_LIT>""" <EOL> if verbose : logging . info ( "<STR_LIT>" % ( PODS_UP_DELAY_IN_SEC , rc_path ) ) <EOL> sleep ( PODS_UP_DELAY_IN_SEC ) <EOL> pods_list = _get_pods_of_rc ( pyk_client , rc . json ( ) , namespace ) <EOL> for pod in pods_list : <EOL> logging . debug ( "<STR_LIT>" % ( pod [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) ) <EOL> _own_resource ( pyk_client , pod [ "<STR_LIT>" ] [ "<STR_LIT>" ] , verbose ) <EOL> def _get_pods_of_rc ( pyk_client , rc , namespace ) : <EOL> """<STR_LIT>""" <EOL> pods_selectors = rc [ "<STR_LIT>" ] [ "<STR_LIT>" ] <EOL> sel = "<STR_LIT>" <EOL> for k , v in pods_selectors . iteritems ( ) : <EOL> sel += "<STR_LIT>" . join ( [ k , "<STR_LIT>" , v , "<STR_LIT:U+002C>" ] ) <EOL> if sel . endswith ( "<STR_LIT:U+002C>" ) : <EOL> sel = sel [ : - <NUM_LIT:1> ] <EOL> pods_of_rc_path = "<STR_LIT>" . join ( [ "<STR_LIT>" , namespace , "<STR_LIT>" , sel ] ) <EOL> pods = pyk_client . execute_operation ( method = "<STR_LIT:GET>" , ops_path = pods_of_rc_path ) <EOL> pods_list = pods . json ( ) [ "<STR_LIT>" ] <EOL> return pods_list <EOL> def _create_ns ( pyk_client , namespace , verbose ) : <EOL> """<STR_LIT>""" <EOL> if namespace == "<STR_LIT:default>" : <EOL> return <EOL> else : <EOL> ns = { } <EOL> ns [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> ns [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> ns [ "<STR_LIT>" ] = { } <EOL> ns [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] = namespace <EOL> ns [ "<STR_LIT>" ] [ "<STR_LIT>" ] = { } <EOL> ns [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> if verbose : logging . info ( "<STR_LIT>" % ( ns ) ) <EOL> pyk_client . execute_operation ( method = '<STR_LIT:POST>' , ops_path = "<STR_LIT>" , payload = util . serialize_tojson ( ns ) ) <EOL> def _download_remote ( remote_ref_file_name , do_cache = False ) : <EOL> """<STR_LIT>""" <EOL> remote_content = "<STR_LIT>" <EOL> real_file_name = _deref_remote ( remote_ref_file_name ) <EOL> if do_cache : <EOL> if not os . path . exists ( real_file_name ) : <EOL> print "<STR_LIT>" % real_file_name <EOL> _download_by_URL ( remote_ref_file_name , real_file_name ) <EOL> else : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> else : <EOL> _download_by_URL ( remote_ref_file_name , real_file_name ) <EOL> return real_file_name <EOL> def _download_by_URL ( remote_ref_file_name , real_file_name ) : <EOL> """<STR_LIT>""" <EOL> with open ( remote_ref_file_name , "<STR_LIT:r>" ) as remote_ref_file : <EOL> res_URL = remote_ref_file . read ( ) . strip ( ) <EOL> remote_content = requests . get ( res_URL ) . text <EOL> logging . debug ( remote_content ) <EOL> with open ( real_file_name , "<STR_LIT:w>" ) as real_file : <EOL> real_file . write ( remote_content ) <EOL> def _deref_remote ( remote_ref_file_name ) : <EOL> """<STR_LIT>""" <EOL> real_file_name , _ = os . path . splitext ( remote_ref_file_name ) <EOL> logging . debug ( real_file_name ) <EOL> return real_file_name <EOL> def _export_init ( here , deployment_descriptor , archive_filename ) : <EOL> """<STR_LIT>""" <EOL> kployfile = deployment_descriptor <EOL> logging . debug ( "<STR_LIT>" % ( archive_filename ) ) <EOL> archive_file = zipfile . ZipFile ( archive_filename , mode = '<STR_LIT:w>' ) <EOL> logging . debug ( "<STR_LIT>" % ( kployfile ) ) <EOL> archive_file . write ( kployfile ) <EOL> return ( archive_filename , archive_file ) <EOL> def _export_add ( archive_file , filename ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( "<STR_LIT>" % ( filename ) ) <EOL> archive_file . write ( filename ) <EOL> def _export_done ( archive_file ) : <EOL> """<STR_LIT>""" <EOL> archive_file . close ( ) <EOL> def _init_from_archive ( archive_filename ) : <EOL> """<STR_LIT>""" <EOL> if zipfile . is_zipfile ( archive_filename ) : <EOL> archive_file = zipfile . ZipFile ( archive_filename , mode = '<STR_LIT:r>' ) <EOL> files = archive_file . namelist ( ) <EOL> logging . debug ( "<STR_LIT>" % ( files ) ) <EOL> for file in files : <EOL> try : <EOL> data = archive_file . read ( file ) <EOL> with open ( file , "<STR_LIT:w>" ) as f : <EOL> f . write ( data ) <EOL> logging . debug ( "<STR_LIT>" % ( file ) ) <EOL> except KeyError : <EOL> logging . debug ( "<STR_LIT>" % ( file ) ) <EOL> def _create_secrets ( pyk_client , app_name , namespace , secrets , verbose ) : <EOL> """<STR_LIT>""" <EOL> secret = { } <EOL> secret [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> secret [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> secret [ "<STR_LIT>" ] = { } <EOL> secret [ "<STR_LIT>" ] [ "<STR_LIT:name>" ] = "<STR_LIT>" <EOL> secret [ "<STR_LIT>" ] [ "<STR_LIT>" ] = { } <EOL> secret [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> secret [ "<STR_LIT:type>" ] = "<STR_LIT>" <EOL> secret [ "<STR_LIT:data>" ] = { } <EOL> for k , v in secrets . iteritems ( ) : <EOL> secret [ "<STR_LIT:data>" ] [ k ] = v <EOL> if verbose : logging . info ( "<STR_LIT>" % ( secret ) ) <EOL> secrets_path = "<STR_LIT>" . join ( [ "<STR_LIT>" , namespace , "<STR_LIT>" ] ) <EOL> pyk_client . execute_operation ( method = '<STR_LIT:POST>' , ops_path = secrets_path , payload = util . serialize_tojson ( secret ) ) <EOL> def _push_app_archive ( workspace , local_app_archive , registry_endpoint , verbose ) : <EOL> """<STR_LIT>""" <EOL> operation_path_URL = "<STR_LIT>" . join ( [ registry_endpoint , "<STR_LIT>" , "<STR_LIT>" , workspace ] ) <EOL> logging . debug ( "<STR_LIT>" % ( operation_path_URL ) ) <EOL> with open ( local_app_archive , "<STR_LIT:rb>" ) as laa : <EOL> archive_content = laa . read ( ) <EOL> res = requests . request ( "<STR_LIT:POST>" , operation_path_URL , data = archive_content ) <EOL> logging . debug ( "<STR_LIT>" % ( res . json ( ) ) ) <EOL> return res <EOL> def _list_apps ( workspace , registry_endpoint , verbose ) : <EOL> """<STR_LIT>""" <EOL> operation_path_URL = "<STR_LIT>" . join ( [ registry_endpoint , "<STR_LIT>" , "<STR_LIT>" , workspace ] ) <EOL> logging . debug ( "<STR_LIT>" % ( operation_path_URL ) ) <EOL> res = requests . request ( "<STR_LIT:GET>" , operation_path_URL ) <EOL> logging . debug ( "<STR_LIT>" % ( res . json ( ) ) ) <EOL> return res <EOL> def _download_app ( workspace , app_id , local_app_archive , registry_endpoint , verbose ) : <EOL> """<STR_LIT>""" <EOL> operation_path_URL = "<STR_LIT>" . join ( [ registry_endpoint , "<STR_LIT>" , app_id , "<STR_LIT>" , workspace ] ) <EOL> logging . debug ( "<STR_LIT>" % ( operation_path_URL ) ) <EOL> res = requests . request ( "<STR_LIT:GET>" , operation_path_URL ) <EOL> if res . status_code == <NUM_LIT:200> : <EOL> with open ( local_app_archive , "<STR_LIT:w>" ) as laa : <EOL> laa . write ( res . content ) <EOL> logging . debug ( "<STR_LIT>" % ( local_app_archive , res . content ) ) <EOL> return True <EOL> else : <EOL> logging . debug ( "<STR_LIT>" % ( res . status_code ) ) <EOL> return False </s>
