U
    L¬÷dþ  ã                   @   s´   d dl m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 )é    )Úlog)Ú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 S )NZ
datasourcer   r	   ÚretriesÚtimeoutÚ
wait_retry)r   Ú
DataSourceÚ__init__ÚdistroÚdictÚmetadatar   ZmergemanydictZget_cfg_by_pathÚBUILTIN_DS_CONFIGZds_cfgÚmetadata_addressÚgetÚ
MD_RETRIESr   Ú
MD_TIMEOUTr   ÚMD_WAIT_RETRYr   Ú_network_config)Ú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_helperZread_sysinfo©r   r    r    r!   Ú_get_sysinfo1   s    zDataSourceUpCloud._get_sysinfoc                 C   s   t j| j| j| j| jdS )N)r   Zsec_betweenr   )r#   Zread_metadatar   r   r   r   r$   r    r    r!   Ú_read_metadata4   s    üz DataSourceUpCloud._read_metadatac              
   C   sœ  |   ¡ \}}|sdS t d|¡ | jr¶zNt d¡ t ¡ }t d|¡ t| j|ƒ t	j
tjd| jd}W 5 Q R X W n> ttjfk
r² } zt	 tt|ƒ¡ W Y ¢dS d }~X Y nX nlz"t d¡ t	j
tjd| jd}W nH tjk
r  } z&t	 tt|ƒ¡ t d|¡ W Y ¢dS d }~X Y nX || _| 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)ZlogfuncÚmsgÚfuncz7Discovering metadata without DHCP-configured networkingzENo DHCP-enabled interfaces available, unable to fetch metadata for %sZinstance_idzinstance-idZhostnamezlocal-hostnameÚnetworkZpublic_keyszpublic-keysZregionÚdefaultZavailability_zoneZvendor_dataZ	user_dataT)r%   ÚLOGÚinfoÚperform_dhcp_setupÚdebugÚcloudnetZfind_fallback_nicr   r   r   Zlog_timer&   r   r   ZInvalidMetaDataExceptionZlogexcÚstrZmetadata_fullr   r   Zvendordata_rawZuserdata_raw)r   Z
is_upcloudZserver_uuidZnicZmdÚer    r    r!   Ú	_get_data<   sV    
ýÿý
ýzDataSourceUpCloud._get_datac                 C   s   t  |  ¡ ¡S r"   )r   Zinstance_id_matches_system_uuidZget_instance_id)r   r   r    r    r!   Úcheck_instance_idq   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.
        r)   z/Unable to get network meta-data from server....)r   r   r   ÚRuntimeErrorr#   Zconvert_network_config)r   Zraw_network_configr    r    r!   Únetwork_configt   s    ÿÿz DataSourceUpCloud.network_configN)Ú__name__Ú
__module__Ú__qualname__Zdsnamer-   r   r%   r&   r2   r3   Úpropertyr5   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)r6   r7   r8   Ú__doc__r-   r    r    r    r!   r:   Œ   s   	r:   c                 C   s   t  | t¡S r"   )r   Zlist_from_dependsÚdatasources)Zdependsr    r    r!   Úget_datasource_list¡   s    r=   N)Z	cloudinitr   Zloggingr   r/   r   r   Zcloudinit.net.dhcpr   Zcloudinit.net.ephemeralr   Zcloudinit.sources.helpersr   r#   Z	getLoggerr6   r+   r   r   r   r   r   r   r:   ZDEP_FILESYSTEMZDEP_NETWORKr<   r=   r    r    r    r!   Ú<module>   s    
r
þ