HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/net/__pycache__/ephemeral.cpython-310.pyc
o

�Ad�9�@s�dZddlZddlZddlmZmZmZmZddlm	Z	ddl
mZddlm
Z
mZmZe�e�ZGdd�d�ZGdd	�d	�ZGd
d�d�ZGdd
�d
�ZdS)z.Module for ephemeral network context managers
�N)�Any�Dict�List�Optional)�subp)�NoDHCPLeaseError�maybe_perform_dhcp_discovery�parse_static_routesc@sbeZdZdZ			ddeeeeffdd�Zdd�Z	dd	�Z
d
d�Zdd
�Zdd�Z
dd�ZdS)�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	
Cs�t||||g�std�||||���zt�|�|_Wnty.}ztd�|��|�d}~ww||_||_||_||_	||_
||_g|_dS)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)	�selfrr�prefix_or_maskrrrr�e�r�9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py�__init__s0������
zEphemeralIPv4Network.__init__cCsX|jrt�|j�rt�d|jd�dS|��|jr!|��dS|jr*|�	�dSdS)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_router�rrrr�	__enter__Ls�
�zEphemeralIPv4Network.__enter__cCs|jD]	}tj|dd�qdS)zTeardown anything we set up.T��captureN)rr)r�	excp_type�
excp_value�excp_traceback�cmdrrr�__exit__is
�zEphemeralIPv4Network.__exit__c	Cs,tjdddddd||fd|jgdd	�d
S)z7Perform the ip command to remove the specified address.r�-family�inet�addr�delz%s/%s�devTr'N)rr)r�addressrrrr�_delete_addressns
�

�z$EphemeralIPv4Network._delete_addresscCsd�|j|j�}t�d|j||j�ztjddddd|d|jd	|jg
d
ddid
�Wn%tjyO}zdt	|j
�vr;�t�d|j|j�WYd}~dSd}~wwtjdddddd	|jdgd
d�|j�dddddd	|jdg�|j�ddddd|d	|jg�dS)z1Perform the ip comands to fully setup the device.z{0}/{1}z:Attempting setup of ephemeral network on %s with %s brd %srr.r/r0�addrr2T�LANG�C)r(�
update_envzFile existsz7Skip ephemeral network setup, %s already has address %sN�link�set�upr'�downr1)
rrrr r!rrr�ProcessExecutionError�str�stderrr�append)r�cidrrrrrr"~s���
����
�
�
����z$EphemeralIPv4Network._bringup_devicec	Cst|jD]4\}}g}|dkrd|g}tjdddd|g|d|jgdd	�|j�d
dddd|g|d|jg�qdS)Nz0.0.0.0�viar�-4�router@r2Tr'rr1)rrrr�insert)r�net_address�gateway�via_argrrrr#�s*�������z+EphemeralIPv4Network._bringup_static_routescCs�tjgd�dd�\}}d|vrt�d|j|���dStjddd	d
|jd|jd|jg	dd�|j�d
ddd	d|jd|jd|jg	�tjddd	d
dd|jd|jg	dd�|j�d
ddd	ddd|jg�dS)z<Perform the ip commands to fully setup the router if needed.)rrD�showz	0.0.0.0/0Tr'�defaultz<Skip ephemeral route setup. %s already has default route: %sNrrCrDr5r2�srcrr1rB)	rr r!r�striprrrrE)r�out�_rrrr$�sd��������z$EphemeralIPv4Network._bringup_router)NNN)�__name__�
__module__�__qualname__�__doc__rrr>rrr&r-r4r"r#r$rrrrr
s�
�-Jr
c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�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.
    cCs|s	td�|���||_dS)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)r
rr)rrrrrrs
zEphemeralIPv6Network.__init__cCs6t�|jd�dkrtjdddd|jdgdd�d	Sd	S)
z�linux kernel does autoconfiguration even when autoconf=0

        https://www.kernel.org/doc/html/latest/networking/ipv6.html
        �	operstater;rr9r:r2Fr'N)r�read_sys_netrrr%rrrr&)s
��zEphemeralIPv6Network.__enter__cGsdS)z%No need to set the link to down stateNr�r�_argsrrrr-4szEphemeralIPv6Network.__exit__N)rOrPrQrRrr&r-rrrrrSs
rSc@s`eZdZ				ddeeeeffdd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dS)�EphemeralDHCPv4NrcCs(||_d|_d|_||_||_||_dS�N)�iface�_ephipv4�lease�
dhcp_log_funcr�tmp_dir)rrZrr]r^rrrr9s
zEphemeralDHCPv4.__init__cCs,|jrt�|j�rt�d|j�dS|��S)zUSetup sandboxed dhcp context, unless connectivity_url can already be
        reached.z:Skip ephemeral DHCP setup, instance has connectivity to %sN)rrrr r!�obtain_leaser%rrrr&Gs�zEphemeralDHCPv4.__enter__cCs|��dS)z Teardown sandboxed dhcp context.N)�
clean_network)rr)r*r+rrrr-TszEphemeralDHCPv4.__exit__cCs*|jrd|_|jsdS|j�ddd�dS)z@Exit _ephipv4 context to teardown of ip configuration performed.N)r\r[r-r%rrrr`Xs
zEphemeralDHCPv4.clean_networkcCs�|jr|jSt|j|j|j�}|st��|d|_t�d|jd|jd|jd�ddddddgd	d
�}|�|�}|dsJt	�
|d|d
�|d<|drVt|d�|d<|jr^|j|d<t
di|��}|��||_|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-addresszrfc3442-classless-static-routeszclassless-static-routes�routers)rrrrrrrrrrrNr)r\rrZr]r^rr r!�extract_dhcp_options_mappingr�mask_and_ipv4_to_bcast_addrr	rr
r&r[)r�leases�nmap�kwargs�ephipv4rrrr_`sL	�
���
��
zEphemeralDHCPv4.obtain_leasecCsDi}|��D]\}}t|t�r|�|||�q|j�|�||<q|SrY)�items�
isinstance�list�get_first_option_valuer\�get)rrf�result�internal_reference�lease_option_namesrrrrc�s
�z,EphemeralDHCPv4.extract_dhcp_options_mappingcCs(|D]}|�|�s|j�|�||<qdSrY)rmr\)r�internal_mappingrprn�different_namesrrrrl�s

��z&EphemeralDHCPv4.get_first_option_value)NNNN)rOrPrQrrr>rrr&r-r`r_rcrlrrrrrX8s�
�
2rXc@s:eZdZdZ			d
dedefdd�Zd	d
�Zdd�ZdS)�EphemeralIPNetworkz9Marries together IPv4 and IPv6 ephemeral context managersFTN�ipv6�ipv4cCs,||_||_||_t��|_d|_||_dS)N�)rrurt�
contextlib�	ExitStack�stack�	state_msgr^)rrrtrur^rrrr�s

zEphemeralIPNetwork.__init__c
Cs|z!|jr|j�t|j|jd��|jr|j�t|j��W|SW|Sty=}z|jr0d|_	n|�WYd}~|Sd}~ww)N)r^zusing link-local ipv6)
rury�
enter_contextrXrr^rtrSrrz)rrrrrr&�s$��	�
���zEphemeralIPNetwork.__enter__cGs|j��dSrY)ry�closerVrrrr-�szEphemeralIPNetwork.__exit__)FTN)rOrPrQrR�boolrr&r-rrrrrs�s��
�rs)rRrw�logging�typingrrrr�
cloudinit.netr�	cloudinitr�cloudinit.net.dhcprrr	�	getLoggerrOr r
rSrXrsrrrr�<module>s
"m