f!8dZddlZddlZddlZddlZddlmZmZmZm Z ddl m Z eje ZdZGddZej"ej$fZGdd ej(Zd Zd Z dd Zd ZdZdZeej8ffgZdZe dk(rGddlZej@dZ!e!jEZ#eZ$e%ejLe$yy)a,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$' | 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. N) atomic_helpersourcessubputil) openstackz 9796-932Ec eZdZdZdZdZdZdZy) PlatformszTemplate/Live/MetadatazUNABLE TO BE IDENTIFIED.zTemplate/Provisioning/Metadataz!Template/Provisioning/No-Metadataz OS-Code/LiveN)__name__ __module__ __qualname__TEMPLATE_LIVE_METADATATEMPLATE_LIVE_NODATATEMPLATE_PROVISIONING_METADATATEMPLATE_PROVISIONING_NODATAOS_CODEF/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceIBMCloud.pyr r ls55%E"#F Grr cReZdZdZdZfdZfdZdZdZdZ e dZ xZ S) DataSourceIBMCloudIBMCloudNchtt| |||d|_d|_d|_d|_yN)superr__init__source_network_config network_jsonplatform)selfsys_cfgdistropaths __class__s rrzDataSourceIBMCloud.__init__s5  $0&%H #  rcjtt| }|d|jd|jd}|S)Nz [ ])rr__str__rr)r rootmstrr$s rr(zDataSourceIBMCloud.__str__s,'68#T]]DKK@ rct}|y|d|_|d|_|d|_|j d|_|j d|_|j d}||_|d|_ tj||_ y #t$r'}tjd |d|_ Yd}~y d}~wwxYw) NFrrmetadatauserdata networkdata vendordata system-uuidz"Invalid content in vendor-data: %sT)read_mdrrr,get userdata_rawrvendordata_pure system_uuidrconvert_vendordatavendordata_raw ValueErrorLOGwarning)r resultsvdes r _get_datazDataSourceIBMCloud._get_datas) ?h'  +  + #KK 3#KK 6 [[ &!"=1 '")"<"rBrEpropertyrN __classcell__)r$s@rrrzs: FK *87 $ $rrcd}tjj|sytj|j j S)Nz/sys/hypervisor/uuid)ospathisfilerload_text_filestriplower) uuid_paths rrDrDs=&I 77>>) $   y ) / / 1 7 7 99rc@tjjdS)Nz /proc/xen)rSrTexistsrrr_is_xenr\s 77>>+ &&rctjj|rd|z}d}tjj|r~tjj|rPtj|jtj|jkD}|d|d|rdnddz }n|d|d|d z }d }n|d |zz }nd d |z}}t j d |||S)z@Return boolean indicating if this boot is ibm provisioning boot.zconfig '%s' exists.Tz log 'z' from currentpreviousz boot.z"' existed, but no reference file 'z'.Fz log '%s' did not exist.zconfig '%s' did not exist.zibm_provisioning=%s: %s)rSrTr[statst_mtimer9rL)prov_cfginst_logboot_refmsgresults r_is_ibm_provisioningrgs  ww~~h#h. 77>>( #ww~~h'GGH%..1B1K1KK!'IZ7  -8 8C:XEII'5 Mrcvd}d}d}ts|Si} tj}t|jD]}||}|jddj}|jddj} |||fvrO||vrt j d ||||o||k(r#| tk7rt jd ||| ||||<|j|ijd } |j|ijd } | rtj| fS| r.trtj | fStj"| fStrtj$dfS|S#tj$r }t j d|Yd}~yd}~wwxYw) zReturn 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.METADATAzCONFIG-2)NNzFailed to run blkid: %sNLABELUUIDz.Duplicate fslabel '%s'. existing=%s current=%sz/Skipping %s with LABEL=%s due to uuid != %s: %sDEVNAME)r\rblkidrProcessExecutionErrorr9r:sortedkeysr2upperIBM_CONFIG_UUIDrLr rrgrr r) label_mdata label_cfg2 not_foundfslabelsdevsr=devdatalabeluuid metadata_path cfg2_paths rget_ibm_platformrs KJI 9Hzz| diik"Cy"%++-xx#))+ j1 1  H  KK@    J 4?#: IIA   /2LLb155i@M Z,00;I!!9--   !<.opathYsww||K599rcJtj|jdS)Nzutf-8)jsonloadsdecode)blobs rload_json_bytesz*metadata_from_dir..load_json_bytes\szz$++g.//r metadata_rawzmeta_data.jsonr- user_dataNr/zvendor_data.jsonr.znetwork_data.jsonzFailed reading path '%s': %szFailed decoding z: z)%s missing required file 'meta_data.json'r, random_seedz.Badly formatted metadata random_seed entry: %s)) public_keysz public-keys)hostnamezlocal-hostname)r|z instance-id)rSrTrrload_binary_fileIOErrorr9rL Exceptionrrr2base64 b64decoder8 TypeError) source_dirrrfilesr;namerTtranslfpathrawr=rzmd_rawmdrenamesmdnamenewnames rrrJs:0 /0/B U;'. u/0/B 12OD  EG %tV Z. @''.C ;&.D c{  %({{>"*$$ 7* D  GJ ^ $F  B  & 0 0 1F GB}  G #) V  .BwK) NS @ II4eQ ? ? @ ,,04a8 &I& ((@1D  sHC><D+E> D(D##D(+ E4EEF&E>>Fc6tj|tSr)rlist_from_depends datasources)dependss rget_datasource_listrs  $ $Wk ::r__main__zQuery IBM Cloud Metadata) description)z#/root/provisioningConfiguration.cfgz/root/swinstall.logz/proc/1/environ)'__doc__rrloggingrS cloudinitrrrrcloudinit.sources.helpersr getLoggerr r9rsr rrr DataSourcerrDr\rgrr1rDEP_FILESYSTEMrrargparseArgumentParserparser parse_argsargsrzprint json_dumpsrrrrs\z  88/g!,, ** B$++B$J:' 3 " @9x!HLb'0023 ; z $X $ $1K LF    D 9D "- " "4 () r