a '¡DgLTã@sdZddlZddlZddlZddlZddlZddlmZmZm Z m Z m Z m Z ddl mZmZmZmZmZddlmZddlmZmZmZmZddlmZe e¡Zdd iZd Z d Z!d Z"e!d Z#e"d Z$ddiZ%dZ&Gdd„de ƒZ'Gdd„dej(ƒZ)eddœdd„Z*Gdd„dej+ƒZ,Gdd„de,ƒZ-e.dœdd„Z/e e.dœd d!„Z0e1dœd"d#„Z2e.e3d$œd%d&„Z4e.e ee.e.fd$œd'd(„Z5e.e.d$œd)d*„Z6d e,j7e,j8e#gd+œe1e e.e e'd,œd-d.„Z9e,ej:ffe-ej:ej;ffgZe?e @e9e$e#gd3�e2ƒd4œ¡ƒdS)5a3Datasource for Oracle (OCI/Oracle Cloud Infrastructure) Notes: * This datasource does not support OCI Classic. OCI Classic provides an EC2 lookalike metadata service. * The UUID provided in DMI data is not the same as the meta-data provided instance-id, but has an equivalent lifespan. * We do need to support upgrade from an instance that cloud-init identified as OpenStack. * Bare metal instances use iSCSI root, virtual machine instances do not. * Both bare metal and virtual machine instances provide a chassis-asset-tag of OracleCloud.com. éN)ÚAnyÚDictÚListÚ NamedTupleÚOptionalÚTuple)Ú atomic_helperÚdmiÚnetÚsourcesÚutil)Ú NetworkConfig)ÚcmdlineÚ ephemeralÚget_interfaces_by_macÚis_netfail_master)Ú wait_for_urlÚconfigure_secondary_nicsFzOracleCloud.comz&http://169.254.169.254/opc/v{version}/z+http://[fd00:c1::a9fe:a9fe]/opc/v{version}/z{path}/Z Authorizationz Bearer Oraclei(#c@sBeZdZUeed<eeefed<eeeefed<eed<dS)ÚReadOpcMetadataResponseÚversionÚ instance_dataÚ vnics_dataÚ imds_url_usedN) Ú__name__Ú __module__Ú __qualname__ÚintÚ__annotations__rÚstrrr©rrúF/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceOracle.pyr4s rc@seZdZdZedœdd„ZdS)ÚKlibcOracleNetworkConfigSourcezÃOverride super class to lower the applicability conditions. If any `/run/net-*.cfg` files exist, then it is applicable. Even if `/run/initramfs/open-iscsi.interface` does not exist. ©ÚreturncCs t|jƒS)zOverride is_applicable)ÚboolÚ_files©Úselfrrr Ú is_applicableBsz,KlibcOracleNetworkConfigSource.is_applicableN)rrrÚ__doc__r$r(rrrr r!;sr!)Únetwork_configr#cCsd|vr dS|ddvr,t d|d¡dStƒ}|ddkr”dd„|dDƒD]@}|d d krPd |vrP|d }| |¡}|s‚qPqPt|ƒrP|d =qPn||dd k�r| d i¡ ¡D]\\}}d|vr²| di¡ d¡}|r²| |¡}|sêq²q²t|ƒr²|dd=|d=||dd<q²dS)aP Search network config physical interfaces to see if any of them are a netfailover master. If found, we prevent matching by MAC as the other failover devices have the same MAC but need to be ignored. Note: we rely on cloudinit.net changes which prevent netfailover devices from being present in the provided network config. For more details about netfailover devices, refer to cloudinit.net module. :param network_config A v1 or v2 network config dict with the primary NIC, and possibly secondary nic configured. This dict will be mutated. rN)ééz+Ignoring unknown network config version: %sr+cSsg|]}d|vr|‘qS)Útyper)Ú.0Úcrrr Ú cóz,_ensure_netfailover_safe..Úconfigr-ÚphysicalÚ mac_addressr,Ú ethernetsÚmatchÚ macaddresszset-nameÚname)ÚLOGÚdebugrÚgetrÚitems)r*Z mac_to_nameZcfgZmacZcur_nameÚ_Zmacaddrrrr Ú_ensure_netfailover_safeGs> þ      r>csêeZdZUdZdZejjejjejj ejj fZ e ejdfe d<dZdZdZ‡fdd „Zedd œ‡fd d „ Zed œdd„Zeed œdd„ƒZdd„Zed œdd„Zdd„Zed œdd„Zed œdd„Zedd„ƒZd"edœd d!„Z ‡Z!S)#ÚDataSourceOracleZOracleN.Únetwork_config_sourcesTéécsttt|ƒj|g|¢Ri|¤Žd|_t t |d|jgi¡tg¡|_ t ƒ|_ gddœ|_ |  ¡}|j|_|j|_dS)NZ datasourcer+©r2r)Úsuperr?Ú__init__Ú _vnics_datar Z mergemanydictZget_cfg_by_pathÚdsnameÚBUILTIN_DS_CONFIGÚds_cfgr!Ú_network_config_sourceÚ_network_configZget_url_paramsZmax_wait_secondsÚ url_max_waitZtimeout_secondsÚ url_timeout)r'Úsys_cfgÚargsÚkwargsZ url_params©Ú __class__rr rE�sþÿ zDataSourceOracle.__init__)Úci_pkl_versionr#csTtƒ |¡t|dƒs"t|ddƒt|dƒs:t|dtƒƒt|dƒsPgddœ|_dS)NrFrJrKr+rC)rDÚ _unpickleÚhasattrÚsetattrr!rK)r'rSrQrr rTžs    ý zDataSourceOracle._unpickler"cCst|j dg¡ƒS)Nr2)r$rKr;r&rrr Ú_has_network_config«sz$DataSourceOracle._has_network_configcCstƒS)z@Check platform environment to report if this datasource may run.)Ú_is_platform_viablerrrr Ú ds_detect®szDataSourceOracle.ds_detectc Cshtƒ|_tjddd�tdœdtjddd�itjddd�tdœdtjddd�if}|jrvt ¡}t j |j |dd|d�}nt   ¡}| ¡ }|j d td ¡}|�,t|p¨||j|jttgd �}Wdƒn1sÐ0Y|sâd St|jd �|_|j}|_|j|_|d |dd|d|ddœ|_d|v�rd|d d¡}|�rPt |¡|_|d d¡|jd<dS)Nr,Úinstance©rÚpath)ÚurlZheadersr]r+T)ÚdistroZ interfaceZipv6Zipv4Úconnectivity_urls_datar©Úfetch_vnics_dataÚmax_waitÚtimeoutÚmetadata_patternsF©r]Z ociAdNameÚidrÚhostnameZ displayName)zavailability-zonez instance-idz launch-indexzlocal-hostnamer8ÚmetadataÚ user_dataZssh_authorized_keysÚ public_keys) Ú_read_system_uuidÚ system_uuidÚIPV4_METADATA_PATTERNÚformatÚ V2_HEADERSÚIPV6_METADATA_PATTERNÚperform_dhcp_setupr Zfind_fallback_nicrZEphemeralIPNetworkr^r Ú nullcontextÚ_is_iscsi_rootrIr;rHÚread_opc_metadatarLrMÚ _get_versioned_metadata_base_urlrÚmetadata_addressrZ_crawled_metadatarrFrhÚbase64Z b64decodeZ userdata_raw) r'r_Znic_nameZnetwork_contextZfetch_primary_nicZfetch_secondary_nicsZfetched_metadataÚdatarirrr Ú _get_data³s‚ÿüÿÿÿüÿÿîû þþü$ ÿ û  ÿ zDataSourceOracle._get_datacCs t |j¡S)z¯quickly check (local only) if self.instance_id is still valid On Oracle, the dmi-provided system uuid differs from the instance-id but has the same life-span.)r Zinstance_id_matches_system_uuidrl)r'rNrrr Úcheck_instance_id sz"DataSourceOracle.check_instance_idcCst |j d¡¡S)Nrj)r Znormalize_pubkey_datarhr;r&rrr Úget_public_ssh_keyssz$DataSourceOracle.get_public_ssh_keyscCs |j ¡S)z)Return whether we are on a iscsi machine.)rJr(r&rrr rsszDataSourceOracle._is_iscsi_rootcCs |j ¡S©N)rJZ render_configr&rrr Ú_get_iscsi_configsz"DataSourceOracle._get_iscsi_configcCs’| ¡r|jSd}| ¡r$| ¡|_| ¡s:t d¡d}|j dtd¡}|sT|r‚z|  |¡Wnt y€t   td¡Yn0t |jƒ|jS)zÞNetwork config is read from initramfs provided files Priority for primary network_config selection: - iscsi - imds If none is present, then we fall back to fallback configuration. FzLCould not obtain network configuration from initramfs. Falling back to IMDS.Trz+Failed to parse IMDS network configuration!)rWrKrsr}r9ÚwarningrIr;rHÚ!_add_network_config_from_opc_imdsÚ Exceptionr Zlogexcr>)r'Ú set_primaryZ set_secondaryrrr r*s0  ÿþ þ  zDataSourceOracle.network_configF)r�c CsV|jdurt d¡dS|s8d|jdvr8t d¡dStƒ}|rH|jn |jdd…}t|ƒD�]ð\}}|or|dk}|d ¡}| dd ¡o˜| d d ¡ }||vr°t d |¡q^||} |rÐt |d d¡} nt |d ¡} |j ddk�r”|�r|�rddig} n ddig} n`g} | d ¡�rB|   d|d ›d| j ›�dœ¡| d ¡�rr|   d|d d›d| j ›�dœ¡| d|t | dœ} |j d  | ¡q^|j ddkr^t d|idœ} | |j d| <|�oÆ|| d<|�oÖ| | d<|�sBg| d<| d ¡�r| d  |d ›d| j ›�¡| d ¡�rB| d  |d d›d| j ›�¡| |j d| <q^dS)aœGenerate primary and/or secondary NIC config from IMDS and merge it. It will mutate the network config to include the secondary VNICs. :param set_primary: If True set primary interface. :raises: Exceptions are not handled within this function. Likely exceptions are KeyError/IndexError (if the IMDS returns valid JSON with unexpected contents). Nz#NIC data is UNSET but should not beZnicIndexrz\VNIC metadata indicates this is a bare metal machine; skipping secondary VNIC configuration.r+ZmacAddrZipv6SubnetCidrBlockFZ privateIpz)Interface with MAC %s not found; skippingZ ipv6AddressesZsubnetCidrBlockrr-Zdhcp6ZdhcpZstaticú/)r-Úaddressr3)r8r-r4ÚmtuÚsubnetsr2r,r7)r„r6r5Zdhcp4Ú addresses) rFr9r~rÚ enumerateÚlowerr;Ú ipaddressÚ ip_networkrKÚappendÚ prefixlenÚMTU) r'r�Zinterfaces_by_macrÚindexZ vnic_dictZ is_primaryr4Z is_ipv6_onlyr8Únetworkr…Zinterface_configrrr rHsž  ÿ  ÿ þþ ÿ    ÿýÿ ÿýÿ ûþ ÿ ÿÿz2DataSourceOracle._add_network_config_from_opc_imds)F)"rrrrGrlr ZNetworkConfigSourceZCMD_LINEZ SYSTEM_CFGZDSZ INITRAMFSr@rrrqrLrMrErrTr$rWÚ staticmethodrYryrzr{rsÚdictr}Úpropertyr*rÚ __classcell__rrrQr r?{s. ü  V -r?c@seZdZdZdS)ÚDataSourceOracleNetFN)rrrrqrrrr r”¼sr”©rvcCs|sdS| t d¡d¡S)NFZopcr)Ú startswithÚIPV4_METADATA_ROOTÚsplitr•rrr Ú_is_ipv4_metadata_urlÀsr™r"cCst d¡}|durdS| ¡S)Nz system-uuid)r Ú read_dmi_datarˆ)Zsys_uuidrrr rkÆs rkcCst d¡}|tkS)Nzchassis-asset-tag)r ršÚCHASSIS_ASSET_TAG)Z asset_tagrrr rXËs rX)r]r#cCsd|vr dSdS)Nz/opc/v2/r,r+rrerrr Ú _url_versionÐsrœcCst|ƒdkrtSdS)Nr,)rœrorerrr Ú _headers_cbÔsr�cCsL|s|Sd|vr"| d¡ddSd|vr<| d¡ddStd|ƒ‚dS)zQ Remove everything following the version number in the metadata address. Zv2rzv2/Zv1zv1/zInvalid metadata address: N)r˜Ú ValueErrorrerrr ruØsrur`)rardr#c sì‡fdd„dDƒ}t d|¡t ¡}t|||tddd�\}}|sXt dd  |¡¡d St d |¡t  |  d ¡¡}t |ƒ} d } |rÞt|  d d¡g|t ¡||tddd�\} } | rÔt  |   d ¡¡} t d| ¡n t d¡t | || |ƒS)aã Fetch metadata from the /opc/ routes from the IMDS. Returns: Optional[ReadOpcMetadataResponse]: If fetching metadata fails, None. If fetching metadata succeeds, a namedtuple containing: - The metadata version as an integer - The JSON-decoded value of the instance data from the IMDS - The JSON-decoded value of the vnics data from the IMDS if `fetch_vnics_data` is True, else None. Alternatively, None if fetching metadata failed - The url that was used to fetch the metadata. This allows for later determining if v1 or v2 endppoint was used and whether the IMDS was reached via IPv4 or IPv6. cs$g|]}ˆD]}|j|dd�‘q qS)rZr[)rn)r.rZmetadata_pattern©rdrr r0üsþz%read_opc_metadata..)r,r+z*Attempting to fetch IMDS metadata from: %sgš™™™™™¹?T)ÚurlsrbrcZ headers_cbZ sleep_timeZconnect_synchronouslyz-Failed to fetch IMDS metadata from any of: %sz, Nz7Successfully fetched instance metadata from IMDS at: %szutf-8rZZvnicsz4Successfully fetched vnics metadata from IMDS at: %sz+Failed to fetch IMDS network configuration!)r9r:ÚtimeÚ monotonicrr�r~ÚjoinÚjsonÚloadsÚdecoderœÚreplacer) rarbrcrdr Z start_timeZurl_that_workedZinstance_responserZmetadata_versionrZ vnics_urlZvnics_responserrŸr rtæs` þ ú þþ ú þ ürtcCs t |t¡Sr|)r Zlist_from_dependsÚ datasources)Zdependsrrr Úget_datasource_listGsr©Ú__main__zú Query Oracle Cloud metadata and emit a JSON object with two keys: `read_opc_metadata` and `_is_platform_viable`. The values of each are the return values of the corresponding functions defined in DataSourceOracle.py.rŸ)rtrX)Ar)rwr‰r¤Zloggingr¡ÚtypingrrrrrrZ cloudinitrr r r r Zcloudinit.distros.networkingr Z cloudinit.netrrrrZcloudinit.url_helperrZ getLoggerrr9rHr›r—ZIPV6_METADATA_ROOTrmrpror�rZKlibcNetworkConfigSourcer!r>Z DataSourcer?r”rr™rkr$rXrrœr�rurLrMrtZDEP_FILESYSTEMZ DEP_NETWORKr¨r©Ú descriptionÚprintZ json_dumpsrrrr Úsz    þ 4Cûú U þþþ  þúÿÿ