o x[h:@svdZddlZddlZddlZddlZddlmZmZmZm Z m Z ddl m Z m Z mZmZddlmZeeZdZGdddZejejfZGd d d e jZd e efd d ZddZ   d!d efddZ d e e ee effddZ!d e eeeffddZ"ded eeeffddZ#ee j$ffgZ%ddZ&edkrddl'Z'e'j(dd Z)e)*Z+e"Z,e-e .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\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-932Ec@seZdZdZdZdZdZdS) PlatformszTemplate/Live/MetadatazTemplate/Provisioning/Metadataz!Template/Provisioning/No-Metadataz OS-Code/LiveN)__name__ __module__ __qualname__TEMPLATE_LIVE_METADATATEMPLATE_PROVISIONING_METADATATEMPLATE_PROVISIONING_NODATAOS_CODErrF/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceIBMCloud.pyr ms r csTeZdZdZdZfddZfddZddZd d Zd d Z e d dZ Z S)DataSourceIBMCloudIBMCloudNcs0tt||||d|_d|_d|_d|_dSN)superr__init__source_network_config network_jsonplatform)selfsys_cfgdistropaths __class__rrrs  zDataSourceIBMCloud.__init__cs$tt|}d||j|jf}|S)Nz %s [%s %s])rr__str__rr)rrootmstrr#rrr%szDataSourceIBMCloud.__str__c Cst}|dur dS|d|_|d|_|d|_|d|_|d|_|d}|d|_z t ||_ Wd St yS}zt d |d|_ WYd}~d Sd}~ww) NFrrmetadatauserdata networkdata vendordata system-uuidz"Invalid content in vendor-data: %sT)read_mdrrr(get userdata_rawr system_uuidrconvert_vendordatavendordata_raw ValueErrorLOGwarning)rresultsvderrr _get_datas&        zDataSourceIBMCloud._get_datacCsd|j|jfS)z/Return the subplatform metadata source details.z%s (%s))rrrrrr_get_subplatformsz#DataSourceIBMCloud._get_subplatformcCs|jdurdS|jtkS)aquickly (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.NF)r0_read_system_uuid)rr rrrcheck_instance_ids  z$DataSourceIBMCloud.check_instance_idcCsb|jtjkr dddS|jdur.|jtjdfvr)tdt j |jdd|_|jStd|jS)Ndisabled)configversionz(network config provided via network_json) known_macsz#no network configuration available.) rr rrrrUNSETr4debugr convert_net_jsonr:rrrnetwork_configs     z!DataSourceIBMCloud.network_config) r rrdsnamer0rr%r9r;r=propertyrF __classcell__rrr#rrzs   rreturncCs&d}tj|s dSt|S)zW Read the system uuid. :return: the system uuid or None if not available. z/sys/hypervisor/uuidN)ospathisfiler load_text_filestriplower) uuid_pathrrrr<s r<cCs tjdS)z Return boolean indicating if this is a xen hypervisor. :return: True if this is a xen hypervisor, False otherwise. z /proc/xen)rKrLexistsrrrr_is_xens rS#/root/provisioningConfiguration.cfg/root/swinstall.log/proc/1/environcCstj|rCd|}d}tj|r.opathblobcSst|dS)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)rrrrload_json_byteses z*metadata_from_dir..load_json_bytesrL translatorc Ssvz t|}||WSty$}ztd||WYd}~dSd}~wty:}z td|d|d}~ww)NzFailed reading path '%s': %szFailed decoding z: )r load_binary_fileIOErrorr4rD Exceptionrr|)rLrrawr8rrr load_fileqs  z$metadata_from_dir..load_file metadata_rawzmeta_data.jsonr) user_datacSs|Srr)xrrr~sz#metadata_from_dir..r+zvendor_data.jsonr*znetwork_data.jsonNz' missing required file 'meta_data.json'r( random_seedz,Badly formatted metadata random_seed entry: )) public_keysz public-keys)hostnamezlocal-hostname)rtz instance-id)strbytesrrrrKrLrrr|base64 b64decoder3 TypeError)rrrrfilesr6namerLtranslfpathmd_rawmdr8renamesold_keynew_keyrrrrzSsD      rzcCs t|tSr)rlist_from_depends datasources)dependsrrrget_datasource_lists r__main__zQuery IBM Cloud Metadata) description)rTrUrV)/__doc__rrloggingrKtypingrrrrr cloudinitrrr r cloudinit.sources.helpersr getLoggerr r4rkr rrrx DataSourcerrr<rSboolr`rwr-rzDEP_FILESYSTEMrrargparseArgumentParserparser parse_argsargsrrprint json_dumpsrrrrsH]  D    <$ W