
    }f%/              	          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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  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"dedee
e#e$f      fdZ%dee   dede#fdZ&dede	e
e#e$df   e
e#e$df   f   fdZ' G d dejP                        Z)e)ejT                  ffgZ+d Z,y)z?Datasource to support the Windows Subsystem for Linux platform.    N)PurePath)AnyListOptionalTupleUnioncast)sourcessubputil)Distro)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     yy+uc!:;M1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#3v;(FNN3|,-. 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_executabler2   :   s    
  !F788 .I #oyybgg&		)3/} 669J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)r2   r   as_posixr   ProcessExecutionErrorr   r   )r1   homer   outs       r   	find_homer8   R   s     
C ii#,,.$8LMNGD!;;=D((B
 	
 YYUD12FCCJJL!!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   )r9   seed_dirs     r   cloud_init_data_dirr>   p   sE     ww||I}5H77=="		>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_namesrF   ~   sK    
 594I4I4K1OZ!1&j,<G 	&,g6_,	 	r   	data_pathc                     	 t        j                  |       }t        j                  |      }||S |S # t        $ r t        j                  d|        Y yw xY w)z
    Tries to load a YAML file as a dict, otherwise returns the file's raw
    binary contents as `bytes`. Returns `None` if no file is found.
    NzNo data found at %s, ignoring.)r   load_binary_file	load_yamlFileNotFoundErrorr-   r.   )rG   bin_data	dict_datas      r   load_yaml_or_binrN      sW    
?((3NN8,	O ?		2I>?s   -2 2 AA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*   r;   r/   r4   r   rJ   rI   rK   r-   r.   error
ValueError)rO   r   metadatametadata_pathmsgs        r   load_instance_metadatarX      s     23HGGLL-!?M
>>$"7"7"FG }H4 M? +""*- 	 			#oO  
		J	

s   (B B98B9c                 T   t         j                  j                  | d      }t         j                  j                  |      syt	        t         j                  j                  |t
        t               z              }t	        t         j                  j                  |t                    }||fS )zg
    Read .ubuntupro user-data if present and return a tuple of agent and
    landscape user-data.
    z.ubuntupro/.cloud-init)NN)r*   r;   r/   r<   rN   LANDSCAPE_DATA_FILEr   AGENT_DATA_FILE)r9   pro_dirlandscape_data
agent_datas       r   load_ubuntu_pro_datar_      sv     ggll9&>?G77==!%
W1MOCDN ""'',,w"HIJ~%%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 )N )super__init__r   )selfsys_cfgrc   rd   ud_proc	__class__s        r   rh   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   casefoldr;   r)   rF   r   keysr   )ri   r=   efexisting_filesffolded_namesfilenames          r   find_user_data_filez!DataSourceWSL.find_user_data_file   s     35**X2F
,.BGG'
 
 1H<== 4D4F4FG
 JJL
 
 % 	:H>..00x 899	: Dx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)NFrQ   z2Unable to check_instance_id from metadata file: %s)get_instance_idr>   r8   rX   r   getr)   rT   r-   warningstr)ri   rj   currentdata_dirrU   errs         r   check_instance_idzDataSourceWSL.check_instance_id   s{     &&(
	*9;7H-hHHhll=999$ 	KKDC 	s   :A BBBc                    t        j                  t              st        j	                  dt               yt               | _        	 t               }t        |      }d }d }	 t        || j
                        | _
        | j                  j                  dk(  rt!        |      \  }}	 |,|*| j#                  |      }t%        |j'                               }t)        ||g      syt+        |t,              st+        |t,              rt/        t0        ||g      | _        yi }g }	|r|}|ra|rt        j	                  d       |D ]  }
|
|v r|	j5                  |
       ||
   ||
<   ! |	r$t        j	                  d	j7                  |	             d
t9        j:                  |      z  | _        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+}t        j                  d|t        |             Y d }~kd }~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: %sTz.Merging both user_data and agent.yaml configs.z% agent.yaml overrides config keys: , z#cloud-config
%s)r   whichr   r-   r.   r   r8   r)   r>   rX   rU   rT   rS   r{   rc   r   r_   rv   rN   r4   any
isinstancebytesr	   r   userdata_rawr"   r/   yamldump)ri   r9   er=   r^   	user_datar~   filemergedoverridden_keyskeys              r   	_get_datazDataSourceWSL._get_data  s   zz+&IIG *_	!I
 'y1
26		2$,,DM ;;x'$8$C!J			 X%9//9,T]]_=	 Iz*+
 j%(Jy%,H $S9j*A BD ,.F		JK! .&=#**3/(os. 		!T/2 0$))F2CCI  	II;Q?	 G$ 	II3SX>	 G$ 	II=C 	sH   
F G ".H 	G(GGHG>>HI  H;;I N)__name__
__module____qualname__dsnamer   r   rh   r   rv   boolr   r   __classcell__)rl   s   @r   ra   ra      sH    F   u  
H 
 
<D &O4 Or   ra   c                 6    t        j                  | t              S r   )r
   list_from_dependsdatasources)dependss    r   get_datasource_listr   h  s    $$Wk::r   )-__doc__loggingr*   typingpathlibr   r   r   r   r   r   r	   r   	cloudinitr
   r   r   cloudinit.distrosr   cloudinit.helpersr   	getLoggerr   r-   r   rR   rZ   r[   r{   r   r'   r2   r8   r>   rF   dictr   rN   rX   r_   
DataSourcera   DEP_FILESYSTEMr   r    r   r   <module>r      s_   F  	   : :  ) ) $ #g! * $ 
1s 
1DI   0"8 "<8 0B T#Y ( tU{1C(D $8$58	B&&
5ud"#U4+<%==>&$GG&& GX W++-.;r   