Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/net/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/net/__pycache__/ephemeral.cpython-38.pyc

U

�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@s\eZdZdZdeeeefd�dd�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�s$td�||||���zt�|�|_Wn2tk
rf}ztd�|��|�W5d}~XYnX||_||_||_||_	||_
||_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�netZipv4_mask_to_net_prefix�prefixr�	interface�ip�	broadcast�router�
static_routes�cleanup_cmds)	�selfrr�prefix_or_maskrrrr�e�r�9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py�__init__s4�����zEphemeralIPv4Network.__init__cCsR|jr(t�|j�r(t�d|jd�dS|��|jr@|��n|jrN|�	�dS)z>Perform ephemeral network setup if interface is not connected.z=Skip ephemeral network setup, instance has connectivity to %sZurlN)
rr�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)rZaddressrrrr�_delete_addressns
�
�z$EphemeralIPv4Network._delete_addresscCs�d�|j|j�}t�d|j||j�z0tjddddd|d|jd	|jg
d
ddid
�WnFtjk
r�}z&dt	|j
�krx�t�d|j|j�W5d}~XYn^Xtjdddddd	|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.r/�addrr1TZLANG�C)r'Z
update_envzFile existsz7Skip ephemeral network setup, %s already has address %sN�link�set�upr&Zdownr0)
rrrrr rrrZProcessExecutionError�str�stderrr�append)rZcidrrrrrr!~s����
��
�
����z$EphemeralIPv4Network._bringup_devicec	Cst|jD]h\}}g}|dkr"d|g}tjdddd|g|d|jgdd	�|j�d
dddd|g|d|jg�qdS)Nz0.0.0.0�viar�-4�router:r1Tr&rr0)rrrr�insert)rZnet_addressZgatewayZvia_argrrrr"�s(������z+EphemeralIPv4Network._bringup_static_routescCs�tjddddgdd�\}}d|kr:t�d|j|���d	Stjdd
dd|jd|jd
|jg	dd�|j�ddd
dd|jd|jd
|jg	�tjdd
dddd|jd|jg	dd�|j�ddd
dddd|jg�d	S)z<Perform the ip commands to fully setup the router if needed.rr=Zshowz	0.0.0.0/0Tr&�defaultz<Skip ephemeral route setup. %s already has default route: %sNr<r3r1�srcrr0r;)	rrr r�striprrrr>)r�out�_rrrr#�sf��������z$EphemeralIPv4Network._bringup_router)NNN)�__name__�
__module__�__qualname__�__doc__rrr8rrr%r,r2r!r"r#rrrrr
s��-Jr
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|std�|���||_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)rrr)rrrrrrszEphemeralIPv6Network.__init__cCs2t�|jd�dkr.tjdddd|jdgdd�d	S)
z�linux kernel does autoconfiguration even when autoconf=0

        https://www.kernel.org/doc/html/latest/networking/ipv6.html
        Z	operstater7rr5r6r1Fr&N)rZread_sys_netrrr$rrrr%)s
�zEphemeralIPv6Network.__enter__cGsdS)z%No need to set the link to down stateNr�rZ_argsrrrr,4szEphemeralIPv6Network.__exit__N)rDrErFrGrr%r,rrrrrHsrHc@sXeZdZdeeeefd�dd�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)rrLrrOrPrrrr9szEphemeralDHCPv4.__init__cCs,|jr$t�|j�r$t�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)rrrrr �obtain_leaser$rrrr%Gs�zEphemeralDHCPv4.__enter__cCs|��dS)z Teardown sandboxed dhcp context.N)�
clean_network)rr(r)r*rrrr,TszEphemeralDHCPv4.__exit__cCs*|jrd|_|jsdS|j�ddd�dS)z@Exit _ephipv4 context to teardown of ip configuration performed.N)rNrMr,r$rrrrRXs
zEphemeralDHCPv4.clean_networkcCs�|jr|jSt|j|j|j�}|s(t��|d|_t�d|jd|jd|jd�ddddddgd	d
�}|�|�}|ds�t	�
|d|d
�|d<|dr�t|d�|d<|jr�|j|d<t
f|�}|��||_|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-routesZrouters)rrrrrrrrrrr)rNrrLrOrPrrr �extract_dhcp_options_mappingrZmask_and_ipv4_to_bcast_addrr	rr
r%rM)rZleases�nmap�kwargsZephipv4rrrrQ`sR	�
���
��

zEphemeralDHCPv4.obtain_leasecCsDi}|��D]2\}}t|t�r.|�|||�q|j�|�||<q|SrK)�items�
isinstance�list�get_first_option_valuerN�get)rrU�resultZinternal_reference�lease_option_namesrrrrT�s
�z,EphemeralDHCPv4.extract_dhcp_options_mappingcCs(|D]}|�|�s|j�|�||<qdSrK)r[rN)rZinternal_mappingr]r\Zdifferent_namesrrrrZ�s
z&EphemeralDHCPv4.get_first_option_value)NNNN)rDrErFrrr8rrr%r,rRrQrTrZrrrrrJ8s��
2rJc@s2eZdZdZdeed�dd�Zdd	�Zd
d�ZdS)
�EphemeralIPNetworkz9Marries together IPv4 and IPv6 ephemeral context managersFTN)�ipv6�ipv4cCs,||_||_||_t��|_d|_||_dS)N�)rr`r_�
contextlib�	ExitStack�stack�	state_msgrP)rrr_r`rPrrrr�s
zEphemeralIPNetwork.__init__c
Cstz:|jr |j�t|j|jd��|jr8|j�t|j��Wn4tk
rn}z|jrZd|_	n|�W5d}~XYnX|S)N)rPzusing link-local ipv6)
r`rd�
enter_contextrJrrPr_rHrre)rrrrrr%�s�zEphemeralIPNetwork.__enter__cGs|j��dSrK)rd�closerIrrrr,�szEphemeralIPNetwork.__exit__)FTN)rDrErFrG�boolrr%r,rrrrr^�s��r^)rGrbZlogging�typingrrrrZ
cloudinit.netrZ	cloudinitrZcloudinit.net.dhcprrr	Z	getLoggerrDrr
rHrJr^rrrr�<module>s
"m