name : DataSourceVMware.cpython-39.opt-1.pyc
a

'�Dg=��@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZm
Z
mZmZddlmZddlmZddlmZmZmZdZe�e�ZdZd	Zd
ZdZd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�Z<e*ej=ffe*ej=ej>ffgZ?d:d;�Z@d<d=�ZAdOd?d@�ZBdAdB�ZCeDeDdC�dDdE�ZEeDeDdC�dFdG�ZFdHdI�ZGdJdK�ZHdLdM�ZIedNk�r�eI�dS)Pz�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)
�N)�
atomic_helper�dmi�net�netinfo�sources�util)�loggers)�guestcust_util)�ProcessExecutionError�subp�whichz/sys/class/dmi/id/product_uuidzNo value foundZenvvar�	guestinfoZimcZredactzcleanup-guestinfo�
VMX_GUESTINFOz---z
local-ipv4z
local-ipv6zwait-on-network�ipv4�ipv6cs�eZdZdZdZd#dd�Zedd��fdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Ze
dd��Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Z�ZS)$�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.
    ZVMwareNcCsTtj�|||||�i|_d|_d|_d|_t|jdft	|j
dft|jdfg|_
dS)NFT)r�
DataSource�__init__�cfg�data_access_method�rpctool�
rpctool_fn�DATA_ACCESS_METHOD_ENVVAR�get_envvar_data_fn�DATA_ACCESS_METHOD_GUESTINFO�get_guestinfo_data_fn�DATA_ACCESS_METHOD_IMC�get_imc_data_fn� possible_data_access_method_list)�self�sys_cfg�distro�pathsZud_proc�r#�F/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceVMware.pyr`s



�zDataSourceVMware.__init__)�ci_pkl_version�returncszt��|�dD]}t||�st||d�qt|d�sBt|di�t|d�svt|dt|jdft|jdft|j	dfg�dS)N)rrrrFT)
�super�	_unpickle�hasattr�setattrrrrrrr)rr%�attr��	__class__r#r$r(us*


��
��zDataSourceVMware._unpicklecCstj�|�}d||jfS)Nz%s [seed=%s])rr�__str__r)r�rootr#r#r$r.�szDataSourceVMware.__str__cCs�d\}}}|jD]8\}}}|r&t�s&q|�\}}}|s>|s>|r||_qJq|js^t�d�dSt�d|���tt|��|_	||_
||_|��|j	s�|j
s�|jr�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�vdrZget_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)Nzinstance-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_datazpublic-keysZpublic_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.<locals>.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_fncCsd\}}}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)Zsystem_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)zgzip+base64zgz+b64zDecoding %s format %s)�base64Zb64zPlain-text data %s)r2r3rZdecomp_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.
    Nzclearing %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)rqrIZnetwork_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
|dvr�|dd
}||v�r||d}t|�dkr�t�d||�n(t|�dk�rd
|dv�r|dd
}|du�r�|du�r�|D]X}||dD]D}	|	d
|k�rBt||d�dk�rB||ddd
}�q2�qB�q2|du�r�|du�r�|D]X}||dD]D}	|	d
|k�r�t||d�dk�r�||ddd
}�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.
    NrZdestination�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�socketZgetaddrinfoZ
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�||dD]}|
�
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�rz<waiting on network: wait4=%s, ready4=%s, wait6=%s, ready6=%sr�zwaiting on network complete)�WAIT_ON_NETWORK�WAIT_ON_NETWORK_IPV4r��boolrZtranslate_bool�WAIT_ON_NETWORK_IPV6r�r[rtr2r3�time�sleep)
r8Zwait_on_ipv4Zwait_on_ipv6r>Zwait_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
rrrLZ	getLoggerrfr2r�rrrrSrTrrurxrzr�r�r�rrr1rsrwr?r|rErDrCr\r�r_r`r
r�ryrUr7r6ZDEP_FILESYSTEMZDEP_NETWORKr�r�r�r�r�r�r�r�r�r>r�r#r#r#r$�<module>s~
 

,+$
�B
X8


© 2025 Cubjrnet7