x[h:@dZddlZddlZddlZddlZddlmZmZmZm Z m Z ddl m Z m Z mZmZddlmZej$eZdZGddZej.ej0fZGd d e j4Zd e efd Zd Z dd efdZ d e e ee effdZ!d e eeeffdZ"ded eeeffdZ#ee jHffgZ%dZ&edk(rGddl'Z'e'jPdZ)e)jUZ+e"Z,e-e j\e,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\necho '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)AnyCallableDictOptionalTuple) atomic_helpersourcessubputil) openstackz 9796-932EceZdZdZdZdZdZy) PlatformszTemplate/Live/MetadatazTemplate/Provisioning/Metadataz!Template/Provisioning/No-Metadataz OS-Code/LiveN)__name__ __module__ __qualname__TEMPLATE_LIVE_METADATATEMPLATE_PROVISIONING_METADATATEMPLATE_PROVISIONING_NODATAOS_CODEF/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceIBMCloud.pyrrms5%E"#F GrrcReZdZdZdZfdZfdZdZdZdZ 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__r#r )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) NFr r#metadatauserdata networkdata vendordata system-uuidz"Invalid content in vendor-data: %sT)read_mdr r#r0get userdata_rawr" system_uuidr convert_vendordatavendordata_raw ValueErrorLOGwarning)r$resultsvdes r _get_datazDataSourceIBMCloud._get_datas) ?h'  +  + #KK 3#KK 6 [[ &"=1 '")"<">) $   y ) / / 1 7 7 99rc@tjjdS)z Return boolean indicating if this is a xen hypervisor. :return: True if this is a xen hypervisor, False otherwise. z /proc/xen)rXrYexistsrrr_is_xenras 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)rXrYr`statst_mtimer<rP)prov_cfginst_logboot_refmsgresults r_is_ibm_provisioningrls  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)rar blkidr ProcessExecutionErrorr<r=sortedkeysr6upperIBM_CONFIG_UUIDrPrrrlrrr) label_mdata label_cfg2 not_foundfslabelsdevsr@devdatalabeluuid metadata_path cfg2_paths rget_ibm_platformrs KJI 9!#Hzz| diik"Cy"%++-xx#))+ j1 1  H  KK@    J 4?#: IIA   /2LLb155i@M Z,00;I!!9--   !<r@s rr5r5/s&'NHd 56 \ !T\ CXN(* C  77== '-GmmD*;.opathbsww||K599rblobcJtj|jdS)z Load JSON from a byte string. This technically could return a list or a str, but we are only assuming a dict here. :param blob: The byte string to load JSON from. :return: The loaded JSON object. zutf-8)jsonloadsdecode)rs rload_json_bytesz*metadata_from_dir..load_json_byteseszz$++g.//rrY translatorc tj|}||S#t$r!}tj d||Yd}~yd}~wt $r }t jd|d|d}~wwxYw)NzFailed reading path '%s': %szFailed decoding z: )r load_binary_fileIOErrorr<rP Exceptionr r)rYrrawr@s r load_filez$metadata_from_dir..load_fileqsp I''-Cc? "  II4dA > I((+;D6A3)GH H Is  A0A A0A++A0 metadata_rawzmeta_data.jsonr1 user_datac|Srr)xs rz#metadata_from_dir..~s1rr3zvendor_data.jsonr2znetwork_data.jsonNz' missing required file 'meta_data.json'r0 random_seedz,Badly formatted metadata random_seed entry: )) public_keysz public-keys)hostnamezlocal-hostname)rz instance-id)strbytesrrrrXrYrr rbase64 b64decoder; TypeError)rrrrfilesr>namerYtranslfpathmd_rawmdr@renamesold_keynew_keys rrrSs:S:S: 0e 0S#X 0II5'3,)?ICI /0/B U;'5 u/0/B 12OD  E!G#1dF Z.!%0 1~&$$lA B  GJ ^ $F  B  & 0 0 1F GB}  G $* f  /BwK* NI& ((>qcB  s'DE ,EE c6tj|tSr)r list_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__rrloggingrXtypingrrrrr cloudinitrr r r cloudinit.sources.helpersr getLoggerrr<rxrrrr DataSourcerrrGraboolrlrr5rDEP_FILESYSTEMrrargparseArgumentParserparser parse_argsargsrprint json_dumpsrrrrsq\z  7788/g!,, ** A$++A$H :8C= :'3 "  @9% x} <=9x!$sCx.)!HR#R$sCx.Rn'0023 ; z $X $ $1K LF    D 9D "- " "4 () r