܍~c78dZddlZddlZddlZddlmZddlmZmZm Z ddl m Z ej e ZdZGddZejejfZGd d ejZd Zd Z ddZdZdZdZeejffgZdZe dkrNddlZej dZ!e!"Z#eZ$e%e j&e$dSdS)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)log)sourcessubputil) openstackz 9796-932Ec"eZdZdZdZdZdZdZdS) 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 ls+55%E"#F GGGrr cXeZdZdZdZfdZfdZdZdZdZ e dZ xZ S) DataSourceIBMCloudIBMCloudNctt||||d|_d|_d|_d|_dSN)superr__init__source_network_config network_jsonplatform)selfsys_cfgdistropaths __class__s rrzDataSourceIBMCloud.__init__sH  $''00&%HHH #  rctt|}|d|jd|jd}|S)Nz [ ])rr__str__rr)r rootmstrr$s rr(zDataSourceIBMCloud.__str__s@'..6688#ttT]]]DKKK@ rct}|dS|d|_|d|_|d|_|d|_|d|_|d}||_|d|_ tj ||_ n9#t$r,}td |d|_ Yd}~nd}~wwxYwd S) 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)) ?5h'  +  + #KK 33#KK 66 [[ & &!"=1 '")"rBrEpropertyrN __classcell__)r$s@rrrzs FK *888777 $ $X $ $ $ $ $rrcd}tj|sdStj|S)Nz/sys/hypervisor/uuid)ospathisfiler load_filestriplower) uuid_paths rrDrDsJ&I 7>>) $ $t >) $ $ * * , , 2 2 4 44rc@tjdS)Nz /proc/xen)rSrTexistsrrr_is_xenr\s 7>>+ & &&r#/root/provisioningConfiguration.cfg/root/swinstall.log/proc/1/environctj|rd|z}d}tj|rstj|rEtj|jtj|jk}|d|d|rdnddz }n|d|d|d z }d }n|d |zz }nd d |z}}t 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_provisioningrjs  w~~hG#h. 7>>( # # 9w~~h'' GH%%.1B1B1KKHH!'7IIZ77 HHHH -8 8CC:XEII'555 Mrcd}d}d}ts|Si} tj}n8#tj$r&}t d|Yd}~dSd}~wwxYwt|D]}||}| dd }| dd } |||fvra||vr$t d ||||||kr*| tkrt d ||| ||||<| |i d } | |i d } | rtj| fS| r*trtj| fStj| fStrtjdfS|S) 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 rrjrr r) label_mdata label_cfg2 not_foundfslabelsdevsr=devdatalabeluuid metadata_path cfg2_paths rget_ibm_platformrs* KJI 99Hz||  % -q111|||||diikk""Cy"%%++--xx##))++ j1 1 1  H   KK@      J  4?#:#: IIA     LLb1155i@@M Z,,00;;I>!9-- >  ! ! E6== s.A#AA#c t\}}|tddS|tvrtd|dS||t d} t j|rt|}ntj |t}n0#tj $r}td|d|d||d}~wwxYw|||S)aRead 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.Nz!This is not an IBMCloud platform.z/Cloud-init is disabled during provisioning: %s.)rrr0z)Failed reading IBM config disk (platform=z path=z): )rr9rL PROVISIONINGrDrSrTisdirmetadata_from_dirrmount_cbrBrokenMetadata RuntimeErrorupdate)rrTretr;r=s rr1r1&s&''NHd 5666t \ ! ! CXNNNt(**  C  7==   ='--GGmD*;<.opathYsw||K5999rcPtj|dS)Nzutf-8)jsonloadsdecode)blobs rload_json_bytesz*metadata_from_dir..load_json_bytes\sz$++g..///r metadata_rawzmeta_data.jsonr- user_dataNr/zvendor_data.jsonr.znetwork_data.jsonF)rzFailed 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)rz instance-id)rSrTrrrVIOErrorr9rL Exceptionrrr2base64 b64decoder8 TypeError) source_dirrrfilesr;namerTtranslfpathrawr=r}md_rawmdrenamesmdnamenewnames rrrJs:::000 /00/B UU;''. uu/00/B 122OD  EG %tV Z.. @.u555CC @ @ @ II4eQ ? ? ? ? ? ? ? ? @ ;&.DD vc{{   ,,04aa8   {{>""*$ 7* D   GJ ^ $F  B  & 0 1F G GB}  I&   (@1D   G #)) V   .BwK NsH*B B1 B,,B1< C C1C,,C1EF /FF c6tj|tSr)rlist_from_depends datasources)dependss rget_datasource_listrs  $Wk : ::r__main__zQuery IBM Cloud Metadata) description)r]r^r_)'__doc__rrrS cloudinitrloggingrrrcloudinit.sources.helpersr getLoggerr r9rvr rrr DataSourcerrDr\rjrr1rDEP_FILESYSTEMrrargparseArgumentParserparser parse_argsargsr}print json_dumpsrrrrs\\z $$$$$$))))))))))//////g!!, * B$B$B$B$B$+B$B$B$J555''' 3 " @999x!!!HLLLb'023 ;;; zOOO $X $1K L L LF     D 799D E/$/$       r