x[h;>dZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z mZmZddlmZddlmZddlmZmZmZdZej2eZdZd Zd Zd Zd Z d Z!dZ"dZ#dZ$dZ%dZ&dZ'dZ(GddejRZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3d Z4d!Z5d"Z6d#Z7d$Z8d%Z9d&Z:d'Z;d(Z'>??r,cd\}}}|jD]-\}}}|r ts|\}}}|s|s|s&||_n|jstj dytj d|j tt||_ ||_ ||_ |j|js|js |jryy)a _get_data loads the metadata, userdata, and vendordata from one of the following locations in the given order: * envvars * guestinfo * imc Please note when updating this function with support for new data transports, the order should match the order in the dscheck_VMware function from the file ds-identify. NNNz)failed to find a valid data access methodFzusing data access method %sT) r%is_vmware_platformrLOGdebuginfo_get_subplatformprocess_metadataload_json_or_yamlmetadata userdata_rawvendordata_raw redact_keys)r&mdudvdr get_data_fnrequire_vmware_platforms r+ _get_datazDataSourceVMware._get_datas"& B 2 2    #&/A/C&=LRRR2*<' && IIA B .0E0E0GH)):2)>? !  ==D--1D1Dr,ct|j}tjd|t ||j |j tj|j|g|_|jy)a(setup(is_new_instance) This is called before user-data and vendor-data have been processed. Unless the datasource has set mode to 'local', then networking per 'fallback' or per 'network_config' will have been written and brought up the OS at this point. zgot host-info: %sN) wait_on_networkrCr=r?advertise_local_ip_addrsrrr mergemanydictpersist_instance_data)r&is_new_instance host_infos r+setupzDataSourceVMware.setupsb$DMM2  $i0 !DLL$//J **DMM9+EF ""$r,cd}|jtk(rt}nD|jtk(rt}n*|jt k(rt }ntjS|jd|ddS)Nz (rC)) rrget_guestinfo_envvar_key_namer!get_guestinfo_key_namer#get_imc_key_namerMETADATA_UNKNOWN)r&get_key_name_fns r+r@z!DataSourceVMware._get_subplatformsl  " "&? ?;O  $ $(D D4O  $ $(> >.O++ +  # # J '  r,c|jSN)rr&s r+get_config_objzDataSourceVMware.get_config_objs xxr,cd|jvrtjdn>tjdd|jj i|jd<|jddS)Nnetworkzusing metadata network configzusing fallback network configconfig)rCr=r>r(generate_fallback_configr^s r+network_configzDataSourceVMware.network_configs`  % II5 6 II5 6$++>>@(DMM) $}}Y'11r,c|jrd|jvr|jdStjtj j |jd<|jdS)Nz instance-id)rCrload_text_filePRODUCT_UUID_FILE_PATHrstriplowerr^s r+get_instance_idz DataSourceVMware.get_instance_id se ==]dmm;==/ /    6 7 > > @ F F H m$}}]++r,c|jrB|jtk(r/tr%tj d|j yy)Nz"Cache fallback is allowed for : %sTF)rr#r<r=r>r@r^s r+check_if_fallback_is_allowedz-DataSourceVMware.check_if_fallback_is_allowedsB  # #''+AA"$ II4d6K6K6M r,cxdD]4}||jvstj|j|cSgS)N)zpublic-keys-datapublic_keys_dataz public-keys public_keys)rCrnormalize_pubkey_data)r&key_names r+get_public_ssh_keysz$DataSourceVMware.get_public_ssh_keyssD  NH 4==(44T]]85LMM N r,cd}t|jvr|jt}n%t|jvr|jt}|jtk(r"t ||j |jyyr])REDACTrCCLEANUP_GUESTINFOrr!guestinfo_redact_keysrr)r&keys_to_redacts r+rFzDataSourceVMware.redact_keys*sh T]] "!]]62N $-- /!]]+<=N  " "&B B ! doo  Cr,cd\}}}tjjtdr!t d}t d}t d}|||fS)z< check to see if there is data via env vars r;rCuserdata vendordata)osenvirongetrguestinfo_envvar)r&rGrHrIs r+r z#DataSourceVMware.get_envvar_data_fn8sK& B ::>>- ,!*-B!*-B!,/BB|r,c8td}td}|r(||_t|_tj d||r*|s||_t |_tj d||jstj dyd} ||j|jS#t$r}tjtd|j||r|j|k(rYd }~y|stjd Yd }~ytjd ||_t |_ ||j|jcYd }~S#t$r-tjtd|j|YYd }~ywxYwd }~wwxYw) zK check to see if there is data via the guestinfo transport vmtoolsdzvmware-rpctoolzdiscovered vmware-rpctool: %szdiscovered vmtoolsd: %szno rpctool discoveredr;cd\}}}tjd|td||}td||}td||}|||fS)Nr;zquery guestinfo with %srCrzr{)r=r?r)rrrGrHrIs r+query_guestinfoz?DataSourceVMware.get_guestinfo_data_fn..query_guestinfo`sR)JBB HH. 8:w ;B:w ;B<*=Br2: r,z%Failed to query guestinfo with %s: %sNz$vmtoolsd fallback option not presentzfallback to vmtoolsd) r rexec_vmware_rpctoolrr=r> exec_vmtoolsd Exceptionrlogexcr?)r&rvmware_rpctoolrerrors r+r"z&DataSourceVMware.get_guestinfo_data_fnDsP $/0 )DL1DO II5~ F "' "/ II/ :|| II- .% & *#4<<A A" * KK7   DLLH4)?@) HH+ ,#DL+DO *&t||T__EE * ;LL * *5" *sHB00 F97F5F'F9EF/F FFFFcd\}}}tj|j}tj|j}|s|st j d|||fStjs|||fStj|j}||||fStjj|}tj|}tj|\} } | rr|rpt j dtj|jj |jj#d|||j$\}}}|_nH| r1|r/t j dtj(|\}}}nt j dt+j,||||fS)zN check to see if there is data via vmware guest customization r;z-Customization for VMware platform is disabledz3Getting data via VMware customization configurationscriptszFGetting data via VMware raw cloudinit data customization configurationz1No allowed customization configuration data found)r is_vmware_cust_enabledr'is_raw_data_cust_enabledds_cfgr=r>is_cust_plugin_availableget_cust_cfg_filer|pathdirnameparse_cust_cfgget_cust_cfg_typeget_data_from_imc_cust_cfgr) cloud_dir get_cpathr(r#get_data_from_imc_raw_data_cust_cfgrdel_dir) r&rGrHrIallow_vmware_custallow_raw_data_cust cust_cfg_file cust_cfg_dircust_cfgis_vmware_cust_cfgis_raw_data_cust_cfgs r+r$z DataSourceVMware.get_imc_data_fns& B+AA$,,O,EE KK !)< IIE FB< 668B< '88E  B< ww}5 !00?  , ,X 6   "3 IIK L%3%N%N $$ $$Y/ & "RR"&9 II. *MMLRR III J \"B|r,r])__name__ __module__ __qualname____doc__dsnamerintr1r8rLrTr@r_propertyrdrjrlrrrFr r"r$ __classcell__)r5s@r+rr9s|"HF *4@7r%8 &22,    J*X7r,rctjd}|tjdyd|j vrtjdyy)Nzsystem-product-namezNo system-product-name foundFvmwarezNot a VMware platformT)r read_dmi_datar=r>ri) system_types r+r<r<sM##$9:K 01 **, , )* r,cxtjd||d}|dvr@tjd||tjt j |}nI|dvr-tjd||t j |}ntjd||}tj |S)z decode returns the decoded string value of data key is a string used to identify the data being decoded in log messages z'Getting encoded data for key=%s, enc=%sN)z gzip+base64zgz+b64zDecoding %s format %s)base64b64zPlain-text data %s)r=r>r decomp_gziprb64d decode_binary)keyenc_typedataraw_datas r+decoders II7hGH,, )8S9##M$6$6t$<= & & )8S9 %%d+ &,   h ''r,chtj|}|j}|r |tk(ry|S)a  get_none_if_empty_val returns None if the provided value, once stripped of its trailing whitespace, is empty or equal to GUESTINFO_EMPTY_YAML_VAL. The return value is always a string, regardless of whether the input is a bytes class or a string. N)rrrhGUESTINFO_EMPTY_YAML_VAL)vals r+get_none_if_empty_valrs2   S !C **,C S44 Jr,c|r|r|sy|jt}|r(tt|||tj d||jt }|r)tt |||tj d|yy)z advertise_local_ip_addrs gets the local IP address information from the provided host_info map and sets the addresses in the guestinfo namespace Nz-advertised local ipv4 address %s in guestinfoz-advertised local ipv6 address %s in guestinfo)r~ LOCAL_IPV4guestinfo_set_valuer=r? LOCAL_IPV6)rSrr local_ipv4 local_ipv6s r+rOrOsq G:z*JJ GZH @*Mz*JJ GZH @*Mr,cNt|}|r|Stjd|y)z handle_returned_guestinfo_val returns the provided value if it is not empty or set to GUESTINFO_EMPTY_YAML_VAL, otherwise None is returned No value found for key %sN)rr=r>)rrs r+handle_returned_guestinfo_valrs(  $C  II)3/ r,cy)Nz vmware-toolsrs r+rYrYs r,c d|zS)Nz guestinfo.rrs r+rXrX#s # r,c\dt|zjjdddS)Nzvmx.._)rXupperreplacers r+rWrW's+ +C0 0 7 7 9 A A#sB OOr,cft|}|syt|dz}tt|||S)N .encoding)guestinfo_envvar_get_valuerrW)rrrs r+rr+s6 $S )C )# *;rXNOVALrrr rrrr)rrrrrrs r+rrNsII2C8!% [#9##>>  U? II13 7 II@# F,S&99  <<5 II13 7 KK>   K 8,    -     s%AA44 C9=AC  C9C44C9c$|dk(rd}tjd|| ||dt|d|y#t$r'}t j td|||Yd}~yd}~wt $rt j td||YywxYw) z| Sets a guestinfo value for the specified key. Set value to an empty string to clear an existing guestinfo key. ry z$Setting guestinfo key=%s to value=%sz info-set Tz.Failed to set guestinfo key=%s to value=%s: %sNzAUnexpected error while trying to set guestinfo key=%s to value=%s)r=r>rXr rrr)rvaluerrrs r+rrzs {II4c5A  )?)DeL     <          +      s9 BA$$(BBcZ|syt|ttfvr|g}|D]}t|}tj d|t |t||stjd|tj d|t |dzd||rttjd|y)z guestinfo_redact_keys redacts guestinfo of all of the keys in the given list. each key will have its value set to "---". Since the value is valid YAML, cloud-init can still read it if it tries. Nz clearing %szfailed to clear %szclearing %s.encodingrryzfailed to clear %s.encoding) typelisttuplerXr=r?rrr)keysrrrrqs r+rvrvs  Dz$&v ?)#. )" )7J  II*H 5 '2"3#4b':N II3X > ?r,c|siSt|tr|S tj|S#tj t f$rtj|cYSwxYw)z load first attempts to unmarshal the provided data as JSON, and if that fails then attempts to unmarshal the data as YAML. If data is None then a new dictionary is returned. ) isinstancedictr load_jsonjsonJSONDecodeError TypeError load_yaml)rs r+rBrBsX  $ $~~d##  ) ,$~~d##$s-.AAc|siSd}d|vr|d}|d=d}d|vr|d}|d=|rt|tjjr-tj ddt j|i}n0tj d|td||}dt|i}tj d|||d<|S) z_ process_metadata processes metadata and loads the optional network configuration. Nraznetwork.encodingz#network data copied to 'config' keyrbznetwork data to be decoded %szmetadata.networkznetwork data %s) r collectionsabcMappingr=r>copydeepcopyrrB)rra network_encdec_nets r+rArAs  GDy/ OKT!-. # $ g{66 7 II; <w!78G II5w ?/gFG+G4G #W-!Y Kr,c6tj|tS)zK Return a list of data sources that match this set of dependencies )rlist_from_depends datasources)dependss r+get_datasource_listrs  $ $Wk ::r,cd}d}tj}|dD]}|ddk(s |d}n|dD]}|ddk(s |d}nd}d}tj}||vrK||d}t|dkDrtj d ||nt|dk(rd |d vr|d d }||vrK||d}t|dkDrtj d ||nt|dk(rd |d vr|d d }|C|A|D]<}||dD]/} | d |k(s t||ddk(s!||dd d }<>|C|A|D]<}||dD]/} | d |k(s t||ddk(s!||dd d }<>||fS) aI Returns the default IPv4 and IPv6 addresses based on the device(s) used for the default route. Please note that None may be returned for either address family if that family has no default route or if there are multiple addresses associated with the device used by the default route for a given address. Nr destination0.0.0.0ifacerz::/0z,device %s has more than one ipv4 address: %siprz,device %s has more than one ipv6 address: %s)r route_info netdev_infolenr=r>) ipv4_ifipv6_ifroutesrouterrnetdevaddrsdev_nameaddrs r+get_default_ip_addrsr s2GG    !F  9 ,GnG   6 )GnG  D D  "F&w' u:> II> Z1_q!18D>D&w' u:> II> Z1_q!18D>D  DL Hx(0 :%#fX.>v.F*G1*L!(+F3A6tv.F*G1*L!(+F3A6ts :: XXg&88F# r,c|jdsiStj|jd}|jd|jj t |jdS)a Takes a cloudinit.netinfo formatted address and converts to netifaces format, since this module was originally written with netifaces as the network introspection module. netifaces ipv6 format: { "netmask": "ffff:ffff:ffff:ffff::/64", "addr": "2001:db8:abcd:1234::1" } cloudinit.netinfo ipv6 format: { "ip": "2001:db8:abcd:1234::1/64", "scope6": "global", } r/)r$r )r~r IPv6Interfacer$ra prefixlenstrr)r rs r+ convert_to_netifaces_ipv6_formatr+|s^ 88D>  " "488D> 2Dll^1T\\%;%;$<=DGG  r,cVddtjtjtjdii}ttj}|r||d<||d<||d<t \}}|r ||t <|r ||t<|ddd}|ddd}|ddd }tj}|D]\}g} g} ||d D]} | jt| ||d D]} | jt| ||jd } | d k(rp| rm| s| ri| } i}| r.g}| D]"}t|ds|j|$||d <| r.g}| D]"}t|ds|j|$||d <||| <| r<| D]7}|d} t| stj |}|d=| r| |d<||| <9| s!| D]7}|d} t| stj |}|d=| r| |d<||| <9_|S)zP Returns host information such as the host name and network interfaces. ra interfaces)by-macby-ipv4by-ipv6hostnamezlocal-hostnamelocal_hostnamer.r/r0rrhwaddrz00:00:00:00:00:00r mac)r OrderedDictrrrr rrrrappendr%r+r~rrr)rSr1 default_ipv4 default_ipv6by_macby_ipv4by_ipv6ifacesr af_inet4af_inet6r r4rr af_inet4_valsip_info af_inet6_valss r+ get_host_inforBs  %113&224&224 It((*+H ( *&. "#&. "#!5!7L, , * , * y !, / 9F "<0;G "<0;G  "F5#8$V, DD OOtimesleep) rC wait_on_ipv4 wait_on_ipv6rNwait_on_ipv4_valwait_on_ipv6_valrS ipv4_ready ipv6_readyrar-r:r;s r+rNrNsvLL(""?3 ? 2./CD *D1/ #223CD ? 2./CD *D1/ #223CD );%Iz:  "O -- *0b[[.4" ..+1r..+1r -4W)%J -4W)%J   IIN   JJqM;  >II+, r,c tjttdt diddddiii}t |}tj||g}ttj|y#t$rYfwxYw)z7 Executed when this file is used as a program. TFrarbdhcpN) r setup_basic_loggingrrDrErHrNrrPprintr json_dumps)rCrSs r+mainrV%s ##%  $ %  Hvtn- H )I!!8Y"78H - " "8 ,-    sA11 A=<A=__main__)ry)Jrrrrrloggingr|rrI cloudinitrrrrrr cloudinit.logr $cloudinit.sources.helpers.vmware.imcr cloudinit.subpr r r rg getLoggerrr=rrr!r#rtrurrrrrDrErHrrr<rrrOrrYrXrWrrrrrrrrvrBrADEP_FILESYSTEM DEP_NETWORKrrr rrrr%r+rBrNrVrr,r+r`s   EE!?==9g!%* '     #Nw))Nb (*$N, PEK   >)X(V?,$"H..01..0C0CDE ;?D.4D84D2Up5p.( zFr,