%PDF- %PDF-
Mini Shell

Mini Shell

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

�

\�`f�<���dZddlZddlZddlmZddlmZmZmZm	Z	m
Z
ddlmZddl
mZmZmZddlmZej&e�ZGd�d�ZGd	�d
�ZGd�d�ZGd
�de�ZGd�d�Zy)z.Module for ephemeral network context managers
�N)�partial)�Any�Callable�Dict�List�Optional)�Dhcpcd�NoDHCPLeaseError�maybe_perform_dhcp_discovery)�ProcessExecutionErrorc�R�eZdZdZ			d
deeeeffd�Zd�Z	d�Z
d�Zd�Zd	�Z
y)�EphemeralIPv4Networka�Context manager which sets up temporary static network configuration.

    No operations are performed if the provided interface already has the
    specified configuration.
    This can be verified with the connectivity_url_data.
    If unconnected, bring up the interface with valid ip, prefix and broadcast.
    If router is provided setup a default route for that interface. Upon
    context exit, clean up the interface leaving no configuration behind.
    N�connectivity_url_datac	��t||||g�stdj||||���	tj|�|_||_||_||_||_	||_
||_g|_||_
|j�d|j
��|_y#t$r }	tdj|	��|	�d}	~	wwxYw)aXSetup context manager and validate call signature.

        @param interface: Name of the network interface to bring up.
        @param ip: IP address to assign to the interface.
        @param prefix_or_mask: Either netmask of the format X.X.X.X or an int
            prefix.
        @param broadcast: Broadcast address for the IPv4 network.
        @param router: Optionally the default gateway IP.
        @param connectivity_url_data: Optionally, a URL to verify if a usable
           connection already exists.
        @param static_routes: Optionally a list of static routes from DHCP
        z5Cannot init network on {0} with {1}/{2} and bcast {3}z4Cannot setup network, invalid prefix or netmask: {0}N�/)�all�
ValueError�format�net�ipv4_mask_to_net_prefix�prefixr�	interface�ip�	broadcast�router�
static_routes�cleanup_cmds�distro�cidr)
�selfrrr�prefix_or_maskrrrr�es
          �9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py�__init__zEphemeralIPv4Network.__init__ s���.�I�r�>�9�=�>��G�N�N��r�>�9���
�
	��5�5�n�E�D�K�&;��"�"������"������*���,.�������w�w�i�q����
�.��	���	���%�v�a�y���
��	�s�B!�!	C
�*C�C
c�z�|jrCtj|j�r$tj	d|jd�y	|j�|jr|j�y|jr|j�yy#t$r|jddd��wxYw)z>Perform ephemeral network setup if interface is not connected.z=Skip ephemeral network setup, instance has connectivity to %s�urlN)rr�has_url_connectivity�LOG�debug�_bringup_devicer�_bringup_static_routesr�_bringup_routerr�__exit__�r s r#�	__enter__zEphemeralIPv4Network.__enter__Ps����%�%��'�'��(B�(B�C��	�	���.�.�u�5��
�	�� � �"��!�!��+�+�-�����$�$�&���$�	��M�M�$��d�+��	�s�,B�>B�B:c�4�|jD]	}|��y)zTeardown anything we set up.N)r)r �	excp_type�
excp_value�excp_traceback�cmds     r#r-zEphemeralIPv4Network.__exit__rs���$�$�	�C��E�	�c�^�tjd|j|j|j�	|j
jj|j|j|j�|j
jj|jd��|jjt|j
jj|jd���|jjt|j
jj|j|j��y#t$rM}dt|j �vr�tjd|j|j"�Yd}~yd}~wwxYw)z2Perform the ip commands to fully setup the device.z:Attempting setup of ephemeral network on %s with %s brd %s�inet��familyzFile existsz7Skip ephemeral network setup, %s already has address %sN)r(r)rrrr�net_ops�add_addr�link_upr�appendr�	link_down�del_addrr�str�stderrr)r r"s  r#r*z$EphemeralIPv4Network._bringup_devicews6���	�	�H��N�N��I�I��N�N�		
�	��K�K���(�(�����	�	�4�>�>�
�
�K�K���'�'����v�'�F����$�$���K�K�'�'�1�1��N�N�!��
�
���$�$���K�K�'�'�0�0�$�.�.�$�)�)��
��%%�	��C����M�1���I�I�I�������
�
��	�s�AE�	F,�AF'�'F,c
�0�|jD]�\}}|jjj|j||�|j
j
dt|jjj|j||�����y)Nr��gateway)	rrr:�append_routerr�insertr�	del_route)r �net_addressrDs   r#r+z+EphemeralIPv4Network._bringup_static_routes�s���%)�$6�$6�	� �K��
�K�K���,�,�����W�
�
���$�$����K�K�'�'�1�1��N�N��#�	�
�	r5c	� �|jjj�}d|vr0tj	d|j
|j
��y|jjj|j
|j|j��|jjdt|jjj|j
|j|j���|jjj|j
d|j��|jjdt|jjj|j
d��y)z<Perform the ip commands to fully setup the router if needed.�defaultz<Skip ephemeral route setup. %s already has default route: %sN)�source_addressrrC)rr:�get_default_router(r)r�strip�	add_routerrrrFrrG)r �outs  r#r,z$EphemeralIPv4Network._bringup_router�s(���k�k�!�!�3�3�5������I�I�N�����	�	��
�

������%�%��N�N�D�K�K����	&�	
�	
��� � �
�����#�#�-�-�������#�w�w�	
�	
�	
�����%�%��N�N�I�t�{�{�	&�	
�	
��� � �
��D�K�K�'�'�1�1�4�>�>�9�M�	
r5�NNN)�__name__�
__module__�__qualname__�__doc__rrr@rr$r/r-r*r+r,�r5r#rrsJ���"�:>��./� (��S�#�X��7�./�` �D�
"�H�0
r5rc�"�eZdZdZd�Zd�Zd�Zy)�EphemeralIPv6Networkz�Context manager which sets up a ipv6 link local address

    The linux kernel assigns link local addresses on link-up, which is
    sufficient for link-local communication.
    c�X�|stdj|���||_||_y)z�Setup context manager and validate call signature.

        @param interface: Name of the network interface to bring up.
        @param ip: IP address to assign to the interface.
        @param prefix: IPv6 uses prefixes, not netmasks
        zCannot init network on {0}N)rrrr)r rrs   r#r$zEphemeralIPv6Network.__init__�s,����9�@�@��K�L�L�"�����r5c��tj|jd�dk7r0|jjj|j�yy)z�linux kernel does autoconfiguration even when autoconf=0

        https://www.kernel.org/doc/html/latest/networking/ipv6.html
        �	operstate�upN)r�read_sys_netrrr:r<r.s r#r/zEphemeralIPv6Network.__enter__�s?��
���D�N�N�K�8�D�@��K�K���'�'����7�Ar5c��y)z%No need to set the link to down stateNrU�r �_argss  r#r-zEphemeralIPv6Network.__exit__�s�r5N)rQrRrSrTr$r/r-rUr5r#rWrW�s����8�4r5rWc�T�eZdZ			d
deeeeffd�Zd�Zd�Z	d�Z
d�Zd�Zd	�Z
y)�EphemeralDHCPv4Nrc�X�||_d|_d|_||_||_||_y�N)�iface�_ephipv4�lease�
dhcp_log_funcrr)r rrdrrgs     r#r$zEphemeralDHCPv4.__init__�s0����
���
���
�*���%:��"���r5c��|jr@tj|j�r!tj	d|j�y|j�S)zUSetup sandboxed dhcp context, unless connectivity_url can already be
        reached.z:Skip ephemeral DHCP setup, instance has connectivity to %sN)rrr'r(r)�obtain_leaser.s r#r/zEphemeralDHCPv4.__enter__sO���%�%��'�'��(B�(B�C��	�	���.�.��
�� � �"�"r5c�$�|j�y)z Teardown sandboxed dhcp context.N)�
clean_network)r r1r2r3s    r#r-zEphemeralDHCPv4.__exit__s�����r5c�f�d|_|jr|jjddd�yy)z@Exit _ephipv4 context to teardown of ip configuration performed.N)rfrer-r.s r#rkzEphemeralDHCPv4.clean_networks+����
��=�=��M�M�"�"�4��t�4�r5c�:�|jr|jSt|j|j|j�|_|js
t��tjd|jd|jd|jd�ddddgd�dd�}|j|�}|d	stj|d
|d�|d	<|dr+|jjj|d�|d<|jr|j|d
<t|jjt�rt!|jfi|��}nt#|jfi|��}|j%�||_|jS)a9Perform dhcp discovery in a sandboxed environment if possible.

        @return: A dict representing dhcp options on the most recent lease
            obtained from the dhclient discovery if run, otherwise an error
            is raised.

        @raises: NoDHCPLeaseError if no leases could be obtained.
        z#Received dhcp lease on %s for %s/%srz
fixed-addresszsubnet-maskzbroadcast-address)zrfc3442-classless-static-routeszclassless-static-routesr�routers)rrr!rrrrr!rrr)rfrrrdrgr
r(r)�extract_dhcp_options_mappingr�mask_and_ipv4_to_bcast_addr�dhcp_client�parse_static_routesr�
isinstancer	�DhcpcdEphemeralIPv4Networkrr/re)r �nmap�kwargs�ephipv4s    r#rizEphemeralDHCPv4.obtain_leases����:�:��:�:��1��K�K����T�%7�%7�
��
��z�z�"�$�$��	�	�1��J�J�{�#��J�J��'��J�J�}�%�		
�%�!�+�,��
 �
���2�2�4�8���k�"�"%�"A�"A��'�(�&��,�#�F�;���/�"����'�'�;�;���'��
��
�
�%�%�.2�.H�.H�F�*�+��d�k�k�-�-�v�6�0����G��G�G�*�4�;�;�A�&�A�G�������
��z�z�r5c��i}|j�D]G\}}t|t�r|j|||��*|jj|�||<�I|Src)�itemsrs�list�get_first_option_valuerf�get)r ru�result�internal_reference�lease_option_namess     r#roz,EphemeralDHCPv4.extract_dhcp_options_mappingPsh����6:�j�j�l�	P�2�� 2��,�d�3��+�+�&�(:�F��.2�Z�Z�^�^�<N�-O��)�*�
	P��
r5c�r�|D]2}|j|�r�|jj|�||<�4yrc)r|rf)r �internal_mappingrr}�different_namess     r#r{z&EphemeralDHCPv4.get_first_option_value[s:�� 2�	K�O��:�:�.�/�+/�:�:�>�>�/�+J��'�(�	Kr5rP)rQrRrSrrr@rr$r/r-rkriror{rUr5r#rara�sI���:>��� (��S�#�X��7�	�#��5�5�n	�Kr5rac�(��eZdZdZ�fd�Zd�Z�xZS)rtz3dhcpcd sets up its own ephemeral network and routesc�r��t�|�|i|��|jjt	|j
jj|jd���|jjt	|j
jj|j|j��y)Nr7r8)�superr$rr=rrr:r>rr?r)r �argsrv�	__class__s   �r#r$z#DhcpcdEphemeralIPv4Network.__init__fs����
���$�)�&�)�	
��� � �����#�#�-�-�����
�	
�	
��� � ��D�K�K�'�'�0�0�$�.�.�$�)�)�L�	
r5c��yrcrUr.s r#r/z$DhcpcdEphemeralIPv4Network.__enter__us��r5)rQrRrSrTr$r/�
__classcell__)r�s@r#rtrtcs���=�

�r5rtc�2�eZdZdZ		ddedefd�Zd�Zd�Zy)	�EphemeralIPNetworkahCombined ephemeral context manager for IPv4 and IPv6

    Either ipv4 or ipv6 ephemeral network may fail to initialize, but if either
    succeeds, then this context manager will not raise exception. This allows
    either ipv4 or ipv6 ephemeral network to succeed, but requires that error
    handling for networks unavailable be done within the context.
    �ipv6�ipv4c�|�||_||_||_tj�|_d|_||_y)N�)rr�r��
contextlib�	ExitStack�stack�	state_msgr)r rrr�r�s     r#r$zEphemeralIPNetwork.__init__�s7��#�����	���	��)�)�+��
� �����r5c���|js|js|Sg}d}|jr<	|jjt	|j
|j��d}|jrQ	|jjt|j
|j��d}|s|jsd|_
|stjd�|d�|S#ttf$r1}tjd|�|j|�Yd}~��d}~wwxYw#t$r1}tjd|�|j|�Yd}~��d}~wwxYw)NFTzFailed to bring up %s for ipv4.zusing link-local ipv6zFailed to bring up %s for ipv6.zGFailed to bring up EphemeralIPNetwork. Datasource setup cannot continuer)r�r�r��
enter_contextrarrrr
r(�infor=rWr��error)r �
exceptions�ephemeral_obtainedr"s    r#r/zEphemeralIPNetwork.__enter__�s=���	�	�T�Y�Y��K��
�"���9�9�

%��
�
�(�(�#���������&*�"�
�9�9�
%��
�
�(�(�(���������&*�"��T�Y�Y�%<�D�N�"�
�I�I�3�
��Q�-�����5*�+;�<�
%����:�D�A��!�!�!�$�$��
%��)�
%����:�D�A��!�!�!�$�$��
%�s0�;C"�4AD%�"D"�1'D�D"�%	E�.'E�Ec�8�|jj�yrc)r��closer^s  r#r-zEphemeralIPNetwork.__exit__�s���
�
���r5N)FT)rQrRrSrT�boolr$r/r-rUr5r#r�r�ys3�������	�
��*�Xr5r�)rTr��logging�	functoolsr�typingrrrrr�
cloudinit.netr�cloudinit.net.dhcpr	r
r�cloudinit.subpr�	getLoggerrQr(rrWrartr�rUr5r#�<module>r�s}������6�6����
1��g����!��{
�{
�|4�4�@mK�mK�`�!5��,D�Dr5

Zerion Mini Shell 1.0