x[ht9dZddlZddlZddlZddlmZddlmZmZmZddl Z ddl m Z m Z m Z ddlmZddlmZddlmZej(eZd Zd Zd Zd Zd efdZd eefdZd efdZd efdZded eefdZ d eefdZ!GddZ"deeded e#fdZ$ded eee"ee"ffdZ%dee"dee"d eefdZ&Gdde jNZ(e(e jRffgZ*d Z+y)!z?Datasource to support the Windows Subsystem for Linux platform.N)PurePath)ListOptionalTuple)sourcessubputil)Distro)type_from_starts_with)Pathsz/usr/bin/wslpathziid-datasource-wsl %s.user-dataz agent.yamlreturnctjtddg\}}t|jjS)zL Returns the name of the current WSL instance as seen from outside. z-am/)r WSLPATH_CMDrrstripname) root_net_path_s A/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceWSL.py instance_namers8yy+uc!:;M1 M((* + 0 00cd}d}g}tjjD]'}|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 9pz aname=drvfsfstypeopts mountpoint)r mountsvaluesappend)FS_TYPEOPTIONS_CONTAINmountedmnts rmounted_win_drivesr%+s` G#OG{{}##%. x=G #3v;(F NN3|, -. Nrct}|s tdd}|D]N}||z}tj|tjs-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%IOErrorosaccessX_OKLOGdebugrjoin)r candidater$cmds rcmd_executabler0;s  !F 788.I#oyybgg&  )3/}  669JJ rc(t}tjd|jddg\}}|j}|stjdtjt d|g\}}t |jS)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)r0ras_posixrProcessExecutionErrorrr)r/homerouts r find_homer6Ss  C ii#,,.$8LMNGD! ;;=D (( B  YY UD1 2FC CJJL !!r user_homectjj|d}tjj|stj d|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)r7seed_dirs rcloud_init_data_dirr<qsE ww||I}5H 77== " >I H rcbtj\}}}|r|n|}d|z|d|dd|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)rdistribution_id version_idversion_codenameversions rcandidate_user_data_file_namesrDsK 594I4I4K1OZ!1&j,>$"5"5m"DE }H4M?+""* -  #o O   J   s(BB98B9cDtjj|d}tjj|syt tjj|t t z}d}tjj|r+tjd|t|t|}t tjj|t}d}tjj|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.) r(r9r-r:rLANDSCAPE_DATA_FILErisfiler+r,r<rFAGENT_DATA_FILE)r7pro_dirlandscape_pathlandscape_data agent_path agent_datas rload_ubuntu_pro_datarksggll9&>?G 77== !  W1MOCDNN ww~~n%  1   *  $N3"'',,w@AJJ ww~~j! + ~ %%rrj user_datac2| |js| |jsy|jS| |js| |jsy|jSt|j|jgsNtj dd|j j d|j j dSi}d}g}t|jtr>|j}|jdijdijdd}t|jtr|rtj d |j}|D]}||vr|j|||||<!|r$tj d j||rB|jdijdr!tj d |||ddd<d tj|jzS) 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. NzaUnable to merge {agent_data.path} and {user_data.path}. Providing as separate user-data #include.z #include  landscapeclienttagsz.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)rIallrPr+r,r9r2 isinstancerJdictgetr r-yamldumpstrip)rjrlmerged user_tagsoverridden_keysagentkeys rmerge_agent_landscape_datars JNN  y}}}} 9==   ~~  **,i.G.G.IJ K  8 OO $ $ & NN # # %  FI(*O)''.&& JJ{B ' + +Hb 9 = =fb I *(($/  IIF G&& %Cf}&&s+*F3K %  IIo.  K488B II-  5>F;  )& 1 M ))F  ! ! # $rcVeZdZdZd dedeffd ZdedefdZde fdZ de fd Z xZ S) DataSourceWSLWSLdistropathsc8t|||||d|_y)Nro)superrMr)rLsys_cfgrrud_proc __class__s rrMzDataSourceWSL.__init__=s &%9rr;rctj|Dcic]'}|jj|j)}}|st d|zt |jDcgc]}|j}}|D]$}||jvst||cSt d|zcc}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(scandirrcasefoldr9r'rDrkeysr)rLr;efexisting_filesf folded_namesfilenames rfind_user_data_filez!DataSourceWSL.find_user_data_fileAs35**X2F ,.BGG    '  1H<= =4D4F4FG  JJL  % :H>..00x 899 : Dx O    s ,B=-Cc|j}|sy tt}t|t }||j dk(S#t tf$r)}tjdt|Yd}~yd}~wwxYw)NFrYz2Unable to check_instance_id from metadata file: %s) get_instance_idr<r6rarrvr'r]r+warningstr)rLrcurrentdata_dirr^errs rcheck_instance_idzDataSourceWSL.check_instance_id_s{&&( *9;7H-h HHhll=99 9$  KKDC   s:ABBBc0tjtstj dtyt |_ t }t|}d}d} t||j |_ |jjdk(rt!|\}} ||t#|j%|}t)||gsyt+|||_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: %subuntuz+Unable to load any user-data file in %s: %sT)rwhichrr+r,rr6r'r<rar^r]r\rrrrkrFrinfoanyr userdata_raw)rLr7er;rjrlrlogs r _get_datazDataSourceWSL._get_datarskzz+& IIG *_ ! I 'y1+/ *.  2$,,DM ;;  x '$8$C !J   X%9&t'?'?'IJ Iz*+6z9MO  II;Q ? G$  II3SX > G$ (#((ciiC =C   sH C D "E D )DD  ED??EF5FFrO) rQrRrSdsnamer r rMrrrUrr __classcell__)rs@rrr:sF F  u  H  <D&242rrc6tj|tSrO)rlist_from_depends datasources)dependss rget_datasource_listrs  $ $Wk ::r),rTloggingr(typingpathlibrrrrrw cloudinitrrr cloudinit.distrosr cloudinit.handlersr cloudinit.helpersr getLoggerrQr+rrZrcrerrr%r0r6r<rDrFrurarkr DataSourcerDEP_FILESYSTEMrrrVrrrsn F (( ))$4#g! *$ 1s 1 DI  0"8"< 8 0B T#Y(,,"8$58 B&& 8J *!5 56&@R$R19*1ER c]RjjG&&j^W++-. ;r