fmdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z mZmZmZddlmZddlmZmZmZdZej0eZdZdZd Zd Zd Zd Z d Z!dZ"dZ#dZ$dZ%dZ&dZ'GddejPZ)dZ*dZ+dZ,dZ-dZ.dZ/dZ0dZ1dZ2dZ3d Z4d!Z5d"Z6d#Z7d$Z8d%Z9d&Z:d'Z;e)ejxffe)ejxejzffgZ>d(Z?d)Z@d0d*ZAd+ZBd,ZCd-ZDd.ZEed/k(reEyy)1a Cloud-Init DataSource for VMware This module provides a cloud-init datasource for VMware systems and supports multiple transports types, including: * EnvVars * GuestInfo * IMC (Guest Customization) Netifaces (https://github.com/al45tair/netifaces) Please note this module relies on the netifaces project to introspect the runtime, network configuration of the host on which this datasource is running. This is in contrast to the rest of cloud-init which uses the cloudinit/netinfo module. The reasons for using netifaces include: * Netifaces is built in C and is more portable across multiple systems and more deterministic than shell exec'ing local network commands and parsing their output. * Netifaces provides a stable way to determine the view of the host's network after DHCP has brought the network online. Unlike most other datasources, this datasource still provides support for JINJA queries based on networking information even when the network is based on a DHCP lease. While this does not tie this datasource directly to netifaces, it does mean the ability to consistently obtain the correct information is paramount. * It is currently possible to execute this datasource on macOS (which many developers use today) to print the output of the get_host_info function. This function calls netifaces to obtain the same runtime network configuration that the datasource would persist to the local system's instance data. However, the netinfo module fails on macOS. The result is either a hung operation that requires a SIGINT to return control to the user, or, if brew is used to install iproute2mac, the ip commands are used but produce output the netinfo module is unable to parse. While macOS is not a target of cloud-init, this feature is quite useful when working on this datasource. For more information about this behavior, please see the following PR comment, https://bit.ly/3fG7OVh. The authors of this datasource are not opposed to moving away from netifaces. The goal may be to eventually do just that. This proviso was added to the top of this module as a way to remind future-us and others why netifaces was used in the first place in order to either smooth the transition away from netifaces or embrace it further up the cloud-init stack. N) atomic_helperdmilognetsourcesutil)guestcust_util)ProcessExecutionErrorsubpwhichz/sys/class/dmi/id/product_uuidzNo value foundenvvar guestinfoimcredactzcleanup-guestinfo VMX_GUESTINFOz---z local-ipv4z local-ipv6zwait-on-networkipv4ipv6ceZdZdZdZddZdeddffd ZdZd Z d Z d Z d Z e d ZdZdZdZdZdZdZxZS)DataSourceVMwareav Setting the hostname: The hostname is set by way of the metadata key "local-hostname". Setting the instance ID: The instance ID may be set by way of the metadata key "instance-id". However, if this value is absent then the instance ID is read from the file /sys/class/dmi/id/product_uuid. Configuring the network: The network is configured by setting the metadata key "network" with a value consistent with Network Config Versions 1 or 2, depending on the Linux distro's version of cloud-init: Network Config Version 1 - http://bit.ly/cloudinit-net-conf-v1 Network Config Version 2 - http://bit.ly/cloudinit-net-conf-v2 For example, CentOS 7's official cloud-init package is version 0.7.9 and does not support Network Config Version 2. imc transport: Either Network Config Version 1 or Network Config Version 2 is supported which depends on the customization type. For LinuxPrep customization, Network config Version 1 data is parsed from the customization specification. For CloudinitPrep customization, Network config Version 2 data is parsed from the customization specification. envvar and guestinfo transports: Network Config Version 2 data is supported as long as the Linux distro's cloud-init package is new enough to parse the data. The metadata key "network.encoding" may be used to indicate the format of the metadata key "network". Valid encodings are base64 and gzip+base64. VMwareNctjj|||||i|_d|_d|_d|_t|jdft|jdft|jdfg|_ y)NFT)r DataSource__init__cfgdata_access_methodrpctool rpctool_fnDATA_ACCESS_METHOD_ENVVARget_envvar_data_fnDATA_ACCESS_METHOD_GUESTINFOget_guestinfo_data_fnDATA_ACCESS_METHOD_IMCget_imc_data_fn possible_data_access_method_list)selfsys_cfgdistropathsud_procs D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceVMware.pyrzDataSourceVMware.__init__sw##D'65'J"& '(?(? G )4+E+Et L #T%9%94 @1 -ci_pkl_versionreturnc 8t||dD]}t||rt||dt|ds t|dit|dsDt|dt|j dft |jdft|jdfgyy)N)rrrr$FT) super _unpicklehasattrsetattrrrr r!r"r#)r%r,attr __class__s r*r0zDataSourceVMware._unpickles .)- *D4&dD) *tU# D% $t?@ 22// 522 ,T-A-A4H  Ar+cdtjj|}|d|jdS)Nz [seed=])rr__str__r)r%roots r*r7zDataSourceVMware.__str__s)!!))$/!%t'>'>??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_platformrLOGerrorinfo_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_networkrBr<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 (rB)) 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_obj)s xxr+cd|jvrtjdn>tjdd|jj i|jd<|jddS)Nnetworkzusing metadata network configzusing fallback network configconfig)rBr<debugr'generate_fallback_configr]s r*network_configzDataSourceVMware.network_config,s`  % II5 6 II5 6$++>>@(DMM) $}}Y'11r+cB|jrd|jvr|jdSttd5}t|j j j |jd<|jdcdddS#1swYyxYw)Nz instance-idr)rBopenPRODUCT_UUID_FILE_PATHstrreadrstriplower)r%id_files r*get_instance_idz DataSourceVMware.get_instance_id7s~ ==]dmm;==/ / (# . 0'+.w||~+>+E+E+G+M+M+ODMM- (==/ 0 0 0s ABBcxdD]4}||jvstj|j|cSgS)N)zpublic-keys-datapublic_keys_dataz public-keys public_keys)rBrnormalize_pubkey_data)r%key_names r*get_public_ssh_keysz$DataSourceVMware.get_public_ssh_keys@sD  NH 4==(44T]]85LMM N r+cd}t|jvr|jt}n%t|jvr|jt}|jtk(r"t ||j |jyyr\)REDACTrBCLEANUP_GUESTINFOrr guestinfo_redact_keysrr)r%keys_to_redacts r*rEzDataSourceVMware.redact_keysKsh 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:rBuserdata vendordata)osenvirongetrguestinfo_envvar)r%rFrGrHs r*rz#DataSourceVMware.get_envvar_data_fnYsK& 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 %srBr|r})r<r>r)rrrFrGrHs r*query_guestinfoz?DataSourceVMware.get_guestinfo_data_fn..query_guestinfosR)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<rb exec_vmtoolsd Exceptionrlogexcr>)r%rvmware_rpctoolrr=s r*r!z&DataSourceVMware.get_guestinfo_data_fnesP $/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<rbis_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%rFrGrHallow_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__dsnamerintr0r7rKrSr?r^propertyrdrnrtrErr!r# __classcell__)r4s@r*rrgsw"HF *4@7r%8 &220   J*X7r+rctjd}|tjdyd|j vrtjdyy)Nzsystem-product-namezNo system-product-name foundFvmwarezNot a VMware platformT)r read_dmi_datar<rbrl) 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<rbr decomp_gziprb64d decode_binary)keyenc_typedataraw_datas r*decoders II7hGH,, )8S9##M$6$6t$<= & & )8S9 %%d+ &,   h ''r+ctj|}|j}t|dk(s |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. rN)rrrklenGUESTINFO_EMPTY_YAML_VAL)vals r*get_none_if_empty_valr s:   S !C **,C 3x1}77 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)rRrr local_ipv4 local_ipv6s r*rNrNsq 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<rb)rrs r*handle_returned_guestinfo_valr3s(  $C  II)3/ r+cy)Nz vmware-toolsrs r*rXrX@s r+c d|zS)Nz guestinfo.rrs r*rWrWDs # r+c\dt|zjjdddS)Nzvmx.._)rWupperreplacers r*rVrVHs+ +C0 0 7 7 9 A A#sB OOr+cft|}|syt|dz}tt|||S)N .encoding)guestinfo_envvar_get_valuerrV)rrrs r*rrLs6 $S )C )# *;>  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. r{ 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<rbrWr rrr)rvaluerrr=s r*rrs {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.encodingrr{zfailed to clear %s.encoding) typelisttuplerWr<r>rrr=)keysrrrrss r*rxrxs  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*rArAsX  $ $~~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. Nr`znetwork.encodingz#network data copied to 'config' keyraznetwork data to be decoded %szmetadata.networkznetwork data %s) r collectionsabcMappingr<rbcopydeepcopyrrA)rr` network_encdec_nets r*r@r@s  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+cRtj}d|vry|d}tj|vrtj|vryd}d}|j tj}|rr|\}}tj |}|rV|j tj}|r5t |dkDrtjd||nd|dvr|dd}|j tj} | rr| \}} tj | } | rV| j tj} | r5t | dkDrtjd| | nd| dvr| dd}|rX|sVj tj} | r5t | dkDrtjd| nd| dvr| dd}|s[|rY j tj}|r8t |dkDrtjd |||fSd|dvr|dd}||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. default)NNNz,device %s has more than one ipv4 address: %saddrrz,device %s has more than one ipv6 address: %s) netifacesgatewaysAF_INETAF_INET6r ifaddressesrr<rb) r default_gwrrgw4rdev4 addr4_famsaf_inet4gw6dev6 addr6_famsaf_inet6s r*get_default_ip_addrsrs,!!#H )$J+   j 0 D D ..** +C 4**40 !~~i&7&78Hx=1$IIF  x{*#A;v.D ..++ ,C 4**40 !~~i&8&89Hx=1$IIF  x{*#A;v.D  D>>)"4"45 8}q  B 8A;&{6* D>>)"3"34 8}q  B :8A;&{6* :r+c&|j}|r|dk(rtj} tj|ddtj dtj }|D]}|ds |d}|S|S#tj$rY|SwxYw)zoGet fully qualified domain name from name. An empty argument is interpreted as meaning the local host. z0.0.0.0Nr)stripr get_hostnamesocket getaddrinfo SOCK_DGRAM AI_CANONNAMEr=)nameaddrsrs r*getfqdnrzs ::II+, r+c tjttdt diddddiii}t |}tj||g}ttj|y#t$rYfwxYw)z7 Executed when this file is used as a program. TFr`radhcpN) rsetup_basic_loggingrr=r>rArMrrOprintr json_dumps)rBrRs r*mainrO-s  !  $ %  Hvtn- H )I!!8Y"78H - " "8 ,-    sA11 A=<A=__main__)r{)Frrrrrloggingr~rrBr cloudinitrrrrrr$cloudinit.sources.helpers.vmware.imcr cloudinit.subpr r r rh getLoggerrr<rrr r"rvrwrrrrr=r>rArrr;rrrNrrXrWrVrrrrrrrrxrAr@DEP_FILESYSTEM DEP_NETWORKrrrrr#r;rMrOrr+r*rXs~5n   AA?==9g!%* '     #Aw))AH (*$N, PEK   >)X(V?,$"H..01..0C0CDE ;Wz.Vr5p.( zFr+