o x[ht9ã@s’dZddlZddlZddlZddlmZddlmZmZmZddl Z ddl m Z m Z m Z ddlmZddlmZddlmZe e¡Zd Zd Zd Zd Zd efdd„Zd eefdd„Zd efdd„Zd efdd„Zded eefdd„Z d eefdd„Z!Gdd„dƒZ"deeded e#fdd„Z$ded eee"ee"ffd d!„Z%d"ee"d#ee"d eefd$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)ÚListÚOptionalÚTuple)ÚsourcesÚsubpÚutil)ÚDistro)Útype_from_starts_with)ÚPathsz/usr/bin/wslpathziid-datasource-wslú %s.user-dataz agent.yamlÚreturncCs"t tddg¡\}}t| ¡ƒjS)zL Returns the name of the current WSL instance as seen from outside. z-amú/)rÚ WSLPATH_CMDrÚrstripÚname)Ú root_net_pathÚ_©rúA/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceWSL.pyÚ instance_names rcCsHd}d}g}t ¡ ¡D]}|d|kr!||dvr!| |d¡q |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ÚmntrrrÚmounted_win_drives+s€r"cCs`tƒ}|s tdƒ‚d}|D]}||}t |tj¡sq t d|¡t|ƒStdd |¡ƒ‚)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!ÚcmdrrrÚcmd_executable;s   ÿr,cCsVtƒ}t d| ¡ddg¡\}}| ¡}|st d¡‚t td|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,rÚas_posixrÚProcessExecutionErrorrr)r+ÚhomerÚoutrrrÚ find_homeSsÿ r1Ú user_homecCs2tj |d¡}tj |¡st d|¡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)r$Úpathr)Úisdirr'r(r)r2Úseed_dirrrrÚcloud_init_data_dirqs   r6cCs6t ¡\}}}|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)rÚget_linux_distro)rÚdistribution_idÚ version_idÚversion_codenameÚversionrrrÚcandidate_user_data_file_namess  ør<c@s,eZdZdZdefdd„Zdefdd„ZdS) Ú ConfigDataz~Models a piece of configuration data as a dict if possible, while retaining its raw representation alongside its file pathr3cCs<t |¡|_||_d|_dt|jƒkrt |j¡|_dSdS)Nztext/cloud-config)rÚload_text_fileÚrawr3Ú config_dictr Ú load_yaml)Úselfr3rrrÚ__init__—s ÿzConfigData.__init__r cCs |jduS©N)r@)rBrrrÚis_cloud_config s zConfigData.is_cloud_configN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrCÚboolrErrrrr=“s r=Ú cloudinitdircCsdti}|dur |Stj | ¡d|¡}z t t |¡¡}Wnty.t   d|¡Ynw|r5d|vrFd|›d|›}t   |¡t |ƒ‚|S)z` Returns the relevant metadata loaded from cloudinit dir based on the instance name ú instance-idNz %s.meta-datazÚFileNotFoundErrorr'r(ÚerrorÚ ValueError)rKrÚmetadataÚ metadata_pathÚmsgrrrÚload_instance_metadata¤s, ÿ þÿ ÿÿ rTcCs’tj |d¡}tj |¡sdSttj |ttƒ¡ƒ}d}tj |¡r0t  d|t |ƒ¡t |ƒ}ttj |t ¡ƒ}d}tj |¡rEt |ƒ}||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$r3r)r4rÚLANDSCAPE_DATA_FILErÚisfiler'r(r6r=ÚAGENT_DATA_FILE)r2Úpro_dirÚlandscape_pathÚlandscape_dataÚ agent_pathÚ agent_datarrrÚload_ubuntu_pro_dataÅs& ÿ ü r]r\Ú user_datacCs\|dus|js|dus|jsdS|jS|dus|js&|dus!|js#dS|jSt| ¡| ¡gƒsAt d¡d|j ¡|j ¡fSi}d}g}t|jt ƒr^|j}|  di¡  di¡  dd¡}t|jt ƒr¥|rkt d¡|j}|D]}||vr{|  |¡||||<qp|rŒt d   |¡¡|r¥|  di¡  d¡r¥t d |¡||ddd<d t  |¡ ¡S) 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 %s %s ÚÚ 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)r?ÚallrEr'r(r3r-Ú isinstancer@ÚdictÚgetrr)ÚyamlÚdumpÚstrip)r\r^ÚmergedÚ user_tagsÚoverridden_keysÚagentÚkeyrrrÚmerge_agent_landscape_dataås\ ÿþ  ÿ   ýý ÿÿrocsZeZdZdZddedef‡fdd„ Zdedefd d „Zde fd d „Z de fd d„Z ‡Z S)Ú DataSourceWSLÚWSLNÚdistroÚpathscstƒ ||||¡d|_dS)Nr_)ÚsuperrCr)rBÚsys_cfgrrrsÚud_proc©Ú __class__rrrC=s zDataSourceWSL.__init__r5r cCsjdd„t |¡Dƒ}|std|ƒ‚dd„t|jƒDƒ}|D]}|| ¡vr.t||ƒSqtd|ƒ‚)z Finds the most precendent of the candidate files that may contain user-data, if any, or None otherwise. cSsi|] }|j ¡|j“qSr)rÚcasefoldr3)Ú.0ÚefrrrÚ Msÿz5DataSourceWSL.find_user_data_file..z%s directory is emptycSsg|]}| ¡‘qSr)ry)rzÚfrrrÚ Ssÿÿz5DataSourceWSL.find_user_data_file..z6%s doesn't contain any of the expected user-data files)r$Úscandirr#r<rÚkeysr)rBr5Úexisting_filesÚ folded_namesÚfilenamerrrÚfind_user_data_fileAs ÿ þ ÿÿz!DataSourceWSL.find_user_data_filec Csr| ¡}|sdSzttƒƒ}t|tƒƒ}|| d¡kWSttfy8}zt  dt |ƒ¡WYd}~dSd}~ww)NFrLz2Unable to check_instance_id from metadata file: %s) Úget_instance_idr6r1rTrrfr#rPr'ÚwarningÚstr)rBruÚcurrentÚdata_dirrQÚerrrrrÚcheck_instance_id_s  þ€ûzDataSourceWSL.check_instance_idc Csbt t¡s t dt¡dStƒ|_ztƒ}Wnty/}z t d|¡WYd}~dSd}~wwt|ƒ}d}d}z t ||jƒ|_ Wnt tfy^}zt  dt |ƒ¡WYd}~dSd}~ww|jjdkrkt|ƒ\}}z|dur{|dur{t| |¡ƒ}Wn$t tfy }z|r‹tjntj }|d|t |ƒƒWYd}~nd}~wwt||gƒs©dSt||ƒ|_dS)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(rr1r#r6rTrQrPrOr‡rrrr]r=r„ÚinfoÚanyroÚ userdata_raw)rBr2Úer5r\r^rŠÚlogrrrÚ _get_datarsZ þ  €þ ÿ€þ  €ý€þ  zDataSourceWSL._get_datarD) rFrGrHÚdsnamer r rCrr„rJr‹r“Ú __classcell__rrrwrrp:s rpcCs t |t¡SrD)rÚlist_from_dependsÚ datasources)ÚdependsrrrÚget_datasource_list®s r™),rIÚloggingr$ÚtypingÚpathlibrrrrrgÚ cloudinitrrrÚcloudinit.distrosr Úcloudinit.handlersr Úcloudinit.helpersr Ú getLoggerrFr'rrMrUrWr‡rr"r,r1r6r<r=rerTr]roÚ DataSourcerpÚDEP_FILESYSTEMr—r™rrrrÚs\      ÿÿ þ!ÿ þ ÿÿ þU oÿ