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

'�DgLT�@sdZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZddl
mZmZmZmZmZddlmZddlmZmZmZmZddlmZe�e�Zdd	iZd
Z dZ!dZ"e!d
Z#e"d
Z$ddiZ%dZ&Gdd�de
�Z'Gdd�dej(�Z)edd�dd�Z*Gdd�dej+�Z,Gdd�de,�Z-e.d�dd�Z/ee.d�d d!�Z0e1d�d"d#�Z2e.e3d$�d%d&�Z4e.eee.e.fd$�d'd(�Z5e.e.d$�d)d*�Z6d	e,j7e,j8e#gd+�e1e	e.ee'd,�d-d.�Z9e,ej:ffe-ej:ej;ffgZ<d/d0�Z=ed1k�rd2Z>e?e�@e9e$e#gd3�e2�d4���dS)5a3Datasource for Oracle (OCI/Oracle Cloud Infrastructure)

Notes:
 * This datasource does not support OCI Classic. OCI Classic provides an EC2
   lookalike metadata service.
 * The UUID provided in DMI data is not the same as the meta-data provided
   instance-id, but has an equivalent lifespan.
 * We do need to support upgrade from an instance that cloud-init
   identified as OpenStack.
 * Bare metal instances use iSCSI root, virtual machine instances do not.
 * Both bare metal and virtual machine instances provide a chassis-asset-tag of
   OracleCloud.com.
�N)�Any�Dict�List�
NamedTuple�Optional�Tuple)�
atomic_helper�dmi�net�sources�util)�
NetworkConfig)�cmdline�	ephemeral�get_interfaces_by_mac�is_netfail_master)�wait_for_url�configure_secondary_nicsFzOracleCloud.comz&http://169.254.169.254/opc/v{version}/z+http://[fd00:c1::a9fe:a9fe]/opc/v{version}/z{path}/Z
Authorizationz
Bearer Oraclei(#c@sBeZdZUeed<eeefed<eeeefed<eed<dS)�ReadOpcMetadataResponse�version�
instance_data�
vnics_data�
imds_url_usedN)	�__name__�
__module__�__qualname__�int�__annotations__r�strrr�rr�F/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceOracle.pyr4s
rc@seZdZdZed�dd�ZdS)�KlibcOracleNetworkConfigSourcez�Override super class to lower the applicability conditions.

    If any `/run/net-*.cfg` files exist, then it is applicable. Even if
    `/run/initramfs/open-iscsi.interface` does not exist.
    ��returncCs
t|j�S)zOverride is_applicable)�bool�_files��selfrrr �
is_applicableBsz,KlibcOracleNetworkConfigSource.is_applicableN)rrr�__doc__r$r(rrrr r!;sr!)�network_configr#cCsd|vrdS|ddvr,t�d|d�dSt�}|ddkr�dd�|dD�D]@}|d	d
krPd|vrP|d}|�|�}|s�qPqPt|�rP|d=qPn||ddk�r|�d
i���D]\\}}d|vr�|�di��d�}|r�|�|�}|s�q�q�t|�r�|dd=|d=||dd<q�dS)aP
    Search network config physical interfaces to see if any of them are
    a netfailover master.  If found, we prevent matching by MAC as the other
    failover devices have the same MAC but need to be ignored.

    Note: we rely on cloudinit.net changes which prevent netfailover devices
    from being present in the provided network config.  For more details about
    netfailover devices, refer to cloudinit.net module.

    :param network_config
       A v1 or v2 network config dict with the primary NIC, and possibly
       secondary nic configured.  This dict will be mutated.

    rN)��z+Ignoring unknown network config version: %sr+cSsg|]}d|vr|�qS)�typer)�.0�crrr �
<listcomp>c�z,_ensure_netfailover_safe.<locals>.<listcomp>�configr-�physical�mac_addressr,�	ethernets�match�
macaddresszset-name�name)�LOG�debugr�getr�items)r*Zmac_to_nameZcfgZmacZcur_name�_Zmacaddrrrr �_ensure_netfailover_safeGs>�



r>cs�eZdZUdZdZejjejjejj	ejj
fZeejdfe
d<dZdZdZ�fdd	�Zedd
��fdd�Zed
�dd�Zeed
�dd��Zdd�Zed
�dd�Zdd�Zed
�dd�Zed
�dd�Zedd��Zd"ed�d d!�Z �Z!S)#�DataSourceOracleZOracleN.�network_config_sourcesT��csttt|�j|g|�Ri|��d|_t�t�|d|jgi�tg�|_	t
�|_gdd�|_|�
�}|j|_|j|_dS)NZ
datasourcer+�r2r)�superr?�__init__�_vnics_datarZ
mergemanydictZget_cfg_by_path�dsname�BUILTIN_DS_CONFIG�ds_cfgr!�_network_config_source�_network_configZget_url_paramsZmax_wait_seconds�url_max_waitZtimeout_seconds�url_timeout)r'�sys_cfg�args�kwargsZ
url_params��	__class__rr rE�s��zDataSourceOracle.__init__)�ci_pkl_versionr#csTt��|�t|d�s"t|dd�t|d�s:t|dt��t|d�sPgdd�|_dS)NrFrJrKr+rC)rD�	_unpickle�hasattr�setattrr!rK)r'rSrQrr rT�s

�
zDataSourceOracle._unpickler"cCst|j�dg��S)Nr2)r$rKr;r&rrr �_has_network_config�sz$DataSourceOracle._has_network_configcCst�S)z@Check platform environment to report if this datasource may run.)�_is_platform_viablerrrr �	ds_detect�szDataSourceOracle.ds_detectc	Csht�|_tjddd�td�dtjddd�itjddd�td�dtjddd�if}|jrvt��}t	j
|j|dd|d�}nt�
�}|��}|j�d	td	�}|�,t|p�||j|jttgd
�}Wd�n1s�0Y|s�dSt|jd�|_|j}|_|j|_|d
|dd|d|dd�|_d|v�rd|d�d�}|�rPt�|�|_|d�d�|jd<dS)Nr,�instance�r�path)�urlZheadersr]r+T)�distroZ	interfaceZipv6Zipv4�connectivity_urls_datar��fetch_vnics_data�max_wait�timeout�metadata_patternsF�r]Z	ociAdName�idr�hostnameZdisplayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer8�metadata�	user_dataZssh_authorized_keys�public_keys) �_read_system_uuid�system_uuid�IPV4_METADATA_PATTERN�format�
V2_HEADERS�IPV6_METADATA_PATTERN�perform_dhcp_setupr
Zfind_fallback_nicrZEphemeralIPNetworkr^r�nullcontext�_is_iscsi_rootrIr;rH�read_opc_metadatarLrM� _get_versioned_metadata_base_urlr�metadata_addressrZ_crawled_metadatarrFrh�base64Z	b64decodeZuserdata_raw)	r'r_Znic_nameZnetwork_contextZfetch_primary_nicZfetch_secondary_nicsZfetched_metadata�datarirrr �	_get_data�s�����������
���$
��
�
zDataSourceOracle._get_datacCst�|j�S)z�quickly check (local only) if self.instance_id is still valid

        On Oracle, the dmi-provided system uuid differs from the instance-id
        but has the same life-span.)rZinstance_id_matches_system_uuidrl)r'rNrrr �check_instance_id	sz"DataSourceOracle.check_instance_idcCst�|j�d��S)Nrj)rZnormalize_pubkey_datarhr;r&rrr �get_public_ssh_keyssz$DataSourceOracle.get_public_ssh_keyscCs
|j��S)z)Return whether we are on a iscsi machine.)rJr(r&rrr rsszDataSourceOracle._is_iscsi_rootcCs
|j��S�N)rJZ
render_configr&rrr �_get_iscsi_configsz"DataSourceOracle._get_iscsi_configcCs�|��r|jSd}|��r$|��|_|��s:t�d�d}|j�dtd�}|sT|r�z|�	|�Wnt
y�t�td�Yn0t
|j�|jS)z�Network config is read from initramfs provided files

        Priority for primary network_config selection:
        - iscsi
        - imds

        If none is present, then we fall back to fallback configuration.
        FzLCould not obtain network configuration from initramfs. Falling back to IMDS.Trz+Failed to parse IMDS network configuration!)rWrKrsr}r9�warningrIr;rH�!_add_network_config_from_opc_imds�	ExceptionrZlogexcr>)r'�set_primaryZ
set_secondaryrrr r*s0

���

zDataSourceOracle.network_configF)r�c
CsV|jdurt�d�dS|s8d|jdvr8t�d�dSt�}|rH|jn|jdd�}t|�D�]�\}}|or|dk}|d��}|�dd	�o�|�d
d	�}||vr�t�d|�q^||}	|r�t�|dd�}
nt�|d
�}
|j	ddk�r�|�r|�rddig}n
ddig}n`g}|�d
��rB|�
d|d
�d|
j��d��|�d��rr|�
d|dd�d|
j��d��|	d|t|d�}|j	d�
|�q^|j	ddkr^td|id�}||j	d|	<|�o�||d<|�o�||d<|�sBg|d<|�d
��r|d�
|d
�d|
j���|�d��rB|d�
|dd�d|
j���||j	d|	<q^dS)a�Generate primary and/or secondary NIC config from IMDS and merge it.

        It will mutate the network config to include the secondary VNICs.

        :param set_primary: If True set primary interface.
        :raises:
        Exceptions are not handled within this function.  Likely
            exceptions are KeyError/IndexError
            (if the IMDS returns valid JSON with unexpected contents).
        Nz#NIC data is UNSET but should not beZnicIndexrz\VNIC metadata indicates this is a bare metal machine; skipping secondary VNIC configuration.r+ZmacAddrZipv6SubnetCidrBlockFZ	privateIpz)Interface with MAC %s not found; skippingZ
ipv6AddressesZsubnetCidrBlockrr-Zdhcp6ZdhcpZstatic�/)r-�addressr3)r8r-r4�mtu�subnetsr2r,r7)r�r6r5Zdhcp4�	addresses)
rFr9r~r�	enumerate�lowerr;�	ipaddress�
ip_networkrK�append�	prefixlen�MTU)
r'r�Zinterfaces_by_macr�indexZ	vnic_dictZ
is_primaryr4Zis_ipv6_onlyr8�networkr�Zinterface_configrrr rHs�

����
�
���	���
�����z2DataSourceOracle._add_network_config_from_opc_imds)F)"rrrrGrlrZNetworkConfigSourceZCMD_LINEZ
SYSTEM_CFGZDSZ	INITRAMFSr@rrrqrLrMrErrTr$rW�staticmethodrYryrzr{rs�dictr}�propertyr*r�
__classcell__rrrQr r?{s.
�
V
-r?c@seZdZdZdS)�DataSourceOracleNetFN)rrrrqrrrr r��sr��rvcCs|sdS|�t�d�d�S)NFZopcr)�
startswith�IPV4_METADATA_ROOT�splitr�rrr �_is_ipv4_metadata_url�sr�r"cCst�d�}|durdS|��S)Nzsystem-uuid)r	�
read_dmi_datar�)Zsys_uuidrrr rk�s
rkcCst�d�}|tkS)Nzchassis-asset-tag)r	r��CHASSIS_ASSET_TAG)Z	asset_tagrrr rX�s
rX)r]r#cCsd|vrdSdS)Nz/opc/v2/r,r+rrerrr �_url_version�sr�cCst|�dkrtSdS)Nr,)r�rorerrr �_headers_cb�sr�cCsL|s|Sd|vr"|�d�ddSd|vr<|�d�ddStd|��dS)zQ
    Remove everything following the version number in the metadata address.
    Zv2rzv2/Zv1zv1/zInvalid metadata address: N)r��
ValueErrorrerrr ru�srur`)rardr#c
s��fdd�dD�}t�d|�t��}t|||tddd�\}}|sXt�dd	�|��d
St�d|�t�	|�
d��}t|�}	d
}
|r�t|�d
d�g|t��||tddd�\}}|r�t�	|�
d��}
t�d|�n
t�d�t
|	||
|�S)a�
    Fetch metadata from the /opc/ routes from the IMDS.

    Returns:
        Optional[ReadOpcMetadataResponse]: If fetching metadata fails, None.
            If fetching metadata succeeds, a namedtuple containing:
            - The metadata version as an integer
            - The JSON-decoded value of the instance data from the IMDS
            - The JSON-decoded value of the vnics data from the IMDS if
                `fetch_vnics_data` is True, else None. Alternatively,
                None if fetching metadata failed
            - The url that was used to fetch the metadata.
                This allows for later determining if v1 or v2 endppoint was
                used and whether the IMDS was reached via IPv4 or IPv6.
    cs$g|]}�D]}|j|dd��qqS)rZr[)rn)r.rZmetadata_pattern�rdrr r0�s�z%read_opc_metadata.<locals>.<listcomp>)r,r+z*Attempting to fetch IMDS metadata from: %sg�������?T)�urlsrbrcZ
headers_cbZ
sleep_timeZconnect_synchronouslyz-Failed to fetch IMDS metadata from any of: %sz, Nz7Successfully fetched instance metadata from IMDS at: %szutf-8rZZvnicsz4Successfully fetched vnics metadata from IMDS at: %sz+Failed to fetch IMDS network configuration!)r9r:�time�	monotonicrr�r~�join�json�loads�decoder��replacer)
rarbrcrdr�Z
start_timeZurl_that_workedZinstance_responserZmetadata_versionrZ	vnics_urlZvnics_responserr�r rt�s`
��
���
�
�rtcCst�|t�Sr|)rZlist_from_depends�datasources)Zdependsrrr �get_datasource_listGsr��__main__z�
        Query Oracle Cloud metadata and emit a JSON object with two keys:
        `read_opc_metadata` and `_is_platform_viable`.  The values of each are
        the return values of the corresponding functions defined in
        DataSourceOracle.py.r�)rtrX)Ar)rwr�r�Zloggingr��typingrrrrrrZ	cloudinitrr	r
rrZcloudinit.distros.networkingr
Z
cloudinit.netrrrrZcloudinit.url_helperrZ	getLoggerrr9rHr�r�ZIPV6_METADATA_ROOTrmrpror�rZKlibcNetworkConfigSourcer!r>Z
DataSourcer?r�rr�rkr$rXrr�r�rurLrMrtZDEP_FILESYSTEMZDEP_NETWORKr�r��description�printZ
json_dumpsrrrr �<module>sz 
�4C��U
���

����

© 2025 Cubjrnet7