%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/cloudinit/sources/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/cloudinit/sources/__pycache__/DataSourceEc2.cpython-312.pyc

�

���f������ddlZddlZddlZddlZddlZddlmZddlmZm	Z	ddl
mZmZm
Z
ddl
mZddl
mZmZddlmZddlmZmZdd	lmZdd
lmZddlmZddlmZdd
lm Z ejBe"�Z#dZ$dZ%Gd�d�Z&d�Z'e&jPe&jRgZ*dZ+Gd�de
jX�Z-Gd�de-�Z.d�Z/d�Z0d�Z1d�Z2d�Z3d�Z4d�Z5d�Z6d �Z7d!�Z8d"�Z9ejtfd#ee;efd$ee;e;fd%ed&ee;e<ffd'�Z=d(e>d)e;d*e>d+ed,e?d-e<d&dfd.�Z@ddd/ejtfd0�ZAd1�ZBd2�ZCe.e
j�ffe-e
j�e
j�ffgZFd3�ZGy)4�N)�suppress)�Dict�List)�dmi�net�sources)�
url_helper)�util�warnings)�Distro)�
EventScope�	EventType)�netplan)�NoDHCPLeaseError)�EphemeralIPNetwork)�NicOrder)�ec2)�
datasource�Ec2�	strict_id�warnc�,�eZdZdZdZdZdZdZdZdZ	dZ
y	)
�
CloudNames�aliyun�aws�	brightbox�zstack�e24cloud�outscale�unknownzno-ec2-metadataN)�__name__�
__module__�__qualname__�ALIYUN�AWS�	BRIGHTBOX�ZSTACK�E24CLOUD�OUTSCALE�UNKNOWN�NO_EC2_METADATA���A/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceEc2.pyrr$s.��
�F�
�C��I�
�F��H��H��G�(�Or-rc�@�|jdk(xrd|jvS)N�zmeta-data/tags/)�code�url)�	exceptions r.�skip_404_tag_errorsr44s ���>�>�S� �G�%6�)�-�-�%G�Gr-zS
ENV{ID_NET_DRIVER}=="vif|ena|ixgbevf", GOTO="cloudinit_hook"
GOTO="cloudinit_end"
c�D��eZdZUdZgd�ZdZgd�Zeee	d<dZ
dZdZe
jZd	Zej$ej(ej*ej,ej.hiZej$ej(ej.hiZeZ�fd
�Zdeddf�fd
�Zd�Zd�Z d�Z!e"d��Z#e"d��Z$e"d��Z%e"d��Z&e"d��Z'e"d��Z(e"d��Z)d�Z*d�Z+d�Z,d�Z-d�Z.e"d��Z/e"d��Z0d�Z1e"d ��Z2d!�Z3d'd"�Z4d#�Z5d$�Z6d'd%�Z7d(d&�Z8�xZ9S))�
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�x�2NFc�v��tt|�|||�d|_d|_t
j|_y�N)�superr6�__init__�metadata_address�identityr�MAC�_fallback_nic_order)�self�sys_cfg�distro�paths�	__class__s    �r.r=zDataSourceEc2.__init__vs1���
�m�T�+�G�V�U�C� $�����
�#+�<�<�� r-�ci_pkl_version�returnc�d��t�|�|�t|_tj
|_yr;)r<�	_unpickle�_EXTRA_HOTPLUG_UDEV_RULES�extra_hotplug_udev_rulesrr@rA)rBrGrFs  �r.rJzDataSourceEc2._unpickle|s$���
���.�)�(A��%�#+�<�<�� r-c��t�S)z5Return the cloud name as identified during _get_data.)�identify_platform�rBs r.�_get_cloud_namezDataSourceEc2._get_cloud_name�s�� �"�"r-c��ttj|jtt
�d�\}}tjd||j|j�|dk(r|jtjk(ry|jtjk(ry|jr�tj�rtjd�y	t|j |j j"dd��5}|j$rd|j$��nd	}tj&tjd
|��|j(��|_ddd�n5tj&tjd
|j(��|_|j*sy|j*j/dd�|_|j*j/d
d�|_|j*j/di�j/di�j/di�|_y#1swY��xYw#t,$rYywxYw)N)rNz0strict_mode: %s, cloud_name=%s cloud_platform=%s�trueFz1FreeBSD doesn't support running dhclient with -sfT)�ipv4�ipv6� �zCrawl of metadata service��logfunc�msg�func�	meta-data�	user-data�dynamic�instance-identity�document)�read_strict_moder
�get_cfg_by_pathrC�STRICT_ID_PATH�STRICT_ID_DEFAULT�LOG�debug�
cloud_name�platformrr*r+�perform_dhcp_setup�
is_FreeBSDrrD�fallback_interface�	state_msg�log_time�crawl_metadata�_crawled_metadatar�get�metadata�userdata_rawr?)rB�strict_mode�_sleep�netwrks     r.�	_get_datazDataSourceEc2._get_data�s���.�� � ����n�.?�
�
�	
���V�	�	�	�>���O�O��M�M�		
��&� �T�_�_�
�8J�8J�%J��
�_�_�
� :� :�
:���"�"���� ��	�	�M�N��
�'��K�K��K�K�2�2���	��
�8<���!�D�N�N�#3� 4�B�I�-1�]�]� #�	�	�7�	�{�C�!�0�0�.�D�*��� &*�]�]��	�	�/��(�(�&�D�"�
�%�%���.�.�2�2�;��E��
� �2�2�6�6�{�D�I����"�"�&�&�y�"�5�
�S�$�b�
)�
�S��R�
 �	
�
�
�=���$�
��
�s+�-H9�AH-�H9�-H6�2H9�9	I�Ic���|jsy|jjdi�jdi�}|jdi�j�D]\}}d|vs�yy)z6Report if this instance type is Ec2 Classic (non-vpc).F�network�
interfaces�macszvpc-idT)rpro�items)rB�	ifaces_md�_mac�mac_datas    r.�is_classic_instancez!DataSourceEc2.is_classic_instance�sh���}�}���M�M�%�%�i��4�8�8��r�J�	�'�m�m�F�B�7�=�=�?�	�N�D�(��8�#��	�r-c�R�|jsy|jjd�S)Nzami-launch-index)rprorOs r.�launch_indexzDataSourceEc2.launch_index�s"���}�}���}�}� � �!3�4�4r-c�x�|js#tjj�|_|jSr;)�_platform_typer6�dsname�lowerrOs r.rgzDataSourceEc2.platform�s.���"�"�"/�"6�"6�"<�"<�">�D���"�"�"r-c��y)Nzlatest/api/tokenr,rOs r.�api_token_routezDataSourceEc2.api_token_route�s��!r-c��y)N�21600r,rOs r.�imdsv2_token_ttl_secondsz&DataSourceEc2.imdsv2_token_ttl_seconds�s��r-c��y)NzX-aws-ec2-metadata-tokenr,rOs r.�imdsv2_token_put_headerz%DataSourceEc2.imdsv2_token_put_header�s��)r-c� �|jdzS)Nz-ttl-seconds)r�rOs r.�imdsv2_token_req_headerz%DataSourceEc2.imdsv2_token_req_header�s���+�+�n�<�<r-c�2�|j|jgSr;)r�r�rOs r.�imdsv2_token_redactz!DataSourceEc2.imdsv2_token_redact�s���,�,�d�.J�.J�K�Kr-c��d}|j�}|jD]�}|j|j|�}	t	j
|||j��}|jdk(rtjd|�|cS|jdk(s�{d}tj|||j���|jS#tj$r!}tjd||�Yd}~��d}~wwxYw)	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)r2�headers�headers_redact��z#Found preferred metadata version %sr0z0Metadata api version %s not present. Headers: %szurl %s raised exception %sN)
�_get_headersr7�formatr>�uhelp�readurlr�r1rdrer��UrlError�min_metadata_version)rB�url_tmplr��api_verr2�resprY�es        r.�get_metadata_api_versionz&DataSourceEc2.get_metadata_api_version�s���3���#�#�%���6�6�	:�G��/�/�$�"7�"7��A�C�
:��}�}��#�#'�#;�#;����9�9��#��I�I�C�W�M�"�N��Y�Y�#�%�L�C��I�I�c�7�D�L�L�9�!	:�"�(�(�(���>�>�
@��	�	�6��Q�?�?��
@�s�"C�C?�C:�:C?c��|jtjk(r�t|dd�sg|j	�}tj||j|j|j|j��jdi�|_|jjd|jd�S|jdS)Nr?��
headers_cbr��exception_cbr_�
instanceIdzinstance-id)rfrr%�getattrr�r�get_instance_identityr>r�r��_refresh_stale_aws_token_cbror?rp)rB�api_versions  r.�get_instance_idzDataSourceEc2.get_instance_id
s����?�?�j�n�n�,��4��T�2�#�;�;�=�� #� 9� 9���)�)�#�0�0�#'�#;�#;�!%�!A�!A�!��#�j�"�%�
�
��=�=�$�$��d�m�m�M�:��
��=�=��/�/r-c��|jtvryg}i}|j}d}|D]*}dj||�}|j	|�|||<�,t
j
d�d}d}|j�}		tj||	j|	jt
j|j|j||jd��	\}}|r|r||_||Sy#tj $rY�&wxYw)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_cbr�r��request_methodr��connect_synchronously)rf� IDMSV2_SUPPORTED_CLOUD_PLATFORMSr�r��appendrdre�get_url_paramsr��wait_for_url�max_wait_seconds�timeout_seconds�warningr��_imds_exception_cbr�r��
_api_token)
rB�mdurlsr��url2base�url_pathr�r2�cur�response�
url_paramss
          r.�_maybe_fetch_api_tokenz$DataSourceEc2._maybe_fetch_api_tokens���?�?�"B�B�������'�'�����	 �C��"�"�3��1�C��K�K����H�S�M�	 �	�	�	�1�2������(�(�*�
�	�!�.�.��#�4�4�"�2�2��+�+��,�,�!�4�4�-�#�7�7�&+�
�M�C�� �8�&�D�O��C�=� ����~�~�	�
�	�s�?A!C1�1D�Dc	��g}d}|j}|j�}|jdkry|jd|j�}|D�cgc]}tj|�s�|��}}t|�t|�k7r4tjdtt|�t|�z
��t|�r|}n!tjd�|j}|j|�}|s�|jtvr�i}	dj!|j"��}
d}|D]*}d	j!||
�}
|j%|
�||	|
<�,t'j(�}t+j,||j|j.tj|j0|j2|�
�\}}|r|	|}|r2||_tjd|j4�t;|�S|jtvr tjd�t;|�Stj7d
|t9t'j(�|z
��t;|�Scc}w)NrF�
metadata_urlsz,Removed the following from metadata urls: %sz+Empty metadata url list! using default listz{ver}/meta-data/instance-id)�ver�GETr�)r�r�r�r�r�r�r�zUsing metadata source: '%s'z)IMDS's HTTP endpoint is probably disabledz(Giving up on md from %s after %s seconds)�ds_cfgr�r�ror�r
�is_resolvable_url�setrdre�list�lenr�r�rfr�r�r�r��time�	monotonicr�r�r�r�r�r>�critical�int�bool)rBr��
start_time�mcfgr�r��x�filteredr>r�r�r�r2r��_s               r.�wait_for_metadata_servicez'DataSourceEc2.wait_for_metadata_servicePsD�����
��{�{���(�(�*�
��&�&�!�+�����/�4�+=�+=�>��%�C�!��)?�)?��)B�A�C��C��x�=�C��K�'��I�I�>��c�&�k�C��M�1�3�
�
�x�=��F��K�K�E�F��'�'�F� �6�6�v�>��!����'G�G��H�4�;�;��-�-�<��H�#�N��
$���&�&�s�H�5�����C� � #���
�
$�
���)�J��'�'��#�4�4�"�2�2��+�+�#�7�7��,�,�-��F�C���#+�C�=� ��$4�D�!��I�I�3�T�5J�5J�K��$�%�%��_�_� @�
@��K�K�C�D��$�%�%�

�L�L�:���D�N�N�$�z�1�2�
��$�%�%��{Ds�I6�,I6c�4�d|jvryd}|jd}t|t�stj	d|�y|j�D]\}}||k(r|}n|dk(s�|dk(s�|}�|�tj	d|�y|}|j
d�sd|z}tjj|�r|S|jtjj|��}|rtj	d||�|S|dk(ry|S)	Nzblock-device-mappingz+block-device-mapping not a dictionary: '%s'�	ephemeral�
ephemeral0z Unable to convert %s to a device�/z/dev/%szRemapped device name %s => %s)rp�
isinstance�dictrdrerz�
startswith�os�path�exists�
_remap_device�basename)rB�name�found�bdm�entname�device�ofound�remappeds        r.�device_name_to_devicez#DataSourceEc2.device_name_to_device�s��"����6�����m�m�2�3���#�t�$��I�I�C�S�I��"�y�y�{�	�O�G�V��$������+�%�$�,�*>���
	��=��I�I�8�$�?��������$���%�E�
�7�7�>�>�%� ��L��%�%�b�g�g�&6�&6�u�&=�>����I�I�5�u�h�G��O��<����
r-c���	|jtjk(r,|jj	d|j
dd�S|j
ddS#t$rYywxYw)N�availabilityZone�	placementzavailability-zone)rfrr%r?rorp�KeyErrorrOs r.�availability_zonezDataSourceEc2.availability_zone�sl��		����*�.�.�0��}�}�(�(�&��M�M�+�.�/B�C���
�}�}�[�1�2E�F�F���	��	�s�AA�A�	A)�(A)c���|jtjk(r:|jj	d�}|j
r|s|j
dd}|S|j
}|�|ddSy)N�region���)rfrr%r?ror�)rBr��azs   r.r�zDataSourceEc2.region�sf���?�?�j�n�n�,��]�]�&�&�x�0�F��%�%�f��/�/���4���M��'�'�B��~��#�2�w��r-c��|sy|jtjk(r*tt	j
|tt�|�yyr;)rfrr*�warn_if_necessaryr
rarbrc)rB�cfg�is_new_instances   r.�activatezDataSourceEc2.activate�s=�����?�?�j�0�0�0���$�$�S�.�:K�L��
�1r-c
�B�|jtjk7r|jS|j�tjd�yd}t
d|jvxr|jtjk(�}|rEtjd�tjtjd|j��|jj}|jj!d�}t#|t$�r�t'||j|tj(|j*dd�|j,�	�}|j/�r�t1j2|j4�|_|j4t6j8j;t<j>�|j4t6j8j;t<j@�ntjd
|�||_|jS)z@Return a network config dict for rendering ENI or netplan files.Nz8Unexpected call to network_config when metadata is None.rwzPMetadata 'network' not present: Refreshing stale metadata from prior to upgrade.zRe-crawl of metadata servicerW�apply_full_imds_network_configT)�fallback_nic�full_network_config�fallback_nic_orderz%Metadata 'network' key not valid: %s.)!�_network_configr�UNSETrprdr�r�rfrr%rer
rl�get_datarDrjror�r��#convert_ec2_metadata_network_config�get_cfg_option_boolr�rAr~�copy�deepcopy�default_update_eventsr
�NETWORK�addr�BOOT�BOOT_LEGACY)rB�result�no_network_metadata_on_aws�iface�net_mds     r.�network_configzDataSourceEc2.network_config�s������7�=�=�0��'�'�'��=�=� ��K�K�J�
����%)��T�]�]�*�
2����:�>�>�1�&
�"�&��I�I�D�
�
�M�M��	�	�2��]�]�
����.�.�����"�"�9�-���f�d�#�9�����"�$(�$<�$<��K�K�!A�4�%�$(�#;�#;��F��'�'�)�-1�]�]��.�.�.��*��*�*�:�+=�+=�>�B�B��N�N���*�*�:�+=�+=�>�B�B��)�)��
�K�K�?��H�%����#�#�#r-c�(�|j�siS|j�}|j}i}|jtvr|j
}|j}d}n.|jtjk(rdx}}t}ndx}x}}	tj||j|j||��}tj|�|d<tj ||j|j|||��|d<|jtj"k(r5tj$||j|j||��}d|i|d<||d	<|S#t&$r*tj(t*d|j�icYSwxYw)
z�Crawl metadata service when available.

        @returns: Dictionary of crawled metadata content containing the keys:
          meta-data, user-data and dynamic.
        Nr�r\)r�r�r��retrieval_exception_ignore_cbr[r^r]z'Failed reading from metadata address %s�_metadata_api_version)r�r�r�rfr�r��#_skip_or_refresh_stale_aws_token_cbrr)r4r�get_instance_userdatar>r�r
�maybe_b64decode�get_instance_metadatar%r��	Exception�logexcrd)	rBr��redact�crawled_metadata�exc_cb�	exc_cb_ud�skip_cb�raw_userdatar?s	         r.rmzDataSourceEc2.crawl_metadata0s����-�-�/��I��3�3�5���)�)�����?�?�>�>��5�5�F��@�@�I��G�
�_�_�
� 3� 3�
3�!%�%�F�Y�)�G�+/�/�F�/�Y�� 	��4�4���%�%��,�,�%�&��L�-1�,@�,@��,N��[�)�,/�,E�,E���%�%��,�,�%�#�.5�
-��[�)����*�.�.�0��4�4���)�)�#�0�0�#)�!'���0C�H�.M� ��+�5@��0�1�����	��K�K��9��%�%�
�
�I�
	�s�
C
E�0F�Fc��|jtvry|�|j}tj	d�|j
|i}dj
|j|j�}	tj|||jd��}|jS#tj$r!}tjd||�Yd}~yd}~wwxYw)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�)r�r�r�z/Unable to get API token: %s raised exception %s)rfr�r�rdrer�r�r>r�r�r�r�r�r��contents)rB�seconds�request_header�	token_urlr�r�s      r.�_refresh_api_tokenz DataSourceEc2._refresh_api_tokenhs����?�?�"B�B���?��3�3�G��	�	�5�6��6�6��@���N�N�4�#8�#8�$�:N�:N�O�	�	��}�}��&�#�7�7�$�	�H�� � � ���~�~�	��K�K�A�9�a�
���		�s�,#B�C�.C
�
Cc�v�tjtj||�}|sy|j||�S)zSCallback will not retry on SKIP_USERDATA_CODES or if no token
        is available.F)r�skip_retry_on_codes�SKIP_USERDATA_CODESr�)rBrYr3�retrys    r.rz1DataSourceEc2._skip_or_refresh_stale_aws_token_cb�s<���'�'��#�#�S�)�
�����/�/��Y�?�?r-c��t|tj�r+|jdk(rtjd�d|_y)z=Exception handler for Ec2 to refresh token if token is stale.i�z+Clearing cached Ec2 API token due to expiryNT)r�r�r�r1rdrer��rBrYr3s   r.r�z)DataSourceEc2._refresh_stale_aws_token_cb�s2���i����0�Y�^�^�s�5J��I�I�C�D�"�D�O�r-c���t|tj�rZ|jrM|jdk\r=|jdk(rtjd�|�tjd�|�yyy)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�r�r�r1rdr�r(s   r.r�z DataSourceEc2._imds_exception_cb�sl���i����0��~�~�)�.�.�C�"7��>�>�S�(��K�K�?�� ���K�K�L�� ��#8�~�1r-c��|jtvriS|j|ji}|j|vr|S|j
s#|j
�|_|j
siS|j|j
iS)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.
        )rfr�r�r�r�r�r"r�)rBr2�request_token_headers   r.r�zDataSourceEc2._get_headers�s����?�?�"B�B��I�
�(�(�$�*G�*G� 
�����3�&�'�'����#�5�5�7�D�O��?�?��	��,�,�d�o�o�>�>r-r;)rV):r!r"r#r�r�r�r7r�str�__annotations__�url_max_wait�url_timeoutr�rr�r�rhr
rr�BOOT_NEW_INSTANCErr�HOTPLUG�supported_update_eventsrrKrLr=r�rJrPrur~�propertyr�rgr�r�r�r�r�r�r�r�r�r�r�r�r�r
rmr"rr�r�r��
__classcell__�rFs@r.r6r6Cs����
�F��M�(��
-���S�	���L��K��J��m�m�O���	����'�'��N�N��!�!����	
���	����'�'����
��� 9��0�0��0��0�
#�6�p��5��5�
�#��#��"��"������*��*��=��=��L��L�)�>0�(0�dH&�T1�f�
��
�������<$��<$�|6 �p!�:@�� �6?r-r6c�&��eZdZdZdZ�fd�Z�xZS)�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.
    Tc����tjtjf}|j|vr"tjd||j�ytt|�#�S)Nz+Local Ec2 mode only supported on %s, not %sF)	rr%r)rfrdrer<r7r�)rB�supported_platformsrFs  �r.r�zDataSourceEc2Local.get_data�sT���)�~�~�z�/B�/B�C���?�?�"5�5��I�I�=�#����
�
��'��7�9�9r-)r!r"r#�__doc__rhr�r4r5s@r.r7r7�s������	:�	:r-r7c�t�	t|�S#t$r!}tj|�|cYd}~Sd}~wwxYwr;)�parse_strict_mode�
ValueErrorrdr�)�cfgval�defaultr�s   r.r`r`�s2��� ��(�(�������A������s�

�	7�2�7�7c���|dury|dury|sy|jd�\}}}|dvrtd|�d	|�d
���|r	t|�}||fSd}||fS#t$r}td|�d	|�d��|�d}~wwxYw)
NT)rRNF)�falseN)rr�,)rRrArzInvalid mode 'z' in strict_id setting 'z+': Expected one of 'true', 'false', 'warn'.zInvalid sleep 'z': not an integer)�	partitionr=r�)r>�moder��sleepr�s     r.r<r<�s�����~��
�������%�%�c�*�N�D�!�U��,�,��:>��
H�
�	
�

�	���J�E���;������;����	���&�"���
��	�s�A�	A2�A-�-A2c��	t|�\}}|dk(ryt	j
d|d|��y#t$r}tj|�Yd}~yd}~wwxYw)NrA�
non_ec2_mdT)rDrE)r<r=rdr�r�show_warning)r>r�rDrEr�s     r.r�r�
sU���'��/���e�
�w������,��$�e�D�������A�����s�0�	A�A�Ac�4�|ddk(rtjSy)N�product_namezAlibaba Cloud ECS)rr$��datas r.�identify_aliyunrMs!���N��2�2�� � � �3r-c�D�|d}|jd�rtjStt�5tj|�jj�jd�rtjcddd�S	ddd�y#1swYyxYw)N�uuidr)	r�rr%rr=rO�UUID�bytes_le�hex)rL�uuid_strs  r.�identify_awsrTs����F�|�H����5�!��~�~��	�*�	�"��9�9�X��'�'�+�+�-�8�8��?��>�>�"�"�?�"��
"��s�AB�Bc�L�|djd�rtjSy)N�serialz.brightbox.com)�endswithrr&rKs r.�identify_brightboxrX,s&���H�~���/�0��#�#�#�1r-c�L�|djd�rtjSy)N�	asset_tagz
.zstack.io)rWrr'rKs r.�identify_zstackr[1s&���K��!�!�,�/�� � � �0r-c�4�|ddk(rtjSy)N�vendorr)rr(rKs r.�identify_e24cloudr^6s���H�~��#��"�"�"�$r-c�~�|ddj�k(r'|ddj�k(rtjSyy)NrJz3DS Outscale VMr]z3DS Outscale)r�rr)rKs r.�identify_outscaler`;sD���^�� 1� 7� 7� 9�9���N�n�2�2�4�4��"�"�"�
5�	:r-c���t�}ttttt
td�f}|D]}	||�}|r|cS�y#t$r"}tjd|||�Yd}~�;d}~wwxYw)Nc�"�tjSr;)rr*)r�s r.�<lambda>z#identify_platform.<locals>.<lambda>Ms��*�$�$�r-z'calling %s with %s raised exception: %s)
�_collect_platform_datarTrXr[r^r`rMrrdr�)rL�checks�checkerr	r�s     r.rNrNCs~��!�#�D�������$��F����	��T�]�F���
����
�	��K�K�9�7�D�!�
�
��	�s�A�	A/�
A*�*A/c�>�d}ttt�5tjd�j�}ddd�|xst
jd�xsd}t
jd�xsd}t
jd�xsd}t
jd�xsd}t
jd�xsd}|j�|j�|j�|j�|j�d	�S#1swY��xYw)
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-uuidrVzsystem-serial-numberzchassis-asset-tagzsystem-manufacturerzsystem-product-name�rOrVrZr]rJ)	r�OSError�UnicodeDecodeErrorr
�load_text_file�stripr�
read_dmi_datar�rhs     r.rdrdZs����D�	�'�-�	.�C��"�"�#9�:�@�@�B��C��9�3�$�$�]�3�9�r�D�
�
�
�5�
6�
<�"�F��!�!�"5�6�<�"�I�
�
�
�4�
5�
;��F��$�$�%:�;�A�r�L���
�
���,�,�.��_�_�&��,�,�.�$�*�*�,���C�C�s�$D�D�
macs_metadata�macs_to_nicsr�rHc	�����	�i}t��dk(st��dk(r|Std�t��fd��j���}d��	t	t|�	�fd����D����cic]\}\}}}||��c}}}}Scc}}}}w)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.
    rc��|dduS)N�r,)�mmds r.rcz"_build_nic_order.<locals>.<lambda>�s��C��F�$�&�r-c�2��|�j|��|fSr;)ro)�macrnros ��r.rcz"_build_nic_order.<locals>.<lambda>�s����m�/�/��4�l�3�6G�H�r-c�D�|j|d�}|�t|�S|Sr;)ror�)�dikt�key�	alt_value�values    r.�_get_key_as_int_orz,_build_nic_order.<locals>._get_key_as_int_or�s(������d�#�����u�:���r-c����|ddtd���|ddtd���tjk(r|dfS|dfS)Nrrznetwork-card�infinity�
device-number�r)�floatr�NIC_NAME)rsr{r�s ��r.rcz"_build_nic_order.<locals>.<lambda>�se���&��A����j�0A��'��A����z�1B��
.��1B�1B�B��A��!��!��V�!�r-)rx)r��filter�map�keys�	enumerate�sorted)
rnror��	nic_order�valid_macs_metadata�iru�
_mac_metadata�	_nic_namer{s
```      @r.�_build_nic_orderr�zs����$!#�I�
�<��A���]�!3�q�!8��� �&��H�����	
�	���3<��#��
�3
���.�A�.��]�I�	�Q�����s�0B
�
dev_config�nic_name�nic_metadatarD�is_ipv4�tablec���|r#|jd�}|jd�}n"|jd�}|jd�}|r|s tjd|rd�yd�y|jd	�sg|d	<|r;	|jj	||�
�}|d}	|d	jd|	|d
��t|t�r|gn|}|D]}|d	j||d���|jd�sg|d<t|t�r|gn|}|D]}|dj||d���y#t$r!}
tjd||
�Yd}
~
��d}
~
wwxYw)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)rD�routersz	0.0.0.0/0)�to�viar�zoCould not perform dhcp discovery on %s to find its gateway. Not adding default route via the gateway. Error: %s)r�r��routing-policy)�fromr�)
rordre�dhcp_client�dhcp_discoveryr�rr�r�r,)
r�r�r�rDr�r��subnet_prefix_routes�ips�lease�gatewayr��prefix_route�ips
             r.�_configure_policy_routingr��s���(�+�/�/�0H�I�����}�-��+�/�/�0I�J�����w�'�� �S��	�	�
��C�	
�
	�!$�	
�
	��>�>�(�#�!�
�8���	��&�&�5�5�h�v�5�N�E��I�&�G�
�x� �'�'�%�"�"��
��*�C�0�
��
!��
-�
���8��#�#�"��
�	
�
��>�>�*�+�')�
�#�$��c�3�'�3�%�S�C��
���#�$�+�+���
�	
�
��I �	��K�K����
�
��	�s�"D<�<	E&�E!�!E&Tc	��did�}|stj�}|dd}|sg|j�D]\}}	|	|k(s�ndddj�i	d�}
|j	|�}|j	d	�rd|
d
<|
|d|	<|St|jtj�}t|||�}
t|j��}|D]�}||}	|j	|�}|s�|
|}|dk(}d
|dzdzi}d|dd|j�i|	d�}
d|z}|r)|j	d�r|sd|d<t|
||	|d|��|j	d	�r0d|
d
<||
d<|r$|j	d�r|st|
||	|d|��t||�|
d<|
ds|
jd�|
|d|	<��t|d�dk(rw|dj�D]a}	|d|	jd�|d|	jdd�|d|	jdd�|d|	jdd��c|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�	ethernetsrxryTF�
macaddress)�dhcp4�dhcp6�match�set-namer�r�r�rzroute-metricrr�d)r��dhcp4-overridesr�r�r�r~z
use-routes)rDr�r�r�r�zdhcp6-overrides�	addressesr�Nr�r�)r�get_interfaces_by_macrzr�ror��network_rendererr�Rendererr�r�r�r��get_secondary_addresses�popr�)�
network_mdrDror�r�r��netcfgrnrur�r�r��
is_netplanr�ry�nic_idx�is_primary_nic�
dhcp_overrider�s                   r.rrs���6��
,�F���0�0�2���|�,�V�4�M��)�/�/�1�	�M�C���<�'��	���"�C�I�I�K�0� �	
�
�%�(�(��-�����G�$�"&�J�w��(2��{��H�%��
��F�3�3�W�5E�5E�F�J� ��|�%7��I��,�#�#�%�&�D��;3����$��$�(�(��-�����C�.�� �A���'�'�A�+��)<�=�
��,��"�C�I�I�K�0� �
�
��g�
���� � ��1�"�*.�M�,�'�%���!�)���

����G�$�"&�J�w��,9�J�(�)�� �$�$�_�5�&�)��!�%�!-�!��
�#:�,��"L�
�;���+�&��N�N�;�'�(2��{��H�%�w;3�|�6�+���1�$��{�+�0�0�2�	F�H��;���)�-�-�.?�@��;���)�-�-�.?��F��;���)�-�-�h��=��;���)�-�-�.>��E�		F�
�Mr-c	�|�|jd�}|jd�}g}tt|t�xrt	|�dkD�r|jt
|d||d��tt|t�xrt	|�dkD�r|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
    r�r�rrr��24zsubnet-ipv6-cidr-block�128)ror�r�r�r��extend�_get_secondary_addressesr�)r�ru�ipv4sr�r�s     r.r�r��s���
���]�+�E����W�%�E��I��J�u�d�#�6��E�
�Q��7����$��6��U�D�
�	
�
�J�u�d�#�6��E�
�Q��7����$��6��U�E�
�	
�
�)��r-c�4�g}|j|�}|}|rt|jd��dk7r#d|vrdnd}tj	d|||||�n|jd�d}|ddD]$}	|jdj
|	|�	���&|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�rrSrTzJCould not parse %s %s for mac %s. %s network config prefix defaults to /%srrNz
{ip}/{prefix})r��prefix)ror��splitrdr�r�r�)
r��cidr_keyrur��default_prefixr��cidrr��ip_typer�s
          r.r�r��s����I����H�%�D�
�F��3�t�z�z�#��'�1�,�"�h�.�&�F�����
-������	
����C���#���!�"�g�G������/�/�2�f�/�E�F�G��r-c�6�tj|t�Sr;)r�list_from_depends�datasources)�dependss r.�get_datasource_listr��s���$�$�W�k�:�:r-)Hr�loggingr�r�rO�
contextlibr�typingrr�	cloudinitrrrr	r�r
r�cloudinit.distrosr�cloudinit.eventr
r�
cloudinit.netr�cloudinit.net.dhcpr�cloudinit.net.ephemeralr�cloudinit.sourcesr�cloudinit.sources.helpersr�	getLoggerr!rdrbrcrr4r%r$r�rK�
DataSourcer6r7r`r<r�rMrTrXr[r^r`rNrdr@r,r�r�r�r�r�rr�r��DEP_FILESYSTEM�DEP_NETWORKr�r�r,r-r.�<module>r�s�����	�����'�'�)�$�$�1�!�/�6�&�)��g����!��3����(�(� H�
%/�N�N�J�4E�4E�#F� ���D
?�G�&�&�D
?�N:��:�.��B
E�!�

� $�
!�
#�
#��.�F$,�<�<�@���T�	�?�@��s�C�x�.�@�!�@�
�#�s�(�^�	@�FR
��R
��R
��	R
�

�R
��
R
��R
�
�R
�p����|�|�
y�x�2�<�'�0�0�2�3��W�+�+�W�-@�-@�A�B���;r-

Zerion Mini Shell 1.0