Current File : //usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyo
�
oB�]c@sqdZddlZddlZddlZddlmZddlmZddlm	Z	ddlm
Z
eje�Z
dZdefd	��YZejejfZd
ejfd��YZd�Zd
�Zdddd�Zd�Zd�Zd�ZeejffgZd�ZedkrmddlZej dd�Z!e!j"�Z#e�Z$e
j%e$�GHndS(s,Datasource for IBMCloud.

IBMCloud is also know as SoftLayer or BlueMix.
IBMCloud hypervisor is xen (2018-03-10).

There are 2 different api exposed launch methods.
 * template: This is the legacy method of launching instances.
   When booting from an image template, the system boots first into
   a "provisioning" mode.  There, host <-> guest mechanisms are utilized
   to execute code in the guest and configure it.  The configuration
   includes configuring the system network and possibly installing
   packages and other software stack.

   After the provisioning is finished, the system reboots.

 * os_code: Essentially "launch by OS Code" (Operating System Code).
   This is a more modern approach.  There is no specific "provisioning" boot.
   Instead, cloud-init does all the customization.  With or without
   user-data provided, an OpenStack ConfigDrive like disk is attached.

   Only disks with label 'config-2' and UUID '9796-932E' are considered.
   This is to avoid this datasource claiming ConfigDrive.  This does
   mean that 1 in 8^16 (~4 billion) Xen ConfigDrive systems will be
   incorrectly identified as IBMCloud.

The combination of these 2 launch methods and with or without user-data
creates 6 boot scenarios.
 A. os_code with user-data
 B. os_code without user-data
    Cloud-init is fully operational in this mode.

    There is a block device attached with label 'config-2'.
    As it differs from OpenStack's config-2, we have to differentiate.
    We do so by requiring the UUID on the filesystem to be "9796-932E".

    This disk will have the following files. Specifically note, there
    is no versioned path to the meta-data, only 'latest':
      openstack/latest/meta_data.json
      openstack/latest/network_data.json
      openstack/latest/user_data [optional]
      openstack/latest/vendor_data.json

    vendor_data.json as of 2018-04 looks like this:
      {"cloud-init":"#!/bin/bash
echo 'root:$6$<snip>' | chpasswd -e"}

    The only difference between A and B in this mode is the presence
    of user_data on the config disk.

 C. template, provisioning boot with user-data
 D. template, provisioning boot without user-data.
    With ds-identify cloud-init is fully disabled in this mode.
    Without ds-identify, cloud-init None datasource will be used.

    This is currently identified by the presence of
    /root/provisioningConfiguration.cfg . That file is placed into the
    system before it is booted.

    The difference between C and D is the presence of the METADATA disk
    as described in E below.  There is no METADATA disk attached unless
    user-data is provided.

 E. template, post-provisioning boot with user-data.
    Cloud-init is fully operational in this mode.

    This is identified by a block device with filesystem label "METADATA".
    The looks similar to a version-1 OpenStack config drive.  It will
    have the following files:

       openstack/latest/user_data
       openstack/latest/meta_data.json
       openstack/content/interfaces
       meta.js

    meta.js contains something similar to user_data.  cloud-init ignores it.
    cloud-init ignores the 'interfaces' style file here.
    In this mode, cloud-init has networking code disabled.  It relies
    on the provisioning boot to have configured networking.

 F. template, post-provisioning boot without user-data.
    With ds-identify, cloud-init will be fully disabled.
    Without ds-identify, cloud-init None datasource will be used.

    There is no information available to identify this scenario.

    The user will be able to ssh in as as root with their public keys that
    have been installed into /root/ssh/.authorized_keys
    during the provisioning stage.

TODO:
 * is uuid (/sys/hypervisor/uuid) stable for life of an instance?
   it seems it is not the same as data's uuid in the os_code case
   but is in the template case.
i����N(tlog(tsources(t	openstack(tutils	9796-932Et	PlatformscBs&eZdZdZdZdZdZRS(sTemplate/Live/MetadatasUNABLE TO BE IDENTIFIED.sTemplate/Provisioning/Metadatas!Template/Provisioning/No-MetadatasOS-Code/Live(t__name__t
__module__tTEMPLATE_LIVE_METADATAtTEMPLATE_LIVE_NODATAtTEMPLATE_PROVISIONING_METADATAtTEMPLATE_PROVISIONING_NODATAtOS_CODE(((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyRms
tDataSourceIBMCloudcBsPeZdZdZd�Zd�Zd�Zd�Zd�Z	e
d��ZRS(tIBMCloudcCsDtt|�j|||�d|_d|_d|_d|_dS(N(tsuperRt__init__tNonetsourcet_network_configtnetwork_jsontplatform(tselftsys_cfgtdistrotpaths((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyRs
			cCs2tt|�j�}d||j|jf}|S(Ns
%s [%s %s](RRt__str__RR(Rtroottmstr((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyR�scCs�t�}|dkrtS|d|_|d|_|d|_|jd�|_|jd�|_|jd�}||_	|d|_
ytj|�|_
Wn,tk
r�}tjd|�d|_
nXtS(	NRRtmetadatatuserdatatnetworkdatat
vendordatassystem-uuids"Invalid content in vendor-data: %s(tread_mdRtFalseRRRtgettuserdata_rawRtvendordata_puretsystem_uuidRtconvert_vendordatatvendordata_rawt
ValueErrortLOGtwarningtTrue(Rtresultstvdte((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyt	_get_data�s"	


	

cCsd|j|jfS(s/Return the subplatform metadata source details.s%s (%s)(RR(R((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyt_get_subplatform�scCs#|jdkrtS|jt�kS(squickly (local check only) if self.instance_id is still valid

        in Template mode, the system uuid (/sys/hypervisor/uuid) is the
        same as found in the METADATA disk.  But that is not true in OS_CODE
        mode.  So we read the system_uuid and keep that for later compare.N(R%RR!t_read_system_uuid(RR((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pytcheck_instance_id�scCs�|jtjkr$idd6dd6S|jdkr}|jdk	rmtjd�tj	|jdd�|_q}tjd�n|jS(Ntdisabledtconfigitversions(network config provided via network_jsont
known_macss#no network configuration available.(
RRRRRRR)tdebugRtconvert_net_json(R((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pytnetwork_config�s
N(RRtdsnameRR%RRR/R0R2tpropertyR9(((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyRzs					
cCs5d}tjj|�sdStj|�j�j�S(Ns/sys/hypervisor/uuid(tostpathtisfileRRt	load_filetstriptlower(t	uuid_path((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyR1�scCstjjd�S(Ns	/proc/xen(R<R=texists(((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyt_is_xen�ss#/root/provisioningConfiguration.cfgs/root/swinstall.logs/proc/1/environcCs�tjj|�r�d|}t}tjj|�r�tjj|�r�tj|�jtj|�jk}|d||rdndf7}q�|d||f7}t}q�|d|7}ntd|}}tjd||�|S(	s@Return boolean indicating if this boot is ibm provisioning boot.sconfig '%s' exists.s log '%s' from %s boot.tcurrenttpreviouss. log '%s' existed, but no reference file '%s'.s log '%s' did not exist.sconfig '%s' did not exist.sibm_provisioning=%s: %s(	R<R=RCR+tstattst_mtimeR!R)R7(tprov_cfgtinst_logtboot_reftmsgtresult((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyt_is_ibm_provisioning�s 
	cCs�d}d}d}t�s|Si}ytj�}Wn'tjk
r^}tjd|�dSXx�t|j��D]�}||}|j	dd�j
�}|j	dd�j
�}	|||fkr�qrn||kr�tjd||||�qrn||kr-|	tkr-tjd|||	|�qrn|||<qrW|j	|i�j	d	�}
|j	|i�j	d	�}|r�t
j|fS|
r�t�r�t
j|
fSt
j|
fSnt�r�t
jd
fS|S(
s�Return a tuple (Platform, path)

    If this is Not IBM cloud, then the return value is (None, None).
    An instance in provisioning mode is considered running on IBM cloud.tMETADATAsCONFIG-2sFailed to run blkid: %stLABELttUUIDs.Duplicate fslabel '%s'. existing=%s current=%ss/Skipping %s with LABEL=%s due to uuid != %s: %stDEVNAMEN(NN(NN(RRDRtblkidtProcessExecutionErrorR)R*tsortedtkeysR"tuppertIBM_CONFIG_UUIDR7RRRNR	RR
(tlabel_mdatat
label_cfg2t	not_foundtfslabelstdevsR.tdevtdatatlabeltuuidt
metadata_patht	cfg2_path((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pytget_ibm_platform�sJ	
		
	
	
cCs�t�\}}|dkr,tjd�dS|tkrLtjd|�dSi|d6|d6t�d6}y7tjj|�r�t	|�}nt
j|t	�}Wn/tj
k
r�}td|||f��nX|j|�|S(sRead data from IBM Cloud.

    @return: None if not running on IBM Cloud.
             dictionary with guaranteed fields: metadata, version
             and optional fields: userdata, vendordata, networkdata.
             Also includes the system uuid from /sys/hypervisor/uuid.s!This is not an IBMCloud platform.s/Cloud-init is disabled during provisioning: %s.RRssystem-uuids8Failed reading IBM config disk (platform=%s path=%s): %sN(ReRR)R7tPROVISIONINGR1R<R=tisdirtmetadata_from_dirRtmount_cbRtBrokenMetadatatRuntimeErrortupdate(RR=tretR,R.((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyR s(
	

cCs0d�}d�}d|d�|fd|d�dfd|d�|fd	|d
�|fg}i}x�|D]�\}}}tjj||�}d}	ytj|dt�}	Wn&tk
r�}
tj	d||
�nX|	dks�|dkr�|	}n?y||	�}Wn,t
k
r8}
tjd
||
f��nX|||<qmW|j
d�dkrrtjd|��ni|d<|d}|d}
d|kr�ytj|d�|
d<Wq�ttfk
r�}
tjd|
��q�Xndddf}x1|D])\}}||kr�|||
|<q�q�W|S(s�Walk source_dir extracting standardized metadata.

    Certain metadata keys are renamed to present a standardized set of metadata
    keys.

    This function has a lot in common with ConfigDriveReader.read_v2 but
    there are a number of inconsistencies, such key renames and as only
    presenting a 'latest' version which make it an unlikely candidate to share
    code.

    @return: Dict containing translated metadata, userdata, vendordata,
        networkdata as present.
    cSstjjdd|�S(NRtlatest(R<R=tjoin(tfname((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pytopathFscSstj|jd��S(Nsutf-8(tjsontloadstdecode(tblob((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pytload_json_bytesIstmetadata_rawsmeta_data.jsonRt	user_dataRsvendor_data.jsonRsnetwork_data.jsonRtsFailed reading path '%s': %ssFailed decoding %s: %ss)%s missing required file 'meta_data.json'Rtrandom_seeds.Badly formatted metadata random_seed entry: %stpublic_keysspublic-keysthostnameslocal-hostnameRbsinstance-idN(Rzspublic-keys(R{slocal-hostname(suuidsinstance-id(RR<R=RoRR?R!tIOErrorR)R7t	ExceptionRRjR"tbase64t	b64decodeR(t	TypeError(t
source_dirRqRvtfilesR,tnameR=ttransltfpathtrawR.R`tmd_rawtmdtrenamestmdnametnewname((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyRh7sP			


	cCstj|t�S(N(Rtlist_from_dependstdatasources(tdepends((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pytget_datasource_list�st__main__tdescriptionsQuery IBM Cloud Metadata(&t__doc__R~RrR<t	cloudinitRtloggingRtcloudinit.sources.helpersRRt	getLoggerRR)RYtobjectRR	R
Rft
DataSourceRR1RDRNReR RhtDEP_FILESYSTEMR�R�targparsetArgumentParsertparsert
parse_argstargsR`t
json_dumps(((sH/usr/lib/python2.7/site-packages/cloudinit/sources/DataSourceIBMCloud.pyt<module>^s:	D			3	!	L