o
    f!                     @   s   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 e
eZdZddgZi ddi d	Zd
d ZG dd dejZdd ZeejejffgZdd ZdS )zb
This file contains code used to gather the user data passed to an
instance on RHEVm and vSphere.
    N)dmisourcessubputilz/etc/sysconfig/cloud-infomodprobefloppyi  	localhost)zblock-device-mappingzinstance-idzlocal-hostname	placementc                 C   sn   | d }| d }z
t | }W |S  ty6   zt | }W Y |S  ty5   t td Y Y dS w w )an  
    Description:
        This callback will be applied by util.mount_cb() on the mounted
        file.

        Deltacloud file name contains deltacloud. Those not using
        Deltacloud but instead instrumenting the injection, could
        drop deltacloud from the file name.

    Input:
        mount_dir - Mount directory

    Returns:
        User Data

    z/deltacloud-user-data.txtz/user-data.txtz Failed accessing user data file.N)r   load_text_filestripIOErrorlogexcLOG)	mount_dirdeltacloud_user_data_fileuser_data_file	user_data r   F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceAltCloud.pyread_user_data_callback&   s   r   c                       sb   e Zd ZdZdd Zdeddf fddZd	d
 Zdd Zdd Z	dd Z
dd Zdd Z  ZS )DataSourceAltCloudAltCloudc                 C   s*   t j| ||| d | _d| _t j| _d S )N)/zfile://)r   
DataSource__init__seedsupported_seed_startsMETADATA_UNKNOWNsource)selfsys_cfgdistropathsr   r   r   r   L   s   zDataSourceAltCloud.__init__ci_pkl_versionreturnNc                    s&   t  | t| dstj| _d S d S )Nr   )super	_unpicklehasattrr   r   r   )r   r#   	__class__r   r   r&   R   s   
zDataSourceAltCloud._unpicklec                 C   s   t j| }d|| jf S )Nz%s [seed=%s])r   r   __str__r   )r   rootr   r   r   r*   W   s   zDataSourceAltCloud.__str__c                 C   s   t jtr$ztt  }W |S  ty#   t	t
dt Y dS w td}|s-dS | }|dr8dS |dr?dS dS )at  
        Description:
            Get the type for the cloud back end this instance is running on
            by examining the string returned by reading either:
                CLOUD_INFO_FILE or
                the dmi data.

        Input:
            None

        Returns:
            One of the following strings:
            'RHEV', 'VSPHERE' or 'UNKNOWN'

        z'Unable to access cloud info file at %s.UNKNOWNzsystem-product-nameRHEVVMWAREVSPHERE)ospathexistsCLOUD_INFO_FILEr   r
   r   upperr   r   r   r   read_dmi_data
startswith)r   
cloud_typesystem_namesys_namer   r   r   get_cloud_type[   s,   



z!DataSourceAltCloud.get_cloud_typec                 C   sb   t d |  }t dt| d|v r|  rdS nd|v r'|  r&dS ndS tt d dS )a  
        Description:
            User Data is passed to the launching instance which
            is used to perform instance configuration.

            Cloud providers expose the user data differently.
            It is necessary to determine which cloud provider
            the current instance is running on to determine
            how to access the user data. Images built with
            image factory will contain a CLOUD_INFO_FILE which
            contains a string identifying the cloud provider.

            Images not built with Imagefactory will try to
            determine what the cloud provider is based on system
            information.
        zInvoked get_data()zcloud_type: %sr-   Tr/   FzFailed accessing user data.)r   debugr:   struser_data_rhevmuser_data_vspherer   r   r   r7   r   r   r   	_get_data   s   
zDataSourceAltCloud._get_datac                 C   s(   |   }|dkrd| _d| | jf S )z(Return the subplatform metadata details.r-   /dev/fd0z%s (%s))r:   r   lowerr?   r   r   r   _get_subplatform   s   z#DataSourceAltCloud._get_subplatformc              
   C   s  d}zt   W n tjy" } zttd| W Y d}~dS d}~ww d}z	tj|dd W n tjtfyK } zttd| W Y d}~dS d}~ww zt|t	}W n) tym } z|j
t
jkrc W Y d}~nd}~w tjy}   ttd| Y nw || _t| _|rd	S dS )
a  
        RHEVM specific userdata read

         If on RHEV-M the user data will be contained on the
         floppy device in file <user_data_file>
         To access it:
           modprobe floppy

           Leverage util.mount_cb to:
               mkdir <tmp mount dir>
               mount /dev/fd0 <tmp mount dir>
               The call back passed to util.mount_cb will do:
                   read <tmp mount dir>/<user_data_file>
        NzFailed modprobe: %sFrA      )r2   timeoutzFailed udevadm_settle: %s
-Failed to mount %s when looking for user dataT)modprobe_floppyr   ProcessExecutionErrorr   r   r   udevadm_settleOSErrormount_cbr   errnoENOENTMountFailedErroruserdata_rawMETA_DATA_NOT_SUPPORTEDmetadata)r   
return_stre
floppy_deverrr   r   r   r=      sF   
z"DataSourceAltCloud.user_data_rhevmc                 C   s   d}t d}|D ];}zt |t}|r|| _W  n+W q	 ty4 } z|jtjkr* W Y d}~q	d}~w t jyD   t 	t
d| Y q	w || _t| _|rOdS dS )a  
        vSphere specific userdata read

        If on vSphere the user data will be contained on the
        cdrom device in file <user_data_file>
        To access it:
           Leverage util.mount_cb to:
               mkdir <tmp mount dir>
               mount /dev/fd0 <tmp mount dir>
               The call back passed to util.mount_cb will do:
                   read <tmp mount dir>/<user_data_file>
        NzLABEL=CDROMrF   TF)r   find_devs_withrK   r   r   rJ   rL   rM   rN   r   r   rO   rP   rQ   )r   rR   
cdrom_list	cdrom_devrU   r   r   r   r>      s4   
z$DataSourceAltCloud.user_data_vsphere)__name__
__module____qualname__dsnamer   intr&   r*   r:   r@   rC   r=   r>   __classcell__r   r   r(   r   r   H   s    +'6r   c                  C   s&   t  t\} }tddt|  d S )NzCommand: %s
Output%s )r   CMD_PROBE_FLOPPYr   r;   join)out_errr   r   r   rG     s   rG   c                 C   s   t | tS )N)r   list_from_dependsdatasources)dependsr   r   r   get_datasource_list"  s   rg   )__doc__rL   loggingr0   os.path	cloudinitr   r   r   r   	getLoggerrY   r   r3   r`   rP   r   r   r   rG   DEP_FILESYSTEMDEP_NETWORKre   rg   r   r   r   r   <module>   s*   	
" L
