o
    th                     @   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r]z+td t }td| t| j| | 	 }W d    n1 s8w   Y  W nL t
tjfy\ } zttt| W Y d }~dS d }~ww zt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 %sz7Discovering 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-   r   r   InvalidMetaDataExceptionr   logexcstrr    r   r   vendordata_rawuserdata_raw)r!   
is_upcloudserver_uuidnicmder$   r$   r%   	_get_data>   sR   

	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_idk   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.
        r0   z/Unable to get network meta-data from server....)r   r   r   RuntimeErrorr'   convert_network_config)r!   raw_network_configr$   r$   r%   network_confign   s   z DataSourceUpCloud.network_configN)__name__
__module____qualname__dsnamer9   r   r*   r-   rG   rJ   propertyrN   r$   r$   r$   r%   r      s    -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)rO   rP   rQ   __doc__r9   r$   r$   r$   r%   rT      s    	rT   c                 C   s   t | tS r&   )r   list_from_dependsdatasources)dependsr$   r$   r%   get_datasource_list   s   rY   )logging	cloudinitr   r;   r   r   cloudinit.net.dhcpr   cloudinit.net.ephemeralr   cloudinit.sources.helpersr   r'   	getLoggerrO   r7   r   r   r   r   r   r   rT   DEP_FILESYSTEMDEP_NETWORKrW   rY   r$   r$   r$   r%   <module>   s"   
k
