Ë
    x[ht9  ã                   óÜ  — 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j(                  e«      Zd	Zd
ZdZdZdefd„Zdee   fd„Zdefd„Zdefd„Zdedee   fd„Z dee   fd„Z! G d„ d«      Z"dee   dede#fd„Z$dedeee"   ee"   f   fd„Z%dee"   dee"   dee   fd„Z& G d„ dejN                  «      Z(e(ejR                  ffgZ*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/wslpathziid-datasource-wslú%s.user-dataz
agent.yamlÚreturnc                  ó†   — t        j                   t        ddg«      \  } }t        | j                  «       «      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Ú_s     úA/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceWSL.pyÚinstance_namer      s8   € ô —y‘y¤+¨u°cÐ!:Ó;Ñ€M�1Ü�M×(Ñ(Ó*Ó+×0Ñ0Ð0ó    c                  óª   — d} d}g }t        j                  «       j                  «       D ]'  }|d   | k(  sŒ||d   v sŒ|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
    Ú9pzaname=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˜|Ñ,Õ-ð.ð €Nr   c                  ó  — t        «       } | st        d«      ‚d}| D ]N  }||z  }t        j                  |t        j                  «      sŒ-t
        j                  d|«       t        |«      c S  t        ddj                  | «      z  «      ‚)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$   Úcmds       r   Úcmd_executabler0   ;   sˆ   € ô
  Ó!€FÙÜÐ7Ó8Ð8ð .€IØò ˆØ˜#‰oˆÜ�y‰y˜œbŸg™gÔ&Øä�	‰	Ð)¨3Ô/Ü˜‹}Òðô Ø6¸¿¹À6Ó9JÑJóð r   c                  ó(  — t        «       } t        j                  d| j                  «       ddg«      \  }}|j                  «       }|st        j                  d«      ‚t        j                  t
        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/Czecho %USERPROFILE%z4No output from cmd.exe to show the user profile dir.z-au)r0   r   Úas_posixr   ÚProcessExecutionErrorr   r   )r/   Úhomer   Úouts       r   Ú	find_homer6   S   s€   € ô Ó
€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                 óÄ   — t         j                  j                  | d«      }t         j                  j                  |«      st        j                  d|«       y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   )r7   Úseed_dirs     r   Úcloud_init_data_dirr<   q   sE   € ô �w‰w�|‰|˜I }Ó5€HÜ�7‰7�=‰=˜Ô"Ü�	‰	Ð>ÀÔIØä�HÓÐr   c                 ób   — t        j                  «       \  }}}|r|n|}d| z  |›d|›d�d|z  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
.user-dataz%s-all.user-datazdefault.user-data)r	   Úget_linux_distro)r   Údistribution_idÚ
version_idÚversion_codenameÚversions        r   Úcandidate_user_data_file_namesrD      sK   € ô
 59×4IÑ4IÓ4KÑ1€O�ZÐ!1Ù&‰jÐ,<€Gð 	˜Ñ&â,ªgÐ6à˜_Ñ,àð	ð 	r   c                   ó(   — e Zd ZdZdefd„Zdefd„Zy)Ú
ConfigDataz~Models a piece of configuration data as a dict if possible, while
    retaining its raw representation alongside its file pathr9   c                 óÎ   — t        j                  |«      | _        || _        d | _        dt        | j                  «      k(  r%t        j                  | j                  «      | _        y y )Nztext/cloud-config)r	   Úload_text_fileÚrawr9   Úconfig_dictr   Ú	load_yaml)Úselfr9   s     r   Ú__init__zConfigData.__init__—   sP   € Ü×+Ñ+¨DÓ1ˆŒØ"ˆŒ	à+/ˆÔàÔ"7¸¿¹Ó"AÒAÜ#Ÿ~™~¨d¯h©hÓ7ˆDÕð Br   r   c                 ó   — | j                   d uS ©N)rJ   )rL   s    r   Úis_cloud_configzConfigData.is_cloud_config    s   € Ø×Ñ tÐ+Ð+r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rM   ÚboolrP   © r   r   rF   rF   “   s    „ ñ@ð8˜Xó 8ð, ô ,r   rF   Úcloudinitdirr   c                 óx  — dt         i}| €|S t        j                  j                  | j	                  «       d|z  «      }	 t        j                  t        j                  |«      «      }|rd|vr(d|› d|› �}t        j                  |«       t        |«      ‚|S # t        $ r t        j                  d|«       Y ŒQw xY w)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(   r9   r-   r2   r	   rK   rH   ÚFileNotFoundErrorr+   r,   ÚerrorÚ
ValueError)rW   r   Ú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Â8B9c                 óD  — t         j                  j                  | d«      }t         j                  j                  |«      syt	        t         j                  j                  |t
        t        «       z  «      «      }d}t         j                  j                  |«      r+t        j                  d|t        | «      «       t        |«      }t	        t         j                  j                  |t        «      «      }d}t         j                  j                  |«      rt        |«      }||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(   r9   r-   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Ô!Ü 
Ó+ˆ
à�~Ð%Ð%r   rj   Ú	user_datac                 ó2  — | �| j                   s|�|j                   sy|j                   S |�|j                   s| �| j                   sy| j                   S t        | j                  «       |j                  «       g«      sNt        j	                  d«       d| j
                  j                  «       ›d|j
                  j                  «       ›d�S i }d}g }t        |j                  t        «      r>|j                  }|j                  di «      j                  di «      j                  dd«      }t        | j                  t        «      r±|rt        j	                  d	«       | j                  }|D ]  }||v r|j                  |«       ||   ||<   Œ! |r$t        j	                  d
j                  |«      «       |rB|j                  di «      j                  d«      r!t        j	                  d|«       ||d   d   d<   dt        j                  |«      j                  «       z  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.
    NzaUnable 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)rI   ÚallrP   r+   r,   r9   r2   Ú
isinstancerJ   ÚdictÚgetr    r-   ÚyamlÚdumpÚstrip)rj   rl   ÚmergedÚ	user_tagsÚoverridden_keysÚagentÚkeys          r   Úmerge_agent_landscape_datar   å   sñ  € ð 	Ð J§N¢NØÐ y§}¢}ØØ�}‰}Ðð 	Ð 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Ó
×
!Ñ
!Ó
#ñ	$ðr   c                   óV   ‡ — e Zd ZdZd
dedefˆ fd„Zdedefd„Zde	fd„Z
de	fd	„Zˆ xZS )ÚDataSourceWSLÚWSLÚdistroÚpathsc                 ó8   •— t         ‰| �  ||||«       d| _        y )Nro   )ÚsuperrM   r   )rL   Úsys_cfgrƒ   r„   Úud_procÚ	__class__s        €r   rM   zDataSourceWSL.__init__=  s   ø€ Ü‰Ñ˜ &¨%°Ô9ØˆÕr   r;   r   c                 óŽ  — t        j                  |«      D �ci c]'  }|j                  j                  «       |j                  “Œ) }}|st        d|z  «      ‚t        | j                  «      D �cg c]  }|j                  «       ‘Œ }}|D ]$  }||j                  «       v sŒt        ||   «      c S  t        d|z  «      ‚c c}w c c}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   Úcasefoldr9   r'   rD   r   Úkeysr   )rL   r;   ÚefÚexisting_filesÚfÚfolded_namesÚfilenames          r   Úfind_user_data_filez!DataSourceWSL.find_user_data_fileA  sÖ   € ô 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=Á-Cc                 ó  — | j                  «       }|sy	 t        t        «       «      }t        |t	        «       «      }||j                  d«      k(  S # t        t        f$ r)}t        j                  dt        |«      «       Y d }~yd }~ww xY w)NFrY   z2Unable to check_instance_id from metadata file: %s)Úget_instance_idr<   r6   ra   r   rv   r'   r]   r+   ÚwarningÚstr)rL   r‡   ÚcurrentÚdata_dirr^   Úerrs         r   Úcheck_instance_idzDataSourceWSL.check_instance_id_  s{   € ð ×&Ñ&Ó(ˆÙØð
	Ü*¬9«;Ó7ˆHÜ-¨h¼»ÓHˆHØ˜hŸl™l¨=Ó9Ñ9Ð9øäœÐ$ò 	Ü�K‰KØDÜ�C“ôô ûð	ús   •:A ÁBÁBÂBc                 ó0  — t        j                  t        «      st        j	                  dt        «       yt        «       | _        	 t        «       }t        |«      }d }d }	 t        || j
                  «      | _
        | j                  j                  dk(  rt!        |«      \  }}	 |€|�t#        | j%                  |«      «      }t)        ||g«      syt+        ||«      | _        y# t        $ r }t        j	                  d|«       Y d }~yd }~ww xY w# t        t        f$ r)}t        j                  dt        |«      «       Y d }~yd }~ww xY w# t        t        f$ r?}|rt        j&                  nt        j                  } |d|t        |«      «       Y d }~ŒÐd }~ww xY w)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   r6   r'   r<   ra   r^   r]   r\   r—   rƒ   r   rk   rF   r“   ÚinfoÚanyr   Úuserdata_raw)rL   r7   Úer;   rj   rl   rš   Úlogs           r   Ú	_get_datazDataSourceWSL._get_datar  sk  € Ü�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ÆFrO   )rQ   rR   rS   Údsnamer
   r   rM   r   r“   rU   r›   r¤   Ú__classcell__)r‰   s   @r   r�   r�   :  sF   ø„ Ø€Fñ ¨ð  °uõ  ð
¨Hð 
¸ó 
ð<¨Dó ð&2˜4÷ 2r   r�   c                 ó6   — t        j                  | t        «      S rO   )r   Úlist_from_dependsÚdatasources)Údependss    r   Úget_datasource_listr«   ®  s   € Ü×$Ñ$ W¬kÓ:Ð:r   ),rT   Úloggingr(   ÚtypingÚpathlibr   r   r   r   rw   Ú	cloudinitr   r   r	   Úcloudinit.distrosr
   Úcloudinit.handlersr   Úcloudinit.helpersr   Ú	getLoggerrQ   r+   r   rZ   rc   re   r—   r   r%   r0   r6   r<   rD   rF   ru   ra   rk   r   Ú
DataSourcer�   ÚDEP_FILESYSTEMr©   r«   rV   r   r   ú<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ôjj�G×&Ñ&ô jð^ �W×+Ñ+Ð-Ð.ð€ó;r   