BYPASS SHELL BY ./RAZORGANZ
Server: nginx/1.20.1
System: Linux iZdzfnv9mwfppeZ 5.10.134-19.2.al8.x86_64 #1 SMP Wed Oct 29 22:47:09 CST 2025 x86_64
User: apache (48)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //lib/python3.6/site-packages/cloudinit/sources/__pycache__/DataSourceEc2.cpython-36.pyc
3

�q�h���@s�ddlZddlZddlZddlmZmZddlmZddlmZ	ddlm
Z
mZddlmZ
ddlmZmZddlmZmZdd	lmZdd
lmZddlmZddlmZdd
lmZe	je�Ze e
j!g�Z"d;Z#dZ$Gdd�d�Z%dd�Z&e%j'gZ(dZ)Gdd�dej*�Z+Gdd�de+�Z,dd�Z-dd�Z.dd �Z/d!d"�Z0d#d$�Z1d%d&�Z2d'd(�Z3d)d*�Z4d+d,�Z5d-d.�Z6ej7fee8efee8e8feee8e9fd/�d0d1�Z:ddd2ej7fd3d4�Z;d5d6�Z<d7d8�Z=e,ej>ffe+ej>ej?ffgZ@d9d:�ZAdS)<�N)�Dict�List)�dmi)�log)�net�sources)�
url_helper)�util�warnings)�
EventScope�	EventType)�
activators)�NoDHCPLeaseError)�EphemeralIPNetwork)�NicOrder)�ec2�
datasource�Ec2�	strict_id�warnc@s(eZdZdZdZdZdZdZdZdZ	dS)	�
CloudNamesZawsZ	brightboxZzstack�e24cloudZoutscale�unknownzno-ec2-metadataN)
�__name__�
__module__�__qualname__�AWS�	BRIGHTBOX�ZSTACK�E24CLOUD�OUTSCALE�UNKNOWN�NO_EC2_METADATA�r#r#�#/usr/lib/python3.6/DataSourceEc2.pyr$srcCs|jdkod|jkS)Ni�zmeta-data/tags/)�code�url)�	exceptionr#r#r$�skip_404_tag_errors3sr(zS
ENV{ID_NET_DRIVER}=="vif|ena|ixgbevf", GOTO="cloudinit_hook"
GOTO="cloudinit_end"
cs�eZdZUdZdddgZdZdddgZeed	Z	d
Z
dZej
ZdZejejejejejhiZejejejhiZeZ�fd
d�Zedd��fdd�Zdd�Zdd�Zdd�Z e!dd��Z"e!dd��Z#e!dd��Z$e!dd��Z%e!d d!��Z&e!d"d#��Z'e!d$d%��Z(d&d'�Z)d(d)�Z*d*d+�Z+d,d-�Z,d.d/�Z-e!d0d1��Z.e!d2d3��Z/d4d5�Z0e!d6d7��Z1e!�fd8d9��Z2d:d;�Z3dGd<d=�Z4d>d?�Z5d@dA�Z6dHdBdC�Z7dIdEdF�Z8�Z9S)J�
DataSourceEc2rzhttp://169.254.169.254zhttp://[fd00:ec2::254]zhttp://instance-data.:8773z
2009-04-04z
2021-03-23z
2018-09-24z
2016-09-02�x�2NFcs&tt|�j|||�d|_tj|_dS)N)�superr)�__init__�metadata_addressr�MAC�_fallback_nic_order)�self�sys_cfg�distro�paths)�	__class__r#r$r-uszDataSourceEc2.__init__)�ci_pkl_version�returncst�j|�t|_tj|_dS)N)r,�	_unpickle�_EXTRA_HOTPLUG_UDEV_RULES�extra_hotplug_udev_rulesrr/r0)r1r6)r5r#r$r8zszDataSourceEc2._unpicklecCst�S)z5Return the cloud name as identified during _get_data.)�identify_platform)r1r#r#r$�_get_cloud_nameszDataSourceEc2._get_cloud_namecCsVttj|jtt�d�\}}tjd||j|j	�|dkrF|jt
jkrFdS|jt
jkrVdS|j
r�tj�rrtjd�dSyVt|j|jddd��8}|jr�d|j��nd	}tjtjd
|��|jd�|_WdQRXWntk
r�dSXntjtjd
|jd�|_|j�sdS|jjdd�|_|jjd
d�|_|jjdd�|_|jjdi�jdi�jdi�|_dS)Nrz0strict_mode: %s, cloud_name=%s cloud_platform=%s�trueFz1FreeBSD doesn't support running dhclient with -sfT)�ipv4�ipv6� �zCrawl of metadata service)�logfunc�msg�funcz	meta-dataz	user-datazvendor-data�dynamiczinstance-identity�document)rN)�read_strict_moder	�get_cfg_by_pathr2�STRICT_ID_PATH�STRICT_ID_DEFAULT�LOG�debug�
cloud_name�platformrr!r"�perform_dhcp_setupZ
is_FreeBSDrr3�fallback_interface�	state_msg�log_time�crawl_metadataZ_crawled_metadatar�get�metadataZuserdata_rawZvendordata_raw�identity)r1Zstrict_modeZ_sleepZnetwrQr#r#r$�	_get_data�sX



zDataSourceEc2._get_datacCsN|js
dS|jjdi�jdi�}x(|jdi�j�D]\}}d|kr2dSq2WdS)z6Report if this instance type is Ec2 Classic (non-vpc).F�network�
interfaces�macszvpc-idT)rUrT�items)r1Z	ifaces_mdZ_macZmac_datar#r#r$�is_classic_instance�sz!DataSourceEc2.is_classic_instancecCs|js
dS|jjd�S)Nzami-launch-index)rUrT)r1r#r#r$�launch_index�szDataSourceEc2.launch_indexcCs.t|d�stjj�|_|js(tjj�|_|jS)N�_platform_type)�hasattrr)�dsname�lowerr^)r1r#r#r$rN�s

zDataSourceEc2.platformcCsdS)Nzlatest/api/tokenr#)r1r#r#r$�api_token_route�szDataSourceEc2.api_token_routecCsdS)NZ21600r#)r1r#r#r$�imdsv2_token_ttl_seconds�sz&DataSourceEc2.imdsv2_token_ttl_secondscCsdS)NzX-aws-ec2-metadata-tokenr#)r1r#r#r$�imdsv2_token_put_header�sz%DataSourceEc2.imdsv2_token_put_headercCs
|jdS)Nz-ttl-seconds)rd)r1r#r#r$�imdsv2_token_req_header�sz%DataSourceEc2.imdsv2_token_req_headercCs|j|jgS)N)rdre)r1r#r#r$�imdsv2_token_redact�sz!DataSourceEc2.imdsv2_token_redactcCs�d}|j�}x�|jD]�}|j|j|�}ytj|||jd�}Wn2tjk
rn}ztj	d||�WYdd}~XqX|j
dkr�tj	d|�|S|j
dkrd}tj	|||j�qW|jS)	aEGet the best supported api version from the metadata service.

        Loop through all extended support metadata versions in order and
        return the most-fully featured metadata api version discovered.

        If extended_metadata_versions aren't present, return the datasource's
        min_metadata_version.
        z{0}/{1}/meta-data/instance-id)r&�headers�headers_redactzurl %s raised exception %sN��z#Found preferred metadata version %si�z0Metadata api version %s not present. Headers: %s)
�_get_headers�extended_metadata_versions�formatr.�uhelp�readurlrf�UrlErrorrKrLr%rg�min_metadata_version)r1Zurl_tmplrgZapi_verr&Zresp�erCr#r#r$�get_metadata_api_version�s$
 

z&DataSourceEc2.get_metadata_api_versioncCsh|jtjkrZt|dd�sF|j�}tj||j|j|j	|j
d�jdi�|_|jjd|j
d�S|j
dSdS)NrV)�
headers_cbrh�exception_cbrFZ
instanceIdzinstance-id)rMrr�getattrrrr�get_instance_identityr.rjrf�_refresh_stale_aws_token_cbrTrVrU)r1�api_versionr#r#r$�get_instance_ids

zDataSourceEc2.get_instance_idc
Cs�|jtkrdSg}i}|j}d}x*|D]"}dj||�}|j|�|||<q&Wtjd�d}d}|j�}	y0tj	||	j
|	jtj|j
|j||jdd�	\}}Wntjk
r�YnX|r�|r�||_||SdS)aGet an API token for EC2 Instance Metadata Service.

        On EC2. IMDS will always answer an API token, unless
        the instance owner has disabled the IMDS HTTP endpoint or
        the network topology conflicts with the configured hop-limit.
        N�PUTz{0}/{1}zFetching Ec2 IMDSv2 API TokenF)	�urls�max_wait�timeout�	status_cbrsrt�request_methodrhZconnect_synchronously)rM� IDMSV2_SUPPORTED_CLOUD_PLATFORMSrbrl�appendrKrL�get_url_paramsrm�wait_for_url�max_wait_seconds�timeout_seconds�warningrj�_imds_exception_cbrfro�
_api_token)
r1�mdurlsr{�url2base�url_pathrr&�cur�response�
url_paramsr#r#r$�_maybe_fetch_api_token s>



z$DataSourceEc2._maybe_fetch_api_tokenc	Csv|j}|j�}|jdkrdS|jd|j�}dd�|D�}t|�t|�krdtjdtt|�t|���t	|�rr|}ntj
d�|j}|j|�}|o�|jt
k�r g}i}dj|jd	�}d
}	x*|D]"}
dj|
|�}|j|�|
||<q�Wtj�}tj||j|jtj
|j|j|	d�\}
}
|
�r ||
}|�r<||_tjd
|j�n2|jt
k�rTtj
d�ntjd|ttj�|��t|�S)NrF�
metadata_urlscSsg|]}tj|�r|�qSr#)r	Zis_resolvable_url)�.0�xr#r#r$�
<listcomp>[sz;DataSourceEc2.wait_for_metadata_service.<locals>.<listcomp>z,Removed the following from metadata urls: %sz+Empty metadata url list! using default listz{ver}/meta-data/instance-id)ZverZGETz{0}/{1})r{r|r}r~rhrsrzUsing metadata source: '%s'z)IMDS's HTTP endpoint is probably disabledz(Giving up on md from %s after %s seconds)�ds_cfgr�r�rTr��setrKrL�list�lenr�r�rMr�rlrpr��timermr�r�rfrjr.Zcritical�int�bool)r1Zmcfgr�r�Zfilteredr.r{r�r�rr&r�Z
start_time�_r#r#r$�wait_for_metadata_serviceRs^





z'DataSourceEc2.wait_for_metadata_servicecCs�d|jkrdSd}|jd}t|t�s6tjd|�dSx6|j�D]*\}}||krV|}P|dkr@|dkr@|}q@W|dkr�tjd|�dS|}|jd�s�d|}tjj	|�r�|S|j
tjj|��}|r�tjd||�|S|dkr�dS|S)	Nzblock-device-mappingz+block-device-mapping not a dictionary: '%s'Z	ephemeralZ
ephemeral0z Unable to convert %s to a device�/z/dev/%szRemapped device name %s => %s)rU�
isinstance�dictrKrLr[�
startswith�os�path�existsZ
_remap_device�basename)r1�name�foundZbdmZentnameZdeviceZofoundZremappedr#r#r$�device_name_to_device�s8



z#DataSourceEc2.device_name_to_devicecCsPy6|jtjkr&|jjd|jdd�S|jddSWntk
rJdSXdS)NZavailabilityZoneZ	placementzavailability-zone)rMrrrVrTrU�KeyError)r1r#r#r$�availability_zone�szDataSourceEc2.availability_zonecCsT|jtjkr6|jjd�}|jr2|r2|jdd�}|S|j}|dk	rP|dd�SdS)N�region����r�)rMrrrVrTr�)r1r�Zazr#r#r$r��szDataSourceEc2.regioncCs,|sdS|jtjkr(ttj|tt�|�dS)N)rMrr!�warn_if_necessaryr	rHrIrJ)r1�cfgZis_new_instancer#r#r$�activate�szDataSourceEc2.activatecCs|jtjkr|jS|jdkr*tjd�dSd}td|jkoD|jtj	k�}|rjtj
d�tjtj
d|j
d�|j}|jjd�}t|t�r�t||j|tj|jdd�|jd	�}|j�r�tj|j�|_|jtjjtj�|jtjjtj �ntjd
|�||_|jS)z@Return a network config dict for rendering ENI or netplan files.Nz8Unexpected call to network_config when metadata is None.rXzPMetadata 'network' not present: Refreshing stale metadata from prior to upgrade.zRe-crawl of metadata service)rBrCrDZapply_full_imds_network_configT)�fallback_nic�full_network_config�fallback_nic_orderz%Metadata 'network' key not valid: %s.)!�_network_configr�UNSETrUrKr�r�rMrrrLr	rR�get_datarPrTr�r��#convert_ec2_metadata_network_configr3Zget_cfg_option_boolr�r0r\�copy�deepcopy�default_update_eventsr�NETWORK�addr�BOOT�BOOT_LEGACY)r1�resultZno_network_metadata_on_awsZifaceZnet_mdr#r#r$�network_config�sJ







zDataSourceEc2.network_configcs:|jdkr4t|dd�}|r(||_d|_ntt|�jS|jS)Nr�)Z_fallback_interfacerur�r,r)rP)r1Z
_legacy_fbnic)r5r#r$rP1s
z DataSourceEc2.fallback_interfacecCs|j�siS|j�}|j}i}|jtkr:|j}|j}d}n&|jtjkrTd}}t	}nd}}}y�t
j||j|j
||d�|d<t
j||j|j
||d�|d<t
j||j|j
|||d�|d<|jtjkr�t
j||j|j
||d�}d|i|d<Wn&tk
�rtjtd	|j�iSX||d
<|S)z�Crawl metadata service when available.

        @returns: Dictionary of crawled metadata content containing the keys:
          meta-data, user-data, vendor-data and dynamic.
        N)rsrhrtz	user-datazvendor-data)rsrhrtZretrieval_exception_ignore_cbz	meta-datazinstance-identityrEz'Failed reading from metadata address %sZ_metadata_api_version)r�rrrfrMr�rw�#_skip_or_refresh_stale_aws_token_cbrr r(rZget_instance_userdatar.rjZget_instance_vendordataZget_instance_metadatarrv�	Exceptionr	ZlogexcrK)r1rxZredactZcrawled_metadataZexc_cbZ	exc_cb_udZskip_cbrVr#r#r$rS>sb
zDataSourceEc2.crawl_metadatacCs�|jtkrdS|dkr|j}tjd�|j|i}dj|j|j�}yt	j
|||jdd�}Wn0t	jk
r�}ztj
d||�dSd}~XnX|jS)z�Request new metadata API token.
        @param seconds: The lifetime of the token in seconds

        @return: The API token or None if unavailable.
        Nz!Refreshing Ec2 metadata API tokenz{}/{}rz)rgrhrz/Unable to get API token: %s raised exception %s)rMr�rcrKrLrerlr.rbrmrnrfror��contents)r1ZsecondsZrequest_headerZ	token_urlr�rqr#r#r$�_refresh_api_token|s$



z DataSourceEc2._refresh_api_tokencCs$tjtj||�}|sdS|j||�S)z^Callback will not retry on SKIP_USERDATA_VENDORDATA_CODES or if no token
        is available.F)rZskip_retry_on_codesZSKIP_USERDATA_VENDORDATA_CODESrw)r1rCr'Zretryr#r#r$r��s
z1DataSourceEc2._skip_or_refresh_stale_aws_token_cbcCs*t|tj�r&|jdkr&tjd�d|_dS)z=Exception handler for Ec2 to refresh token if token is stale.i�z+Clearing cached Ec2 API token due to expiryNT)r�rmror%rKrLr�)r1rCr'r#r#r$rw�s
z)DataSourceEc2._refresh_stale_aws_token_cbcCsDt|tj�r@|jr@|jdkr@|jdkr2tjd�n
tjd�|�dS)a2Fail quickly on proper AWS if IMDSv2 rejects API token request

        Guidance from Amazon is that if IMDSv2 had disabled token requests
        by returning a 403, or cloud-init malformed requests resulting in
        other 40X errors, we want the datasource detection to fail quickly
        without retries as those symptoms will likely not be resolved by
        retries.

        Exceptions such as requests.ConnectionError due to IMDS being
        temporarily unroutable or unavailable will still retry due to the
        callsite wait_for_url.
        i�i�zLEc2 IMDS endpoint returned a 403 error. HTTP endpoint is disabled. Aborting.z2Fatal error while requesting Ec2 IMDSv2 API tokensN)r�rmror%rKr�)r1rCr'r#r#r$r��s

z DataSourceEc2._imds_exception_cbrAcCsN|jtkriS|j|ji}|j|kr(|S|jsB|j�|_|jsBiS|j|jiS)z�Return a dict of headers for accessing a url.

        If _api_token is unset on AWS, attempt to refresh the token via a PUT
        and then return the updated token header.
        )rMr�rercrbr�r�rd)r1r&Zrequest_token_headerr#r#r$rj�s


zDataSourceEc2._get_headers)N)N)rA):rrrr`r�rprkr�strZurl_max_waitZurl_timeoutr�rr�r�rOrr�rZBOOT_NEW_INSTANCEr�r�ZHOTPLUGZsupported_update_eventsr�r9r:r-r�r8r<rWr\�propertyr]rNrbrcrdrerfrrryr�r�r�r�r�r�r�rPrSr�r�rwr�rj�
__classcell__r#r#)r5r$r)Bsd


9

2I3
	?
>


r)cs$eZdZdZdZ�fdd�Z�ZS)�DataSourceEc2LocalayDatasource run at init-local which sets up network to query metadata.

    In init-local, no network is available. This subclass sets up minimal
    networking with dhclient on a viable nic so that it can talk to the
    metadata service. If the metadata service provides network configuration
    then render the network configuration for that instance based on metadata.
    Tcs8tjtjf}|j|kr*tjd||j�dStt|�j�S)Nz+Local Ec2 mode only supported on %s, not %sF)	rrr rMrKrLr,r�r�)r1Zsupported_platforms)r5r#r$r��s
zDataSourceEc2Local.get_data)rrr�__doc__rOr�r�r#r#)r5r$r��sr�cCs8yt|�Stk
r2}ztj|�|Sd}~XnXdS)N)�parse_strict_mode�
ValueErrorrKr�)�cfgval�defaultrqr#r#r$rG�s

rGcCs�|dkrd
S|dkrdS|s dS|jd�\}}}|d
krHtd||f��|r�yt|�}Wq�tk
r�}ztd	||f�|�WYdd}~Xq�Xnd}||fS)NTr=F�falserr�,zUInvalid mode '%s' in strict_id setting '%s': Expected one of 'true', 'false', 'warn'.z<Invalid sleep '%s' in strict_id setting '%s': not an integer)r=N)r�N)rr)r=r�r)�	partitionr�r�)r��moder��sleeprqr#r#r$r��s*
r�cCs^yt|�\}}Wn*tk
r:}ztj|�dSd}~XnX|dkrHdStjd|d|d�dS)Nr�Z
non_ec2_mdT)r�r�)r�r�rKr�r
Zshow_warning)r�r�r�r�rqr#r#r$r�s
r�cCs4|djd�r0|ddks*|d|dkr0tjSdS)N�uuidr�uuid_source�
hypervisor�serial)r�rr)�datar#r#r$�identify_aws+sr�cCs|djd�rtjSdS)Nr�z.brightbox.com)�endswithrr)r�r#r#r$�identify_brightbox5sr�cCs|djd�rtjSdS)N�	asset_tagz
.zstack.io)r�rr)r�r#r#r$�identify_zstack:sr�cCs|ddkrtjSdS)N�vendorr)rr)r�r#r#r$�identify_e24cloud?sr�cCs*|ddj�kr&|ddj�kr&tjSdS)N�product_namez3DS Outscale VMr�z3DS Outscale)rarr )r�r#r#r$�identify_outscaleDsr�cCstt�}tttttdd�f}xT|D]L}y||�}|r6|SWq tk
rj}ztjd|||�WYdd}~Xq Xq WdS)NcSstjS)N)rr!)r�r#r#r$�<lambda>Usz#identify_platform.<locals>.<lambda>z'calling %s with %s raised exception: %s)	�_collect_platform_datar�r�r�r�r�r�rKr�)r�ZchecksZcheckerr�rqr#r#r$r;Ls

r;cCs�i}ytjd�j�}d|d<Wn&tk
rDtjd�}d|d<YnX|dkrRd}|j�|d<tjd	�}|dkrtd}|j�|d
<tjd�}|dkr�d}|j�|d<tjd
�}|r�|ndj�|d<tjd�}|r�|ndj�|d<|S)a�Returns a dictionary of platform info from dmi or /sys/hypervisor.

    Keys in the dictionary are as follows:
       uuid: system-uuid from dmi or /sys/hypervisor
       uuid_source: 'hypervisor' (/sys/hypervisor/uuid) or 'dmi'
       serial: dmi 'system-serial-number' (/sys/.../product_serial)
       asset_tag: 'dmidecode -s chassis-asset-tag'
       vendor: dmi 'system-manufacturer' (/sys/.../sys_vendor)
       product_name: dmi 'system-product-name' (/sys/.../system-manufacturer)

    On Ec2 instances experimentation is that product_serial is upper case,
    and product_uuid is lower case.  This returns lower case values for both.
    z/sys/hypervisor/uuidr�r�zsystem-uuidrNrAr�zsystem-serial-numberr�zchassis-asset-tagr�zsystem-manufacturerr�zsystem-product-namer�)r	Z	load_file�stripr�rZ
read_dmi_datara)r�r�r�r�r�r�r#r#r$r�bs.




r�)�
macs_metadata�macs_to_nicsr�r7csni}t��dkst��dkr |Stdd�t��fdd��j���}dd��dd�tt|��fd	d�d
��D�S)aI
    Builds a dictionary containing macs as keys and nic orders as values,
    taking into account `network-card` and `device-number` if present.

    Note that the first NIC will be the primary NIC as it will be the one with
    [network-card] == 0 and device-number == 0 if present.

    @param macs_metadata: dictionary with mac address as key and contents like:
    {"device-number": "0", "interface-id": "...", "local-ipv4s": ...}
    @macs_to_nics: dictionary with mac address as key and nic name as value

    @return: Dictionary with macs as keys and nic orders as values.
    rcSs|ddk	S)Nr�r#)�mmdr#r#r$r��sz"_build_nic_order.<locals>.<lambda>cs|�j|��|fS)N)rT)�mac)r�r�r#r$r��scSs |j|d�}|dk	rt|�S|S)N)rTr�)Zdikt�keyZ	alt_value�valuer#r#r$�_get_key_as_int_or�sz,_build_nic_order.<locals>._get_key_as_int_orcSsi|]\}\}}}||�qSr#r#)r��ir�Z
_mac_metadataZ	_nic_namer#r#r$�
<dictcomp>�sz$_build_nic_order.<locals>.<dictcomp>cs@�|ddtd���|ddtd���tjkr6|dn|dfS)Nr�znetwork-cardZinfinityz
device-number�r)�floatrZNIC_NAME)r�)r�r�r#r$r��s)r�)r��filter�map�keys�	enumerate�sorted)r�r�r��	nic_orderZvalid_macs_metadatar#)r�r�r�r�r$�_build_nic_order�sr�TcCs�did�}|stj�}|dd}|s�x|j�D]\}}	|	|kr0Pq0Wddd|j�i|	d�}
|j|�}|jd	�rxd|
d
<|
|d|	<|S|jtjk}t|||�}
t	|j
��}�xz|D�]p}||}	|j|�}|s�q�|
|}|dk}d
|ddi}d|dd|j�i|	d�}
|�r.|jd��r.|�r.d|d<d|}g|
d<y|jj|	|d�}|d}Wn2t
k
�r�}ztjd|	|�WYdd}~XnX|
djd||d��|d}t|t��r�|gn|}x"|D]}|
dj||d���q�Wg|
d<|d}t|t��r|gn|}x"|D]}|
dj||d���qW|jd	��r�d|
d
<||
d<|�r�|jd��r�|�r�d|}|d }t|t��r�|gn|}x"|D]}|
dj||d���q�Wg|
d<|d	}t|t��r�|gn|}x"|D]}|
dj||d���q�Wt||�|
d!<|
d!�s|
jd!�|
|d|	<q�Wt|d�dk�r�xd|dj
�D]T}	|d|	jd"�|d|	jdd�|d|	jdd�|d|	jdd��qJW|S)#a8Convert ec2 metadata to network config version 2 data dict.

    @param: network_md: 'network' portion of EC2 metadata.
       generally formed as {"interfaces": {"macs": {}} where
       'macs' is a dictionary with mac address as key and contents like:
       {"device-number": "0", "interface-id": "...", "local-ipv4s": ...}
    @param: distro: instance of Distro.
    @param: macs_to_nics: Optional dict of mac addresses and nic names. If
       not provided, get_interfaces_by_mac is called to get it from the OS.
    @param: fallback_nic: Optionally provide the primary nic interface name.
       This nic will be guaranteed to minimally have a dhcp4 configuration.
    @param: full_network_config: Boolean set True to configure all networking
       presented by IMDS. This includes rendering secondary IPv4 and IPv6
       addresses on all NICs and rendering network config on secondary NICs.
       If False, only the primary nic will be configured and only with dhcp
       (IPv4/IPv6).

    @return A dict of network config version 2 based on the metadata and macs.
    r�)�version�	ethernetsrYrZTFZ
macaddress)�dhcp4�dhcp6�matchzset-name�ipv6sr�r�rzroute-metricr��d)r�zdhcp4-overridesr�r�zset-namez
device-numberz
use-routesZroutes)r3ZrouterszoCould not perform dhcp discovery on %s to find its gateway. Not adding default route via the gateway. Error: %sNz	0.0.0.0/0)�toZvia�tablezsubnet-ipv4-cidr-block)r�r�zrouting-policyzlocal-ipv4s)�fromr�zdhcp6-overrideszsubnet-ipv6-cidr-block�	addresseszdhcp4-overrides)rZget_interfaces_by_macr[rarTZnetwork_activatorr
ZNetplanActivatorr�r�r�Zdhcp_clientZdhcp_discoveryrrKr�r�r�r��get_secondary_addresses�popr�)Z
network_mdr3r�r�r�r�Znetcfgr�r�Znic_nameZ
dev_config�nic_metadataZ
is_netplanr�rZZnic_idxZis_primary_nicZ
dhcp_overrider�ZleaseZgatewayrqZsubnet_prefix_routesZprefix_route�ipv4sr>r�r?r#r#r$r��s�















r�cCs�|jd�}|jd�}g}tt|t�o.t|�dk�rH|jt|d||d��tt|t�o^t|�dk�rx|jt|d||d��t|�S)z�Parse interface-specific nic metadata and return any secondary IPs

    :return: List of secondary IPv4 or IPv6 addresses to configure on the
    interface
    zlocal-ipv4sr�r�zsubnet-ipv4-cidr-blockZ24zsubnet-ipv6-cidr-blockZ128)rTr�r�r�r��extend�_get_secondary_addressesr�)rr�rr�r�r#r#r$r�s

rc
Cs�g}|j|�}|}|s*t|jd��dkrPd|kr6dnd}tjd|||||�n|jd�d}x(|dd�D]}	|jdj|	|d	��qlW|S)
z�Return list of IP addresses as CIDRs for secondary IPs

    The CIDR prefix will be default_prefix if cidr_key is absent or not
    parseable in nic_metadata.
    r�r�r>r?zJCould not parse %s %s for mac %s. %s network config prefix defaults to /%sr�Nz
{ip}/{prefix})�ip�prefix)rTr��splitrKr�r�rl)
rZcidr_keyr�ZipsZdefault_prefixr�ZcidrrZip_typerr#r#r$r�s 
rcCstj|t�S)N)rZlist_from_depends�datasources)Zdependsr#r#r$�get_datasource_list�sr
)rrr)Br�r�r�ZtypingrrZ	cloudinitrrZloggingrrrrmr	r
Zcloudinit.eventrrZ
cloudinit.netr
Zcloudinit.net.dhcprZcloudinit.net.ephemeralrZcloudinit.sourcesrZcloudinit.sources.helpersrZ	getLoggerrrK�	frozensetZ	NOT_FOUNDZSKIP_METADATA_URL_CODESrIrJrr(rr�r9Z
DataSourcer)r�rGr�r�r�r�r�r�r�r;r�r/r�r�r�r�rrZDEP_FILESYSTEMZDEP_NETWORKr	r
r#r#r#r$�<module>sf
!!

2@3