a 'Dg|9@sdZddlZddlZddlZddlmZddlmZmZmZddl Z ddl m Z m Z m Z ddlmZddlmZddlmZeeZd Zd Zd Zd Zed ddZeed ddZed ddZed ddZeeedddZ eed ddZ!GdddZ"eeee#dddZ$eeee"ee"fdd d!Z%ee"ee"eed"d#d$Z&Gd%d&d&e j'Z(e(e j)ffgZ*d'd(Z+dS))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.yamlreturncCs"ttddg\}}t|jS)zL Returns the name of the current WSL instance as seen from outside. z-am/)r WSLPATH_CMDrrstripname)Z root_net_path_rC/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceWSL.py instance_names rcCsHd}d}g}tD]*}|d|kr||dvr||dq|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 Z9pz aname=drvfsZfstypeZoptsZ mountpoint)rmountsvaluesappend)ZFS_TYPEZOPTIONS_CONTAINZmountedmntrrrmounted_win_drives+srcCsdt}|stdd}|D]2}||}t|tjs6qtd|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) rIOErrorosaccessX_OKLOGdebugrjoin)r candidatercmdrrrcmd_executable;s   r%cCsVt}td|ddg\}}|}|s6tdttd|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%ras_posixrZProcessExecutionErrorrr)r$homeroutrrr find_homeSs r)) user_homercCs2tj|d}tj|s*td|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)rpathr"isdirr r!r)r*seed_dirrrrcloud_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_codenameversionrrrcandidate_user_data_file_namess  r0c@s,eZdZdZedddZedddZdS) 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|jkr4t|j|_dS)Nztext/cloud-config)rload_text_filerawr+ config_dictr load_yaml)selfr+rrr__init__s  zConfigData.__init__r cCs |jduSN)r4)r6rrris_cloud_configszConfigData.is_cloud_configN)__name__ __module__ __qualname____doc__rr7boolr9rrrrr1s r1) cloudinitdirrrcCsdti}|dur|Stj|d|}ztt|}Wnty\t d|Yn0|rjd|vrd|d|}t |t ||S)z` Returns the relevant metadata loaded from cloudinit dir based on the instance name instance-idNz %s.meta-datazMsz5DataSourceWSL.find_user_data_file..z%s directory is emptycSsg|] }|qSr)ra)rbfrrr Ssz5DataSourceWSL.find_user_data_file..z6%s doesn't contain any of the expected user-data filesN)rscandirrr0rkeysr)r6r-Zexisting_filesZ folded_namesfilenamerrrfind_user_data_fileAs   z!DataSourceWSL.find_user_data_filer c Csv|}|sdSz&tt}t|t}||dkWSttfyp}zt dt |WYd}~dSd}~00dS)NFr@z2Unable to check_instance_id from metadata file: %s) Zget_instance_idr.r)rGrrTrrDr Zwarningstr)r6r^currentZdata_dirrEerrrrrcheck_instance_id_s  zDataSourceWSL.check_instance_idc CslttstdtdSt|_z t}Wn2ty^}ztd|WYd}~dSd}~00t|}d}d}zt ||j|_ Wn:t tfy}zt dt |WYd}~dSd}~00|jjdkrt|\}}z&|dur|durt||}WnLt tfyH}z.|rtjntj }|d|t |WYd}~n d}~00t||gs\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)rwhichrr r!rr)rr.rGrErDrCrjr[rrLr1riinfoanyrYZ userdata_raw)r6r*er-rKrMrllogrrr _get_datarsL       zDataSourceWSL._get_data)N) r:r;r<Zdsnamer r r7rrir>rmrs __classcell__rrr_rrZ:s rZcCs t|tSr8)rZlist_from_depends datasources)Zdependsrrrget_datasource_listsrv),r=ZloggingrtypingpathlibrrrrrUZ cloudinitrrrZcloudinit.distrosr Zcloudinit.handlersr Zcloudinit.helpersr Z getLoggerr:r rrArHrJrjrrr%r)r.r0r1rSrGrLrYZ DataSourcerZZDEP_FILESYSTEMrurvrrrrsF       " !  Uo