o
    f                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZ e eZddiZd	Zd
Zd
ZG dd dejZG dd deZeejffeejejffgZdd ZdS )    N)net)sourcesutil)NoDHCPLeaseError)EphemeralDHCPv4)upcloudmetadata_urlz'http://169.254.169.254/metadata/v1.json      c                   @   sH   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	e
dd ZdS )DataSourceUpCloudUpCloudFc                 C   s   t j| ||| || _t | _tt|ddgi t	g| _
| j
d | _| j
dt| _| j
dt| _| j
dt| _d | _d | _d S )N
datasourcer   r   retriestimeout
wait_retry)r   
DataSource__init__distrodictmetadatar   mergemanydictget_cfg_by_pathBUILTIN_DS_CONFIGds_cfgmetadata_addressget
MD_RETRIESr   
MD_TIMEOUTr   MD_WAIT_RETRYr   _network_configmetadata_full)selfsys_cfgr   paths r$   E/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceUpCloud.pyr   "   s   
zDataSourceUpCloud.__init__c                 C   s   t  S N)	uc_helperread_sysinfor!   r$   r$   r%   _get_sysinfo3   s   zDataSourceUpCloud._get_sysinfoc                 C   s   t j| j| j| j| jdS )N)r   sec_betweenr   )r'   read_metadatar   r   r   r   r)   r$   r$   r%   _read_metadata6   s   z DataSourceUpCloud._read_metadatac              
   C   s  |   \}}|s
dS td| | jrcz1td t }td| t| j| t	j
tjd| jd}W d    n1 s>w   Y  W nR ttjfyb } zt	tt| W Y d }~dS d }~ww ztd t	j
tjd| jd}W n" tjy } zt	tt| td| W Y d }~dS d }~ww || _|d	|| jd
< |d| jd< |d| jd< |d| jd< |dd| jd< |dd | _|dd | _dS )NFz"Running on UpCloud. server_uuid=%szFinding a fallback NICz*Discovering metadata via DHCP interface %szReading from metadata service)logfuncmsgfuncz7Discovering metadata without DHCP-configured networkingzENo DHCP-enabled interfaces available, unable to fetch metadata for %sinstance_idzinstance-idhostnamezlocal-hostnamenetworkpublic_keyszpublic-keysregiondefaultavailability_zonevendor_data	user_dataT)r*   LOGinfoperform_dhcp_setupdebugcloudnetfind_fallback_nicr   r   r   log_timer-   r   r   InvalidMetaDataExceptionlogexcstrr    r   r   vendordata_rawuserdata_raw)r!   
is_upcloudserver_uuidnicmder$   r$   r%   	_get_data>   sb   

	zDataSourceUpCloud._get_datac                 C   s   t |  S r&   )r   instance_id_matches_system_uuidget_instance_id)r!   r"   r$   r$   r%   check_instance_ids   s   z#DataSourceUpCloud.check_instance_idc                 C   s6   | j r| j S | jd}|stdt|| _ | j S )z
        Configure the networking. This needs to be done each boot,
        since the IP and interface information might have changed
        due to reconfiguration.
        r3   z/Unable to get network meta-data from server....)r   r   r   RuntimeErrorr'   convert_network_config)r!   raw_network_configr$   r$   r%   network_configv   s   z DataSourceUpCloud.network_configN)__name__
__module____qualname__dsnamer<   r   r*   r-   rK   rN   propertyrR   r$   r$   r$   r%   r      s    5r   c                   @   s   e Zd ZdZdZdS )DataSourceUpCloudLocalaz  
    Run in init-local using a DHCP discovery prior to metadata crawl.

    In init-local, no network is available. This subclass sets up minimal
    networking with dhclient on a viable nic so that it can talk to the
    metadata service. If the metadata service provides network configuration
    then render the network configuration for that instance based on metadata.
    TN)rS   rT   rU   __doc__r<   r$   r$   r$   r%   rX      s    	rX   c                 C   s   t | tS r&   )r   list_from_dependsdatasources)dependsr$   r$   r%   get_datasource_list   s   r]   )logging	cloudinitr   r>   r   r   cloudinit.net.dhcpr   cloudinit.net.ephemeralr   cloudinit.sources.helpersr   r'   	getLoggerrS   r:   r   r   r   r   r   r   rX   DEP_FILESYSTEMDEP_NETWORKr[   r]   r$   r$   r$   r%   <module>   s"   
s
