Current File : //usr/lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceWSL.cpython-312.pyc
�

Ӈg|9����dZddlZddlZddlZddlmZddlmZmZmZddl	Z	ddl
mZmZm
Z
ddlmZddlmZddlmZej(e�Zd	Zd
ZdZdZd
efd�Zd
eefd�Zd
efd�Zd
efd�Zded
eefd�Z d
eefd�Z!Gd�d�Z"deeded
e#fd�Z$ded
eee"ee"ffd�Z%dee"dee"d
eefd�Z&Gd�dejN�Z(e(ejRffgZ*d �Z+y)!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�returnc��tjtddg�\}}t|j��jS)zL
    Returns the name of the current WSL instance as seen from outside.
    z-am�/)r�WSLPATH_CMDr�rstrip�name)�
root_net_path�_s  �A/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceWSL.py�
instance_namers8���y�y�+�u�c�!:�;��M�1��M�(�(�*�+�0�0�0�c��d}d}g}tj�j�D]'}|d|k(s�||dvs�|j|d��)|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
    �9pzaname=drvfs�fstype�opts�
mountpoint)r	�mounts�values�append)�FS_TYPE�OPTIONS_CONTAIN�mounted�mnts    r�mounted_win_drivesr%+s`��
�G�#�O��G��{�{�}�#�#�%�.���x�=�G�#��3�v�;�(F��N�N�3�|�,�-�.��Nrc��t�}|std��d}|D]N}||z}tj|tj�s�-t
j
d|�t|�cStddj|�z��)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, )	r%�IOError�os�access�X_OK�LOG�debugr�join)r�	candidater$�cmds    r�cmd_executabler0;s���
 �
!�F���7�8�8�.�I�����#�o���y�y��b�g�g�&���	�	�)�3�/���}��
��6����6�9J�J��rc�(�t�}tjd|j�ddg�\}}|j�}|stjd��tjt
d|g�\}}t
|j��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)r0r�as_posixr�ProcessExecutionErrorrr)r/�homer�outs    r�	find_homer6Ss����
�C��i�i��#�,�,�.�$�8L�M�N�G�D�!��;�;�=�D���(�(�B�
�	
��Y�Y��U�D�1�
2�F�C���C�J�J�L�!�!r�	user_homec���tjj|d�}tjj|�stjd|�yt
|�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)r7�seed_dirs  r�cloud_init_data_dirr<qsE���w�w�|�|�I�}�5�H�
�7�7�=�=��"��	�	�>��I���H��rc�b�tj�\}}}|r|n|}d|z|�d|�d�d|zdgS)z
    Return a list of candidate file names that may contain user-data
    in some supported format, ordered by precedence.
    r
�-z
.user-dataz%s-all.user-datazdefault.user-data)r	�get_linux_distro)r�distribution_id�
version_id�version_codename�versions     r�candidate_user_data_file_namesrDsK��
59�4I�4I�4K�1�O�Z�!1�&�j�,<�G�	��&�,�g�6��_�,��	�	rc�(�eZdZdZdefd�Zdefd�Zy)�
ConfigDataz~Models a piece of configuration data as a dict if possible, while
    retaining its raw representation alongside its file pathr9c���tj|�|_||_d|_dt|j�k(r%tj|j�|_yy)Nztext/cloud-config)r	�load_text_file�rawr9�config_dictr�	load_yaml)�selfr9s  r�__init__zConfigData.__init__�sP���+�+�D�1���"��	�+/����"7����"A�A�#�~�~�d�h�h�7�D��Brrc��|jduS�N)rJ)rLs r�is_cloud_configzConfigData.is_cloud_config�s�����t�+�+rN)�__name__�
__module__�__qualname__�__doc__rrM�boolrP�rrrFrF�s ��@�8�X�8�,��,rrF�cloudinitdirrc�x�dti}|�|Stjj|j	�d|z�}	tjtj|��}|rd|vr(d|�d|��}tj|�t|��|S#t$rtjd|�Y�QwxYw)z`
    Returns the relevant metadata loaded from cloudinit dir based on the
    instance name
    �instance-idz%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(r9r-r2r	rKrH�FileNotFoundErrorr+r,�error�
ValueError)rWr�metadata�
metadata_path�msgs     r�load_instance_metadatara�s����2�3�H������G�G�L�L������-�!?��M�
��>�>�$�"5�"5�m�"D�E���}�H�4��M�?�+"�"*��
-�	�	�	�	�#����o���O���
��	�	�J��	
�
�s�(B�B9�8B9c�D�tjj|d�}tjj|�syt	tjj|t
t
�z��}d}tjj|�r+tjd|t|��t|�}t	tjj|t��}d}tjj|�rt|�}||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.)
r(r9r-r:r�LANDSCAPE_DATA_FILEr�isfiler+r,r<rF�AGENT_DATA_FILE)r7�pro_dir�landscape_path�landscape_data�
agent_path�
agent_datas      r�load_ubuntu_pro_datark�s����g�g�l�l�9�&>�?�G�
�7�7�=�=��!���
�����W�1�M�O�C�D��N��N�	�w�w�~�~�n�%��	�	�
1���	�*�		
�$�N�3���"�'�'�,�,�w��@�A�J��J�	�w�w�~�~�j�!��
�+�
��~�%�%rrj�	user_datac��|�t|j�dk(r'|�t|j�dk(ry|jS|�t|j�dk(r'|�t|j�dk(ry|jSt|j�|j�g�sNtjd�d|jj��d|jj��d�Si}d}g}t|jt�r>|j}|jdi�jdi�jd	d�}t|jt�r�|rtjd
�|j}|D]}||vr|j|�||||<�!|r$tjdj|��|rB|jdi�jd�r!tjd|�||ddd	<d
tj|�j!�zS)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
�
��	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)�lenrI�allrPr+r,r9r2�
isinstancerJ�dict�getr r-�yaml�dump�strip)rjrl�merged�	user_tags�overridden_keys�agent�keys       r�merge_agent_landscape_datar��s����S����0�A�5����I�M�M� 2�a� 7���}�}����C�	�
�
�.�!�3����Z�^�^�!4��!9���~�~���
�*�*�,�i�.G�.G�.I�J�K��	�	�
8�	
�

�O�O�$�$�&��N�N�#�#�%�
�	
��F��I�(*�O��)�'�'��.��&�&���J�J�{�B�'�+�+�H�b�9�=�=�f�b�I�	��*�(�(�$�/���I�I�F�G��&�&���	%�C��f�}��&�&�s�+���*�F�3�K�	%���I�I����o�.�
�����K��4�8�8��B��I�I�-��
�
5>�F�;���)�&�1�	M�
�)�)�F�
�
!�
!�
#�	$�rc�V��eZdZdZd
dedef�fd�
Zdedefd�Zde	fd�Z
de	fd	�Z�xZS)�
DataSourceWSL�WSL�distro�pathsc�8��t�|�||||�d|_y)Nro)�superrMr)rL�sys_cfgr�r��ud_proc�	__class__s     �rrMzDataSourceWSL.__init__=s���
����&�%��9���rr;rc��tj|�D�cic]'}|jj�|j��)}}|std|z��t
|j�D�cgc]}|j���}}|D]$}||j�vs�t||�cStd|z��cc}wcc}w)z�
        Finds the most precendent of the candidate files that may contain
        user-data, if any, or None otherwise.
        z%s directory is emptyz6%s doesn't contain any of the expected user-data files)
r(�scandirr�casefoldr9r'rDr�keysr)rLr;�ef�existing_files�f�folded_names�filenames       r�find_user_data_filez!DataSourceWSL.find_user_data_fileAs���35�*�*�X�2F�
�,.�B�G�G�������'�
��
���1�H�<�=�=�4�D�4F�4F�G�
��
�J�J�L�
��
�%�	:�H��>�.�.�0�0���x� 8�9�9�	:��D�x�O�
�	
��
��
s�,B=�-Cc��|j�}|sy	tt��}t|t	��}||jd�k(S#ttf$r)}tjdt|��Yd}~yd}~wwxYw)NFrYz2Unable to check_instance_id from metadata file: %s)�get_instance_idr<r6rarrwr'r]r+�warning�str)rLr��current�data_dirr^�errs      r�check_instance_idzDataSourceWSL.check_instance_id_s{���&�&�(����
	�*�9�;�7�H�-�h�
��H�H��h�l�l�=�9�9�9����$�	��K�K�D��C��
���	�s�:A�B�B�Bc�0�tjt�stj	dt�yt�|_	t
�}t|�}d}d}	t||j
�|_
|jjdk(rt!|�\}}	|�|�t#|j%|��}t)||g�syt+||�|_y#t$r }tj	d|�Yd}~yd}~wwxYw#ttf$r)}tjdt|��Yd}~yd}~wwxYw#ttf$r?}|rtj&ntj}|d|t|��Yd}~��d}~wwxYw)Nz5No WSL command %s found. Cannot detect WSL datasourceFz#Unable to detect WSL datasource: %szUnable to load metadata: %s�ubuntuz+Unable to load any user-data file in %s: %sT)r�whichrr+r,rr6r'r<rar^r]r\r�r�rrkrFr��info�anyr��userdata_raw)rLr7�er;rjrlr��logs        r�	_get_datazDataSourceWSL._get_datarsk���z�z�+�&��I�I�G��
��*�_���	�!��I�
'�y�1��+/�
�*.�	�	�2��$�,�,��D�M��;�;���x�'�$8��$C�!�J�	�
	�� �X�%9�&�t�'?�'?��'I�J�	��I�z�*�+��6�z�9�M�����O�	��I�I�;�Q�?���	���G�$�	��I�I�3�S��X�>���	���G�$�	�(�#�(�(�c�i�i�C��=���C��
�
��	�sH�
C �D�"E� 	D	�)D�D	�E�D?�?E�F�5F�FrO)
rQrRrS�dsnamer
rrMrr�rUr�r��
__classcell__)r�s@rr�r�:sF���
�F� �� �u� �
�H�
��
�<�D��&2�4�2rr�c�6�tj|t�SrO)r�list_from_depends�datasources)�dependss r�get_datasource_listr��s���$�$�W�k�:�:r),rT�loggingr(�typing�pathlibrrrrrx�	cloudinitrrr	�cloudinit.distrosr
�cloudinit.handlersr�cloudinit.helpersr�	getLoggerrQr+rrZrcrer�rr%r0r6r<rDrFrvrarkr��
DataSourcer��DEP_FILESYSTEMr�r�rVrr�<module>r�sn��F��	�
��(�(��)�)�$�4�#��g����!�� ��*��$����
1�s�
1�
�D��I�
� ���0"�8�"�<�8����0B���T�#�Y��(,�,�"��8�$��58��	��B&��&�
�8�J���*�!5�5�6�&�@R���$�R�19�*�1E�R�
�c�]�R�jj�G�&�&�j�^�W�+�+�-�.���;r