name : DataSourceEc2.cpython-39.pyc
a

'�Dg���@sddlZddlZddlZddlZddlZddlmZddlmZm	Z	ddl
mZmZm
Z
ddl
mZddl
mZmZddlmZddlmZmZdd	lmZdd
lmZddlmZddlmZdd
lm Z e�!e"�Z#dZ$dZ%Gdd�d�Z&dd�Z'e&j(e&j)gZ*dZ+Gdd�de
j,�Z-Gdd�de-�Z.dd�Z/dd�Z0dd�Z1dd �Z2d!d"�Z3d#d$�Z4d%d&�Z5d'd(�Z6d)d*�Z7d+d,�Z8d-d.�Z9ej:fee;efee;e;feee;e<fd/�d0d1�Z=e>e;e>ee?e<dd2�d3d4�Z@ddd5ej:fd6d7�ZAd8d9�ZBd:d;�ZCe.e
jDffe-e
jDe
jEffgZFd<d=�ZGdS)>�N)�suppress)�Dict�List)�dmi�net�sources)�
url_helper)�util�warnings)�Distro)�
EventScope�	EventType)�netplan)�NoDHCPLeaseError)�EphemeralIPNetwork)�NicOrder)�ec2)Z
datasource�Ec2Z	strict_id�warnc@s,eZdZdZdZdZdZdZdZdZ	dZ
d	S)
�
CloudNamesZaliyunZawsZ	brightboxZzstack�e24cloudZoutscale�unknownzno-ec2-metadataN)�__name__�
__module__�__qualname__�ALIYUN�AWS�	BRIGHTBOX�ZSTACK�E24CLOUD�OUTSCALE�UNKNOWN�NO_EC2_METADATA�r#r#�C/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceEc2.pyr$srcCs|jdkod|jvS)N�zmeta-data/tags/)�code�url)�	exceptionr#r#r$�skip_404_tag_errors4sr)zS
ENV{ID_NET_DRIVER}=="vif|ena|ixgbevf", GOTO="cloudinit_hook"
GOTO="cloudinit_end"
cs�eZdZUdZgd�ZdZgd�Zeee	d<dZ
dZdZe
jZd	ZejejejejejhiZejejejhiZeZ�fd
d�Zedd��fd
d�Zdd�Zdd�Z dd�Z!e"dd��Z#e"dd��Z$e"dd��Z%e"dd��Z&e"dd��Z'e"dd ��Z(e"d!d"��Z)d#d$�Z*d%d&�Z+d'd(�Z,d)d*�Z-d+d,�Z.e"d-d.��Z/e"d/d0��Z0d1d2�Z1e"d3d4��Z2d5d6�Z3dBd7d8�Z4d9d:�Z5d;d<�Z6dCd=d>�Z7dDd@dA�Z8�Z9S)E�
DataSourceEc2r)zhttp://169.254.169.254zhttp://[fd00:ec2::254]zhttp://instance-data.:8773z
2009-04-04)z
2021-03-23z
2018-09-24z
2016-09-02�extended_metadata_versions���2NFcs,tt|��|||�d|_d|_tj|_dS�N)�superr*�__init__�metadata_address�identityr�MAC�_fallback_nic_order)�self�sys_cfg�distro�paths��	__class__r#r$r0vszDataSourceEc2.__init__)�ci_pkl_version�returncst��|�t|_tj|_dSr.)r/�	_unpickle�_EXTRA_HOTPLUG_UDEV_RULES�extra_hotplug_udev_rulesrr3r4)r5r;r9r#r$r=|szDataSourceEc2._unpicklecCst�S)z5Return the cloud name as identified during _get_data.)�identify_platform�r5r#r#r$�_get_cloud_name�szDataSourceEc2._get_cloud_namecCsFtt�|jtt�d�\}}t�d||j|j	�|dkrF|jt
jkrFdS|jt
jkrVdS|j
r�t��rrt�d�dSzbt|j|jjddd��8}|��|_t�d|jr�d	|j��nd
�Wd�n1s�0YWq�ty�YdS0n
|��|_|j�sdS|j�dd�|_|j�dd�|_|j�d
i��di��di�|_dS)N)rNz0strict_mode: %s, cloud_name=%s cloud_platform=%s�trueFz1FreeBSD doesn't support running dhclient with -sfT)�ipv4�ipv6zCrawled metadata service%s� ��	meta-data�	user-data�dynamic�instance-identity�document)�read_strict_moder	�get_cfg_by_pathr6�STRICT_ID_PATH�STRICT_ID_DEFAULT�LOG�debug�
cloud_name�platformrr!r"�perform_dhcp_setupZ
is_FreeBSDrr7�fallback_interface�crawl_metadataZ_crawled_metadataZ	state_msgr�get�metadataZuserdata_rawr2)r5Zstrict_modeZ_sleepZnetwr#r#r$�	_get_data�s^���
�
�&

���zDataSourceEc2._get_datacCsL|js
dS|j�di��di�}|�di���D]\}}d|vr0dSq0dS)z6Report if this instance type is Ec2 Classic (non-vpc).F�network�
interfaces�macszvpc-idT)rYrX�items)r5Z	ifaces_mdZ_macZmac_datar#r#r$�is_classic_instance�sz!DataSourceEc2.is_classic_instancecCs|js
dS|j�d�S)Nzami-launch-index)rYrXrAr#r#r$�launch_index�szDataSourceEc2.launch_indexcCs|jstj��|_|jSr.)Z_platform_typer*�dsname�lowerrAr#r#r$rT�szDataSourceEc2.platformcCsdS)Nzlatest/api/tokenr#rAr#r#r$�api_token_route�szDataSourceEc2.api_token_routecCsdS)NZ21600r#rAr#r#r$�imdsv2_token_ttl_seconds�sz&DataSourceEc2.imdsv2_token_ttl_secondscCsdS)NzX-aws-ec2-metadata-tokenr#rAr#r#r$�imdsv2_token_put_header�sz%DataSourceEc2.imdsv2_token_put_headercCs
|jdS)Nz-ttl-seconds)rerAr#r#r$�imdsv2_token_req_header�sz%DataSourceEc2.imdsv2_token_req_headercCs|j|jgSr.)rerfrAr#r#r$�imdsv2_token_redact�sz!DataSourceEc2.imdsv2_token_redactcCs�d}|��}|jD]�}|�|j|�}ztj|||jd�}Wn4tjyn}zt�	d||�WYd}~qd}~00|j
dkr�t�	d|�|S|j
dkrd}t�	|||j�q|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 %sr%z0Metadata api version %s not present. Headers: %s)
�_get_headersr+�formatr1�uhelp�readurlrg�UrlErrorrQrRr&rh�min_metadata_version)r5Zurl_tmplrhZapi_verr'Zresp�e�msgr#r#r$�get_metadata_api_version�s&

�
$

z&DataSourceEc2.get_metadata_api_versioncCsh|jtjkrZt|dd�sF|��}tj||j|j|j	|j
d��di�|_|j�d|j
d�S|j
dSdS)Nr2��
headers_cbri�exception_cbrLZ
instanceIdzinstance-id)rSrr�getattrrsr�get_instance_identityr1rkrg�_refresh_stale_aws_token_cbrXr2rY)r5�api_versionr#r#r$�get_instance_ids ��
�zDataSourceEc2.get_instance_idc
Cs�|jtvrdSg}i}|j}d}|D]"}d�||�}|�|�|||<q$t�d�d}d}|��}	z0tj	||	j
|	jtj|j
|j||jdd�	\}}Wntjy�Yn0|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�PUT�{0}/{1}zFetching Ec2 IMDSv2 API TokenF)	�urls�max_wait�timeout�	status_cbrurv�request_methodriZconnect_synchronously)rS� IDMSV2_SUPPORTED_CLOUD_PLATFORMSrcrl�appendrQrR�get_url_paramsrm�wait_for_url�max_wait_seconds�timeout_seconds�warningrk�_imds_exception_cbrgro�
_api_token)
r5�mdurlsr~�url2base�url_pathr�r'�cur�response�
url_paramsr#r#r$�_maybe_fetch_api_tokens@



�z$DataSourceEc2._maybe_fetch_api_tokenc	Csvg}d}|j}|��}|jdkr$dS|�d|j�}dd�|D�}t|�t|�krlt�dtt|�t|���t	|�rz|}nt�
d�|j}|�|�}|�s |jt
v�r i}dj|jd	�}	d
}
|D]"}d�||	�}|�|�|||<q�t��}tj||j|jtj
|j|j|
d�\}}
|�r ||}|�r<||_t�d
|j�n2|jt
v�rTt�
d�nt�d|tt��|��t|�S)NrF�
metadata_urlscSsg|]}t�|�r|�qSr#)r	Zis_resolvable_url)�.0�xr#r#r$�
<listcomp>V�z;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)ZverZGETr})r~rr�r�rirur�zUsing metadata source: '%s'z)IMDS's HTTP endpoint is probably disabledz(Giving up on md from %s after %s seconds)�ds_cfgr�r�rXr��setrQrR�list�lenr�r�rSr�rlrpr��time�	monotonicrmr�r�rgrkr1Zcritical�int�bool)r5r~Z
start_timeZmcfgr�r�Zfilteredr1r�r�r�r'r��_r#r#r$�wait_for_metadata_serviceKsl
�

���

�

�z'DataSourceEc2.wait_for_metadata_servicecCs�d|jvrdSd}|jd}t|t�s6t�d|�dS|��D],\}}||krV|}ql|dkr>|dkr>|}q>|dur�t�d|�dS|}|�d�s�d|}tj�	|�r�|S|�
tj�|��}|r�t�d||�|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)rY�
isinstance�dictrQrRr^�
startswith�os�path�existsZ
_remap_device�basename)r5�name�foundZbdmZentnameZdeviceZofoundZremappedr#r#r$�device_name_to_device�s8



z#DataSourceEc2.device_name_to_devicecCsTz:|jtjkr(|j�d|jdd�WS|jddWSWntyNYdS0dS)NZavailabilityZoneZ	placementzavailability-zone)rSrrr2rXrY�KeyErrorrAr#r#r$�availability_zone�s�zDataSourceEc2.availability_zonecCsR|jtjkr4|j�d�}|jr0|s0|jdd�}|S|j}|durN|dd�SdS)N�region���)rSrrr2rXr�)r5r��azr#r#r$r��s
zDataSourceEc2.regioncCs,|sdS|jtjkr(tt�|tt�|�dSr.)rSrr!�warn_if_necessaryr	rNrOrP)r5�cfgZis_new_instancer#r#r$�activate�s�zDataSourceEc2.activatec	Cs�|jtjkr|jS|jdur*t�d�dSd}|jj}|j�d�}t	|t
�r�t||j|t�
|jdd�|jd�}|��r�t�|j�|_|jtj�tj�|jtj�tj�nt�d|�||_|jS)z@Return a network config dict for rendering ENI or netplan files.Nz8Unexpected call to network_config when metadata is None.r[Zapply_full_imds_network_configT)�fallback_nic�full_network_config�fallback_nic_orderz%Metadata 'network' key not valid: %s.)�_network_configr�UNSETrYrQr�r7rVrXr�r��#convert_ec2_metadata_network_configr	Zget_cfg_option_boolr�r4r_�copy�deepcopy�default_update_eventsr�NETWORK�addr
�BOOT�BOOT_LEGACY)r5�resultZifaceZnet_mdr#r#r$�network_config�sB
�
�����zDataSourceEc2.network_configc	Cs|��siS|��}|j}i}|jtvr:|j}|j}d}n&|jtjkrTd}}t	}nd}}}zxt
j||j|j
||d�}t�|�|d<t
j||j|j
|||d�|d<|jtjkr�t
j||j|j
||d�}d|i|d<Wn(t�yt�td|j�iYS0||d	<|S)
z�Crawl metadata service when available.

        @returns: Dictionary of crawled metadata content containing the keys:
          meta-data, user-data and dynamic.
        NrtrI)rurirvZretrieval_exception_ignore_cbrHrKrJz'Failed reading from metadata address %sZ_metadata_api_version)r�rsrgrSr�ry�#_skip_or_refresh_stale_aws_token_cbrr r)rZget_instance_userdatar1rkr	Zmaybe_b64decodeZget_instance_metadatarrx�	ExceptionZlogexcrQ)	r5rzZredactZcrawled_metadataZexc_cbZ	exc_cb_udZskip_cbZraw_userdatar2r#r#r$rWs`
��
��
zDataSourceEc2.crawl_metadatac
Cs�|jtvrdS|dur|j}t�d�|j|i}d�|j|j�}zt	j
|||jdd�}Wn6t	jy�}zt�
d||�WYd}~dSd}~00|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{}/{}r|)rhrir�z/Unable to get API token: %s raised exception %s)rSr�rdrQrRrfrlr1rcrmrnrgror��contents)r5ZsecondsZrequest_headerZ	token_urlr�rqr#r#r$�_refresh_api_tokenTs(


�
�z DataSourceEc2._refresh_api_tokencCs$t�tj||�}|sdS|�||�S)zSCallback will not retry on SKIP_USERDATA_CODES or if no token
        is available.F)rZskip_retry_on_codesZSKIP_USERDATA_CODESry)r5rrr(Zretryr#r#r$r�qs�z1DataSourceEc2._skip_or_refresh_stale_aws_token_cbcCs*t|tj�r&|jdkr&t�d�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&rQrRr��r5rrr(r#r#r$ry{s
z)DataSourceEc2._refresh_stale_aws_token_cbcCsDt|tj�r@|jr@|jdkr@|jdkr2t�d�n
t�d�|�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&rQr�r�r#r#r$r��s

��z DataSourceEc2._imds_exception_cbrGcCsN|jtvriS|j|ji}|j|vr(|S|jsB|��|_|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.
        )rSr�rfrdrcr�r�re)r5r'Zrequest_token_headerr#r#r$rk�s
�

zDataSourceEc2._get_headers)N)N)rG):rrrrar�rpr+r�str�__annotations__Zurl_max_waitZurl_timeoutr�rr�r�rUrr�r
ZBOOT_NEW_INSTANCEr�r�ZHOTPLUGZsupported_update_eventsr�r>r?r0r�r=rBrZr_�propertyr`rTrcrdrerfrgrsr{r�r�r�r�r�r�r�rWr�r�ryr�rk�
__classcell__r#r#r9r$r*Csv
��
��3







2J3


	
/8


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|vr*t�d||j�dStt|���S)Nz+Local Ec2 mode only supported on %s, not %sF)	rrr rSrQrRr/r��get_data)r5Zsupported_platformsr9r#r$r��s
�zDataSourceEc2Local.get_data)rrr�__doc__rUr�r�r#r#r9r$r��sr�c
CsBz
t|�WSty<}zt�|�|WYd}~Sd}~00dSr.)�parse_strict_mode�
ValueErrorrQr�)�cfgval�defaultrqr#r#r$rM�s


rMc
Cs�|durdS|durdS|s dS|�d�\}}}|dvrHtd||f��|r�zt|�}Wq�ty�}ztd	||f�|�WYd}~q�d}~00nd}||fS)
NT)rCNF)�falseN)rr�,)rCr�rzUInvalid 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)�	partitionr�r�)r��moder��sleeprqr#r#r$r��s4�����r�c
Csdzt|�\}}Wn0ty@}zt�|�WYd}~dSd}~00|dkrNdStjd|d|d�dS)Nr�Z
non_ec2_mdT)r�r�)r�r�rQr�r
Zshow_warning)r�r�r�r�rqr#r#r$r��s
r�cCs|ddkrtjSdS)N�product_namezAlibaba Cloud ECS)rr��datar#r#r$�identify_aliyunsr�cCsn|d}|�d�rtjStt��:t�|�j���d�rLtjWd�SWd�n1s`0YdS)N�uuidr)	r�rrrr�r�ZUUIDZbytes_le�hex)r�Zuuid_strr#r#r$�identify_awss

2r�cCs|d�d�rtjSdS)N�serialz.brightbox.com)�endswithrrr�r#r#r$�identify_brightboxsr�cCs|d�d�rtjSdS)N�	asset_tagz
.zstack.io)r�rrr�r#r#r$�identify_zstacksr�cCs|ddkrtjSdS)N�vendorr)rrr�r#r#r$�identify_e24cloud"sr�cCs*|dd��kr&|dd��kr&tjSdS)Nr�z3DS Outscale VMr�z3DS Outscale)rbrr r�r#r#r$�identify_outscale's
��r�cCszt�}ttttttdd�f}|D]T}z||�}|r<|WSWq tyr}zt�	d|||�WYd}~q d}~00q dS)NcSstjSr.)rr!)r�r#r#r$�<lambda>9r�z#identify_platform.<locals>.<lambda>z'calling %s with %s raised exception: %s)
�_collect_platform_datar�r�r�r�r�r�r�rQr�)r�ZchecksZcheckerr�rqr#r#r$r@/s$�	�r@cCs�d}ttt��t�d���}Wd�n1s20Y|pLt�d�pLd}t�d�pZd}t�d�phd}t�d�pvd}t�d�p�d}|��|��|��|��|��d	�S)
aFReturns 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
       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.
    Nz/sys/hypervisor/uuidzsystem-uuidrGzsystem-serial-numberzchassis-asset-tagzsystem-manufacturerzsystem-product-name�r�r�r�r�r�)	r�OSError�UnicodeDecodeErrorr	Zload_text_file�striprZ
read_dmi_datarbr�r#r#r$r�Fs
,�r�)�
macs_metadata�macs_to_nicsr�r<csni}t��dkst��dkr |Stdd�t��fdd������}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|dduS)N�r#�Zmmdr#r#r$r�~r�z"_build_nic_order.<locals>.<lambda>cs|��|��|fSr.)rX)�mac)r�r�r#r$r��r�cSs |�|d�}|durt|�S|Sr.)rXr�)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>�s�z$_build_nic_order.<locals>.<dictcomp>cs@�|ddtd���|ddtd���tjkr6|dn|dfS)Nr�znetwork-cardZinfinity�
device-number�r)�floatrZNIC_NAMEr�)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_orderfs&��
���r)�
dev_config�nic_name�nic_metadatar7�is_ipv4�tabler<c

CsJ|r|�d�}|�d�}n|�d�}|�d�}|r6|sNt�d|rDdnd�dS|�d	�s`g|d	<|r�z|jj||d
�}|d}	Wn2ty�}
zt�d||
�WYd}
~
n d}
~
00|d	�d
|	|d��t|t	�r�|gn|}|D]}|d	�||d��q�|�d��sg|d<t|t	��r"|gn|}|D]}|d�||d���q*dS)a�
    Configure policy-based routing on secondary NICs / secondary IPs to
    ensure outgoing packets are routed via the correct interface.

    @param: dev_config: network cfg v2 to be updated inplace.
    @param: nic_name: nic name. Only used if ipv4.
    @param: nic_metadata: nic metadata from IMDS.
    @param: distro: Instance of Distro. Only used if ipv4.
    @param: is_ipv4: Boolean indicating if we are acting over ipv4 or not.
    @param: table: Routing table id.
    �subnet-ipv4-cidr-block�local-ipv4szsubnet-ipv6-cidr-blocks�ipv6szANot enough IMDS information to configure policy routing for IPv%s�4�6N�routes)r7ZrouterszoCould not perform dhcp discovery on %s to find its gateway. Not adding default route via the gateway. Error: %sz	0.0.0.0/0)�toZviar)r
r�routing-policy)�fromr)
rXrQrRZdhcp_clientZdhcp_discoveryrr�r�r�r�)
rrrr7rrZsubnet_prefix_routes�ipsZleaseZgatewayrqZprefix_route�ipr#r#r$�_configure_policy_routing�s`



�
�	��
������rTc	Cs2did�}|st��}|dd}|s�|��D]\}}	|	|kr.qDq.ddd|��i|	d�}
|�|�}|�d	�rvd|
d
<|
|d|	<|St|jtj�}t	|||�}
t
|���}|D�]}||}	|�|�}|s�q�|
|}|dk}d
|ddi}d|dd|��i|	d�}
d|}|�rB|�d��rB|�sBd|d<t|
||	|d|d�|�d	��r�d|
d
<||
d<|�r�|�d��r�|�s�t|
||	|d|d�t
||�|
d<|
d�s�|
�d�|
|d|	<q�t|d�dk�r.|d��D]T}	|d|	�d�|d|	�dd�|d|	�dd�|d|	�dd��q�|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�	ethernetsr\r]TFZ
macaddress)�dhcp4�dhcp6�match�set-namer	rrrzroute-metricr��d)r�dhcp4-overridesrrrr�z
use-routes)r7rrrrzdhcp6-overrides�	addressesrNrr)rZget_interfaces_by_macr^rbrXr�Znetwork_rendererrZRendererrr�r�r�get_secondary_addresses�popr�)Z
network_mdr7r�r�r�r�Znetcfgr�r�rrrZ
is_netplanrr]Znic_idxZis_primary_nicZ
dhcp_overriderr#r#r$r��s�

�

�


���������

r�cCs�|�d�}|�d�}g}tt|t�o.t|�dk�rH|�t|d||d��tt|t�o^t|�dk�rx|�t|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
    rr	r�rZ24zsubnet-ipv6-cidr-blockZ128)rXr�r�r�r��extend�_get_secondary_addressesr�)rr�Zipv4sr	rr#r#r$rzs 


��
��rc
Cs�g}|�|�}|}|r(t|�d��dkrNd|vr4dnd}t�d|||||�n|�d�d}|dd�D]}	|�dj|	|d	��qh|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�rDrEzJCould not parse %s %s for mac %s. %s network config prefix defaults to /%sr�Nz
{ip}/{prefix})r�prefix)rXr��splitrQr�r�rl)
rZcidr_keyr�r�default_prefixrZcidrr Zip_typerr#r#r$r�s"
�
rcCst�|t�Sr.)rZlist_from_depends�datasources)Zdependsr#r#r$�get_datasource_list�sr$)Hr�Zloggingr�r�r��
contextlibr�typingrrZ	cloudinitrrrrrmr	r
Zcloudinit.distrosrZcloudinit.eventrr
Z
cloudinit.netrZcloudinit.net.dhcprZcloudinit.net.ephemeralrZcloudinit.sourcesrZcloudinit.sources.helpersrZ	getLoggerrrQrOrPrr)rrr�r>Z
DataSourcer*r�rMr�r�r�r�r�r�r�r�r@r�r3r�r�rr�r�rr�rrZDEP_FILESYSTEMZDEP_NETWORKr#r$r#r#r#r$�<module>s�
w!
#�


�D�X�
|
�

© 2025 Cubjrnet7