o
    th                     @   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