3 \me @sdZddlZddlZddlZddlZddlmZmZmZm Z ej e Z dZ ddgZiddid Zd d ZGd d d ejZddZeejejffgZddZdS)zb This file contains code used to gather the user data passed to an instance on RHEVm and vSphere. N)dmisourcessubputilz/etc/sysconfig/cloud-infoZmodprobeZfloppyiZ localhost)zblock-device-mappingz instance-idzlocal-hostnameZ placementcCsp|d}|d}ytj|j}WnHtk rjytj|j}Wn tk rdtjtddSXYnX|S)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_filestripIOErrorlogexcLOG)Z mount_dirZdeltacloud_user_data_fileZuser_data_fileZ user_datar (/usr/lib/python3.6/DataSourceAltCloud.pyread_user_data_callback&s  r c@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ dS)DataSourceAltCloudZAltCloudcCs"tjj||||d|_d|_dS)N/file://)rr)r DataSource__init__seedZsupported_seed_starts)selfZsys_cfgZdistropathsr r r rLszDataSourceAltCloud.__init__cCstjj|}d||jfS)Nz %s [seed=%s])rr__str__r)rrootr r r rQs zDataSourceAltCloud.__str__c CstjjtrJytjtjj}Wn"tk rDtj t dtdSX|St j d}|s\dS|j}|j drrdS|j drdSdS)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.ZUNKNOWNzsystem-product-nameRHEVZVMWAREVSPHERE)ospathexistsCLOUD_INFO_FILErrrupperrr r rZ read_dmi_data startswith)r cloud_typeZ system_nameZsys_namer r r get_cloud_typeUs&    z!DataSourceAltCloud.get_cloud_typecCsbtjd|j}tjdt|d|kr8|jrRdSnd|krN|jrRdSndStjtddS)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. zInvoked get_data()zcloud_type: %srTrFzFailed accessing user data.)r debugr!struser_data_rhevmuser_data_vsphererr )rr r r r _get_data~s  zDataSourceAltCloud._get_datacCs:|j}t|dstj|_|dkr(d|_d|j|jfS)z(Return the subplatform metadata details.sourcerz/dev/fd0z%s (%s))r!hasattrrZMETADATA_UNKNOWNr'lower)rr r r r _get_subplatforms  z#DataSourceAltCloud._get_subplatformc,Csd}y tWn0tjk r>}ztjtd|dSd}~XnXd}ytj|ddWn4tjtfk r}ztjtd|dSd}~XnXytj|t }WnRtk r}z|j t j krWYdd}~Xn$tj k rtjtd|YnX||_ t|_|rd SdSdS) a RHEVM specific userdata read If on RHEV-M the user data will be contained on the floppy device in file To access it: modprobe floppy Leverage util.mount_cb to: mkdir mount /dev/fd0 The call back passed to util.mount_cb will do: read / NzFailed modprobe: %sFz/dev/fd0)rZtimeoutzFailed udevadm_settle: %s z-Failed to mount %s when looking for user dataT)modprobe_floppyrZProcessExecutionErrorrr r Zudevadm_settleOSErrormount_cbr errnoENOENTMountFailedError userdata_rawMETA_DATA_NOT_SUPPORTEDmetadata)r return_streZ floppy_deverrr r r r$s6   z"DataSourceAltCloud.user_data_rhevmcCsd}tjd}x||D]t}ytj|t}|r2||_PWqtk rd}z|jtjkrTWYdd}~Xqtjk rtj t d|YqXqW||_ t |_ |rdSdSdS)a vSphere specific userdata read If on vSphere the user data will be contained on the cdrom device in file To access it: Leverage util.mount_cb to: mkdir mount /dev/fd0 The call back passed to util.mount_cb will do: read / Nz LABEL=CDROMz-Failed to mount %s when looking for user dataTF)rZfind_devs_withr.r r'r-r/r0r1r r r2r3r4)rr5Z cdrom_listZ cdrom_devr7r r r r%s*    z$DataSourceAltCloud.user_data_vsphereN) __name__ __module__ __qualname__Zdsnamerrr!r&r*r$r%r r r r rHs)' 6rcCs&tjt\}}tjddjt|dS)NzCommand: %s Output%s )rCMD_PROBE_FLOPPYr r"join)outZ_errr r r r, sr,cCs tj|tS)N)rZlist_from_depends datasources)Zdependsr r r get_datasource_listsr@)__doc__r/ZloggingrZos.pathZ cloudinitrrrrZ getLoggerr8r rr<r3r rrr,ZDEP_FILESYSTEMZ DEP_NETWORKr?r@r r r r  s$ "F