o
    x[h¿  ã                   @   s˜   d dl Z d dlZd dlmZ d dlmZmZmZmZ e 	e
¡ZdgZdZdZG dd„ dejƒZd	efd
d„Zdefdd„ZeejejffgZdd„ ZdS )é    N)ÚOptional)ÚdmiÚsourcesÚ
url_helperÚutilzhttp://169.254.169.254é   ÚCloudCIXc                       sl   e Zd ZdZdZdZdZ‡ fdd„Zdd„ Zde	fd	d
„Z
dee fdd„Zedd„ ƒZedd„ ƒZ‡  ZS )ÚDataSourceCloudCIXr   é   é   c                    s$   t t| ƒ |||¡ d | _d | _d S ©N)Úsuperr	   Ú__init__Ú_metadata_urlÚ_net_cfg)ÚselfÚsys_cfgÚdistroÚpaths©Ú	__class__© úF/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudCIX.pyr      s   
zDataSourceCloudCIX.__init__c              
   C   s`   z|   ¡ }W n tjy  } zt d|¡ W Y d}~dS d}~ww |d | _t |d ¡| _dS )z6
        Fetch the user data and the metadata
        z0Failed to read data from CloudCIX datasource: %sNFú	meta-dataú	user-dataT)	Úcrawl_metadata_servicer   ÚInvalidMetaDataExceptionÚLOGÚerrorÚmetadatar   Údecode_binaryÚuserdata_raw)r   Úcrawled_datar   r   r   r   Ú	_get_data   s   ÿ€ü
zDataSourceCloudCIX._get_dataÚreturnc                 C   s,   |   ¡ }|d u rt d¡‚t||  ¡ ƒ}|S )Nz Could not determine metadata URL)Údetermine_md_urlr   r   Úread_metadataÚget_url_params)r   Úmd_urlÚdatar   r   r   r   /   s   ÿz)DataSourceCloudCIX.crawl_metadata_servicec                 C   sÚ   | j r| j S |  ¡ }tjt|j|jd�\}}|sd S ttddƒD ]I}t 	|d 
|¡d¡}z
tj|| jd�}W n tjyP } zt d||¡ W Y d }~q d }~ww | ¡ rct 	|d 
|¡¡| _  | j S t d|¡ q | j S )	N)Úmax_waitÚtimeoutr   éÿÿÿÿzv{0}r   )r+   zURL %s raised exception %szNo metadata found at URL %s)r   r'   r   Úwait_for_urlÚMETADATA_URLSÚmax_wait_secondsÚtimeout_secondsÚrangeÚMETADATA_VERSIONÚcombine_urlÚformatÚreadurlÚurl_timeoutÚUrlErrorr   ÚdebugÚok)r   Ú
url_paramsÚbase_urlÚ_ÚversionÚurlÚresponseÚer   r   r   r%   9   s:   
ýÿ€þ
ÿþz#DataSourceCloudCIX.determine_md_urlc                   C   s   t ƒ S r   )Úis_platform_viabler   r   r   r   Ú	ds_detect\   s   zDataSourceCloudCIX.ds_detectc                 C   s(   | j r| j S | jsd S | jd | _ | j S )NÚnetwork)r   r   )r   r   r   r   Únetwork_config`   s   z!DataSourceCloudCIX.network_config)Ú__name__Ú
__module__Ú__qualname__ÚdsnameÚurl_retriesÚurl_timeout_secondsÚurl_sec_between_retriesr   r#   Údictr   r   Ústrr%   ÚstaticmethodrB   ÚpropertyrD   Ú__classcell__r   r   r   r   r	      s    
#
r	   r$   c                   C   s   t  d¡tkS )Nzsystem-product-name)r   Úread_dmi_dataÚCLOUDCIX_DMI_NAMEr   r   r   r   rA   k   s   rA   r;   c           
      C   s
  i }ddt jfddt jff}|D ]r\}}}ztjt | |¡|j|j|jd�}W n tj	yE } zt
 d|› d| › d|› d|› �¡‚d	}~ww | ¡ s\t
 d
|› d| › d|› d|j› �¡‚z	||jƒ||< W q tjjy‚ }	 zt
 d| › d|› d|	› �¡|	‚d	}	~	ww |S )a;  
    Read metadata from metadata server at base_url

    :returns: dictionary of retrieved metadata and user data containing the
              following keys: meta-data, user-data
    :param: base_url: meta data server's base URL
    :param: url_params: dictionary of URL retrieval parameters. Valid keys are
            `retries`, `sec_between` and `timeout`.
    :raises: InvalidMetadataException upon network error connecting to metadata
             URL, error response from meta data server or failure to
             decode/parse metadata and userdata payload.
    r   r   Úuserdatar   )r>   ÚretriesÚsec_betweenr+   zFailed to fetch IMDS z: ú/Nz	No valid z found. URL z returned code zInvalid JSON at )r   Ú	load_jsonÚmaybe_b64decoder   r5   r3   Únum_retriesÚsec_between_retriesr0   r7   r   r   r9   ÚcodeÚcontentsÚjsonÚdecoderÚJSONDecodeError)
r;   r:   ÚmdÚleaf_key_format_callbackÚurl_leafÚnew_keyÚformat_callbackr?   r   Úexcr   r   r   r&   o   sZ   

þ

üÿÿÿÿ€ÿÿÿÿÿÿþ€ÿr&   c                 C   s   t  | t¡S r   )r   Úlist_from_dependsÚdatasources)Údependsr   r   r   Úget_datasource_list¦   s   ri   )r]   ÚloggingÚtypingr   Ú	cloudinitr   r   r   r   Ú	getLoggerrE   r   r.   r2   rR   Ú
DataSourcer	   ÚboolrA   rM   r&   ÚDEP_FILESYSTEMÚDEP_NETWORKrg   ri   r   r   r   r   Ú<module>   s   
Z2ÿ