a '¡Dg=�ã@sþdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z mZmZddlmZddlmZddlmZmZmZdZe e¡ZdZd Zd Zd Zd Z d Z!dZ"dZ#dZ$dZ%dZ&dZ'dZ(Gdd„dej)ƒZ*dd„Z+dd„Z,dd„Z-dd„Z.dd „Z/d!d"„Z0d#d$„Z1d%d&„Z2d'd(„Z3d)d*„Z4d+d,„Z5d-d.„Z6d/d „Z7d0d1„Z8d2d3„Z9d4d5„Z:d6d7„Z;d8d9„ZffgZ?d:d;„Z@d|s>|r||_qJq|js^t d¡dSt d| ¡¡tt|ƒƒ|_ ||_ ||_ |  ¡|j s¢|j s¢|j r¦dSdSdS)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 %sTN) rÚis_vmware_platformrÚLOGÚdebugÚinfoÚ_get_subplatformÚprocess_metadataÚload_json_or_yamlÚmetadataZ userdata_rawZvendordata_rawÚ redact_keys)rÚmdÚudÚvdrZ get_data_fnZrequire_vmware_platformr#r#r$Ú _get_data“s. ü    zDataSourceVMware._get_datacCsDt|jƒ}t d|¡t||j|jƒt |j|g¡|_|  ¡dS)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_networkr8r2r4Úadvertise_local_ip_addrsrrrÚ mergemanydictZpersist_instance_data)rZis_new_instanceÚ host_infor#r#r$ÚsetupÌs  zDataSourceVMware.setupcCsLd}|jtkrt}n&|jtkr$t}n|jtkr4t}ntjSd|j|dƒfS)Nz%s (%s)r8) rrÚget_guestinfo_envvar_key_namerÚget_guestinfo_key_namerÚget_imc_key_namerZMETADATA_UNKNOWN)rZget_key_name_fnr#r#r$r5ès   þz!DataSourceVMware._get_subplatformcCs|jS©N)r©rr#r#r$Úget_config_objûszDataSourceVMware.get_config_objcCsBd|jvrt d¡nt d¡d|j ¡i|jd<|jddS)NÚnetworkzusing metadata network configzusing fallback network configÚconfig)r8r2r3r!Zgenerate_fallback_configrGr#r#r$Únetwork_configþs     ÿ zDataSourceVMware.network_configcCs<|jrd|jvr|jdSt t¡ ¡ ¡|jd<|jdS)Nz instance-id)r8rZload_text_fileÚPRODUCT_UUID_FILE_PATHÚrstripÚlowerrGr#r#r$Úget_instance_id s  ÿz DataSourceVMware.get_instance_idcCs.|jr*|jtkr*tƒr*t d| ¡¡dSdS)Nz"Cache fallback is allowed for : %sTF)rrr1r2r3r5rGr#r#r$Úcheck_if_fallback_is_allowedsÿþýÿz-DataSourceVMware.check_if_fallback_is_allowedcCs,dD]"}||jvrt |j|¡SqgS)N)zpublic-keys-dataZpublic_keys_dataz public-keysZ public_keys)r8rZnormalize_pubkey_data)rÚkey_namer#r#r$Úget_public_ssh_keyss z$DataSourceVMware.get_public_ssh_keyscCsLd}t|jvr|jt}nt|jvr.|jt}|jtkrHt||j|jƒdSrF)ÚREDACTr8ÚCLEANUP_GUESTINFOrrÚguestinfo_redact_keysrr)rZkeys_to_redactr#r#r$r9*s      ÿzDataSourceVMware.redact_keyscCs:d\}}}tj td¡r0tdƒ}tdƒ}tdƒ}|||fS)z< check to see if there is data via env vars r0Úr8ÚuserdataÚ vendordata)ÚosÚenvironÚgetrÚguestinfo_envvar)rr:r;r<r#r#r$r8s  z#DataSourceVMware.get_envvar_data_fnc CsTtdƒ}tdƒ}|r,||_t|_t d|¡|rL|s@||_t|_t d|¡|js`t d¡dSdd„}z||j|jƒWSt�yN}z¼t  td |j|¡|r¸|j|kr¸WYd }~dS|sÔt  d ¡WYd }~dSt  d ¡||_t|_z||j|jƒWWYd }~St�y8t  td |j|¡YWYd }~dS0WYd }~n d }~00d S) zK check to see if there is data via the guestinfo transport Úvmtoolsdzvmware-rpctoolzdiscovered vmware-rpctool: %szdiscovered vmtoolsd: %szno rpctool discoveredr0cSsDd\}}}t d|¡td||ƒ}td||ƒ}td||ƒ}|||fS)Nr0zquery guestinfo with %sr8rWrX)r2r4r )rrr:r;r<r#r#r$Úquery_guestinfo`s      z?DataSourceVMware.get_guestinfo_data_fn..query_guestinfoz%Failed to query guestinfo with %s: %sNz$vmtoolsd fallback option not presentzfallback to vmtoolsd) r rÚexec_vmware_rpctoolrr2r3Ú exec_vmtoolsdÚ ExceptionrÚlogexcr4)rr]Zvmware_rpctoolr^Úerrorr#r#r$rDsT   ü   üz&DataSourceVMware.get_guestinfo_data_fnc Csd\}}}t |j¡}t |j¡}|s>|s>t d¡|||fSt ¡sP|||fSt |j¡}|durn|||fSt j   |¡}t  |¡}t  |¡\} } | rÐ|rÐt d¡t |jj|j d¡|||j¡\}}}|_n.| rô|rôt d¡t |¡\}}}n t d¡t |¡|||fS)zN check to see if there is data via vmware guest customization r0z-Customization for VMware platform is disabledNz3Getting data via VMware customization configurationZscriptszFGetting data via VMware raw cloudinit data customization configurationz1No allowed customization configuration data found)r Zis_vmware_cust_enabledr Zis_raw_data_cust_enabledZds_cfgr2r3Zis_cust_plugin_availableZget_cust_cfg_filerYÚpathÚdirnameZparse_cust_cfgZget_cust_cfg_typeZget_data_from_imc_cust_cfgr"Z cloud_dirZ get_cpathr!rZ#get_data_from_imc_raw_data_cust_cfgrZdel_dir) rr:r;r<Zallow_vmware_custZallow_raw_data_custZ cust_cfg_fileZ cust_cfg_dirZcust_cfgZis_vmware_cust_cfgZis_raw_data_cust_cfgr#r#r$r�sL  ÿ       ý  ûÿÿ   z DataSourceVMware.get_imc_data_fn)N)Ú__name__Ú __module__Ú __qualname__Ú__doc__ZdsnamerÚintr(r.r=rBr5rHÚpropertyrKrOrPrRr9rrrÚ __classcell__r#r#r,r$r9s$$ 9     LrcCs>t d¡}|dur t d¡dSd| ¡vr:t d¡dSdS)Nzsystem-product-namezNo system-product-name foundFZvmwarezNot a VMware platformT)rZ read_dmi_datar2r3rN)Z system_typer#r#r$r1Ês    r1cCsvt d||¡d}|dvr:t d||¡t t |¡¡}n2|dvr\t d||¡t |¡}nt d|¡|}t |¡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)Úbase64Zb64zPlain-text data %s)r2r3rZ decomp_gziprZb64dÚ decode_binary)ÚkeyÚenc_typeÚdataÚraw_datar#r#r$ÚdecodeÕs  rscCs.t |¡}| ¡}t|ƒdks&|tkr*dS|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. rN)rrnrMÚlenÚGUESTINFO_EMPTY_YAML_VAL)Úvalr#r#r$Úget_none_if_empty_valês rwcCsd|r |r |sdS| t¡}|r8tt|||ƒt d|¡| t¡}|r`tt|||ƒt d|¡dS)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_IPV4Úguestinfo_set_valuer2r4Ú LOCAL_IPV6)rArrZ local_ipv4Z local_ipv6r#r#r$r?üs    r?cCs t|ƒ}|r|St d|¡dS)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)rwr2r3)rorvr#r#r$Úhandle_returned_guestinfo_vals  r|cCsdS)Nz vmware-toolsr#©ror#r#r$rEsrEcCsd|S)Nz guestinfo.r#r}r#r#r$rD#srDcCsdt|ƒ ¡ ddd¡S)Nzvmx.Ú.Ú_éÿÿÿÿ)rDÚupperÚreplacer}r#r#r$rC'srCcCs,t|ƒ}|sdSt|dƒ}tt|ƒ||ƒS)Nú .encoding)Úguestinfo_envvar_get_valuersrC)rorvrpr#r#r$r\+s  r\cCst|ƒ}t|tj |d¡ƒS)NrV)rCr|rYrZr[)roZenv_keyr#r#r$r„3sr„cCst||gƒ\}}||fSrF©r ©rÚargÚstdoutÚstderrr#r#r$r_8sr_cCst|d|gƒ\}}||fS)Nz--cmdr…r†r#r#r$r`=sr`cCs4t|||ƒ}|sdSt|d||ƒ}tt|ƒ||ƒS)zj guestinfo returns the guestinfo value for the provided key, decoding the value when required Nrƒ)Úguestinfo_get_valuersrD)rorrrvrpr#r#r$r Bs  c Csàt d|¡zH||dt|ƒƒ\}}|tkr:t d|¡n|sJt d|¡t||ƒWSty¤}z8|jtkr|t d|¡nt  td||¡|‚WYd}~n@d}~0t yÚ}z t  td||¡|‚WYd}~n d}~00dS)z: Returns a guestinfo value for the specified key. z"Getting guestinfo value for key %sz info-get r{z(Failed to get guestinfo value for key %sz,Failed to get guestinfo value for key %s: %sNzCUnexpected error while trying to get guestinfo value for key %s: %s) r2r3rDÚNOVALrcr|r r‰rrbra)rorrrˆr‰rcr#r#r$rŠNs:  ÿ   üûrŠc Cs’|dkr d}t d||¡z||dt|ƒ|fƒWdStyl}zt td|||¡WYd}~n*d}~0tyŒt td||¡Yn0dS) z| Sets a guestinfo value for the specified key. Set value to an empty string to clear an existing guestinfo key. rVú z$Setting guestinfo key=%s to value=%szinfo-set %s %sTz.Failed to set guestinfo key=%s to value=%s: %sNzAUnexpected error while trying to set guestinfo key=%s to value=%s)r2r3rDr rrbra)roÚvaluerrrcr#r#r$ryzs0 ÿû û rycCs„|sdSt|ƒttfvr|g}|D]\}t|ƒ}t d|¡t|t||ƒsTt d|¡t d|¡t|dd||ƒs"t d|¡q"dS)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.encodingrƒrVzfailed to clear %s.encoding) ÚtypeÚlistÚtuplerDr2r4ryrurc)ÚkeysrrrorQr#r#r$rU¥s ÿ  rUc CsJ|siSt|tƒr|Sz t |¡WStjtfyDt |¡YS0dS)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. N)Ú isinstanceÚdictrZ load_jsonÚjsonZJSONDecodeErrorÚ TypeErrorZ load_yaml)rqr#r#r$r7»s  r7cCs¤|siSd}d|vr"|d}|d=d}d|vr<|d}|d=|r t|tjjƒrht d¡dt |¡i}n$t d|¡td||ƒ}dt |ƒi}t d|¡||d<|S) z_ process_metadata processes metadata and loads the optional network configuration. NrIznetwork.encodingz#network data copied to 'config' keyrJznetwork data to be decoded %szmetadata.networkznetwork data %s) r’Ú collectionsÚabcÚMappingr2r3ÚcopyÚdeepcopyrsr7)rqrIZ network_encZdec_netr#r#r$r6Ìs*   ÿ r6cCs t |t¡S)zK Return a list of data sources that match this set of dependencies )rZlist_from_dependsÚ datasources)Zdependsr#r#r$Úget_datasource_listõsrœc Csd}d}t ¡}|dD]}|ddkr|d}q6q|dD]}|ddkr>|d}q\q>d}d}t ¡}||vrÀ||d}t|ƒdkrœt d ||¡n$t|ƒdkrÀd |d vrÀ|d d }||v�r||d}t|ƒdkròt d ||¡n(t|ƒdk�rd |d v�r|d d }|du�rŒ|du�rŒ|D]X}||dD]D} | d |k�rBt||dƒdk�rB||dd d }�q2�qB�q2|du�rþ|du�rþ|D]X}||dD]D} | d |k�r´t||dƒdk�r´||dd d }�q¤�q´�q¤||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. NrZ destinationú0.0.0.0Zifacerz::/0éz,device %s has more than one ipv4 address: %sÚiprz,device %s has more than one ipv6 address: %s)rZ route_infoÚ netdev_infortr2r3) Zipv4_ifZipv6_ifZroutesZrouterrZnetdevÚaddrsÚdev_nameÚaddrr#r#r$Úget_default_ip_addrsüsV       ÿ    ÿ $$r¤rVcCsp| ¡}|r|dkrt ¡}zt |ddtjdtj¡}WntjyLYn 0|D]}|drR|d}qlqR|S)zoGet fully qualified domain name from name. An empty argument is interpreted as meaning the local host. r�Nré)ÚstriprÚ get_hostnameÚsocketZ getaddrinfoZ SOCK_DGRAMZ AI_CANONNAMErc)Únamer¡r£r#r#r$Úgetfqdn>s ÿrªcCs&t tj|¡}|o$|jp"|jp"|j S)zn Returns false if the address is loopback, link local or unspecified; otherwise true is returned. )rZmaybe_get_addressÚ ipaddressÚ ip_addressÚ is_link_localÚ is_loopbackÚis_unspecified)rvr£r#r#r$Úis_valid_ip_addrUsÿr°)r£r&cCs,| d¡siS| d¡| d¡| d¡dœS)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 ipv4 format: { "broadcast": "10.15.255.255", "netmask": "255.240.0.0", "addr": "10.0.1.4" } cloudinit.netinfo ipv4 format: { "ip": "10.0.1.4", "mask": "255.240.0.0", "bcast": "10.15.255.255", "scope": "global", } rŸZbcastÚmask)Ú broadcastÚnetmaskr£)r[)r£r#r#r$Ú convert_to_netifaces_ipv4_format`s  ýr´cCs>| d¡siSt | d¡¡}|j›d|jj›�t|jƒdœS)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³rIÚ prefixlenÚstrrŸ)r£rr#r#r$Ú convert_to_netifaces_ipv6_format|s  þr¹cCsDddt ¡t ¡t ¡dœii}tt ¡ƒ}|rH||d<||d<||d<tƒ\}}|r^||t<|rj||t<|ddd}|ddd}|ddd }t  ¡}|D�]–}g} g} ||d D]} |   t | ƒ¡qÀ||d D]} |   t | ƒ¡qà||  d ¡} | d k�rq¦| �r¤| �s | �r¤| } i}| �rbg}| D]"}t|dƒ�sL�q6|  |¡�q6||d <| �rœg}| D]"}t|dƒ�s†�qp|  |¡�qp||d <||| <| �rò| D]B}|d} t| ƒ�sÈ�q®t |¡}|d=| �ræ| |d<||| <�q®| r¦| D]B}|d} t| ƒ�s�qút |¡}|d=| �r2| |d<||| <�qúq¦|S)zP Returns host information such as the host name and network interfaces. rIÚ interfaces)úby-macúby-ipv4úby-ipv6Úhostnamezlocal-hostnameZlocal_hostnamer»r¼r½rrZhwaddrz00:00:00:00:00:00r£Úmac)r–Ú OrderedDictrªrr§r¤rxrzrr Úappendr´r¹r[r°r™rš)rAr¾Z default_ipv4Z default_ipv6Zby_macÚby_ipv4Úby_ipv6Zifacesr¢Zaf_inet4Zaf_inet6r£r¿rorvZ af_inet4_valsZip_infoZ af_inet6_valsr#r#r$Ú get_host_info•sŽýÿÿ         rÄc Cs2d}d}t|vrl|t}t|vrB|t}t|tƒr8|}n t |¡}t|vrl|t}t|tƒrb|}n t |¡}d\}}}|du�r$tƒ}| d¡p’i} |  d¡p i} |  d¡p®i} |  d¡p¼i} |rÞ| rÒt | ƒdknd}|sÞd}|rþ| ròt | ƒdknd}|sþd}|durvt   d||||¡t   d ¡qvt   d ¡|S) NF)NFFrIrºr¼r½rzZwait_on_ipv4_valZwait_on_ipv6_valrAZ ipv4_readyZ ipv6_readyrIrºrÂrÃr#r#r$r>ísP      û  r>cCsdz t ¡WntyYn0ttdtdiddddiii}t|ƒ}t ||g¡}t t   |¡ƒdS)z7 Executed when this file is used as a program. TFrIrJZdhcpN) rZsetup_basic_loggingrarÅrÆrÈr>rr@ÚprintrZ json_dumps)r8rAr#r#r$Úmain%s  þ ûrÌÚ__main__)rV)Jrir–r™r«r”ZloggingrYr¨rÉZ cloudinitrrrrrrZ cloudinit.logrZ$cloudinit.sources.helpers.vmware.imcr Zcloudinit.subpr r r rLZ getLoggerrfr2r‹rrrrSrTrrurxrzrÅrÆrÈrrr1rsrwr?r|rErDrCr\r„r_r`r rŠryrUr7r6ZDEP_FILESYSTEMZ DEP_NETWORKr›rœr¤rªr°r“r´r¹rÄr>rÌr#r#r#r$Ú s~        ,+$ þB  X8