Your IP : 216.73.217.13


Current Path : /lib/python3/dist-packages/cloudinit/sources/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceOracle.cpython-38.pyc

U

�Ad�:�@s�dZddlZddlZddlmZddlmZmZddlm	Z	ddlm
ZddlmZm
Z
mZddlmZdd	lmZmZmZmZdd
lmZmZe�e�ZddiZd
ZdZedZdZ ddiZ!edd�Z"Gdd�dej#�Z$edd�dd�Z%Gdd�de
j&�Z'ee(d�dd�Z)e*d�dd �Z+d/e,e(e,e-d"�d#d$�Z.dd%�e*e"d&�d'd(�Z/e'e
j0ffgZ1d)d*�Z2ed+k�r�ddl3Z3d,Z4e3j5e4d-�Z6e6�7�e8e�9e/�e+�d.���dS)0a3Datasource 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(#Z
Authorizationz
Bearer Oracle�OpcMetadataz version instance_data vnics_datac@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��self�r�D/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceOracle.py�
is_applicable9sz,KlibcOracleNetworkConfigSource.is_applicableN)�__name__�
__module__�__qualname__�__doc__rrrrrrr2sr)�network_configrcCsd|krdS|ddkr,t�d|d�dSt�}|ddkr�dd�|dD�D]@}|d	d
krPd|krP|d}|�|�}|s�qPqPt|�rP|d=qPn||ddk�r|�d
i���D]\\}}d|kr�|�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.

    �versionN)��z+Ignoring unknown network config version: %sr#cSsg|]}d|kr|�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!Zmac_to_nameZcfgZmacZcur_name�_Zmacaddrrrr�_ensure_netfailover_safe>s>�



r5cs�eZdZUdZdZdZejjejj	ejj
ejjfZe
ejdfed<gdd�Zeed<�fdd	�Zed
�dd�Zed
�d
d�Zdd�Zed
�dd�Zdd�Zed
�dd�Zed
�dd�Zedd��Zded�dd�Z�ZS) �DataSourceOracleZOracleN.�network_config_sourcesr#)r)r"�_network_configcsJtt|�j|f|�|�d|_t�t�|d|jgi�tg�|_	t
�|_dS)NZ
datasource)�superr6�__init__�_vnics_datar	Z
mergemanydictZget_cfg_by_path�dsname�BUILTIN_DS_CONFIG�ds_cfgr�_network_config_source)r�sys_cfg�args�kwargs��	__class__rrr:�s��zDataSourceOracle.__init__rcCst|j�dg��S)Nr))rr8r2rrrr�_has_network_config�sz$DataSourceOracle._has_network_configcCst�S)z@Check platform environment to report if this datasource may run.)�_is_platform_viablerrrrrF�sz$DataSourceOracle._is_platform_viablec	Cs�|��sdSt�|_tjt��tjddd�t	d�|j
��d�}|��}|j
�dtd�}|�t|ph|d�}W5QRX|j}|_tj|jd	�|_|j|_|d
|dd|d
|dd�|_d|kr�|d�d�}|r�t�|�|_|d�d�|jd<dS)NFr$�instance�r"�path)�url�headers)ZifaceZconnectivity_url_dataZtmp_dirr��fetch_vnics_data)r"Z	ociAdName�idrZhostnameZdisplayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer/�metadata�	user_dataZssh_authorized_keys�public_keysT)rF�_read_system_uuid�system_uuidrZEphemeralDHCPv4rZfind_fallback_nic�METADATA_PATTERN�format�
V2_HEADERSZdistroZget_tmp_exec_path�_is_iscsi_rootr>r2r=�read_opc_metadata�
instance_dataZ_crawled_metadata�
METADATA_ROOTr"Zmetadata_address�
vnics_datar;rO�base64Z	b64decodeZuserdata_raw)rZnetwork_contextZfetch_primary_nicZfetch_secondary_nicsZfetched_metadata�datarPrrr�	_get_data�sL��
�����
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_uuidrS)rr@rrr�check_instance_id�sz"DataSourceOracle.check_instance_idcCst�|j�d��S)NrQ)rZnormalize_pubkey_datarOr2rrrr�get_public_ssh_keys�sz$DataSourceOracle.get_public_ssh_keyscCs
|j��S)z)Return whether we are on a iscsi machine.)r?rrrrrrW�szDataSourceOracle._is_iscsi_rootcCs
|j��S�N)r?Z
render_configrrrr�_get_iscsi_config�sz"DataSourceOracle._get_iscsi_configcCs�|��r|jSd}|��r$|��|_|��s:t�d�d}|j�dtd�}|sT|r�z|�	|�Wn t
k
r�t�td�YnXt
|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!)rEr8rWrbr0�warningr>r2r=�!_add_network_config_from_opc_imds�	Exceptionr	�logexcr5)r�set_primaryZ
set_secondaryrrrr!�s0

���

zDataSourceOracle.network_configF)rgcCsZ|jdkrt�d�dS|s8d|jdkr8t�d�dSt�}|rH|jn|jdd�}t|�D]�\}}|op|dk}|d��}||kr�t�d|�q^||}t�|d	�}	|jd
dk�r|r�ddi}
nd
|d�d|	j	��d�}
|d|t
|
gd�}|jd�|�q^|jd
dkr^t
d|id|d�}|�sF|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 beZnicIndexrz\VNIC metadata indicates this is a bare metal machine; skipping secondary VNIC configuration.r#ZmacAddrz)Interface with MAC %s not found; skippingZsubnetCidrBlockr"r%ZdhcpZstaticZ	privateIp�/)r%Zaddressr*)r/r%r+�mtuZsubnetsr)r$r.F)rir-Zdhcp6Zdhcp4Z	addressesr,)r;r0rcr
�	enumerate�lower�	ipaddressZ
ip_networkr8Z	prefixlen�MTU�append)rrgZinterfaces_by_macr[�indexZ	vnic_dictZ
is_primaryr+r/ZnetworkZsubnetZinterface_configrrrrdsX

��
����z2DataSourceOracle._add_network_config_from_opc_imds)F)rrrr<rSZvendordata_purerZNetworkConfigSourceZCMD_LINEZDSZ	INITRAMFSZ
SYSTEM_CFGr7r�__annotations__r8�dictr:rrErFr^r_r`rWrb�propertyr!rd�
__classcell__rrrCrr6rs(
�0
-r6rcCst�d�}|dkrdS|��S)Nzsystem-uuid)r�
read_dmi_datark)Zsys_uuidrrrrRQs
rRcCst�d�}|tkS)Nzchassis-asset-tag)rrt�CHASSIS_ASSET_TAG)Z	asset_tagrrrrFVs
rFr$)�metadata_versionrI�retriesrcCs*ttj||d�|dkrtnd|d�j��S)NrHr#)rJrKrw)rrTrUrVZ	_responseZjson)rvrIrwrrr�_fetch[s
�rxrL)rMrcCs�d}zt|dd�}Wn$tk
r8d}t|dd�}YnXd}|rtzt|dd�}Wn tk
rrt�td�YnXt|||�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$rG)rIr#NZvnicsz+Failed to fetch IMDS network configuration!)rxrr	rfr0r)rMrvrYr[rrrrXcsrXcCst�|t�Sra)rZlist_from_depends�datasources)Zdependsrrr�get_datasource_list�srz�__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)rXrF)r$):r r\rl�collectionsr�typingrrZ	cloudinitrrZloggingrrr	Zcloudinit.distros.networkingr
Z
cloudinit.netrrr
rZcloudinit.url_helperrrZ	getLoggerrr0r=rurZrTrmrVrZKlibcNetworkConfigSourcerr5Z
DataSourcer6�strrRrrF�intrqrxrXZDEP_FILESYSTEMryrz�argparser|�ArgumentParser�parser�
parse_args�printZ
json_dumpsrrrr�<module>sV
�
4` 
�
���