o
    x[hÉ  ã                   @   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
þ