o x[h;@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.query_guestinfoz%Failed to query guestinfo with %s: %sNz$vmtoolsd fallback option not presentzfallback to vmtoolsd) r rexec_vmware_rpctoolrr7r8 exec_vmtoolsd Exceptionrlogexcr9)r#rpvmware_rpctoolrqerrorr(r(r)rDsZ       z&DataSourceVMware.get_guestinfo_data_fnc Csd\}}}t|j}t|j}|s|std|||fSts(|||fSt|j}|dur7|||fSt j |}t |}t |\} } | rh|rhtdt|jj|jd|||j\}}}|_n| rz|rztdt|\}}}ntdt||||fS)zN check to see if there is data via vmware guest customization r5z-Customization for VMware platform is disabledNz3Getting 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_cfgr7r8is_cust_plugin_availableget_cust_cfg_filerlpathdirnameparse_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#rArBrCallow_vmware_custallow_raw_data_cust cust_cfg_file cust_cfg_dircust_cfgis_vmware_cust_cfgis_raw_data_cust_cfgr(r(r)r!sL              z DataSourceVMware.get_imc_data_fnrS)__name__ __module__ __qualname____doc__dsnamerintr-r3rFrMr:rUpropertyrYr^r_rdr@rrr! __classcell__r(r(r1r)r9s&$ 9      LrcCs>td}|durtddSd|vrtddSdS)Nzsystem-product-namezNo system-product-name foundFvmwarezNot a VMware platformT)r read_dmi_datar7r8r]) system_typer(r(r)r6s    r6cCsvtd||d}|dvrtd||tt|}n|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)base64b64zPlain-text data %s)r7r8r decomp_gziprb64d decode_binary)keyenc_typedataraw_datar(r(r)decodes   rcCs&t|}|}|r|tkrdS|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)rrr\GUESTINFO_EMPTY_YAML_VAL)valr(r(r)get_none_if_empty_vals  rcCsh|r|r|sdS|t}|rtt|||td||t}|r2tt|||td|dSdS)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)rn LOCAL_IPV4guestinfo_set_valuer7r9 LOCAL_IPV6)rLrr local_ipv4 local_ipv6r(r(r)rHs    rHcCs 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)rr7r8)rrr(r(r)handle_returned_guestinfo_vals  rcCsdS)Nz vmware-toolsr(rr(r(r)rPsrPcCsd|S)Nz guestinfo.r(rr(r(r)rO#srOcCsdt|dddS)Nzvmx.._)rOupperreplacerr(r(r)rN'srNcCs,t|}|sdSt|d}tt|||S)N .encoding)guestinfo_envvar_get_valuerrN)rrrr(r(r)ro+s  rocCst|}t|tj|dS)Nri)rNrrlrmrn)renv_keyr(r(r)r3srcCst||g\}}||fSrSr rargstdoutstderrr(r(r)rr8srrcCst|d|g\}}||fS)Nz--cmdrrr(r(r)rs=srscCs4t|||}|s dSt|d||}tt|||S)zj guestinfo returns the guestinfo value for the provided key, decoding the value when required Nr)guestinfo_get_valuerrO)rrrrrr(r(r)rBs c Cstd|z$||dt|\}}|tkrtd|n|s%td|t||WStyS}z|jtkr>td|n t td|||WYd}~dSd}~wt yh}z t td|||d}~ww)z: Returns a guestinfo value for the specified key. z"Getting guestinfo value for key %sz info-get rz(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) r7r8rONOVALrwrr rrrurt)rrrrrrwr(r(r)rNsB      rc Cs|dkrd}td||z||dt||fWdSty7}zttd|||WYd}~dSd}~wtyHttd||YdSw) z| Sets a guestinfo value for the specified key. Set value to an empty string to clear an existing guestinfo key. ri 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)r7r8rOr rrurt)rvaluerrrwr(r(r)rzs6  rcCs|sdSt|ttfvr|g}|D].}t|}td|t|t||s*td|td|t|dd||s?td|qdS)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.encodingrrizfailed to clear %s.encoding) typelisttuplerOr7r9rrrw)keysrrrrcr(r(r)rgs     rgc CsF|siSt|tr |Szt|WStjtfy"t|YSw)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)rr(r(r)r<s  r<cCs|siSd}d|vr|d}|d=d}d|vr|d}|d=|rPt|tjjr4tddt|i}ntd|td||}dt |i}td|||d<|S) z_ process_metadata processes metadata and loads the optional network configuration. NrVznetwork.encodingz#network data copied to 'config' keyrWznetwork data to be decoded %szmetadata.networkznetwork data %s) r collectionsabcMappingr7r8copydeepcopyrr<)rrV network_encdec_netr(r(r)r;s*    r;cCs t|tS)zK Return a list of data sources that match this set of dependencies )rlist_from_depends datasources)dependsr(r(r)get_datasource_lists rc Csd}d}t}|dD]}|ddkr|d}nq |dD]}|ddkr-|d}nqd}d}t}||vr`||d}t|dkrNtd ||nt|dkr`d |d vr`|d d }||vr||d}t|dkrxtd ||nt|dkrd |d vr|d d }|dur|dur|D]'}||dD]} | d |krt||ddkr||dd d }nqq|dur|dur|D]'}||dD]} | d |krt||ddkr||dd d }nqq||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_infolenr7r8) ipv4_ifipv6_ifroutesrouterrnetdevaddrsdev_nameaddrr(r(r)get_default_ip_addrss^             rricCst|}|r |dkrt}zt|ddtjdtj}Wn tjy'Y|Sw|D] }|dr7|d}|Sq*|S)zoGet fully qualified domain name from name. An empty argument is interpreted as meaning the local host. rNr)stripr get_hostnamesocket getaddrinfo SOCK_DGRAM AI_CANONNAMErw)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. )rmaybe_get_address ipaddress ip_address is_link_local is_loopbackis_unspecified)rrr(r(r)is_valid_ip_addrUsrrr+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", } rbcastmask) broadcastnetmaskr)rn)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)rnr IPv6InterfacerrV prefixlenstrr)rrr(r(r) convert_to_netifaces_ipv6_format|s rcCsddtttdii}tt}|r$||d<||d<||d<t\}}|r/||t<|r5||t<|ddd}|ddd}|ddd }t }|D]}g} g} ||d D] } | t | q_||d D] } | t | qo|| d } | d krqS| r| s| r| } i}| rg}| D]}t|dsq| |q||d <| rg}| D]}t|dsq| |q||d <||| <| r| D]}|d} t| sqt|}|d=| r| |d<||| <q| r | D]}|d} t| sqt|}|d=| r| |d<||| <qqS|S)zP Returns host information such as the host name and network interfaces. rV interfaces)by-macby-ipv4by-ipv6hostnamezlocal-hostnamelocal_hostnamerrrrrhwaddrz00:00:00:00:00:00rmac)r OrderedDictrrrrrrrrappendrrrnrrr)rLr default_ipv4 default_ipv6by_macby_ipv4by_ipv6ifacesraf_inet4af_inet6rrrr af_inet4_valsip_info af_inet6_valsr(r(r) get_host_infos           rc Cs6d}d}t|vr6|t}t|vr!|t}t|tr|}nt|}t|vr6|t}t|tr1|}nt|}d\}}}|durt}|dpHi} | dpOi} | dpVi} | dp]i} |rn| rht | dknd}|snd}|r~| rxt | dknd}|s~d}|durt d||||t d |dus?t d |S) NF)NFFrVrrrrzs         ,+ $ B X8