name : DataSourceWSL.cpython-39.pyc
a

'�Dg|9�@s�dZddlZddlZddlZddlmZddlmZmZmZddl	Z	ddl
mZmZm
Z
ddlmZddlmZddlmZe�e�Zd	Zd
ZdZdZed
�dd�Zeed
�dd�Zed
�dd�Zed
�dd�Zeeed�dd�Z eed
�dd�Z!Gdd�d�Z"eeee#d�dd�Z$eeee"ee"fd�d d!�Z%ee"ee"eed"�d#d$�Z&Gd%d&�d&ej'�Z(e(ej)ffgZ*d'd(�Z+dS))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/wslpathziid-datasource-wsl�%s.user-dataz
agent.yaml��returncCs"t�tddg�\}}t|���jS)zL
    Returns the name of the current WSL instance as seen from outside.
    z-am�/)r�WSLPATH_CMDr�rstrip�name)Z
root_net_path�_�r�C/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceWSL.py�
instance_names	rcCsHd}d}g}t����D]*}|d|kr||dvr|�|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
    Z9pzaname=drvfsZfstypeZoptsZ
mountpoint)r�mounts�values�append)ZFS_TYPEZOPTIONS_CONTAINZmounted�mntrrr�mounted_win_drives+srcCsdt�}|std��d}|D]2}||}t�|tj�s6qt�d|�t|�Stdd�|���dS)z?
    Returns the Linux path to the Windows host's cmd.exe.
    zWindows drives are not mounted.z%s/Windows/System32/cmd.exezFound cmd.exe at <%s>z,Couldn't find cmd.exe in any mount point: %sz, N)	r�IOError�os�access�X_OK�LOG�debugr�join)r�	candidater�cmdrrr�cmd_executable;s�r%cCsVt�}t�d|��ddg�\}}|��}|s6t�d��t�td|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/Czecho %USERPROFILE%z4No output from cmd.exe to show the user profile dir.z-au)r%r�as_posixrZProcessExecutionErrorrr)r$�homer�outrrr�	find_homeSs�	r))�	user_homercCs2tj�|d�}tj�|�s*t�d|�dSt|�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)r*�seed_dirrrr�cloud_init_data_dirqs
r.cCs6t��\}}}|r|n|}d|d||fd|dgS)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-datazdefault.user-data)rZget_linux_distro)rZdistribution_idZ
version_idZversion_codename�versionrrr�candidate_user_data_file_namess
�r0c@s,eZdZdZed�dd�Zed�dd�ZdS)	�
ConfigDataz~Models a piece of configuration data as a dict if possible, while
    retaining its raw representation alongside its file path)r+cCs8t�|�|_||_d|_dt|j�kr4t�|j�|_dS)Nztext/cloud-config)r�load_text_file�rawr+�config_dictr
�	load_yaml)�selfr+rrr�__init__�s
zConfigData.__init__r
cCs
|jduS�N)r4)r6rrr�is_cloud_config�szConfigData.is_cloud_configN)�__name__�
__module__�__qualname__�__doc__rr7�boolr9rrrrr1�s	r1)�cloudinitdirrrcCs�dti}|dur|Stj�|��d|�}zt�t�|��}Wnty\t	�
d|�Yn0|rjd|vr�d|�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_IDrr+r"r&rr5r2�FileNotFoundErrorr r!�error�
ValueError)r?r�metadataZ
metadata_path�msgrrr�load_instance_metadata�s*��
��
rGcCs�tj�|d�}tj�|�sdSttj�|tt���}d}tj�|�r`t�	d|t
|��t|�}ttj�|t��}d}tj�|�r�t|�}||fS)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.)
rr+r"r,r�LANDSCAPE_DATA_FILEr�isfiler r!r.r1�AGENT_DATA_FILE)r*Zpro_dirZlandscape_pathZlandscape_dataZ
agent_path�
agent_datarrr�load_ubuntu_pro_data�s&��rL)rK�	user_datarcCs�|dust|j�dkr6|dus,t|j�dkr0dS|jS|dusLt|j�dkrl|dusbt|j�dkrfdS|jSt|��|��g�s�t�d�d|j��|j��fSi}d}g}t|j	t
�r�|j	}|�di��di��dd�}t|j	t
��rv|r�t�d	�|j	}|D]&}||v�r|�|�||||<�q|�r@t�d
�
|��|�rv|�di��d��rvt�d|�||ddd<dt�|���S)
aMerge 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.
    NrzaUnable to merge {agent_data.path} and {user_data.path}. Providing as separate user-data #include.z#include
%s
%s
�Z	landscapeZclient�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)�lenr3�allr9r r!r+r&�
isinstancer4�dict�getrr"�yaml�dump�strip)rKrMZmergedZ	user_tagsZoverridden_keysZagent�keyrrr�merge_agent_landscape_data�s^��	�


�����rYcsVeZdZdZdeed��fdd�
Zeed�dd�Ze	d	�d
d�Z
e	d	�dd
�Z�ZS)�
DataSourceWSLZWSLN)�distro�pathscst��||||�d|_dS)NrN)�superr7r)r6�sys_cfgr[r\Zud_proc��	__class__rrr7=szDataSourceWSL.__init__)r-rcCsndd�t�|�D�}|s$td|��dd�t|j�D�}|D] }||��vr<t||�Sq<td|��dS)z�
        Finds the most precendent of the candidate files that may contain
        user-data, if any, or None otherwise.
        cSsi|]}|j��|j�qSr)r�casefoldr+)�.0Zefrrr�
<dictcomp>Msz5DataSourceWSL.find_user_data_file.<locals>.<dictcomp>z%s directory is emptycSsg|]}|���qSr)ra)rb�frrr�
<listcomp>Ss�z5DataSourceWSL.find_user_data_file.<locals>.<listcomp>z6%s doesn't contain any of the expected user-data filesN)r�scandirrr0r�keysr)r6r-Zexisting_filesZfolded_names�filenamerrr�find_user_data_fileAs���z!DataSourceWSL.find_user_data_filer
c
Csv|��}|sdSz&tt��}t|t��}||�d�kWSttfyp}zt�	dt
|��WYd}~dSd}~00dS)NFr@z2Unable to check_instance_id from metadata file: %s)Zget_instance_idr.r)rGrrTrrDr Zwarning�str)r6r^�currentZdata_dirrE�errrrr�check_instance_id_s
�zDataSourceWSL.check_instance_idc
Cslt�t�st�dt�dSt�|_z
t�}Wn2ty^}zt�d|�WYd}~dSd}~00t|�}d}d}zt	||j�|_
Wn:ttfy�}zt�dt
|��WYd}~dSd}~00|jjdkr�t|�\}}z&|du�r�|du�r�t|�|��}WnLttf�yH}z.|�rtjntj}|d|t
|��WYd}~n
d}~00t||g��s\dSt||�|_dS)Nz5No WSL command %s found. Cannot detect WSL datasourceFz#Unable to detect WSL datasource: %szUnable to load metadata: %sZubuntuz+Unable to load any user-data file in %s: %sT)r�whichrr r!rr)rr.rGrErDrCrjr[rrLr1ri�info�anyrYZuserdata_raw)r6r*�er-rKrMrl�logrrr�	_get_datarsL
�
�
�zDataSourceWSL._get_data)N)
r:r;r<Zdsnamer	rr7rrir>rmrs�
__classcell__rrr_rrZ:s
rZcCst�|t�Sr8)rZlist_from_depends�datasources)Zdependsrrr�get_datasource_list�srv),r=Zloggingr�typing�pathlibrrrrrUZ	cloudinitrrrZcloudinit.distrosr	Zcloudinit.handlersr
Zcloudinit.helpersrZ	getLoggerr:r rrArHrJrjrrr%r)r.r0r1rSrGrLrYZ
DataSourcerZZDEP_FILESYSTEMrurvrrrr�<module>sF

�"�!�Uo
�

© 2025 Cubjrnet7