o
    f(#                     @   s`  d dl Z d dlmZmZmZmZmZmZ d dlm	Z	 e 
eZdZdZdZdZdZd	ZG d
d dejZeejejffgZdd ZeeeeefddZeeeeefddZedkrd dlZejddZejddded ejddded ejddeded ejd ded!ed ejd"d#ed$ed e  Z!ee!j"e!j#e!j$e!j%e!j&d%Z'e(e)e' dS dS )&    N)atomic_helperdmihelperssources
url_helperutil)ec2zhttp://169.254.169.254z1.0i  
      Exoscalec                       sX   e Zd ZdZdZ fddZdd Zdd Zd	d
 Zdd Z	dd Z
edd Z  ZS )DataSourceExoscaler   x   c                    s|   t t| ||| td | jdt| _| jdt	| _
t| jdt| _| jdt| _| jdt| _i | _d S )Nz$Initializing the Exoscale datasourcemetadata_urlapi_versionpassword_server_porttimeoutretries)superr   __init__LOGdebugds_cfggetMETADATA_URLr   API_VERSIONr   intPASSWORD_SERVER_PORTr   URL_TIMEOUTurl_timeoutURL_RETRIESurl_retriesextra_config)selfsys_cfgdistropaths	__class__ F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceExoscale.pyr      s   

zDataSourceExoscale.__init__c                 C   s<   d}| j d}t|}|d| drtd dS dS )z<Adjust set_passwords module to run 'always' during each bootset_passwordssemconfig_Nz5Overriding module set_passwords with frequency always)r%   get_ipath_curr   FileSemaphoresclearr   r   )r"   cfgis_new_instancemodsem_path
sem_helperr(   r(   r)   activate*   s   	
zDataSourceExoscale.activatec                 C   s6   d | j| j}tj|g| j| jtjd\}}t	|S )z.Wait for the metadata service to be reachable.z{}/{}/meta-data/instance-id)urlsmax_waitr   	status_cb)
formatr   r   r   wait_for_urlurl_max_waitr   r   criticalbool)r"   r   url	_responser(   r(   r)   wait_for_metadata_service9   s   
z,DataSourceExoscale.wait_for_metadata_servicec                 C   s6   t jtjd| jd}|si S t| j| j| j| j	| j
S )zw
        Crawl the metadata service when available.

        @returns: Dictionary of crawled metadata content.
        z waiting for the metadata servicelogfuncmsgfunc)r   log_timer   infor@   read_metadatar   r   r   r   r    )r"   metadata_readyr(   r(   r)   crawl_metadataI   s   z!DataSourceExoscale.crawl_metadatac                 C   sh   t jtjd| jd}|sdS |d | _|d | _|d}i }|r)d|ddid	}t | j	|g| _	dS )
zFetch the user data, the metadata and the VM password
        from the metadata service.

        Please refer to the datasource documentation for details on how the
        metadata server and password server are crawled.
        zCrawl of metadata servicerA   F	user-data	meta-datapasswordTexpire)
ssh_pwauthrL   chpasswd)
r   rE   r   r   rI   userdata_rawmetadatar   mergemanydictr!   )r"   datarL   password_configr(   r(   r)   	_get_data`   s*   


	zDataSourceExoscale._get_datac                 C   s   | j S N)r!   )r"   r(   r(   r)   get_config_obj   s   z!DataSourceExoscale.get_config_objc                   C   s   t dtS )Nzsystem-product-name)r   read_dmi_data
startswithEXOSCALE_DMI_NAMEr(   r(   r(   r)   	ds_detect   s   
zDataSourceExoscale.ds_detect)__name__
__module____qualname__dsnamer;   r   r5   r@   rI   rU   rW   staticmethodr[   __classcell__r(   r(   r&   r)   r      s    +r   c                 C   s   t | tS rV   )r   list_from_dependsdatasources)dependsr(   r(   r)   get_datasource_list   s   re   c                 C   sZ   d | ||}tj|dddi||d}|jd}|dv rdS tj|dddi||d |S )	zObtain the VM's password if set.

    Once fetched the password is marked saved. Future calls to this method may
    return empty string or 'saved_password'.z	{}:{}/{}/NDomU_Requestsend_my_password)ssl_detailsheadersr   r   zutf-8) saved_passwordrk   )r9   r   read_file_or_urlcontentsdecode)r   r   r   r   r    password_urlresponserL   r(   r(   r)   get_password   s*   rq   c              
   C   s   i }||d< zt j|| ||d|d< t j|| ||d|d< W n ty: } zttd| | i W  Y d}~S d}~ww zt|| |||d|d< W |S  tye } zttd	| || W Y d}~|S d}~ww )
z8Query the metadata server and return the retrieved data._metadata_api_version)r   r   rJ   rK   z(failed reading from metadata url %s (%s)N)r   r   r   r    r   rL   z2failed to read from password server url %s:%s (%s))r   get_instance_userdataget_instance_metadata	Exceptionr   logexcr   rq   )r   r   r   r   r    crawled_metadataer(   r(   r)   rG      sH   
rG   __main__zQuery Exoscale Metadata)descriptionz
--endpointURLz The url of the metadata service.)metavarhelpdefaultz	--versionVERSIONz.The version of the metadata endpoint to query.z	--retriesNUMz,The number of retries querying the endpoint.)r|   typer}   r~   z	--timeoutz.The time in seconds to wait before timing out.z--password-portPORTz/The port on which the password endpoint listens)r   r   r   r   r    )*logging	cloudinitr   r   r   r   r   r   cloudinit.sources.helpersr   	getLoggerr\   r   r   r   r   r   r   rZ   
DataSourcer   DEP_FILESYSTEMDEP_NETWORKrc   re   rq   rG   argparseArgumentParserparseradd_argumentr   
parse_argsargsendpointversionpassword_portr   r   rS   print
json_dumpsr(   r(   r(   r)   <module>   s    
 
'
+