o
    w7e:                  	   @   s  d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZmZ eeZddiZdZdZed Z dZ!ddiZ"eddZ#G dd dej$Z%deddfddZ&G dd dej'Z(dee) fddZ*de+fdd Z,d1d"e-d#e)d$e-de.fd%d&Z/dd'd(e+de#fd)d*Z0e(ej1ffgZ2d+d, Z3ed-krddl4Z4d.Z5e4j6e5d/Z7e78  e9e	:e0 e, d0 dS dS )2a3  Datasource 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)atomic_helperdmi)log)netsourcesutil)NetworkConfig)cmdline	ephemeralget_interfaces_by_macis_netfail_master)UrlErrorreadurlconfigure_secondary_nicsFzOracleCloud.comz&http://169.254.169.254/opc/v{version}/z{path}/i(#  AuthorizationzBearer OracleOpcMetadataz version instance_data vnics_datac                   @   s   e Zd ZdZdefddZdS )KlibcOracleNetworkConfigSourcezOverride 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                 C   s
   t | jS )zOverride 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__r   r   r   r   r   r   r   2   s    r   network_configr   c                 C   s  d| vrdS | d dvrt d| d  dS t }| d dkrJdd | d D D ]}|d	 d
krGd|v rG|d }||}|s@q(t|rG|d= q(dS | d dkr| di  D ]/\}}d|v r|di d}|r||}|ssqXt|r|d d= |d= ||d d< qXdS dS )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%   c                 S   s   g | ]}d |v r|qS )typer   ).0cr   r   r   
<listcomp>Z   s    z,_ensure_netfailover_safe.<locals>.<listcomp>configr'   physicalmac_addressr&   	ethernetsmatch
macaddresszset-namename)LOGdebugr   getr   items)r#   mac_to_namecfgmaccur_name_macaddrr   r   r   _ensure_netfailover_safe>   sH   



r<   c                       s   e Zd ZU dZdZdZejjejj	ejj
ejjfZeejdf ed< g ddZeed<  fdd	Zd
efddZed
efddZdd Zd
efddZdd Zd
efddZd
efddZedd ZddefddZ  ZS ) DataSourceOracleOracleN.network_config_sourcesr%   )r+   r$   _network_configc                    sP   t t| j|g|R i | d | _tt|d| jgi tg| _	t
 | _d S )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__r   r   rC      s   zDataSourceOracle.__init__r   c                 C   s   t | jdg S )Nr+   )r   r@   r4   r   r   r   r   _has_network_config      z$DataSourceOracle._has_network_configc                   C   s   t  S )z@Check platform environment to report if this datasource may run.)_is_platform_viabler   r   r   r   	ds_detect   s   zDataSourceOracle.ds_detectc                 C   s   t  | _tj| jt tjdddt	dd}| 
  }| jdtd }| t|p,|d}W d    n1 s9w   Y  |j }| _tj|jd| _|j| _|d	 |d
 d|d |d d| _d|v r~|d d}|rtt|| _|d d| jd< dS )Nr&   instancer$   path)urlheaders)ifaceconnectivity_url_datar   fetch_vnics_data)r$   	ociAdNameidr   hostnamedisplayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer1   metadata	user_datassh_authorized_keyspublic_keysT)_read_system_uuidsystem_uuidr   EphemeralDHCPv4distror   find_fallback_nicMETADATA_PATTERNformat
V2_HEADERS_is_iscsi_rootrI   r4   rH   read_opc_metadatainstance_data_crawled_metadataMETADATA_ROOTr$   metadata_address
vnics_datarD   ra   base64	b64decodeuserdata_raw)r   network_contextfetch_primary_nicfetch_secondary_nicsfetched_metadatadatarb   r   r   r   	_get_data   sJ   

zDataSourceOracle._get_datac                 C   s   t | jS )zquickly 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_uuidrf   )r   rK   r   r   r   check_instance_id   s   z"DataSourceOracle.check_instance_idc                 C   s   t | jdS )Nrd   )r	   normalize_pubkey_datara   r4   r   r   r   r   get_public_ssh_keys   rQ   z$DataSourceOracle.get_public_ssh_keysc                 C   
   | j  S )z)Return whether we are on a iscsi machine.)rJ   r   r   r   r   r   rm      r   zDataSourceOracle._is_iscsi_rootc                 C   r   N)rJ   render_configr   r   r   r   _get_iscsi_config   s   
z"DataSourceOracle._get_iscsi_configc                 C   s   |   r| jS d}|  r|  | _|   std d}| jdtd }|s*|rAz| 	| W n t
y@   ttd Y nw t| j | jS )zNetwork 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.Tr   z+Failed to parse IMDS network configuration!)rP   r@   rm   r   r2   warningrI   r4   rH   !_add_network_config_from_opc_imds	Exceptionr
   logexcr<   )r   set_primaryset_secondaryr   r   r   r#      s2   


	zDataSourceOracle.network_configFr   c                 C   sV  | j du rtd dS |sd| j d v rtd dS t }|r$| j n| j dd }t|D ]y\}}|o8|dk}|d  }||vrJtd| q/|| }t|d	 }	| jd
 dkr|rcddi}
nd|d  d|	j	 d}
|d|t
|
gd}| jd | q/| jd
 dkrt
d|id|d}|s|d  d|	j	 g|d< || jd |< q/dS )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nicIndexr   z\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,   )r1   r'   r-   mtusubnetsr+   r&   r0   F)r   r/   dhcp6dhcp4	addressesr.   )rD   r2   r   r   	enumeratelower	ipaddress
ip_networkr@   	prefixlenMTUappend)r   r   interfaces_by_macrs   index	vnic_dict
is_primaryr-   r1   networksubnetinterface_configr   r   r   r     s\   


z2DataSourceOracle._add_network_config_from_opc_imds)F)r   r    r!   rG   rf   vendordata_purer	   NetworkConfigSourceCMD_LINE
SYSTEM_CFGDS	INITRAMFSr?   r   __annotations__r@   dictrC   r   rP   staticmethodrS   r|   r~   r   rm   r   propertyr#   r   __classcell__r   r   rN   r   r=   r   s,   
 .
-r=   c                  C   s   t d} | d u rd S |  S )Nzsystem-uuid)r   read_dmi_datar   )sys_uuidr   r   r   re   P  s   
re   c                  C   s   t d} | tkS )Nzchassis-asset-tag)r   r   CHASSIS_ASSET_TAG)	asset_tagr   r   r   rR   U  s   
rR   r&   metadata_versionrV   retriesc                 C   s*   t tj| |d| dkrtnd |dj S )NrU   r%   )rW   rX   r   )r   rj   rk   rl   	_responsejson)r   rV   r   r   r   r   _fetchZ  s   r   r[   r\   c                 C   s|   d}zt |dd}W n ty   d}t |dd}Y nw d}| r8zt |dd}W n ty7   ttd Y nw t|||S )aC  Fetch 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&   rT   )rV   r%   Nvnicsz+Failed to fetch IMDS network configuration!)r   r   r
   r   r2   r   )r\   r   ro   rs   r   r   r   rn   b  s   rn   c                 C   s   t | tS r   )r	   list_from_dependsdatasources)dependsr   r   r   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)rn   rR   )r&   );r"   rt   r   collectionsr   typingr   r   	cloudinitr   r   r   loggingr   r	   r
   cloudinit.distros.networkingr   cloudinit.netr   r   r   r   cloudinit.url_helperr   r   	getLoggerr   r2   rH   r   rq   rj   r   rl   r   KlibcNetworkConfigSourcer   r<   
DataSourcer=   strre   r   rR   intr   r   rn   DEP_FILESYSTEMr   r   argparser   ArgumentParserparser
parse_argsprint
json_dumpsr   r   r   r   <module>   sX   

4 _
 