a 'Dg=@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z m Z m Z m Z mZmZddlmZddlmZddlmZmZmZdZeeZdZd Zd Zd Zd Z d Z!dZ"dZ#dZ$dZ%dZ&dZ'dZ(Gdddej)Z*ddZ+ddZ,ddZ-ddZ.dd Z/d!d"Z0d#d$Z1d%d&Z2d'd(Z3d)d*Z4d+d,Z5d-d.Z6d/d Z7d0d1Z8d2d3Z9d4d5Z:d6d7Z;d8d9ZffgZ?d:d;Z@d|s>|r||_qJq|js^tddStd|tt||_ ||_ ||_ | |j s|j s|j rdSdSdS)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) ris_vmware_platformrLOGdebuginfo_get_subplatformprocess_metadataload_json_or_yamlmetadataZ userdata_rawZvendordata_raw redact_keys)rmdudvdrZ get_data_fnZrequire_vmware_platformr#r#r$ _get_datas.     zDataSourceVMware._get_datacCsDt|j}td|t||j|jt|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_networkr8r2r4advertise_local_ip_addrsrrr mergemanydictZpersist_instance_data)rZis_new_instance host_infor#r#r$setups  zDataSourceVMware.setupcCsLd}|jtkrt}n&|jtkr$t}n|jtkr4t}ntjSd|j|dfS)Nz%s (%s)r8) rrget_guestinfo_envvar_key_namerget_guestinfo_key_namerget_imc_key_namerZMETADATA_UNKNOWN)rZget_key_name_fnr#r#r$r5s   z!DataSourceVMware._get_subplatformcCs|jSN)rrr#r#r$get_config_objszDataSourceVMware.get_config_objcCsBd|jvrtdntdd|ji|jd<|jddS)Nnetworkzusing metadata network configzusing fallback network configconfig)r8r2r3r!Zgenerate_fallback_configrGr#r#r$network_configs     zDataSourceVMware.network_configcCs<|jrd|jvr|jdStt|jd<|jdS)Nz instance-id)r8rZload_text_filePRODUCT_UUID_FILE_PATHrstriplowerrGr#r#r$get_instance_id s  z DataSourceVMware.get_instance_idcCs.|jr*|jtkr*tr*td|dSdS)Nz"Cache fallback is allowed for : %sTF)rrr1r2r3r5rGr#r#r$check_if_fallback_is_allowedsz-DataSourceVMware.check_if_fallback_is_allowedcCs,dD]"}||jvrt|j|SqgS)N)zpublic-keys-dataZpublic_keys_dataz public-keysZ public_keys)r8rZnormalize_pubkey_data)rkey_namer#r#r$get_public_ssh_keyss z$DataSourceVMware.get_public_ssh_keyscCsLd}t|jvr|jt}nt|jvr.|jt}|jtkrHt||j|jdSrF)REDACTr8CLEANUP_GUESTINFOrrguestinfo_redact_keysrr)rZkeys_to_redactr#r#r$r9*s      zDataSourceVMware.redact_keyscCs:d\}}}tjtdr0td}td}td}|||fS)z< check to see if there is data via env vars r0r8userdata vendordata)osenvirongetrguestinfo_envvar)rr:r;r<r#r#r$r8s  z#DataSourceVMware.get_envvar_data_fnc CsTtd}td}|r,||_t|_td||rL|s@||_t|_td||js`tddSdd}z||j|jWStyN}zt td |j||r|j|krWYd }~dS|st d WYd }~dSt d ||_t|_z||j|jWWYd }~Sty8t 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\}}}td|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 rexec_vmware_rpctoolrr2r3 exec_vmtoolsd Exceptionrlogexcr4)rr]Zvmware_rpctoolr^errorr#r#r$rDsT      z&DataSourceVMware.get_guestinfo_data_fnc Csd\}}}t|j}t|j}|s>|s>td|||fStsP|||fSt|j}|durn|||fSt j |}t |}t |\} } | r|rtdt|jj|jd|||j\}}}|_n.| r|rtdt|\}}}n tdt||||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_filerYpathdirnameZparse_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$rsL              z DataSourceVMware.get_imc_data_fn)N)__name__ __module__ __qualname____doc__Zdsnamerintr(r.r=rBr5rHpropertyrKrOrPrRr9rrr __classcell__r#r#r,r$r9s$$ 9     LrcCs>td}|dur tddSd|vr:tddSdS)Nzsystem-product-namezNo system-product-name foundFZvmwarezNot a VMware platformT)rZ read_dmi_datar2r3rN)Z system_typer#r#r$r1s    r1cCsvtd||d}|dvr:td||tt|}n2|dvr\td||t|}ntd||}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)keyenc_typedataraw_datar#r#r$decodes  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)rrnrMlenGUESTINFO_EMPTY_YAML_VAL)valr#r#r$get_none_if_empty_vals rwcCsd|r |r |sdS|t}|r8tt|||td||t}|r`tt|||td|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_IPV4guestinfo_set_valuer2r4 LOCAL_IPV6)rArrZ local_ipv4Z local_ipv6r#r#r$r?s    r?cCs t|}|r|Std|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|dddS)Nzvmx.._)rDupperreplacer}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|dS)NrV)rCr|rYrZr[)roZenv_keyr#r#r$r3srcCst||g\}}||fSrFr rargstdoutstderrr#r#r$r_8sr_cCst|d|g\}}||fS)Nz--cmdrrr#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 Cstd|zH||dt|\}}|tkr:td|n|sJtd|t||WSty}z8|jtkr|td|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) r2r3rDNOVALrcr|r rrrbra)rorrrrrcr#r#r$rNs:     rc Cs|dkr d}td||z||dt||fWdStyl}zttd|||WYd}~n*d}~0tyttd||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)rovaluerrrcr#r#r$ryzs0   rycCs|sdSt|ttfvr|g}|D]\}t|}td|t|t||sTtd|td|t|dd||s"td|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.encodingrrVzfailed to clear %s.encoding) typelisttuplerDr2r4ryrurc)keysrrrorQr#r#r$rUs   rUc CsJ|siSt|tr|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) isinstancedictrZ load_jsonjsonZJSONDecodeError TypeErrorZ load_yaml)rqr#r#r$r7s  r7cCs|siSd}d|vr"|d}|d=d}d|vr<|d}|d=|rt|tjjrhtddt|i}n$td|td||}dt |i}td|||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 collectionsabcMappingr2r3copydeepcopyrsr7)rqrIZ network_encZdec_netr#r#r$r6s*    r6cCs t|tS)zK Return a list of data sources that match this set of dependencies )rZlist_from_depends datasources)Zdependsr#r#r$get_datasource_listsrc Csd}d}t}|dD]}|ddkr|d}q6q|dD]}|ddkr>|d}q\q>d}d}t}||vr||d}t|dkrtd ||n$t|dkrd |d vr|d d }||vr||d}t|dkrtd ||n(t|dkrd |d vr|d d }|dur|dur|D]X}||dD]D} | d |krBt||ddkrB||dd d }q2qBq2|dur|dur|D]X}||dD]D} | d |krt||ddkr||dd d }qqq||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 destination0.0.0.0Zifacerz::/0z,device %s has more than one ipv4 address: %siprz,device %s has more than one ipv6 address: %s)rZ route_info netdev_infortr2r3) Zipv4_ifZipv6_ifZroutesZrouterrZnetdevaddrsdev_nameaddrr#r#r$get_default_ip_addrssV            $$rrVcCsp|}|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. rNr)stripr get_hostnamesocketZ getaddrinfoZ SOCK_DGRAMZ AI_CANONNAMErc)namerrr#r#r$getfqdn>s rcCs&ttj|}|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_loopbackis_unspecified)rvrr#r#r$is_valid_ip_addrUsr)rr&cCs,|dsiS|d|d|ddS)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", } rZbcastmask) broadcastnetmaskr)r[)rr#r#r$ convert_to_netifaces_ipv4_format`s  rcCs>|dsiSt|d}|jd|jjt|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/)rr)r[r IPv6InterfacerrI prefixlenstrr)rrr#r#r$ convert_to_netifaces_ipv6_format|s  rcCsDddtttdii}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 krq| r| s | r| } i}| rbg}| D]"}t|dsLq6| |q6||d <| rg}| D]"}t|dsqp| |qp||d <||| <| r| D]B}|d} t| sȐqt|}|d=| r| |d<||| <q| r| D]B}|d} t| sqt|}|d=| r2| |d<||| <qq|S)zP Returns host information such as the host name and network interfaces. rI interfaces)by-macby-ipv4by-ipv6hostnamezlocal-hostnameZlocal_hostnamerrrrrZhwaddrz00:00:00:00:00:00rmac)r OrderedDictrrrrrxrzrrappendrrr[rrr)rArZ default_ipv4Z default_ipv6Zby_macby_ipv4by_ipv6ZifacesrZaf_inet4Zaf_inet6rrrorvZ af_inet4_valsZip_infoZ af_inet6_valsr#r#r$ get_host_infos         rc Cs2d}d}t|vrl|t}t|vrB|t}t|tr8|}n t|}t|vrl|t}t|trb|}n t|}d\}}}|dur$t}|dpi} | dpi} | dpi} | dpi} |r| rt | dknd}|sd}|r| rt | dknd}|sd}|durvt d||||t d qvt d |S) NF)NFFrIrrrrzZwait_on_ipv4_valZwait_on_ipv6_valrAZ ipv4_readyZ ipv6_readyrIrrrr#r#r$r>sP        r>cCsdz tWntyYn0ttdtdiddddiii}t|}t||g}t t |dS)z7 Executed when this file is used as a program. TFrIrJZdhcpN) rZsetup_basic_loggingrarrrr>rr@printrZ json_dumps)r8rAr#r#r$main%s   r__main__)rV)JrirrrrZloggingrYrrZ cloudinitrrrrrrZ cloudinit.logrZ$cloudinit.sources.helpers.vmware.imcr Zcloudinit.subpr r r rLZ getLoggerrfr2rrrrrSrTrrurxrzrrrrrr1rsrwr?r|rErDrCr\rr_r`r rryrUr7r6ZDEP_FILESYSTEMZ DEP_NETWORKrrrrrrrrrr>rr#r#r#r$ s~        ,+$ B  X8