o
    x[h~"  ã                   @   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r®d 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   s4   |   ¡ }|st d¡ i S t| j| j| j| j| jƒS )zw
        Crawl the metadata service when available.

        @returns: Dictionary of crawled metadata content.
        z,Unable to get response from metadata service)	r@   r   ÚerrorÚread_metadatar   r   r   r   r    )r"   Úmetadata_readyr(   r(   r)   Úcrawl_metadataI   s   
ûz!DataSourceExoscale.crawl_metadatac                 C   s\   |   ¡ }|sdS |d | _|d | _| d¡}i }|r#d|ddidœ}t | j|g¡| _dS )zçFetch 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.
        Fú	user-dataú	meta-dataÚpasswordTÚexpire)Ú
ssh_pwauthrG   Úchpasswd)rD   Úuserdata_rawÚmetadatar   r   Úmergemanydictr!   )r"   ÚdatarG   Ú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@   rD   rP   rR   ÚstaticmethodrV   Ú__classcell__r(   r(   r&   r)   r      s    'r   c                 C   s   t  | t¡S rQ   )r   Úlist_from_dependsÚdatasources)Údependsr(   r(   r)   Úget_datasource_list•   s   r`   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 )	z�Obtain 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_passwordrf   )r9   r   Úread_file_or_urlÚcontentsÚdecode)r   r   r   r   r    Úpassword_urlÚresponserG   r(   r(   r)   Úget_password™   s*   ÿûûrl   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   rE   rF   z(failed reading from metadata url %s (%s)N)r   r   r   r    r   rG   z2failed to read from password server url %s:%s (%s))r   Úget_instance_userdataÚget_instance_metadataÚ	Exceptionr   Úlogexcr   rl   )r   r   r   r   r    Úcrawled_metadataÚer(   r(   r)   rB   ¿   sH   
ÿÿÿ€üû÷û€÷rB   Ú__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.)rw   Útyperx   ry   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   Ú	getLoggerrW   r   r   r   r   r   r   rU   Ú
DataSourcer   ÚDEP_FILESYSTEMÚDEP_NETWORKr^   r`   rl   rB   ÚargparseÚArgumentParserÚparserÚadd_argumentr   Ú
parse_argsÚargsÚendpointÚversionÚpassword_portr   r   rN   ÚprintÚ
json_dumpsr(   r(   r(   r)   Ú<module>   s�    
yÿ
û'
û+üüûûûûÐ