o
    wÚ7e¶  ã                   @   sŽ   d dl 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mZ e e¡ZG dd	„ d	ejƒZeZeejffgZd
d„ ZdS )é    N)Ú	b64decode)Údmi)Úlog)Úsources)ÚDataSourceHostname)ÚSERIAL_PORTÚCepkoc                   @   sR   e Zd ZdZdZdd„ Zedd„ ƒZdd„ Zd	d
„ Z	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
    Ú
CloudSigmac                 C   s$   t ƒ | _d| _tj | |||¡ d S )NÚ )r   ÚcepkoÚssh_public_keyr   Ú
DataSourceÚ__init__)ÚselfÚsys_cfgÚdistroÚpaths© r   úH/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceCloudSigma.pyr      s   zDataSourceCloudSigma.__init__c                  C   s>   t  d¡ t d¡} | st  d¡ dS t  d| ¡ d|  ¡ v 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 %sÚ
cloudsigma)ÚLOGÚdebugr   Úread_dmi_dataÚlower)Úsys_product_namer   r   r   Ú	ds_detect"   s   


zDataSourceCloudSigma.ds_detectc                 C   sÄ   d}z| j  ¡ j}|d }W n ty   t d¡ Y dS w |  | d¡g¡| _|t	j
kr/dS | dd¡ d¡}| d	d¡| _d	|v rIt| jƒ| _d
| di ¡v rX|d d
 | _|| _|d | _dS )zj
        Metadata is the whole server context and /meta/cloud-config is used
        as userdata.
        NÚmetaz+CloudSigma: Unable to read from serial portFzcloudinit-dsmodeÚbase64_fieldsr   ú,zcloudinit-user-dataÚ	cloudinitÚvendor_datar   T)r   ÚallÚresultÚ	Exceptionr   r   Ú_determine_dsmodeÚgetÚdsmoder   ÚDSMODE_DISABLEDÚsplitÚuserdata_rawr   Úvendordata_rawÚmetadatar   )r   r'   Úserver_contextÚserver_metar   r   r   r   Ú	_get_data1   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   sD   t  d| jd ¡r| jd dd… }n
| jd  d¡d }t|dƒ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   F)ÚreÚmatchr,   r)   r   )r   ÚfqdnÚ
resolve_ipÚmetadata_onlyÚretr   r   r   Úget_hostnameW   s   
z!DataSourceCloudSigma.get_hostnamec                 C   s   | j gS )N)r   r0   r   r   r   Úget_public_ssh_keysb   s   z(DataSourceCloudSigma.get_public_ssh_keysc                 C   s
   | j d S )Nr4   )r,   r0   r   r   r   Úget_instance_ide   s   
z$DataSourceCloudSigma.get_instance_idN)FFF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údsnamer   Ústaticmethodr   r/   r1   r<   r=   r>   r   r   r   r   r	      s    
"
r	   c                 C   s   t  | t¡S )zK
    Return a list of data sources that match this set of dependencies
    )r   Úlist_from_dependsÚdatasources)Údependsr   r   r   Úget_datasource_lists   s   rH   )r6   Úbase64r   r    r   r   Úloggingr   Úcloudinit.sourcesr   Ú$cloudinit.sources.helpers.cloudsigmar   r   Ú	getLoggerr?   r   r   r	   ÚDataSourceCloudSigmaNetÚDEP_FILESYSTEMrF   rH   r   r   r   r   Ú<module>   s   
W
ÿ