
    x[h                        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 j                  e      ZddiZd	Zd
Zd
Z G d dej(                        Z G d de      Zeej.                  ffeej.                  ej0                  ffgZd Zy)    N)net)sourcesutil)NoDHCPLeaseError)EphemeralDHCPv4)upcloudmetadata_urlz'http://169.254.169.254/metadata/v1.json      c                   B    e Zd ZdZdZd Zd Zd Zd Zd Z	e
d        Zy	)
DataSourceUpCloudUpCloudFc                    t         j                  j                  | |||       || _        t	               | _        t        j                  t        j                  |ddgi       t        g      | _
        | j                  d   | _        | j                  j                  dt              | _        | j                  j                  dt              | _        | j                  j                  dt"              | _        d | _        d | _        y )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   pathss       E/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceUpCloud.pyr   zDataSourceUpCloud.__init__"   s    ##D'65A(($$W|Y.GL!
 !%N ;{{y*={{y*=++//,F#!    c                 *    t        j                         S N)	uc_helperread_sysinfor$   s    r'   _get_sysinfozDataSourceUpCloud._get_sysinfo3   s    %%''r(   c                     t        j                  | j                  | j                  | j                  | j
                        S )N)r   sec_betweenr   )r+   read_metadatar   r   r   r   r-   s    r'   _read_metadataz DataSourceUpCloud._read_metadata6   s3    &&!!LLLL	
 	
r(   c                    | j                         \  }}|syt        j                  d|       | j                  rp	 t        j	                  d       t        j                         }t        j	                  d|       t        | j                  |      5  | j                         }d d d        n&	 t        j	                  d       | j                         }| _        |j#                  d|      | j$                  d<   |j#                  d	      | j$                  d
<   |j#                  d      | j$                  d<   |j#                  d      | j$                  d<   |j#                  dd      | j$                  d<   |j#                  dd       | _        |j#                  dd       | _        y# 1 sw Y   xY w# t        t        j                  f$ r-}t        j                  t        t        |             Y d }~yd }~ww xY w# t        j                  $ rC}t        j                  t        t        |             t        j                  d|       Y d }~yd }~ww xY w)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   r2   r   r   InvalidMetaDataExceptionr   logexcstrr#   r   r   vendordata_rawuserdata_raw)r$   
is_upcloudserver_uuidnicmdes         r'   	_get_datazDataSourceUpCloud._get_data>   s   $($5$5$7![ 5{C""		23002		FL$T[[#6 /,,.B/		M ((*  ')vvm['Im$*,&&*<&'#%66)#4i ')vvm'<m$-/VVHi-H)* ff]D9FF;5;/ /$g&F&FG CQ( 33 CQ(6
 sH   AF) F F) *%G2 F&"F) )G/#G**G/2I9IIc                 H    t        j                  | j                               S r*   )r   instance_id_matches_system_uuidget_instance_id)r$   r%   s     r'   check_instance_idz#DataSourceUpCloud.check_instance_idk   s    66t7K7K7MNNr(   c                     | j                   r| j                   S | j                  j                  d      }|st        d      t	        j
                  |      | _         | 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.
        r6   z/Unable to get network meta-data from server....)r"   r   r   RuntimeErrorr+   convert_network_config)r$   raw_network_configs     r'   network_configz DataSourceUpCloud.network_confign   sg     '''!]]..y9!A   )?? 
 ###r(   N)__name__
__module____qualname__dsnamer?   r   r.   r2   rM   rQ   propertyrV    r(   r'   r   r      s>    F ""(
+ZO $ $r(   r   c                       e Zd ZdZdZy)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)rW   rX   rY   __doc__r?   r\   r(   r'   r^   r^      s     r(   r^   c                 6    t        j                  | t              S r*   )r   list_from_dependsdatasources)dependss    r'   get_datasource_listrd      s    $$Wk::r(   )logging	cloudinitr   rA   r   r   cloudinit.net.dhcpr   cloudinit.net.ephemeralr   cloudinit.sources.helpersr   r+   	getLoggerrW   r=   r   r   r    r!   r   r   r^   DEP_FILESYSTEMDEP_NETWORKrb   rd   r\   r(   r'   <module>rm      s     % # / 3 :g!#%NO  

h$** h$V
. 
 g4467//1D1DEF;r(   