
"T`                 @   s   d  d l  m Z d  d l Z d  d l m Z m Z d  d l m Z d  d l m Z	 d  d l m
 Z
 e	 j e  Z Gd d   d e
 j  Z e Z e e
 j f f g Z d	 d
   Z d S)    )	b64decodeN)CepkoSERIAL_PORT)dmi)log)sourcesc               @   sy   e  Z d  Z d Z d Z d d   Z d d   Z d d   Z d	 d
   Z d d d d d  Z	 d d   Z
 d d   Z d S)DataSourceCloudSigmaz
    Uses cepko in order to gather the server context from the VM.

    For more information about CloudSigma's Server Context:
    http://cloudsigma-docs.readthedocs.org/en/latest/server_context.html
    Z
CloudSigmac             C   s2   t    |  _ d |  _ t j j |  | | |  d  S)N )r   cepkossh_public_keyr   
DataSource__init__)selfZsys_cfgZdistropaths r   H/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudSigma.pyr      s    	zDataSourceCloudSigma.__init__c             C   sS   t  j d  t j d  } | s3 t  j d  d St  j d |  d | j   k S)z
        Uses dmi data to detect if this instance of cloud-init is running
        in the CloudSigma's infrastructure.
        z0determining hypervisor product name via dmi datazsystem-product-namez-system-product-name not available in dmi dataFzdetected hypervisor as %sZ
cloudsigma)LOGdebugr   Zread_dmi_datalower)r   Zsys_product_namer   r   r   is_running_in_cloudsigma"   s    z-DataSourceCloudSigma.is_running_in_cloudsigmac             C   s  d } |  j    s d Sy  |  j j   j } | d } Wn# t k
 r[ t j d  d SYn X|  j | j d  g  |  _	 | t
 j k r d S| j d d  j d  } | j d	 d  |  _ d	 | k r t |  j  |  _ d
 | j d i   k r| d d
 |  _ | |  _ | d |  _ d S)zj
        Metadata is the whole server context and /meta/cloud-config is used
        as userdata.
        NFmetaz+CloudSigma: Unable to read from serial portzcloudinit-dsmodebase64_fieldsr	   ,zcloudinit-user-data	cloudinitZvendor_datar   T)r   r
   allresult	Exceptionr   r   Z_determine_dsmodegetdsmoder   ZDSMODE_DISABLEDsplitZuserdata_rawr   Zvendordata_rawmetadatar   )r   r   Zserver_contextZserver_metar   r   r   r   	_get_data0   s,    		zDataSourceCloudSigma._get_datac             C   s   d t  S)z/Return the subplatform metadata source details.z
cepko (%s))r   )r   r   r   r   _get_subplatformS   s    z%DataSourceCloudSigma._get_subplatformFc             C   sJ   t  j d |  j d  r. |  j d d d  S|  j d j d  d Sd S)z
        Cleans up and uses the server's name if the latter is set. Otherwise
        the first part from uuid is being used.
        z^[A-Za-z0-9 -_\.]+$nameN=   uuid-r   )rematchr    r   )r   ZfqdnZ
resolve_ipZmetadata_onlyr   r   r   get_hostnameW   s    z!DataSourceCloudSigma.get_hostnamec             C   s
   |  j  g S)N)r   )r   r   r   r   get_public_ssh_keysa   s    z(DataSourceCloudSigma.get_public_ssh_keysc             C   s   |  j  d S)Nr%   )r    )r   r   r   r   get_instance_idd   s    z$DataSourceCloudSigma.get_instance_idN)__name__
__module____qualname____doc__Zdsnamer   r   r!   r"   r)   r*   r+   r   r   r   r   r      s   #
r   c             C   s   t  j |  t  S)zK
    Return a list of data sources that match this set of dependencies
    )r   Zlist_from_dependsdatasources)Zdependsr   r   r   get_datasource_listr   s    r1   )base64r   r'   Zcloudinit.cs_utilsr   r   r   r   r   Zloggingr   Z	getLoggerr,   r   r   r   ZDataSourceCloudSigmaNetZDEP_FILESYSTEMr0   r1   r   r   r   r   <module>   s   V