
"T`#                 @   sI  d  d l  m Z d  d l  m Z d  d l  m Z d  d l  m Z d  d l  m Z d  d l  m Z d  d l  m	 Z	 e j
 e  Z d Z d	 Z d
 Z d Z d Z d Z Gd d   d e j  Z e e j e j f f g Z d d   Z e e e e e d d  Z e e e e e d d  Z e d k rEd  d l Z e j d d  Z e j d d d d d d e e j d  d d! d d" d e e j d# d d$ d% e d d& d e e j d' d d$ d% e d d( d e e j d) d d* d% e d d+ d e e j    Z! e d, e! j" d- e! j# d. e! j$ d/ e! j% d0 e! j&  Z' e( e	 j) e'   d S)1    )dmi)	ec2_utils)log)sources)helpers)
url_helper)utilzhttp://169.254.169.254z1.0i  
      Exoscalec                   sv   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
 d d   Z   S)DataSourceExoscaler   x   c                s   t  t |   j | | |  t j d  |  j j d t  |  _ |  j j d t	  |  _
 t |  j j d t   |  _ |  j j d t  |  _ |  j j d t  |  _ i  |  _ d  S)Nz$Initializing the Exoscale datasourcemetadata_urlapi_versionpassword_server_porttimeoutretries)superr   __init__LOGdebugZds_cfggetMETADATA_URLr   API_VERSIONr   intPASSWORD_SERVER_PORTr   URL_TIMEOUTurl_timeoutURL_RETRIESurl_retriesextra_config)selfZsys_cfgZdistropaths)	__class__ F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceExoscale.pyr       s    zDataSourceExoscale.__init__c             C   sN   d } |  j  j d  } t j |  } | j d | d  rJ t j d  d S)z<Adjust set-passwords module to run 'always' during each bootZset_passwordsZsemZconfig_Nz5Overriding module set-passwords with frequency always)r"   Zget_ipath_curr   ZFileSemaphoresclearr   r   )r!   ZcfgZis_new_instancemodZsem_pathZ
sem_helperr$   r$   r%   activate,   s
    	zDataSourceExoscale.activatec          	   C   sX   d j  |  j |  j  } t j d | g d |  j d |  j d t j  \ } } t	 |  S)z.Wait for the metadata service to be reachable.z{}/{}/meta-data/instance-idZurlsZmax_waitr   Z	status_cb)
formatr   r   r   Zwait_for_urlurl_max_waitr   r   Zcriticalbool)r!   r   ZurlZ	_responser$   r$   r%   wait_for_metadata_service;   s    				z,DataSourceExoscale.wait_for_metadata_servicec             C   sS   t  j d t j d d d |  j  } | s. i  St |  j |  j |  j |  j	 |  j
  S)zw
        Crawl the metadata service when available.

        @returns: Dictionary of crawled metadata content.
        logfuncmsgz waiting for the metadata servicefunc)r   log_timer   infor,   read_metadatar   r   r   r   r   )r!   Zmetadata_readyr$   r$   r%   crawl_metadataI   s    		z!DataSourceExoscale.crawl_metadatac             C   s   |  j    s d St j d t j d d d |  j  } | s> d S| d |  _ | d |  _ | j d  } i  } | r d	 d
 d | d d d i i } t j	 |  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.
        Fr-   r.   zCrawl of metadata servicer/   z	user-dataz	meta-datapasswordZ
ssh_pwauthTZchpasswdZexpire)_is_platform_viabler   r0   r   r   r3   Zuserdata_rawZmetadatar   Zmergemanydictr    )r!   datar4   Z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  j d  j t  S)Nzsystem-product-name)r   Zread_dmi_data
startswithEXOSCALE_DMI_NAME)r!   r$   r$   r%   r5      s    z&DataSourceExoscale._is_platform_viable)__name__
__module____qualname__Zdsnamer*   r   r(   r,   r3   r7   r8   r5   r$   r$   )r#   r%   r      s   ,r   c             C   s   t  j |  t  S)N)r   Zlist_from_dependsdatasources)Zdependsr$   r$   r%   get_datasource_list   s    r?   c          
   C   s   d j  |  | |  } t j | d d d d d i d | d | } | j j d	  } | d k rd d St j | d d d d d i d | 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	{}:{}/{}/Zssl_detailsNZheadersZDomU_RequestZsend_my_passwordr   r   zutf-8 saved_password)r@   rA   )r)   r   Zread_file_or_urlcontentsdecode)r   r   r   r   r   Zpassword_urlZresponser4   r$   r$   r%   get_password   s$    			rD   c              C   s  i  } | | d <yH t  j | |  d | d | | d <t  j | |  d | d | | d <Wn? t k
 r } z t j t d |  |  i  SWYd d } ~ Xn Xy/ t d | d	 |  d
 | d | d |  | d <Wn> t k
 r	} z t j t d |  | |  WYd d } ~ Xn X| S)z8Query the metadata server and return the retrieved data.Z_metadata_api_versionr   r   z	user-dataz	meta-dataz(failed reading from metadata url %s (%s)Nr   r   r   r   r   r4   z2failed to read from password server url %s:%s (%s))ec2Zget_instance_userdataZget_instance_metadata	Exceptionr   Zlogexcr   rD   )r   r   r   r   r   Zcrawled_metadataer$   r$   r%   r2      s8    

 r2   __main__NZdescriptionzQuery Exoscale Metadataz
--endpointmetavarZURLhelpz The url of the metadata service.defaultz	--versionVERSIONz.The version of the metadata endpoint to query.z	--retriesZNUMtypez,The number of retries querying the endpoint.z	--timeoutz.The time in seconds to wait before timing out.z--password-portZPORTz/The port on which the password endpoint listensr   r   r   r   r   )*Z	cloudinitr   r   rE   r   Zloggingr   r   r   r   Z	getLoggerr;   r   r   r   r   r   r   r:   Z
DataSourcer   ZDEP_FILESYSTEMZDEP_NETWORKr>   r?   rD   r2   argparseArgumentParserparseradd_argumentr   
parse_argsargsZendpointversionZpassword_portr   r   r6   printZ
json_dumpsr$   r$   r$   r%   <module>   s   w"				