o
    ¦®Øf|9  ã                   @   s’  d Z ddlZddlZddlZddlmZ ddlmZmZmZ ddl	Z	ddl
mZmZmZ ddlmZ ddlmZ ddlmZ e e¡Zd	Zd
ZdZdZdefdd„Zdee fdd„Zdefdd„Zdefdd„Zdedee fdd„Z dee fdd„Z!G dd„ dƒZ"dee dede#fdd„Z$dedeee" ee" f fd d!„Z%d"ee" d#ee" dee fd$d%„Z&G d&d'„ d'ej'ƒZ(e(ej)ffgZ*d(d)„ Z+dS )*z?Datasource to support the Windows Subsystem for Linux platform.é    N)ÚPurePath)ÚListÚOptionalÚTuple)ÚsourcesÚsubpÚutil)ÚDistro)Útype_from_starts_with)ÚPathsz/usr/bin/wslpathziid-datasource-wslú%s.user-dataz
agent.yamlÚreturnc                  C   s"   t   tddg¡\} }t|  ¡ ƒjS )zL
    Returns the name of the current WSL instance as seen from outside.
    z-amú/)r   ÚWSLPATH_CMDr   ÚrstripÚname)Úroot_net_pathÚ_© r   úA/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceWSL.pyÚinstance_name   s   	r   c                  C   sH   d} d}g }t  ¡  ¡ D ]}|d | kr!||d v r!| |d ¡ q|S )z”
    Return a list of mount points of the Windows drives inside the current
    WSL instance, if drives are mounted, or an empty list otherwise
    Ú9pzaname=drvfsÚfstypeÚoptsÚ
mountpoint)r   ÚmountsÚvaluesÚappend)ÚFS_TYPEÚOPTIONS_CONTAINÚmountedÚmntr   r   r   Úmounted_win_drives+   s   €r"   c                  C   s`   t ƒ } | s	tdƒ‚d}| D ]}|| }t |tj¡sqt d|¡ t|ƒ  S tdd | ¡ ƒ‚)z?
    Returns the Linux path to the Windows host's cmd.exe.
    zWindows drives are not mounted.z%s/Windows/System32/cmd.exezFound cmd.exe at <%s>z,Couldn't find cmd.exe in any mount point: %sz, )	r"   ÚIOErrorÚosÚaccessÚX_OKÚLOGÚdebugr   Újoin)r   Ú	candidater!   Úcmdr   r   r   Úcmd_executable;   s   ÿr,   c                  C   sV   t ƒ } t d|  ¡ ddg¡\}}| ¡ }|st d¡‚t td|g¡\}}t| ¡ ƒS )zó
    Finds the user's home directory path as a WSL path.

    raises: IOError when no mountpoint with cmd.exe is found
               ProcessExecutionError when either cmd.exe is unable to retrieve
               the user's home directory
    z/initz/Czecho %USERPROFILE%z4No output from cmd.exe to show the user profile dir.z-au)r,   r   Úas_posixr   ÚProcessExecutionErrorr   r   )r+   Úhomer   Úoutr   r   r   Ú	find_homeS   s   ÿ	r1   Ú	user_homec                 C   s2   t j | d¡}t j |¡st d|¡ dS t|ƒS )z¥
    Returns the Windows user profile .cloud-init directory translated as a
    Linux path accessible inside the current WSL instance, or None if not
    found.
    z.cloud-initz*cloud-init user data dir %s doesn't exist.N)r$   Úpathr)   Úisdirr'   r(   r   )r2   Úseed_dirr   r   r   Úcloud_init_data_dirq   s
   r6   c                 C   s6   t  ¡ \}}}|r|n|}d|  d||f d| dgS )z
    Return a list of candidate file names that may contain user-data
    in some supported format, ordered by precedence.
    r   z%s-%s.user-dataz%s-all.user-datazdefault.user-data)r   Úget_linux_distro)r   Údistribution_idÚ
version_idÚversion_codenameÚversionr   r   r   Úcandidate_user_data_file_names   s   
ør<   c                   @   s,   e Zd ZdZdefdd„Zdefdd„ZdS )	Ú
ConfigDataz~Models a piece of configuration data as a dict if possible, while
    retaining its raw representation alongside its file pathr3   c                 C   s<   t  |¡| _|| _d | _dt| jƒkrt  | j¡| _d S d S )Nztext/cloud-config)r   Úload_text_fileÚrawr3   Úconfig_dictr
   Ú	load_yaml)Úselfr3   r   r   r   Ú__init__—   s   ÿzConfigData.__init__r   c                 C   s
   | j d uS ©N)r@   )rB   r   r   r   Úis_cloud_config    s   
zConfigData.is_cloud_configN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rC   ÚboolrE   r   r   r   r   r=   “   s    	r=   Úcloudinitdirc                 C   s   dt i}| du r
|S tj |  ¡ d| ¡}z
t t |¡¡}W n ty.   t	 
d|¡ Y nw |r5d|vrFd|› d|› }t	 |¡ t|ƒ‚|S )z`
    Returns the relevant metadata loaded from cloudinit dir based on the
    instance name
    úinstance-idNz%s.meta-dataz<No instance metadata found at %s. Using default instance-id.z Metadata at z5 does not contain instance-id key. Instead received: )ÚDEFAULT_INSTANCE_IDr$   r3   r)   r-   r   rA   r>   ÚFileNotFoundErrorr'   r(   ÚerrorÚ
ValueError)rK   r   ÚmetadataÚmetadata_pathÚmsgr   r   r   Úload_instance_metadata¤   s,   ÿþÿÿÿ
rT   c                 C   s’   t j | d¡}t j |¡sdS tt j |ttƒ  ¡ƒ}d}t j |¡r0t 	d|t
| ƒ¡ t|ƒ}tt j |t¡ƒ}d}t j |¡rEt|ƒ}||fS )zg
    Read .ubuntupro user-data if present and return a tuple of agent and
    landscape user-data.
    z.ubuntupro/.cloud-init)NNNzYLandscape configuration found: %s. Organization policy ignores any local user-data in %s.)r$   r3   r)   r4   r   ÚLANDSCAPE_DATA_FILEr   Úisfiler'   r(   r6   r=   ÚAGENT_DATA_FILE)r2   Úpro_dirÚlandscape_pathÚlandscape_dataÚ
agent_pathÚ
agent_datar   r   r   Úload_ubuntu_pro_dataÅ   s&   ÿür]   r\   Ú	user_datac                 C   s|  | du st | jƒdkr|du st |jƒdkrdS |jS |du s&t |jƒdkr6| du s1t | jƒdkr3dS | jS t|  ¡ | ¡ gƒsQt d¡ d| j ¡ |j ¡ f S i }d}g }t|j	t
ƒrn|j	}| di ¡ di ¡ dd¡}t| j	t
ƒrµ|r{t d	¡ | j	}|D ]}||v r‹| |¡ || ||< q€|rœt d
 |¡¡ |rµ| di ¡ d¡rµt d|¡ ||d d d< dt |¡ ¡  S )a  Merge agent.yaml data provided by Ubuntu Pro for WSL
    and user data provided either by Landscape or the local user,
    according to the UP4W specific rules.

    When merging is not possible, provide #include directive to allow
    cloud-init to merge separate parts.
    Nr   zaUnable to merge {agent_data.path} and {user_data.path}. Providing as separate user-data #include.z#include
%s
%s
Ú Ú	landscapeÚclientÚtagsz.Merging both user_data and agent.yaml configs.z% agent.yaml overrides config keys: , zFLandscape client conf updated with user-data landscape.client.tags: %sz@#cloud-config
# WSL datasouce Merged agent.yaml and user_data
%s)Úlenr?   ÚallrE   r'   r(   r3   r-   Ú
isinstancer@   ÚdictÚgetr   r)   ÚyamlÚdumpÚstrip)r\   r^   ÚmergedÚ	user_tagsÚoverridden_keysÚagentÚkeyr   r   r   Úmerge_agent_landscape_dataå   s\   ÿþ	ÿ

ýýÿÿrp   c                       sZ   e Zd ZdZddedef‡ fdd„Zdedefd	d
„Zde	fdd„Z
de	fdd„Z‡  ZS )ÚDataSourceWSLÚWSLNÚdistroÚpathsc                    s   t ƒ  ||||¡ d| _d S )Nr_   )ÚsuperrC   r   )rB   Úsys_cfgrs   rt   Úud_proc©Ú	__class__r   r   rC   =  s   
zDataSourceWSL.__init__r5   r   c                 C   sj   dd„ t  |¡D ƒ}|std| ƒ‚dd„ t| jƒD ƒ}|D ]}|| ¡ v r.t|| ƒ  S qtd| ƒ‚)z
        Finds the most precendent of the candidate files that may contain
        user-data, if any, or None otherwise.
        c                 S   s   i | ]	}|j  ¡ |j“qS r   )r   Úcasefoldr3   )Ú.0Úefr   r   r   Ú
<dictcomp>M  s    ÿz5DataSourceWSL.find_user_data_file.<locals>.<dictcomp>z%s directory is emptyc                 S   s   g | ]}|  ¡ ‘qS r   )rz   )r{   Úfr   r   r   Ú
<listcomp>S  s    ÿÿz5DataSourceWSL.find_user_data_file.<locals>.<listcomp>z6%s doesn't contain any of the expected user-data files)r$   Úscandirr#   r<   r   Úkeysr   )rB   r5   Úexisting_filesÚfolded_namesÚfilenamer   r   r   Úfind_user_data_fileA  s   ÿþÿÿz!DataSourceWSL.find_user_data_filec              
   C   sr   |   ¡ }|sdS zttƒ ƒ}t|tƒ ƒ}|| d¡kW S  ttfy8 } zt 	dt
|ƒ¡ W Y d }~dS d }~ww )NFrL   z2Unable to check_instance_id from metadata file: %s)Úget_instance_idr6   r1   rT   r   rg   r#   rP   r'   ÚwarningÚstr)rB   rv   ÚcurrentÚdata_dirrQ   Úerrr   r   r   Úcheck_instance_id_  s   
þ€ûzDataSourceWSL.check_instance_idc              
   C   sb  t  t¡st dt¡ dS tƒ | _ztƒ }W n ty/ } zt d|¡ W Y d }~dS d }~ww t|ƒ}d }d }z	t	|| jƒ| _
W n ttfy^ } zt dt|ƒ¡ W Y d }~dS d }~ww | jjdkrkt|ƒ\}}z|d u r{|d ur{t|  |¡ƒ}W n$ ttfy  } z|r‹tjntj}|d|t|ƒƒ W Y d }~nd }~ww t||gƒs©dS t||ƒ| _dS )Nz5No WSL command %s found. Cannot detect WSL datasourceFz#Unable to detect WSL datasource: %szUnable to load metadata: %sÚubuntuz+Unable to load any user-data file in %s: %sT)r   Úwhichr   r'   r(   r   r1   r#   r6   rT   rQ   rP   rO   rˆ   rs   r   r]   r=   r…   ÚinfoÚanyrp   Úuserdata_raw)rB   r2   Úer5   r\   r^   r‹   Úlogr   r   r   Ú	_get_datar  sZ   
þ
€þ
ÿ€þ€ý€þ	zDataSourceWSL._get_datarD   )rF   rG   rH   Údsnamer	   r   rC   r   r…   rJ   rŒ   r”   Ú__classcell__r   r   rx   r   rq   :  s    rq   c                 C   s   t  | t¡S rD   )r   Úlist_from_dependsÚdatasources)Údependsr   r   r   Úget_datasource_list®  s   rš   ),rI   Úloggingr$   ÚtypingÚpathlibr   r   r   r   rh   Ú	cloudinitr   r   r   Úcloudinit.distrosr	   Úcloudinit.handlersr
   Úcloudinit.helpersr   Ú	getLoggerrF   r'   r   rM   rU   rW   rˆ   r   r"   r,   r1   r6   r<   r=   rf   rT   r]   rp   Ú
DataSourcerq   ÚDEP_FILESYSTEMr˜   rš   r   r   r   r   Ú<module>   s\   
ÿÿ
þ!ÿ
þ ÿÿ
þU
oÿ