x[hOddlZddlZddlZddlZddlZddlmZmZmZddl m Z ddl m Z m Z ddlmZddlmZddlmZdd lmZdd lmZdd lmZej4eZd Zd ZdZdZ dZ!dZ"dZ#d3dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ, d4dZ-dZ.dZ/dZ0d Z1d!Z2 d5d"Z3d#Z4d$Z5d%Z6d&Z7d'Z8d(Z9d)Z:d*Z;d+Zd.Z?d/Z@d0ZAd1ZBd2ZCy)6N) performancesubputil)Config)PostCustomScriptPreCustomScript) ConfigFile)NicConfigurator)PasswordConfigurator)GuestCustErrorEnum)GuestCustEventEnum)GuestCustStateEnumz/var/log/cloud-init.logqueryNicsSupported connected/var/run/vmware-imc deployPkgzenable-custom-scriptsc*|syd}d} tjd|tjd|gdg\}}|r|j}||fS#t$r7}tjdtj |Yd}~||fSd}~wwxYw)NzError sending the RPC commandzSending RPC command: %szvmware-rpctoolr)rcszFailed to send RPC command)loggerdebugrrstrip Exception exception)rpcouterres U/usr/lib/python3/dist-packages/cloudinit/sources/helpers/vmware/imc/guestcust_util.pysend_rpcr!,s  C )C .4YY 0#6QC@ c **,C :  12 : sAA B*B  Bcdd}|r tdz|z}nt}d|||fz}t|\}}||fS)Nr@zdeployPkg.update.state %d %d %s)CLOUDINIT_LOG_FILEr!) custstate custerror errormessagemessagerrrs r set_customization_statusr)BsFG$s*\9$ +y)W.M MC#JS# :c|syd}tjj|sytjd|5t |d5}|j |}ddddddS#1swYxYw#1swYSxYw)zReads the NICS from the specified file path and returns the content @param nicsfilepath: Absolute file path to the NICS.txt file. NizReading r)ospathexistsrTimedopenread) nicsfilepath NICS_SIZEfpnicss r get_nics_to_enabler7Os I 77>>, '   Xl^4 5"tc8" wwy!"" K """ Ks# A; A/A;/A8 4A;;Bcp|stjdyd}d}d}t|D]}tjd|t t j tj|\}}|stj||z`|tk7rtjdyt|D]g}t t j tj|\}}|r"|tk(rtjd|ytj|itjd|y)Nz No Nics foundrz(Trying to connect interfaces, attempt %dz-NICS connection status query is not supportedzNICS are connected on %d secondz2Can't connect network interfaces after %d attempts)rwarningrangerr)rGUESTCUST_STATE_RUNNINGrGUESTCUST_EVENT_ENABLE_NICStimesleepQUERY_NICS_SUPPORTEDGUESTCUST_EVENT_QUERY_NICSNICS_STATUS_CONNECTEDinfo)r6enableNicsWaitRetriesenableNicsWaitCountenableNicsWaitSecondsattemptr_errcounts r enable_nicsrJfs './. ?I.  6 6  : :  d  JJ*-BB C  & & NNJ K ./ .E2"::"==KS$ s33 =uE JJ, - ..6 NN<r*ctjdstjd|Sddd||g} tj|}|}tjd |j}|r9|jd j}tjd ||||Stjd ||||S#tj$rx}|j dk(r'tjddj ||n6tjd||j tj||cYd }~Sd }~wwxYw)a$Return the value of [section] key from VMTools configuration. @param section: String of section to read from VMTools config @returns: String value from key in [section] or defaultVal if [section] is not present or vmware-toolbox-cmd is not installed. zvmware-toolbox-cmdz9vmware-toolbox-cmd not installed, returning default valueconfiggetEzRvmware-toolbox-cmd returned 69 (unavailable) for cmd: %s. Return default value: %s zFailed running %s[%s]Nz ([^=]+)=(.*)zGet tools config: [%s] %s = %sz%C99C>ct|dd\}}}|j}|rCtjj t |}tjj |s tdd|ztj|y tj|} tjdt!j"|}|j*}|rtjj t |}tjj |s tdd |ztj|y tj|j-d d }t/t0j2t&j4t|d |||fS#t$r/}tdt|tj|Yd}~yd}~wwxYw#t j$$r0}tdt|t&j(|Yd}~Md}~wwxYw#t$r/}td t|tj|Yd}~yd}~wwxYw)Nrl)NNNz,Error locating the cloud-init meta data filezMeta data file is not found: %sz'Error loading cloud-init meta data filez'Validating if meta data is valid or notz&Error parsing the cloud-init meta dataz+Error locating the cloud-init userdata filezUserdata file is not found: %s rz&Error loading cloud-init userdata filern)remeta_data_namer-r.rTrjr/rur GUESTCUST_EVENT_CUSTOMIZE_FAILEDrload_text_filerrvrryaml safe_load YAMLErrorr !GUESTCUST_ERROR_WRONG_META_FORMATuser_data_namereplacer)rryrz) r}rrrmd_filemd_pathrud_fileud_paths r #get_data_from_imc_raw_data_cust_cfgrs(I&!JBB%%G'',,/17;ww~~g& !>1G;"CC   & &$$W-B  LLB C#B)) ggll#3#5w?G77>>'*%A4w>&GG  * *((199$C//22(L) B<_ & !9A"CC   & &~~  !8A"DD    * *%<F&GG  * *sH F *G%H G'%GGH,%HH I(%IIc ii}}|jr;|jr |jdz|jz|d<n|j|d<|jr|j|d<|jr|j|d<||fS)N.zlocal-hostnametimezonez instance-id) host_name domain_namer instance_id)r}rrs r roro7s"B   ""S(8+?+??  $,#5#5B "++J$00= 9r*ct|j|j|j|}|j ||}t |Srg)r r6 name_servers dns_suffixesgenerategen_v2_network_config)r}use_system_devices configurertnicConfiguratorethernets_dicts r rsrsGsI&  O %--iBN  00r*c d|dS)NrP)version ethernetsri)rs r rrUs ~ 66r*ctjj|d}tjj|r+tj d|t t|yy)Nznics.txtz%s file found, to connect nics)r-r.rTr/rrrJr7)r~ nics_files r rxrxYsH \:6I ww~~i  5yA&y12!r*c2tj|dd S)Ndisable_vmware_customizationTrget_cfg_option_bool)sys_cfgs r is_vmware_cust_enabledr`s"''/ r*c0tj|ddS)Nallow_raw_dataTr)ds_cfgs r is_raw_data_cust_enabledrfs  # #F,r?)filenamemaxwaitnaplendirpathwaited fileFullPaths r rrszF& 7 ww||GX6 77>>, '  JK 6& 7  r*cd}d}|}|s|S t|j||}|dkrtj d||||}|S#t$rtj d||YCwxYw)Nvmware_cust_file_max_waitzFailed to get '%s', using %srz/Invalid value '%s' for '%s', using '%s' instead)intrM ValueErrorrr:)rdefault_max_waitmax_wait_cfg_optionrs r rrs5H  vzz"57GHI!| =     $ O   *    sA A+*A+c||j}t|tjj |d}|xr| S)Ndata) marker_idcheck_marker_existsr-r.rT)r{r}product_markerhas_marker_files r rprps;''N) Y7O  1/11r*c|sytjj|d|zdz}tjj|ryy)ag Check the existence of a marker file. Presence of marker file determines whether a certain code path is to be executed. It is needed for partial guest customization in VMware. @param markerid: is an unique string representing a particular product marker. @param: marker_dir: The directory in which markers exist. F .markerfile-.txtT)r-r.rTr/)markerid marker_dir markerfiles r rrs> j.8*Cf*LMJ ww~~j! r*c:|j}|r t||syy)NFT)rcreate_marker_file)r{r}rs r rrrrs ''N!)X6 r*c t|jtjj |dy#t $r/}t dt|tj|Yd}~yd}~wwxYw)NrzError creating marker filesFT) setup_marker_filesrr-r.rTrrurvrr)r{r}rs r rrs` 8--rww||Iv/NO   ) F  ? ?   s47 A/%A**A/cltjdtjj |d|zdz}tj |D]G}|j dstjtjj ||It|djy)a# Create a new marker file. Marker files are unique to a full customization workflow in VMware environment. @param marker_id: is an unique string representing a particular product marker. @param: marker_dir: The directory in which markers exist. zHandle marker creationrrz .markerfilewN) rrr-r.rTlistdir startswithrdel_filer1close)rr marker_filefnames r rrs LL)*'',,z>I+E+NOKJ';   M * MM"'',,z59 :; c  "r*cd}d}d}d}|j}|rt|}|r t|||}t||}|r0|r.tj j |d} t|||| }|r |xr|xr|S|S)NFz per-instance)custom_script_namecheck_custom_script_enablementdo_pre_custom_scriptdo_password_customizationr-r.rTdo_post_custom_script) r|r}r~ris_pre_custom_successfulis_password_custom_successfulis_post_custom_successfulis_custom_script_enabled custom_script ccScriptsDirs r rqrqs$$)! %$//M#A(#K #';-( $%>h$O!1ww||M>B $9 m\<% ! $ *- *) )(r*c t||}|jy#t$r/}tdt |t j |Yd}~yd}~wwxYw)Nz(Error executing pre-customization scriptFT)r executerrurvrr)r}rr~precustrs r rrsW !->   6 F  ? ?   s A%AAc t|||}|jy#t$r/}tdt |t j |Yd}~yd}~wwxYw)Nz)Error executing post-customization scriptFT)rrrrurvrr)r}rr~rpostcustrs r rrs[ #M<N   7 F  ? ?   s A%AAcd}d}|jrtjdd}ttt |}|j dk7rtddtj||Sd}|S)NFfalsez=Set default value to true due to customization configuration.truez-Custom script is disabled by VM Administratorz'Error checking custom script enablementT) default_run_post_scriptrrraIMC_TOOLS_CONF_GROUPNAME"IMC_TOOLS_CONF_ENABLE_CUST_SCRIPTSlowerrur GUESTCUST_ERROR_SCRIPT_DISABLED)r}r default_valuecustom_script_enablements r rr$s$M'' K  / *   %%'61 ; 5  > >    $#$( ##r*cFtjdt}|j} |j}|s|r|j |||ytjd y#t $r/}tdt|tj|Yd}~yd}~wwxYw)NzApplying password customizationzChanging password is not neededz%Error applying password configurationFT) rrr admin_passwordreset_passwordrrrurvrr)r}rpwdConfigurator admin_pwd reset_pwdrs r rr=s LL23*,O''I ++   % %iF C  LL: ;   3 F  ? ?   s#A(A(( B 1%BB c*tt|Srg)rr )cfg_files r parse_cust_cfgrRs *X& ''r*cd\}}|jrd}tjd||fSd}tjd||fS)N)FFTz!raw cloudinit data cust cfg foundzvmware cust cfg found)rrr)r}is_vmware_cust_cfgis_raw_data_cust_cfgs r get_cust_cfg_typer VsY/;,,# 89  4 55" ,-  4 55r*cfd}d}|D](}t||}|stjd|yy)N)z/usr/lib/vmware-toolsz/usr/lib64/vmware-toolsz/usr/lib/open-vm-toolsz/usr/lib64/open-vm-toolsz'/usr/lib/x86_64-linux-gnu/open-vm-toolsz(/usr/lib/aarch64-linux-gnu/open-vm-toolsz%/usr/lib/i386-linux-gnu/open-vm-toolszlibdeployPkgPlugin.soz$Found the customization plugin at %sTF) search_filerr) search_paths cust_pluginr.cust_plugin_paths r is_cust_plugin_availablerasKL*K&t[9  LL68H   r*c|r|sytj|D],\}}}||vs tjj||cSyrg)r-walkr.rT)rrroot_dirsfiless r r r vsK ( ggg.0eU u 77<<h/ /0 r*ctjtd||ttj |t ||y)zS Set customization status to the underlying VMware Virtualization Platform z%s: %sN)rlogexcrr)rr<re)prefixrUeventr}s r rurus1 KK&%0/GGO(F#r*rg)TFN)r9r)Dloggingr-rVr>r cloudinitrrrrLrconfig_custom_scriptrr config_filer config_nicr config_passwdr guestcust_errorr guestcust_eventrguestcust_stater getLogger__name__rr$r@rBrhrrr!r)r7rJrarerjrrrorsrrxrrrrrrprrrrrrqrrrrrr rr rurir*r r%s" --C#'////   8 $/+#$ &%<" , .'T-d%P?D "BF 173 D0.C"22" #")6  $2*(6*$r*