509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
\¼meX:  ã               @   sœ  d Z ddl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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j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$eddœdd„Z%G dd„ dej&ƒZ'ee( dœdd„Z)e*dœdd„Z+d-e,e(e,e-d œd!d"„Z.d
d#œe*e"d$œd%d&„Z/e'ej0ffgZ1d'd(„ Z2ed)k�r˜ddl3Z3d*Z4e3j5e4d+�Z6e6j7ƒ  e8e
j9e/ƒ e+ƒ d,œƒƒ dS ).a3  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Ú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(#  ZAuthorizationzBearer OracleÚOpcMetadataz version instance_data vnics_datac               @   s   e Zd ZdZedœdd„ZdS )Ú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             C   s
   t | jƒS )zOverride is_applicable)ÚboolÚ_files)Úself© r   ú&/usr/lib/python3.6/DataSourceOracle.pyÚis_applicable8   s    z,KlibcOracleNetworkConfigSource.is_applicableN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   1   s   r   )Únetwork_configr   c             C   s  d| krdS | d dkr,t jd| d ƒ dS tƒ }| d dkr˜xØdd„ | d D ƒD ]@}|d	 d
krRd|krR|d }|j|ƒ}|s„qRqRt|ƒrR|d= qRW n€| d dk�rxp| jdi ƒjƒ D ]\\}}d|kr¸|jdi ƒjdƒ}|r¸|j|ƒ}|sðq¸q¸t|ƒr¸|d d= |d= ||d d< q¸W 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: %sc             S   s   g | ]}d |kr|‘qS )Útyper   )Ú.0Úcr   r   r   ú
<listcomp>Y   s    z,_ensure_netfailover_safe.<locals>.<listcomp>Úconfigr#   ÚphysicalÚmac_addressÚ	ethernetsÚmatchÚ
macaddresszset-nameÚname)r!   r"   )ÚLOGÚdebugr   Úgetr   Úitems)r   Zmac_to_nameZcfgZmacZcur_nameÚ_Zmacaddrr   r   r   Ú_ensure_netfailover_safe=   s<    



r3   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 g ddœZe‡ fdd„Zedœd	d
„Zeedœdd„ƒZdd„ Zedœdd„Zdd„ Zedœdd„Zedœdd„Zedd„ ƒZdedœdd„Z‡  ZS )ÚDataSourceOracleZOracleN.r!   )r'   r    c                sJ   t t| ƒj|f|ž|Ž d | _tjtj|d| jgi ƒtgƒ| _	t
ƒ | _d S )NZ
datasource)Úsuperr4   Ú__init__Ú_vnics_datar	   ZmergemanydictZget_cfg_by_pathÚdsnameÚBUILTIN_DS_CONFIGÚds_cfgr   Ú_network_config_source)r   Úsys_cfgÚargsÚkwargs)Ú	__class__r   r   r6      s    
zDataSourceOracle.__init__)r   c             C   s   t | jjdg ƒƒS )Nr'   )r   Ú_network_configr0   )r   r   r   r   Ú_has_network_config‹   s    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jƒ tjddd�t	dœd�}| j
ƒ  }| jjdtd ƒ}|� t|pX|d�}W d Q R X |j }| _tj|jd�| _|j| _|d	 |d
 d|d |d dœ| _d|krè|d jdƒ}|rÔtj|ƒ| _|d jdƒ| jd< dS )Nr"   Úinstance)r    Úpath)ÚurlÚheaders)ZifaceZconnectivity_url_datar   )Úfetch_vnics_data)r    Z	ociAdNameÚidr   ZhostnameZdisplayName)zavailability-zonezinstance-idzlaunch-indexzlocal-hostnamer-   ÚmetadataÚ	user_dataZssh_authorized_keysÚpublic_keysT)Ú_read_system_uuidÚsystem_uuidr   ZEphemeralDHCPv4Zdistror   Zfind_fallback_nicÚMETADATA_PATTERNÚformatÚ
V2_HEADERSÚ_is_iscsi_rootr:   r0   r9   Úread_opc_metadataÚinstance_dataZ_crawled_metadataÚMETADATA_ROOTr    Zmetadata_addressÚ
vnics_datar7   rJ   Úbase64Z	b64decodeZuserdata_raw)r   Znetwork_contextZfetch_primary_nicZfetch_secondary_nicsZfetched_metadataÚdatarK   r   r   r   Ú	_get_data“   s:    

zDataSourceOracle._get_datac             C   s   t j| 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   Zinstance_id_matches_system_uuidrN   )r   r<   r   r   r   Úcheck_instance_idÁ   s    z"DataSourceOracle.check_instance_idc             C   s   t j| jjdƒƒS )NrL   )r   Znormalize_pubkey_datarJ   r0   )r   r   r   r   Úget_public_ssh_keysÈ   s    z$DataSourceOracle.get_public_ssh_keysc             C   s
   | j jƒ S )z)Return whether we are on a iscsi machine.)r;   r   )r   r   r   r   rR   Ë   s    zDataSourceOracle._is_iscsi_rootc             C   s
   | j jƒ S )N)r;   Zrender_config)r   r   r   r   Ú_get_iscsi_configÏ   s    z"DataSourceOracle._get_iscsi_configc             C   s”   | j ƒ r| jS d}| jƒ r$| jƒ | _| j ƒ s:tjdƒ d}| jjdtd ƒ}|sT|r„y| j	|ƒ W n  t
k
r‚   tjtdƒ Y nX t| jƒ | jS )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.Tr   z+Failed to parse IMDS network configuration!)rA   r@   rR   r\   r.   Úwarningr:   r0   r9   Ú!_add_network_config_from_opc_imdsÚ	Exceptionr	   Úlogexcr3   )r   Úset_primaryZset_secondaryr   r   r   r   Ò   s*    



zDataSourceOracle.network_configF)ra   c             C   sb  | j dkrtjdƒ dS | r:d| j d kr:tjdƒ dS tƒ }|rJ| j n| j dd… }�xt|ƒD ]ö\}}|ov|dk}|d jƒ }||krštjd|ƒ qd|| }tj|d	 ƒ}	| jd
 dk�r|rÎddi}
nd|d › d|	j	› �dœ}
|d|t
|
gdœ}| jd j|ƒ qd| jd
 dkrdt
d|id|dœ}|�sL|d › d|	j	› �g|d< || jd |< qdW 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 beZnicIndexr   z\VNIC metadata indicates this is a bare metal machine; skipping secondary VNIC configuration.r!   ZmacAddrz)Interface with MAC %s not found; skippingZsubnetCidrBlockr    r#   ZdhcpZstaticZ	privateIpú/)r#   Zaddressr(   )r-   r#   r)   ÚmtuZsubnetsr'   r"   r,   F)rc   r+   Zdhcp6Zdhcp4Z	addressesr*   )r7   r.   r]   r   Ú	enumerateÚlowerÚ	ipaddressZ
ip_networkr@   Z	prefixlenÚMTUÚappend)r   ra   Zinterfaces_by_macrV   ÚindexZ	vnic_dictZ
is_primaryr)   r-   ZnetworkZsubnetZinterface_configr   r   r   r^      sL    



z2DataSourceOracle._add_network_config_from_opc_imds)F)r   r   r   r8   rN   Zvendordata_purer   ZNetworkConfigSourceZCMD_LINEZ
SYSTEM_CFGZDSZ	INITRAMFSZnetwork_config_sourcesr   r@   Údictr6   r   rA   ÚstaticmethodrC   rY   rZ   r[   rR   r\   Úpropertyr   r^   Ú__classcell__r   r   )r?   r   r4   q   s&   
..r4   )r   c              C   s   t jdƒ} | d krd S | jƒ S )Nzsystem-uuid)r   Úread_dmi_datare   )Zsys_uuidr   r   r   rM   O  s    
rM   c              C   s   t jdƒ} | tkS )Nzchassis-asset-tag)r   rn   ÚCHASSIS_ASSET_TAG)Z	asset_tagr   r   r   rB   T  s    
rB   r"   )Úmetadata_versionrE   Úretriesr   c             C   s*   t tj| |d�| dkrtnd |d�jjƒ S )N)r    rE   r!   )rF   rG   rq   )r   rO   rP   rQ   Z	_responseZjson)rp   rE   rq   r   r   r   Ú_fetchY  s    rr   )rH   )rH   r   c             C   s€   d}yt |dd�}W n$ tk
r8   d}t |dd�}Y nX d}| rtyt |dd�}W n  tk
rr   tjtdƒ Y nX 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"   rD   )rE   r!   NZvnicsz+Failed to fetch IMDS network configuration!)rr   r   r	   r`   r.   r   )rH   rp   rT   rV   r   r   r   rS   a  s    rS   c             C   s   t j| tƒS )N)r   Zlist_from_dependsÚdatasources)Zdependsr   r   r   Úget_datasource_list†  s    rt   Ú__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)rS   rB   )r"   ):r   rW   rf   ZloggingÚcollectionsr   Ztypingr   r   Z	cloudinitr   r   r   r   r	   Zcloudinit.distros.networkingr
   Zcloudinit.netr   r   r   r   Zcloudinit.url_helperr   r   Z	getLoggerr   r.   r9   ro   rU   rO   rg   rQ   r   ZKlibcNetworkConfigSourcer   r3   Z
DataSourcer4   ÚstrrM   r   rB   Úintrj   rr   rS   ZDEP_FILESYSTEMrs   rt   Úargparserv   ÚArgumentParserÚparserÚ
parse_argsÚprintZ
json_dumpsr   r   r   r   Ú<module>   sH   

4 _ 
