
"T`s                 @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l m Z	 d  d l m
 Z
 d  d l m Z d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l m Z d  d l  m! Z! m" Z" m# Z# m$ Z$ m% Z% e	 j& e'  Z( d Z) d Z* d Z+ Gd d   d e j,  Z- Gd d   d e-  Z. d d   Z/ d d d d d  Z0 d d  d d! d"  Z1 d d d d# d$  Z2 d% d&   Z3 d' d(   Z4 d) d*   Z5 d+ d,   Z6 d d- d.  Z7 d/ d0   Z8 d1 d2   Z9 d3 d4   Z: d5 d6   Z; Gd7 d8   d8 e<  Z= e- e j> f f e. e j> e j? f f f Z@ d9 d:   ZA d; d<   ZB d= d>   ZC d? d@   ZD dA dB   ZE dC dD   ZF dE dF   ZG d S)G    N)minidom)dmi)log)safeyaml)sources)subp)util)Config)PreCustomScriptPostCustomScript)
ConfigFile)NicConfigurator)PasswordConfigurator)GuestCustErrorEnum)GuestCustEventEnum)GuestCustStateEnum)enable_nicsget_nics_to_enableset_customization_statusget_tools_configset_gc_statusZ	deployPkgzenable-custom-scriptsz/var/run/vmware-imcc               @   sp   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z	 e
 d d    Z d S)DataSourceOVFZOVFc             C   s   t  j j |  | | |  d  |  _ t j j | j d  |  _ d  |  _ i  |  _	 d |  _
 d |  _ d  |  _ d  |  _ d  |  _ d |  _ d  S)NZovf/file://TF)r   r   )r   
DataSource__init__seedospathjoinseed_direnvironmentcfgsupported_seed_startsvmware_customization_supported_network_config_vmware_nics_to_enable_vmware_cust_conf_vmware_cust_found)selfsys_cfgdistropaths r-   A/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceOVF.pyr   :   s    								zDataSourceOVF.__init__c             C   s#   t  j j |   } d | |  j f S)Nz%s [seed=%s])r   r   __str__r   )r)   rootr-   r-   r.   r/   G   s    zDataSourceOVF.__str__c       -   f   C   s	  g  } i  } d } d } d  } d  } d d i } t  |  j j  \ } }	 t j d  }
 |
 d  k rp t j d  | r t j j	 |  j j |  } t
 |	  \ } } } |	 |  _ | j |  n|
 rd |
 j   k rt j d  |  j s t j d  nd; } d } d  } x4 | D], } t | |  } | rt j d |  PqW| rt |  j  } t j d t j d d d t d d | f  } n t j d  d  } | rDt j j |  } t |  } t |  |  _ t j d |  y t |  j  \ } } } WqQt k
 r@} z! t d | t j | |  j  WYd  d  } ~ XqQXn t j d  | st j |  j d d  rt j d  d  } t  | o|  } | rt! |  j d  t j d  yC t" | |  \ } } } } | r| |  _# n |  j$ j%   |  _# Wn t& j' k
 r@} z! t d | t( j) | |  j  WYd  d  } ~ XnA t* k
 r} z! t d | t j | |  j  WYd  d  } ~ Xn Xd |  _+ | j d   t j, |  t- t. j/ t( j0  t! |  j d!  n| rd |  _1 y,t! |  j d  t2 |  j  \ } } } t3 |  |  _1 |  j j4 } t5 | t j j	 |  j j6 d"   } | oM| } |  j j7 } | r| rd# } |  j j8 rt j d$  d% } t9 t: t; |  } | j   d% k rd& } t j |  t- t. j< t( j=  t> |   t j j	 |  j j? d'  d(  }  WnA t* k
 rH} z! t d) | t j | |  j  WYd  d  } ~ Xn X| r| ry t@ | |  }! |! jA   WnA t* k
 r} z! t d* | t j | |  j  WYd  d  } ~ Xn Xy2 t j d+  tB |  j d d |  j$ jC  |  _# WnA t* k
 r+} z! t d, | t jD | |  j  WYd  d  } ~ Xn X| rDt j d-  tE   }" |  j jF }# yB |  j jG }$ |# so|$ r|" jH |# |$ |  j$  n t j d.  WnA t* k
 r} z! t d/ | t j | |  j  WYd  d  } ~ Xn X| rDy  tI | | |   }% |% jA   WnA t* k
 rC} z! t d0 | t j | |  j  WYd  d  } ~ Xn X| ry& tJ | t j j	 |  j j6 d"   WnA t* k
 r} z! t d1 | t j | |  j  WYd  d  } ~ Xn Xd |  _+ | j d   t j, t j j |   tK |  j1  t- t. j/ t( j0  t! |  j d!  nv d2 tL f d3 tM f g }& d  }' x$ |& D] \ }' }( |(   }	 |	 r;Pq;W|	 rt
 |	  \ } } } |	 |  _ | j |'  tN |  d4 k rd5 Sd6 | k r_	| d6 r_	| d6 }) d5 }* x' |  jO D] }+ |) jP |+  r|+ }* PqW|* s	t j d7 |) |   d5 St jQ |) d8 d  \ }, } } t j d9 |)  t jR | |, g  } | j |)  t jR | | g  } d: j	 |  |  _S | |  _T | |  _U | |  _V | |  _W d S)<N zinstance-idz	iid-dsovfzsystem-product-namezNo system-product-name foundvmwarez$VMware Virtualization Platform foundz3Skipping the check for VMware Customization support/usr/lib/vmware-tools/usr/lib64/vmware-tools/usr/lib/open-vm-tools/usr/lib64/open-vm-toolszlibdeployPkgPlugin.soz$Found the customization plugin at %sZlogfuncmsgzwaiting for configuration filefuncargszcust.cfgz&Did not find the customization plugin.z,Found VMware Customization Config File at %szFile(s) missing in directoryz-Did not find VMware Customization Config FileZdisable_vmware_customizationTz.Customization for VMware platform is disabled.ZStartedz0Start to load cloud-init meta data and user dataz&Error parsing the cloud-init meta dataz&Error loading cloud-init configurationzvmware-toolsZ
Successfuldatafalsez=Set default value to true due to customization configuration.truez-Custom script is disabled by VM Administratorscriptszper-instancez+Error parsing the customization Config Filez(Error executing pre-customization scriptz#Preparing the Network configurationz%Error preparing Network ConfigurationzApplying password customizationzChanging password is not neededz%Error applying Password Configurationz)Error executing post-customization scriptzError creating marker fileszcom.vmware.guestInfoZisor   Fseedfromz Seed from %s not supported by %sZtimeoutzUsing seeded cache data from %s,)r3   r4   r5   r6   )Xget_ovf_envr,   r    r   read_dmi_dataLOGdebugr   r   r   read_ovf_environmentr!   appendlowerr$   search_fileget_max_wait_from_cfgZds_cfgr   Zlog_timewait_for_imc_cfg_filedirnamer   r	   r'   collect_imc_file_pathsFileNotFoundError_raise_error_statusGuestCustEventZ GUESTCUST_EVENT_CUSTOMIZE_FAILEDZget_cfg_option_boolr*   boolr   load_cloudinit_datar%   r+   Zgenerate_fallback_configr   Z	YAMLErrorr   Z!GUESTCUST_ERROR_WRONG_META_FORMAT	Exceptionr(   del_dirr   r   ZGUESTCUST_STATE_DONEZGUESTCUST_ERROR_SUCCESSr&   read_vmware_imcr   Z	marker_idcheck_marker_existsZ	cloud_dirZcustom_script_nameZdefault_run_post_scriptr    CONFGROUPNAME_GUESTCUSTOMIZATION&GUESTCUSTOMIZATION_ENABLE_CUST_SCRIPTSGUESTCUST_STATE_RUNNINGZGUESTCUST_ERROR_SCRIPT_DISABLEDRuntimeErrorZ	get_cpathr
   Zexecuteget_network_config_from_confosfamilyZ$GUESTCUST_EVENT_NETWORK_SETUP_FAILEDr   Zadmin_passwordZreset_password	configurer   setup_marker_filesr   transport_vmware_guestinfotransport_iso9660lenr#   
startswithZread_seededZmergemanydictr   metadataZuserdata_rawZvendordata_rawr"   )-r)   foundmdudZvdZvmwareImcConfigFilePathZnicspathZdefaultsZseedfilecontentssystem_typer   r"   Zsearch_pathsZpluginZdeployPkgPluginPathr   max_waitmd_pathZ
imcdirpathZcfud_patheZuse_raw_datanetworkZproduct_markerZhasmarkerfileZspecial_customizationZcustomscriptZdefValZcustScriptConfigr7   ZccScriptsDirZprecustZpwdConfiguratorZadminpwdZresetpwdZpostcustZnpnameZ	transfuncr>   Z	seedfoundprotoZmd_seedr-   r-   r.   	_get_dataK   s   		 				 		
	!	
			 	
			
	
				zDataSourceOVF._get_datac             C   s7   t  j d  j   } | d k r, d |  j Sd |  j S)Nzsystem-product-namer2   zvmware (%s)zovf (%s))r   rA   rF   r   )r)   rf   r-   r-   r.   _get_subplatform  s    zDataSourceOVF._get_subplatformc             C   s>   d |  j  k r g  S|  j  d } t | t  r3 | S| g Sd  S)Nzpublic-keys)ra   
isinstancelist)r)   Zpksr-   r-   r.   get_public_ssh_keys  s    z!DataSourceOVF.get_public_ssh_keysc             C   s   |  j  S)N)r"   )r)   r-   r-   r.   get_config_obj  s    zDataSourceOVF.get_config_objc             C   s   |  j  S)N)r%   )r)   r-   r-   r.   network_config  s    zDataSourceOVF.network_configN)__name__
__module____qualname__Zdsnamer   r/   rn   ro   rr   rs   propertyrt   r-   r-   r-   r.   r   6   s    6r   c               @   s   e  Z d  Z d d   Z d S)DataSourceOVFNetc             C   sG   t  j |  | | |  t j j | j d  |  _ d |  _ d |  _ d  S)Nzovf-nethttp://https://F)rz   r{   )r   r   r   r   r   r    r#   r$   )r)   r*   r+   r,   r-   r-   r.   r     s    	zDataSourceOVFNet.__init__N)ru   rv   rw   r   r-   r-   r-   r.   ry     s   ry   c             C   s   d } d } | } |  s | Sy t  |  j | |   } Wn% t k
 r_ t j d | |  Yn X| d k r t j d | | |  | } | S)N   Zvmware_cust_file_max_waitzFailed to get '%s', using %sr   z/Invalid value '%s' for '%s', using '%s' instead)intget
ValueErrorrB   warning)r"   Zdefault_max_waitZmax_wait_cfg_optionrg   r-   r-   r.   rH     s    		rH         c             C   sl   d } x_ | | k  rg t  j j | |   } t  j j |  r@ | St j d  t j |  | | 7} q	 Wd  S)Nr   z,Waiting for VMware Customization Config File)r   r   r   isfilerB   rC   timeZsleep)filenameZmaxwaitZnaplendirpathZwaitedZfileFullPathr-   r-   r.   rI     s    rI   TFc             C   s:   t  |  j |  } | j | |  } t | |  j |  j  S)N)r   nicsZgenerateget_network_configZname_serversZdns_suffixes)configZuse_system_devicesr[   rZ   ZnicConfiguratorZnics_cfg_listr-   r-   r.   rY     s
    rY   c             C   sA   |  } | s | r1 | j  d d d | d | i  d d d | i S)NtypeZ
nameserverZaddresssearchversion   r   )rE   )r   Znameserversr   Zconfig_listr-   r-   r.   r     s
    r   c             C   sy   i  } i  } d  } |  j  rL |  j r? |  j  d |  j | d <n |  j  | d <|  j rb |  j | d <d | d <| | | f S)N.zlocal-hostnametimezoneziid-vmware-imczinstance-id)Z	host_nameZdomain_namer   )r   rc   r"   rd   r-   r-   r.   rS     s    			
rS   c       	      C   s   t  |   } i  } i  } d  } d g } d d d d g } x | j   D] \ } } | d k rd d } | | k r} | | | <qF | | k r | | | <qF | d k rF y t j | j    } WqF t k
 r | j   } YqF XqF W| | | f S)NZpasswordr>   zlocal-hostnamezpublic-keyszinstance-idZhostnamez	user-data)get_propertiesitemsbase64Z	b64decodeencoderQ   )	re   propsrc   r"   rd   Z	cfg_propsZmd_propsZpropvalr-   r-   r.   rD     s&    	rD   c             C   s   d } xz | D]r } t  j j |  |  } t  j j |  r y t j |  } | | f SWq t k
 r~ t j t d |  Yq Xq Wd S)	Novf-env.xmlovf_env.xmlOVF_ENV.XMLOVF-ENV.XMLzFailed loading ovf file %sF)r   r   r   r   )NF)	r   r   r   r   r   	load_filerQ   ZlogexcrB   )rJ   Z	env_namesfnameZfull_fnre   r-   r-   r.   r@     s    r@   c             C   s   |  s
 d St  |  t  s) t d |    t j j |   }  |  j d  r] |  j d  d
 }  |  j d  r |  j d  d }  n |  j	 d  d k r d S|  s d Sd } t j
 j d |  } t j |  } | j |   d	 k	 S)zTest if devname matches known list of devices which may contain iso9660
       filesystems.

    Be helpful in accepting either knames (with no leading /dev/) or full path
    names, but do not allow paths outside of /dev/, like /dev/foo/bar/xxx.
    Fz Unexpected input for devname: %sz/dev/r   r   r   z^(sr[0-9]+|hd[a-z]|xvd.*)ZCLOUD_INIT_CDROM_DEV_REGEXNr   )rp   strr   r   r   normpathr`   	partitionsplitcountenvironr~   recompilematch)ZdevnameZdefault_regexZdevname_regexZcdmatchr-   r-   r.   maybe_cdrom_device  s"    r   c       
      C   s6  t  j   } xt | j   D]f \ } } | d } | d k rD |  rD q t |  sS q | d } t |  \ } } | d k	 r | Sq W|  r d } n d  } d d   t  j | r d | n d   D }	 xk |	 D]c } y" t  j | t d | \ } } Wn( t  j k
 rt j	 d	 |  w Yn X| d k	 r | Sq Wd  S)
NfstypeZiso9660Z
mountpointFc             S   s"   g  |  ] } t  |  r |  q Sr-   )r   ).0devr-   r-   r.   
<listcomp>R  s   	 z%transport_iso9660.<locals>.<listcomp>zTYPE=%smtypez%s not mountable as iso9660)
r   mountsr   r   r@   Zfind_devs_withZmount_cbZMountFailedErrorrB   rC   )
Zrequire_isor   r   infor   ZmpZ_fnamere   r   Zdevsr-   r-   r.   r^   =  s2    

		#"r^   c              C   s   d }  d  } t  j |   s | S|  d g } y6 t  j  |  \ } } | rM | St j d | |  WnZ t  j k
 r } z7 | j d k r t j d |  | j  t j |  WYd  d  } ~ Xn X| S)Nzvmware-rpctoolzinfo-get guestinfo.ovfEnvz%cmd %s exited 0 with empty stdout: %sr   z%s exited with code %d)r   ZwhichrB   rC   ZProcessExecutionErrorZ	exit_coder   )ZrpctoolZ	not_foundcmdoutZ_errrj   r-   r-   r.   r]   b  s     r]   c             C   sG   g  } |  j    s | Sx* |  j D] } | |  r  | j |  q  W| S)N)hasChildNodesZ
childNodesrE   )ZnodeZfilter_funcZretZchildr-   r-   r.   
find_childt  s    r   c       	      C   s   t  j |   } | j j d k r- t d   | j j   sH t d   d } t | j d d    } t |  d k r t d   i  } t | d d	 d    } xH | D]@ } | j j	 | d
  j
 } | j j	 | d  j
 } | | | <q W| S)NZEnvironmentzNo Environment NodezNo Child Nodesz)http://schemas.dmtf.org/ovf/environment/1c             S   s   |  j  d k S)NZPropertySection)	localName)nr-   r-   r.   <lambda>  s    z get_properties.<locals>.<lambda>r   zNo 'PropertySection'sc             S   s   |  j  d k S)NZProperty)r   )r   r-   r-   r.   r     s    keyvalue)r   ZparseStringZdocumentElementr   XmlErrorr   r   r_   Z
attributesZgetNamedItemNSr   )	re   ZdomZenvNsURIZpropSectionsr   Z	propElemselemr   r   r-   r-   r.   r   ~  s$    	
r   c             C   sX   |  s | r d  Sx? t  j |   D]. \ } } } | | k r" t  j j | |  Sq" Wd  S)N)r   walkr   r   )r   r   r0   Z_dirsfilesr-   r-   r.   rG     s    rG   c               @   s   e  Z d  Z d S)r   N)ru   rv   rw   r-   r-   r-   r.   r     s   r   c             C   s   t  j |  t  S)N)r   Zlist_from_dependsdatasources)Zdependsr-   r-   r.   get_datasource_list  s    r   c             C   sA   |  s
 d St  j j | d |  d  } t  j j |  r= d Sd S)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.
    Fz.markerfile-z.txtT)r   r   r   exists)markerid
marker_dir
markerfiler-   r-   r.   rT     s    	rT   c             C   s   t  j d  t j j | d |  d  } xB t j |  D]1 } | j d  r: t j t j j | |   q: Wt	 | d  j
   d S)a"  
    Create a new marker file.
    Marker files are unique to a full customization workflow in VMware
    environment.
    @param markerid: is an unique string representing a particular product
                     marker.
    @param: marker_dir: The directory in which markers exist.

    zHandle marker creationz.markerfile-z.txtz.markerfilewN)rB   rC   r   r   r   listdirr`   r   Zdel_fileopenclose)r   r   r   r   r-   r-   r.   r\     s    
 r\   c             C   sS   t  j d |  |  t t j |  t | |   t j t j	 j
 |   |  d S)z
    Raise error and send customization status to the underlying VMware
    Virtualization Platform. Also, cleanup the imc directory.
    z%s: %sN)rB   rC   r   r   rW   r   r   rR   r   r   rJ   )prefixerrorZeventZconfig_fileconfr-   r-   r.   rM     s    rM   c             C   s   t  j d |  |  i  } d } d } t t j |    } d | k rP | d } | rq t j |  j d d  } | | i  | f S)z
    Load the cloud-init meta data, user data, cfg and network from the
    given files

    @return: 4-tuple of configuration
        metadata, userdata, cfg={}, network

    @raises: FileNotFoundError if md_path or ud_path are absent
    z+load meta data from: %s: user data from: %sNrk   r1   )rB   rC   safeload_yaml_or_dictr   r   replace)rh   ri   rc   rd   rk   r-   r-   r.   rP     s    
	

rP   c             C   s   |  s
 i  St  j |   S)z
    The meta data could be JSON or YAML. Since YAML is a strict superset of
    JSON, we will unmarshal the data as YAML. If data is None then a new
    dictionary is returned.
    )r   load)r:   r-   r-   r.   r     s    r   c             C   s   d } d } d } |  j  } | r t j j t |  } t j j |  sX t d |   |  j } | r t j j t |  } t j j |  s t d |   n= t j j t d  } t j j |  s t j	 d |  d } | | | f S)a\  
    collect all the other imc files.

    metadata is preferred to nics.txt configuration data.

    If metadata file exists because it is specified in customization
    configuration, then metadata is required and userdata is optional.

    @return a 3-tuple containing desired configuration file paths if present
        Expected returns:
             1. user provided metadata and userdata (md_path, ud_path, None)
             2. user provided metadata (md_path, None, None)
             3. user-provided network config (None, None, nics_path)
             4. No config found (None, None, None)
    Nzmeta data file is not found: %szuser data file is not found: %sznics.txtz%s does not exist.)
Zmeta_data_namer   r   r   VMWARE_IMC_DIRr   rL   Zuser_data_namerB   rC   )Z	cust_confrh   ri   Z	nics_pathZmd_fileZud_filer-   r-   r.   rK     s(    	
	rK   )Hr   r   r   r   Zxml.domr   Z	cloudinitr   r   Zloggingr   r   r   r   Z+cloudinit.sources.helpers.vmware.imc.configr	   Z9cloudinit.sources.helpers.vmware.imc.config_custom_scriptr
   r   Z0cloudinit.sources.helpers.vmware.imc.config_filer   Z/cloudinit.sources.helpers.vmware.imc.config_nicr   Z2cloudinit.sources.helpers.vmware.imc.config_passwdr   Z4cloudinit.sources.helpers.vmware.imc.guestcust_errorr   Z4cloudinit.sources.helpers.vmware.imc.guestcust_eventr   rN   Z4cloudinit.sources.helpers.vmware.imc.guestcust_stater   Z3cloudinit.sources.helpers.vmware.imc.guestcust_utilr   r   r   r   r   Z	getLoggerru   rB   rU   rV   r   r   r   ry   rH   rI   rY   r   rS   rD   r@   r   r^   r]   r   r   rG   rQ   r   ZDEP_FILESYSTEMZDEP_NETWORKr   r   rT   r\   rM   rP   r   rK   r-   r-   r-   r.   <module>   sf   ( e	)%
