
    x[hO                        S SK r S SKrS SKrS SKrS SKrS SKJrJrJr  SSK	J
r
  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  \ R4                  " \5      rSrSrSrSr Sr!Sr"S r#S3S jr$S r%S r&S r'S r(S r)S r*S r+S r, S4S jr-S r.S r/S r0S  r1S! r2 S5S" jr3S# r4S$ r5S% r6S& r7S' r8S( r9S) r:S* r;S+ r<S, r=S- r>S. r?S/ r@S0 rAS1 rBS2 rCg)6    N)performancesubputil   )Config)PostCustomScriptPreCustomScript)
ConfigFile)NicConfigurator)PasswordConfigurator)GuestCustErrorEnum)GuestCustEventEnum)GuestCustStateEnumz/var/log/cloud-init.logqueryNicsSupported	connected/var/run/vmware-imc	deployPkgzenable-custom-scriptsc                 6   U (       d  g SnSn [         R                  SU 5        [        R                  " SU /S/S9u  pU(       a  UR                  5       nX4$ ! [         a6  n[         R                  S5        [         R                  U5         S nAX4$ S nAff = f)N zError sending the RPC commandzSending RPC command: %szvmware-rpctoolr   )rcszFailed to send RPC command)loggerdebugr   rstrip	Exception	exception)rpcouterres       U/usr/lib/python3/dist-packages/cloudinit/sources/helpers/vmware/imc/guestcust_util.pysend_rpcr!   ,   s    
C
)C.4YY 0#6QC@
**,C
 :	  12:	s   AA 
B"*BBc                 h    SnU(       a  [         S-   U-   nO[         nSXU4-  n[        U5      u  pVXV4$ )Nr   @zdeployPkg.update.state %d %d %s)CLOUDINIT_LOG_FILEr!   )	custstate	custerrorerrormessagemessager   r   r   s          r    set_customization_statusr)   B   s@    G$s*\9$
+yW.M
MC#JS:    c                 :   U (       d  gSn[         R                  R                  U 5      (       d  g[        R                  " SU  35         [        U S5       nUR                  U5      nSSS5        SSS5        W$ ! , (       d  f       N= f! , (       d  f       W$ = f)zReads the NICS from the specified file path and returns the content

@param nicsfilepath: Absolute file path to the NICS.txt file.
Ni   zReading r)ospathexistsr   Timedopenread)nicsfilepath	NICS_SIZEfpnicss       r    get_nics_to_enabler7   O   s     I77>>,''			Xl^4	5tc8	wwy!8	5
 K8 8	5	5
 Ks$   	BA:(B:
B	B
Bc                    U (       d  [         R                  S5        g SnSnSn[        U5       GH  n[         R                  SU5        [	        [
        R                  [        R                  U 5      u  pVU(       d  [        R                  " X#-  5        Mf  U[        :w  a  [         R                  S5          g [        U5       Hn  n[	        [
        R                  [        R                  U 5      u  pVU(       a#  U[        :X  a  [         R                  SU5            g [        R                  " U5        Mp     GM     [         R                  SU5        g )NzNo Nics found   r   z(Trying to connect interfaces, attempt %dz-NICS connection status query is not supportedzNICS are connected on %d secondz2Can't connect network interfaces after %d attempts)r   warningranger   r)   r   GUESTCUST_STATE_RUNNINGr   GUESTCUST_EVENT_ENABLE_NICStimesleepQUERY_NICS_SUPPORTEDGUESTCUST_EVENT_QUERY_NICSNICS_STATUS_CONNECTEDinfo)r6   enableNicsWaitRetriesenableNicsWaitCountenableNicsWaitSecondsattemptr   _errcounts           r    enable_nicsrJ   f   s   './?I.66::

 JJ*BC&&NNJK./E2"::"==KS
 s33=uEJJ,- 0 06 NN<r*   c                    [         R                  " S5      (       d  [        R                  S5        U$ SSSX/n [         R                   " U5      nUn[        R                  " S
UR                  5      nU(       a8  UR                  S5      R                  5       n[        R                  SXU5        U$ [        R                  SU UU5        U$ ! [         R                   ax  nUR
                  S:X  a'  [        R                  SSR                  U5      U5        O5[        R                  SX5R
                  5        [        R                  U5        Us S	nA$ S	nAff = f)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.
zvmware-toolbox-cmdz9vmware-toolbox-cmd not installed, returning default valueconfiggetE   zRvmware-toolbox-cmd returned 69 (unavailable) for cmd: %s. Return default value: %s zFailed running %s[%s]Nz([^=]+)=(.*)   zGet tools config: [%s] %s = %sz<Tools config: [%s] %s is not found, return default value: %s)r   whichr   r   ProcessExecutionError	exit_codejoinerrorr   rematchstdoutgroupstrip)sectionkey
defaultValcmdr   r   retValuems           r    get_tools_configra      s$    ::*++G	
 5'
?Ciin H
#**-A771:##%5wXN O 	J		
 O5 %% ;;"LL,	 LL0#{{CQs   C E A-EEEc                 T    U (       a!  U R                   (       a  SU-  n[        U5      $ g )Nzinfo-set guestinfo.gc.status %s)post_gc_statusr!   )rL   gcMsgr   s      r    set_gc_statusre      s$    &''/%7}r*   c                      [         $ N)IMC_DIR_PATH r*   r    get_imc_dir_pathrj      s    r*   c                    0 S S 0 4u  pVpx[        US5        [        U5      u  pX[        X5      n	U	(       a  [        XX45      (       d  g[	        X5      (       d  g [
        R                  S5        [        USSUR                  5      US'   [        U5        [        [         R"                  [$        R&                  5        [        US5        XVXx4$ ! [         a/  n
[        S[        U
5      [        R                  U5         S n
A
gS n
A
ff = f)NStarted)NNNNz#Preparing the Network configurationTnetworkz%Error preparing Network Configuration
Successful)re   )get_non_network_data_from_vmware_cust_cfgcheck_markersdo_special_customizationrecheck_markersr   r   %get_network_data_from_vmware_cust_cfgosfamilyr   set_cust_error_statusstrr   $GUESTCUST_EVENT_NETWORK_SETUP_FAILEDconnect_nicsr)   r   GUESTCUST_STATE_DONEr   GUESTCUST_ERROR_SUCCESS)	cloud_dirscripts_cpathcust_cfgcust_cfg_dirdistromdudvdcfgis_special_customizationr   s              r    get_data_from_imc_cust_cfgr      s     $b(OBB(I&9(CIR,YA'\
 
 ,9//'(:;=dD&//
9 //22 (L)B  (3FCC		
 ((s   0C 
D%D  Dc                    [        U S5        Su  pnU R                  nU(       GaU  [        R                  R	                  [        5       U5      n[        R                  R                  U5      (       d   [        SSU-  [        R                  U 5        g [        R                  " U5      n [        R                  S5        [         R"                  " U5      nU R*                  nU(       a  [        R                  R	                  [        5       U5      n[        R                  R                  U5      (       d   [        SS	U-  [        R                  U 5        g [        R                  " U5      R-                  S
S5      n[/        [0        R2                  [&        R4                  5        [        U S5        XU4$ ! [         a/  n[        S[        U5      [        R                  U 5         S nAgS nAff = f! [         R$                   a0  n[        S[        U5      [&        R(                  U 5         S nAGNYS nAff = f! [         a/  n[        S[        U5      [        R                  U 5         S nAgS nAff = f)Nrl   )NNNz,Error locating the cloud-init meta data filezMeta 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 filezUserdata file is not found: %sr   z&Error loading cloud-init userdata filern   )re   meta_data_namer-   r.   rT   rj   r/   ru   r    GUESTCUST_EVENT_CUSTOMIZE_FAILEDr   load_text_filer   rv   r   r   yaml	safe_load	YAMLErrorr   !GUESTCUST_ERROR_WRONG_META_FORMATuser_data_namereplacer)   r   ry   rz   )	r}   r   r   r   md_filemd_pathr   ud_fileud_paths	            r    #get_data_from_imc_raw_data_cust_cfgr      s   (I&!JBB%%G'',,/17;ww~~g&&!>1G;"CC	 &		&$$W-B		LLBC#B ))ggll#3#5w?G77>>'**%A4w>&GG	 *	*((199$C //22 (L)B<_  	&!9A"CC	 &	& ~~ 	!8A"DD	 	*  *%<F&GG	 **sH   F3 *+G/ &H6 3
G,=%G''G,/H3%H..H36
I/ %I**I/c                 0   0 0 p!U R                   (       a@  U R                  (       a   U R                   S-   U R                  -   US'   OU R                   US'   U R                  (       a  U R                  US'   U R                  (       a  U R                  US'   X4$ )N.zlocal-hostnametimezonezinstance-id)	host_namedomain_namer   instance_id)r}   r   r   s      r    ro   ro   7  s    """S(8+?+??   $,#5#5B "++J$00=9r*   c                     [        U R                  U R                  U R                  U5      nUR	                  X#5      n[        U5      $ rg   )r   r6   name_serversdns_suffixesgenerategen_v2_network_config)r}   use_system_devices	configurert   nicConfiguratorethernets_dicts         r    rs   rs   G  sG     &	O %--iBN 00r*   c                     SU S.$ )NrP   )version	ethernetsri   )r   s    r    r   r   U  s    ~66r*   c                     [         R                  R                  U S5      n[         R                  R                  U5      (       a+  [        R                  SU5        [        [        U5      5        g g )Nznics.txtz%s file found, to connect nics)r-   r.   rT   r/   r   r   rJ   r7   )r~   	nics_files     r    rx   rx   Y  sK    \:6I	ww~~i  5yA&y12 !r*   c                 <    [         R                  " U SS5      (       + $ )Ndisable_vmware_customizationTr   get_cfg_option_bool)sys_cfgs    r    is_vmware_cust_enabledr   `  s"    ''/  r*   c                 2    [         R                  " U SS5      $ )Nallow_raw_dataTr   )ds_cfgs    r    is_raw_data_cust_enabledr   f  s    ##F,<dCCr*   c                     [        U 5      n[        R                  " S5         [        SU5      nS S S 5        W(       a  [        R                  SU5        U$ [        R                  S5        g ! , (       d  f       NC= f)N3Waiting for VMware customization configuration filezcust.cfgz3Found VMware customization configuration file at %sz0No VMware customization configuration file found)get_max_wait_from_cfgr   r0   wait_for_cust_cfg_filer   r   )r   max_waitcust_cfg_file_paths      r    get_cust_cfg_filer   j  sm     %V,H			=
 4JI
 A	
 "!GH
 
s   A,,
A:c                    SnX::  a  SnXA:  a{  [         R                  R                  X05      n[         R                  R                  U5      (       a  U$ [        R                  S5        [        R                  " U5        XB-  nXA:  a  M{  g )Nr   r   r   )r-   r.   rT   isfiler   r   r>   r?   )filenamemaxwaitnaplendirpathwaitedfileFullPaths         r    r   r     ss     F

ww||G677>>,''JK

6 
 r*   c                     SnSnUnU (       d  U$  [        U R                  X!5      5      nUS:  a  [        R	                  SUUU5        UnU$ ! [         a    [        R	                  SUU5         NEf = f)N   vmware_cust_file_max_waitzFailed to get '%s', using %sr   z/Invalid value '%s' for '%s', using '%s' instead)intrM   
ValueErrorr   r:   )r   default_max_waitmax_wait_cfg_optionr   s       r    r   r     s    5H
vzz"5HI !|=		
 $O  
*	

s   A !A10A1c                     UR                   n[        U[        R                  R	                  U S5      5      nU=(       a    U(       + $ )Ndata)	marker_idcheck_marker_existsr-   r.   rT   )r{   r}   product_markerhas_marker_files       r    rp   rp     s;    ''N)Y7O 1/11r*   c                     U (       d  g[         R                  R                  USU -   S-   5      n[         R                  R                  U5      (       a  gg)aK  
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.   rT   r/   )markerid
marker_dir
markerfiles      r    r   r     sA     j.8*Cf*LMJ	ww~~j!!r*   c                 L    UR                   nU(       a  [        X5      (       d  gg)NFT)r   create_marker_file)r{   r}   r   s      r    rr   rr     s!    ''N!)66r*   c                      [        UR                  [        R                  R	                  U S5      5        g! [
         a/  n[        S[        U5      [        R                  U5         S nAgS nAff = f)Nr   zError creating marker filesFT)
setup_marker_filesr   r-   r.   rT   r   ru   rv   r   r   )r{   r}   r   s      r    r   r     s`    	8--rww||Iv/NO   )F??		
 s   47 
A0%A++A0c                 ~   [         R                  S5        [        R                  R	                  USU -   S-   5      n[        R
                  " U5       HN  nUR                  S5      (       d  M  [        R                  " [        R                  R	                  X5      5        MP     [        US5      R                  5         g)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.
zHandle marker creationr   r   z.markerfilewN)r   r   r-   r.   rT   listdir
startswithr   del_filer1   close)r   r   marker_filefnames       r    r   r     s     LL)*'',,z>I+E+NOKJ'M**MM"'',,z9: ( 	c  "r*   c                 8   SnSnSnSnUR                   nU(       a  [        U5      nU(       a  [        XU5      n[        X5      nU(       a3  U(       a,  [        R
                  R                  U S5      n	[        XX)5      nU(       a  U=(       a    U=(       a    U$ U$ )NFzper-instance)custom_script_namecheck_custom_script_enablementdo_pre_custom_scriptdo_password_customizationr-   r.   rT   do_post_custom_script)
r|   r}   r~   r   is_pre_custom_successfulis_password_custom_successfulis_post_custom_successfulis_custom_script_enabledcustom_scriptccScriptsDirs
             r    rq   rq     s    $$)! %$//M#A(#K #';($ %>h$O!1ww||M>B$9\%
! $ *-*)	

 )(r*   c                      [        X5      nUR                  5         g! [         a/  n[        S[	        U5      [
        R                  U 5         S nAgS nAff = f)Nz(Error executing pre-customization scriptFT)r	   executer   ru   rv   r   r   )r}   r   r~   precustr   s        r    r   r     sU    
!->   6F??		
 s    
A%AAc                      [        XU5      nUR                  5         g! [         a/  n[        S[	        U5      [
        R                  U 5         S nAgS nAff = f)Nz)Error executing post-customization scriptFT)r   r   r   ru   rv   r   r   )r}   r   r~   r   postcustr   s         r    r   r     sY    
#MN   7F??		
 s    
A%AAc                     SnSnU R                   (       a  [        R                  S5        Sn[        [        [
        U5      nUR                  5       S:w  a  [        SS[        R                  U 5        U$ SnU$ )NFfalsez=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_scriptr   r   ra   IMC_TOOLS_CONF_GROUPNAME"IMC_TOOLS_CONF_ENABLE_CUST_SCRIPTSlowerru   r   GUESTCUST_ERROR_SCRIPT_DISABLED)r}   r   default_valuecustom_script_enablements       r    r   r   $  s    $M''K	
 / * 
  %%'61;5>>		
 $# $( ##r*   c                 Z   [         R                  S5        [        5       nU R                  n U R                  nU(       d  U(       a  UR                  X4U5        g[         R                  S5         g! [         a/  n[        S[        U5      [        R                  U 5         S nAgS nAff = f)NzApplying password customizationzChanging password is not neededz%Error applying password configurationFT)r   r   r   admin_passwordreset_passwordr   r   ru   rv   r   r   )r}   r   pwdConfigurator	admin_pwd	reset_pwdr   s         r    r   r   =  s    
LL23*,O''I++		%%iFC  LL:;   3F??		
 s   ,A1 A1 1
B*;%B%%B*c                 *    [        [        U 5      5      $ rg   )r   r
   )cfg_files    r    parse_cust_cfgr  R  s    *X&''r*   c                     Su  pU R                   (       a  Sn[        R                  S5        X4$ Sn[        R                  S5        X4$ )N)FFTz!raw cloudinit data cust cfg foundzvmware cust cfg found)r   r   r   )r}   is_vmware_cust_cfgis_raw_data_cust_cfgs      r    get_cust_cfg_typer	  V  sP    /;,#89 55 ",-55r*   c                  r    Sn SnU  H-  n[        X!5      nU(       d  M  [        R                  SU5          g   g)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-toolszlibdeployPkgPlugin.soz$Found the customization plugin at %sTF)search_filer   r   )search_pathscust_pluginr.   cust_plugin_paths       r    is_cust_plugin_availabler  a  sG    L *K&t9LL68H   r*   c                     U (       a  U(       d  g [         R                  " U 5       H,  u  p#nX;   d  M  [         R                  R                  X!5      s  $    g rg   )r-   walkr.   rT   )r   r   root_dirsfiless        r    r  r  v  s@    ( ggg.U77<<// / r*   c                     [         R                  " [        SX5        [        [        R
                  U5        [        X05        g)zK
Set customization status to the underlying VMware Virtualization Platform
z%s: %sN)r   logexcr   r)   r   r<   re   )prefixrU   eventr}   s       r    ru   ru     s-     	KK&0/GGO(#r*   rg   )TFN)   r9   r   )Dloggingr-   rV   r>   r   	cloudinitr   r   r   rL   r   config_custom_scriptr   r	   config_filer
   
config_nicr   config_passwdr   guestcust_errorr   guestcust_eventr   guestcust_stater   	getLogger__name__r   r$   r@   rB   rh   r   r   r!   r)   r7   rJ   ra   re   rj   r   r   ro   rs   r   rx   r   r   r   r   r   rp   r   rr   r   r   rq   r   r   r   r   r  r	  r  r  ru   ri   r*   r    <module>r%     s     	 	   - -  C # ' / / / /			8	$ / + # $& %< "
,
.'T-d%P?D" BF173D0 .C"22"#")6$2*(6*$r*   