HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceOracle.cpython-310.pyc
o

�Ad�:�	@s�dZddlZddlZddlmZddlmZmZddlm	Z	ddlm
ZddlmZm
Z
mZddlmZdd	lmZmZmZmZdd
lmZmZe�e�ZddiZd
ZdZedZdZ ddiZ!edd�Z"Gdd�dej#�Z$deddfdd�Z%Gdd�de
j&�Z'dee(fdd�Z)de*fdd �Z+d1d"e,d#e(d$e,de-fd%d&�Z.dd'�d(e*de"fd)d*�Z/e'e
j0ffgZ1d+d,�Z2ed-kr�ddl3Z3d.Z4e3j5e4d/�Z6e6�7�e8e�9e/�e+�d0���dSdS)2a3Datasource 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)�
namedtuple)�Optional�Tuple)�dmi)�log)�net�sources�util)�
NetworkConfig)�cmdline�	ephemeral�get_interfaces_by_mac�is_netfail_master)�UrlError�readurl�configure_secondary_nicsFzOracleCloud.comz&http://169.254.169.254/opc/v{version}/z{path}/i(#�
Authorizationz
Bearer Oracle�OpcMetadataz version instance_data vnics_datac@seZdZdZdefdd�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��self�r�D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceOracle.py�
is_applicable9�
z,KlibcOracleNetworkConfigSource.is_applicableN)�__name__�
__module__�__qualname__�__doc__rrrrrrr2sr�network_configrcCsd|vrdS|ddvrt�d|d�dSt�}|ddkrJdd�|dD�D]}|d	d
krGd|vrG|d}|�|�}|s@q(t|�rG|d=q(dS|ddkr�|�d
i���D]/\}}d|vr�|�di��d�}|r�|�|�}|ssqXt|�r�|dd=|d=||dd<qXdSdS)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.

    �versionN)��z+Ignoring unknown network config version: %sr$cSsg|]}d|vr|�qS)�typer)�.0�crrr�
<listcomp>Zsz,_ensure_netfailover_safe.<locals>.<listcomp>�configr&�physical�mac_addressr%�	ethernets�match�
macaddresszset-name�name)�LOG�debugr
�getr�items)r"�mac_to_name�cfg�mac�cur_name�_�macaddrrrr�_ensure_netfailover_safe>sH�
��


��r;cs�eZdZUdZdZdZejjejj	ejj
ejjfZe
ejdfed<gdd�Zeed<�fdd	�Zd
efdd�Zd
efd
d�Zdd�Zd
efdd�Zdd�Zd
efdd�Zd
efdd�Zedd��Zddefdd�Z�ZS) �DataSourceOracle�OracleN.�network_config_sourcesr$)r*r#�_network_configcsPtt|�j|g|�Ri|��d|_t�t�|d|jgi�tg�|_	t
�|_dS)N�
datasource)�superr<�__init__�_vnics_datar	�
mergemanydict�get_cfg_by_path�dsname�BUILTIN_DS_CONFIG�ds_cfgr�_network_config_source)r�sys_cfg�args�kwargs��	__class__rrrB�s��zDataSourceOracle.__init__rcCst|j�dg��S)Nr*)rr?r3rrrr�_has_network_config��z$DataSourceOracle._has_network_configcCst�S)z@Check platform environment to report if this datasource may run.)�_is_platform_viablerrrrrQ�sz$DataSourceOracle._is_platform_viablecCs|��sdSt�|_tjt��tjddd�t	d�|j
��d�}|��}|j
�dtd�}|�t|p4|d�}Wd�n1sAwY|j}|_tj|jd	�|_|j|_|d
|dd|d
|dd�|_d|vr�|d�d�}|r|t�|�|_|d�d�|jd<dS)NFr%�instance�r#�path)�url�headers)�iface�connectivity_url_data�tmp_dirr��fetch_vnics_data)r#�	ociAdName�idr�hostname�displayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer0�metadata�	user_data�ssh_authorized_keys�public_keysT)rQ�_read_system_uuid�system_uuidr�EphemeralDHCPv4r�find_fallback_nic�METADATA_PATTERN�format�
V2_HEADERS�distro�get_tmp_exec_path�_is_iscsi_rootrHr3rG�read_opc_metadata�
instance_data�_crawled_metadata�
METADATA_ROOTr#�metadata_address�
vnics_datarCr`�base64�	b64decode�userdata_raw)r�network_context�fetch_primary_nic�fetch_secondary_nics�fetched_metadata�datararrr�	_get_data�sN��
�����
�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.)r�instance_id_matches_system_uuidre)rrJrrr�check_instance_id�sz"DataSourceOracle.check_instance_idcCst�|j�d��S)Nrc)r�normalize_pubkey_datar`r3rrrr�get_public_ssh_keys�rPz$DataSourceOracle.get_public_ssh_keyscC�
|j��S)z)Return whether we are on a iscsi machine.)rIrrrrrrm�rzDataSourceOracle._is_iscsi_rootcCr��N)rI�
render_configrrrr�_get_iscsi_config�s
z"DataSourceOracle._get_iscsi_configcCs�|��r|jSd}|��r|��|_|��st�d�d}|j�dtd�}|s*|rAz|�	|�Wnt
y@t�td�Ynwt
|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!)rOr?rmr�r1�warningrHr3rG�!_add_network_config_from_opc_imds�	Exceptionr	�logexcr;)r�set_primary�
set_secondaryrrrr"�s2

����
	zDataSourceOracle.network_configFr�cCsV|jdurt�d�dS|sd|jdvrt�d�dSt�}|r$|jn|jdd�}t|�D]y\}}|o8|dk}|d��}||vrJt�d|�q/||}t�|d	�}	|jd
dkr�|rcddi}
n
d
|d�d|	j	��d�}
|d|t
|
gd�}|jd�|�q/|jd
dkr�t
d|id|d�}|s�|d�d|	j	��g|d<||jd|<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 be�nicIndexrz\VNIC metadata indicates this is a bare metal machine; skipping secondary VNIC configuration.r$�macAddrz)Interface with MAC %s not found; skipping�subnetCidrBlockr#r&�dhcp�static�	privateIp�/)r&�addressr+)r0r&r,�mtu�subnetsr*r%r/F)r�r.�dhcp6�dhcp4�	addressesr-)rCr1r�r
�	enumerate�lower�	ipaddress�
ip_networkr?�	prefixlen�MTU�append)rr��interfaces_by_macrs�index�	vnic_dict�
is_primaryr,r0�network�subnet�interface_configrrrr�s\

��
������z2DataSourceOracle._add_network_config_from_opc_imds)F)rrr rFre�vendordata_purer�NetworkConfigSource�CMD_LINE�DS�	INITRAMFS�
SYSTEM_CFGr>r�__annotations__r?�dictrBrrOrQr|r~r�rmr��propertyr"r��
__classcell__rrrMrr<rs*
�0
-r<cCst�d�}|durdS|��S)Nzsystem-uuid)r�
read_dmi_datar�)�sys_uuidrrrrdQs
rdcCst�d�}|tkS)Nzchassis-asset-tag)rr��CHASSIS_ASSET_TAG)�	asset_tagrrrrQVs
rQr%�metadata_versionrT�retriescCs*ttj||d�|dkr
tnd|d�j��S)NrSr$)rUrVr�)rrhrirj�	_response�json)r�rTr�rrr�_fetch[s��r�rZr[cCs|d}zt|dd�}Wntyd}t|dd�}Ynwd}|r8zt|dd�}Wnty7t�td�Ynwt|||�S)aCFetch metadata from the /opc/ routes.

    :return:
        A namedtuple containing:
          The metadata version as an integer
          The JSON-decoded value of the instance data endpoint on the IMDS
          The JSON-decoded value of the vnics data endpoint if
            `fetch_vnics_data` is True, else None

    r%rR)rTr$N�vnicsz+Failed to fetch IMDS network configuration!)r�rr	r�r1r)r[r�rorsrrrrncs��rncCst�|t�Sr�)r�list_from_depends�datasources)�dependsrrr�get_datasource_list�sr��__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.)�description)rnrQ)r%):r!rtr��collectionsr�typingrr�	cloudinitrr�loggingrrr	�cloudinit.distros.networkingr
�
cloudinit.netrrr
r�cloudinit.url_helperrr�	getLoggerrr1rGr�rqrhr�rjr�KlibcNetworkConfigSourcerr;�
DataSourcer<�strrdrrQ�intr�r�rn�DEP_FILESYSTEMr�r��argparser��ArgumentParser�parser�
parse_args�print�
json_dumpsrrrr�<module>sX
�
4`
 �����