Current File : //usr/lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceOracle.cpython-312.pyc
�

ӇgLT���dZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZmZddl
mZmZmZmZmZddlmZddlmZmZmZmZddlmZej8e�Zdd	iZd
Z dZ!dZ"e!d
zZ#e"d
zZ$ddiZ%dZ&Gd�de
�Z'Gd�dejP�Z)deddfd�Z*Gd�dejV�Z,Gd�de,�Z-de.fd�Z/dee.fd�Z0de1fd�Z2d e.de3fd!�Z4d e.deee.e.ffd"�Z5d e.de.fd#�Z6d	e,jne,jpe#gd$�d%e1d&e	e.dee'fd'�Z9e,ejtffe-ejtejvffgZ<d(�Z=ed)k(r,d*Z>e?ej�e9e$e#g�+�e2�d,���yy)-a3Datasource 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}/�
Authorizationz
Bearer Oraclei(#c�P�eZdZUeed<eeefed<eeeefed<eed<y)�ReadOpcMetadataResponse�version�
instance_data�
vnics_data�
imds_url_usedN)	�__name__�
__module__�__qualname__�int�__annotations__r�strrr���D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceOracle.pyrr4s.��
�L���S��>�!���c�3�h��(�(��r#rc��eZdZdZdefd�Zy)�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.
    �returnc�,�t|j�S)zOverride is_applicable)�bool�_files��selfs r$�
is_applicablez,KlibcOracleNetworkConfigSource.is_applicableBs���D�K�K� � r#N)rrr�__doc__r)r-r"r#r$r&r&;s���!�t�!r#r&�network_configr'c�<�d|vry|ddvrtjd|d�yt�}|ddk(rT|dD�cgc]	}d|vs�|��c}D]8}|ddk(s�d	|vs�|d	}|j|�}|s�*t	|�s�6|d	=�:y|dd
k(r�|jdi�j�D]_\}}d|vs�|jdi�jd
�}|s�/|j|�}|s�Ct	|�s�O|dd
=|d=||dd<�ayycc}w)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: %sr1�config�type�physical�mac_addressr2�	ethernets�match�
macaddresszset-name�name)�LOG�debugr�getr�items)r/�mac_to_name�c�cfg�mac�cur_name�_�macaddrs        r$�_ensure_netfailover_saferFGsY�� ��&���i� ��.��	�	�9��9�%�	
�	�'�)�K��i� �A�%�-�h�7�G�!�6�Q�;�A�G�	/�C��6�{�j�(� �C�'��m�,�C�*���s�3�H�#� �*�8�4��
�.�	/�
�	�	"�a�	'�$�(�(��b�9�?�?�A�
	8�F�A�s��#�~��'�'�'�2�.�2�2�<�@���*���w�7�H�#� �*�8�4���L��6��
�O�/7��G��V�,�
	8�
(��Hs�	D�
Dc���eZdZUdZdZejjejjejjejjfZeejdfe
d<dZdZdZ�fd�Zd	ed
df�fd�Zd
efd�Zed
efd
��Zd�Zd
efd�Zd�Zd
efd�Zd
efd�Zed��Zddefd�Z �xZ!S)�DataSourceOracle�OracleN.�network_config_sourcesT��c�Z��tt|�
|g|��i|��d|_t	j
t	j|d|jgi�tg�|_	t�|_gdd�|_|j�}|j|_|j |_y)N�
datasourcer1�r3r)�superrH�__init__�_vnics_datar
�
mergemanydict�get_cfg_by_path�dsname�BUILTIN_DS_CONFIG�ds_cfgr&�_network_config_source�_network_config�get_url_params�max_wait_seconds�url_max_wait�timeout_seconds�url_timeout)r,�sys_cfg�args�kwargs�
url_params�	__class__s     �r$rQzDataSourceOracle.__init__�s����
���.�w�H��H��H�����(�(��$�$�W�|�T�[�[�.I�2�N�!�
�
���'E�&F��#�02�q�%A����(�(�*�
�&�7�7���%�5�5��r#�ci_pkl_versionr'c����t�|�|�t|d�s
t|dd�t|d�st|dt	��t|d�sgdd�|_yy)NrRrXrYr1rO)rP�	_unpickle�hasattr�setattrr&rY)r,rdrcs  �r$rfzDataSourceOracle._unpickle�sd���
���.�)��t�]�+��D�-��.��t�5�6���(�.�0�
�
�t�.�/�.0�Q�#?�D� �0r#c�L�t|jjdg��S)Nr3)r)rYr=r+s r$�_has_network_configz$DataSourceOracle._has_network_config�s ���D�(�(�,�,�X�r�:�;�;r#c��t�S)z@Check platform environment to report if this datasource may run.)�_is_platform_viabler"r#r$�	ds_detectzDataSourceOracle.ds_detect�s
��#�$�$r#c��t�|_tjdd��td�dtjdd��it
jdd��td�dt
jdd��if}|jr9tj�}tj|j|dd|��}ntj�}|j�}|jj!d	t"d	�}|5t%|xs||j&|j(t
tg�
�}ddd�syt+|j,��|_|j0x}|_|j4|_|d
|dd|d|dd�|_d|vrQ|dj!d�}|rt;j<|�|_|dj!d�|j8d<y#1swY��xYw)Nr2�instance�r�path)�url�headersrrr1T)�distro�	interface�ipv6�ipv4�connectivity_urls_datar��fetch_vnics_data�max_wait�timeout�metadata_patternsF�rr�	ociAdName�idr�hostname�displayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer:�metadata�	user_data�ssh_authorized_keys�public_keys) �_read_system_uuid�system_uuid�IPV4_METADATA_PATTERN�format�
V2_HEADERS�IPV6_METADATA_PATTERN�perform_dhcp_setupr�find_fallback_nicr�EphemeralIPNetworkrtr
�nullcontext�_is_iscsi_rootrWr=rV�read_opc_metadatar\r^� _get_versioned_metadata_base_urlr�metadata_addressr�_crawled_metadatarrRr��base64�	b64decode�userdata_raw)	r,rx�nic_name�network_context�fetch_primary_nic�fetch_secondary_nics�fetched_metadata�datar�s	         r$�	_get_datazDataSourceOracle._get_data�s6��,�.���-�3�3��J�4��&�	
��,�3�3��J�4��
�-�3�3��J�4��&�	
��,�3�3��J�4��
�%"
��2�"�"��,�,�.�H�'�:�:��{�{�"���'=��O�#�.�.�0�O� $� 3� 3� 5�5��#�{�{���&��8�9� 
��
�		�0�!2�!J�6J��*�*��(�(�)�)�#�	 ��		� ��!A� �.�.�!
���)9�(F�(F�F��t�%�+�6�6���"&�k�!2���:��"�:�.���'�
��
�����Z�(�,�,�[�9�I��$*�$4�$4�Y�$?��!�+/�
�+;�+?�+?�%�,�D�M�M�-�(��M		�		�s�2G6�6G?c�@�tj|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_uuidr�)r,r_s  r$�check_instance_idz"DataSourceOracle.check_instance_id	s��
�6�6�t�7G�7G�H�Hr#c�^�tj|jjd��S)Nr�)r�normalize_pubkey_datar�r=r+s r$�get_public_ssh_keysz$DataSourceOracle.get_public_ssh_keyss"���,�,�T�]�]�->�->�}�-M�N�Nr#c�6�|jj�S)z)Return whether we are on a iscsi machine.)rXr-r+s r$r�zDataSourceOracle._is_iscsi_roots���*�*�8�8�:�:r#c�6�|jj�S�N)rX�
render_configr+s r$�_get_iscsi_configz"DataSourceOracle._get_iscsi_configs���*�*�8�8�:�:r#c���|j�r|jSd}|j�r|j�|_|j�stjd�d}|jjdtd�}|s|r	|j|�t|j�|jS#t$rtjtd�Y�FwxYw)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!)rjrYr�r�r;�warningrWr=rV�!_add_network_config_from_opc_imds�	Exceptionr
�logexcrF)r,�set_primary�
set_secondarys   r$r/zDataSourceOracle.network_configs����#�#�%��'�'�'������ �#'�#9�#9�#;�D� ��'�'�)��K�K�(�
��K������&��8�9�
�
��-�
��6�6�{�C�	!��!5�!5�6��#�#�#���
�����A��
�s�C�#C+�*C+r�c�b�|j�tjd�y|s'd|jdvrtjd�yt�}|r|jn|jdd}t	|�D�].\}}|xr|dk(}|dj�}|j
dd	�xr|j
d
d	�}||vrtjd|��e||}	|rtj|dd�}
ntj|d
�}
|jddk(r�|r|rddig}nyddig}nsg}|j
d
�r&|jd|d
�d|
j��d��|j
d�r)|jd|dd�d|
j��d��|	d|t|d�}|jdj|���a|jddk(s��utd|id�}||jd|	<|xr||d<|xr||d<|svg|d<|j
d
�r&|dj|d
�d|
j���|j
d�r)|dj|dd�d|
j���||jd|	<��1y)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.r1�macAddr�ipv6SubnetCidrBlockF�	privateIpz)Interface with MAC %s not found; skipping�
ipv6Addresses�subnetCidrBlockrr4�dhcp6�dhcp�static�/)r4�addressr5)r:r4r6�mtu�subnetsr3r2r9)r�r8r7�dhcp4�	addresses)
rRr;r�r�	enumerate�lowerr=�	ipaddress�
ip_networkrY�append�	prefixlen�MTU)
r,r��interfaces_by_macr�index�	vnic_dict�
is_primaryr6�is_ipv6_onlyr:�networkr��interface_configs
             r$r�z2DataSourceOracle._add_network_config_from_opc_imdsHs:�����#��K�K�=�>���
�d�.>�.>�q�.A� A�
�K�K�9�
�
�1�3��)4�T�%�%�$�:J�:J�1�2�:N�
�!*�*� 5�N	K��E�9�$�3��!��J�#�I�.�4�4�6�K�$�=�=�%�u��8��m�m�K��7�7�
��"3�3����?����$�[�1�D��#�.�.��o�.�q�1���$�.�.�y�9J�/K�L���#�#�I�.�!�3��#�$*�G�#4�"5��$*�F�#3�"4�� �G� �}�}�[�1����(0�'0��'=�&>�a�'.�'8�'8�&9�%;���!�}�}�_�5����(0�'0��'A�!�'D�&E�Q�'.�'8�'8�&9�%;���!�&�#.��&�$� ��$�$�X�.�5�5�6F�G��%�%�i�0�A�5��*�K�8�$� �;K��$�$�[�1�$�7�,6�,G�<� ��)�,6�,K�|�;K� ��)�!�46�$�[�1� �}�}�[�1�(��5�<�<�(��5�6�a��8I�8I�7J�K��!�}�}�_�5�(��5�<�<�(��9�!�<�=�Q�&�0�0�1�3��;K��$�$�[�1�$�7�]N	Kr#)F)"rrrrUr�r�NetworkConfigSource�CMD_LINE�
SYSTEM_CFG�DS�	INITRAMFSrJrr r�r\r^rQrrfr)rj�staticmethodrmr�r�r�r��dictr��propertyr/r��
__classcell__)rcs@r$rHrH{s���
�F��K��#�#�,�,��#�#�.�.��#�#�&�&��#�#�-�-�	G��E�'�"=�"=�s�"B�C�����L��K�6�"@��@��@�<�T�<��%�t�%��%�T�lI�D�I�O�;��;�;�4�;��+$��+$�ZqK�T�qKr#rHc��eZdZdZy)�DataSourceOracleNetFN)rrrr�r"r#r$r�r��s���r#r�r�c�V�|sy|jtjd�d�S)NF�opcr)�
startswith�IPV4_METADATA_ROOT�split)r�s r$�_is_ipv4_metadata_urlr��s*�����&�&�'9�'?�'?��'F�q�'I�J�Jr#c�T�tjd�}|�dS|j�S)Nzsystem-uuid)r
�
read_dmi_datar�)�sys_uuids r$r�r��s)��� � ��/�H��#�4�9����)9�9r#c�>�tjd�}|tk(S)Nzchassis-asset-tag)r
r��CHASSIS_ASSET_TAG)�	asset_tags r$rlrl�s���!�!�"5�6�I��)�)�)r#rrc��d|vrdSdS)Nz/opc/v2/r2r1r"r~s r$�_url_versionr��s���c�!�1�(�q�(r#c�.�t|�dk(rtSdS)Nr2)r�r�r~s r$�_headers_cbr��s��%�c�*�a�/�:�9�T�9r#c��|s|Sd|vr|jd�ddzSd|vr|jd�ddzStd|z��)zQ
    Remove everything following the version number in the metadata address.
    �v2rzv2/�v1zv1/zInvalid metadata address: )r��
ValueErrorr~s r$r�r��s[����
��s�{��y�y���q�!�E�)�)�	
����y�y���q�!�E�)�)��5��;�<�<r#ryrzr}c	��dD��cgc]}|D]}|j|d�����}}}tjd|�tj�}t|||tdd��\}}	|s&tjdd	j|��y
tjd|�tj|	jd��}
t|�}d
}|r�t|jdd
�g|tj�|z
z
|tdd��\}
}|
r;tj|jd��}tjd|
�ntjd�t||
||�Scc}}w)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.
    )r2r1rorpz*Attempting to fetch IMDS metadata from: %sg�������?T)�urlsr{r|�
headers_cb�
sleep_time�connect_synchronouslyz-Failed to fetch IMDS metadata from any of: %sz, Nz7Successfully fetched instance metadata from IMDS at: %szutf-8�vnicsz4Successfully fetched vnics metadata from IMDS at: %sz+Failed to fetch IMDS network configuration!)r�r;r<�time�	monotonicrr�r��join�json�loads�decoder��replacer)rzr{r|r}r�metadata_patternr��
start_time�url_that_worked�instance_responser�metadata_versionr�	vnics_url�vnics_responses               r$r�r��s���0��� 1��
�	����j��A��A��D���I�I�:�D�A����!�J�)5�
�����"�
*�&�O�&�����;��I�I�d�O�	
���	�	�E��	
��J�J�0�7�7��@�A�M�$�O�4���J��%1�!�)�)�*�g�>�?�����!1�J�!>�?��"��"&�
%
�!�	�>�����N�$9�$9�'�$B�C�J��I�I�F��
�

�K�K�E�F�"�����	���ks�!E>c�6�tj|t�Sr�)r�list_from_depends�datasources)�dependss r$�get_datasource_listrGs���$�$�W�k�:�: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.)r})r�rl)Ar.r�r�r��loggingr��typingrrrrrr�	cloudinitr	r
rrr
�cloudinit.distros.networkingr�
cloudinit.netrrrr�cloudinit.url_helperr�	getLoggerrr;rVr�r��IPV6_METADATA_ROOTr�r�r�r�r�KlibcNetworkConfigSourcer&rF�
DataSourcerHr�r!r�r�r)rlrr�r�r�r\r^r��DEP_FILESYSTEM�DEP_NETWORKrr�description�print�
json_dumpsr"r#r$�<module>rs4��������?�?�<�<�6���.��g����!������&��=��B��*�Y�6��*�Y�6����
/�
����j��	!�W�%E�%E�	!�18�]�18�t�18�h~K�w�)�)�~K�B
�*��K�C�K�:�8�C�=�:�
*�T�*�
)�c�)�c�)�:�S�:�X�d�3��8�n�5�:�=�#�=�#�=� #�
�
*�
*��(�(�$9�#:�P��P�
�C�y�P��%�&�
P�j��.�.�0�1���"�"����	
��	��;��z�� �K�
� �
� � �%6�.�0E�F�&�
(;�'<�

�		
��r#