�
܍~c�9 � �� � d Z ddlZddlZddlmZmZmZ ddlmZ ddl m
Z
ddlmZm
Z
mZ ej e� � Z G d� d� � Z G d� d � � Z G d
� d� � Z G d� d
� � ZdS )z.Module for ephemeral network context managers
� N)�Any�Dict�List)�subp)�NoDHCPLeaseError�maybe_perform_dhcp_discovery�parse_static_routesc �Z � e Zd ZdZ ddeeef fd�Zd� Zd� Z d� Z
d� Zd � Zd
� Z
dS )�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 �x � t ||||g� � s%t d� ||||� � � � � t j |� � | _ n5# t $ r(}t d� |� � � � |�d}~ww xY w|| _ || _ || _ || _ || _
|| _ g | _ dS )aX 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_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) �selfr r �prefix_or_maskr r r r �es �9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py�__init__zEphemeralIPv4Network.__init__ s� � �, �I�r�>�9�=�>�>� ��G�N�N��r�>�9�� �� �
�
��5�n�E�E�D�K�K��� � � ���%�v�a�y�y�� � �
����� ���� &;��"�"������"������*���')����s �A �
B�#B�Bc �2 � | j rAt j | j � � r(t � d| j d � � dS | � � � | j r| � � � dS | j r| � � � dS dS )z>Perform ephemeral network setup if interface is not connected.z=Skip ephemeral network setup, instance has connectivity to %s�urlN)
r r �has_url_connectivity�LOG�debug�_bringup_devicer �_bringup_static_routesr �_bringup_router�r s r � __enter__zEphemeralIPv4Network.__enter__L s� � ��%� ��'��(B�C�C�
�� � ���.�u�5�� � �
�������� �� #��'�'�)�)�)�)�)�
�[� #�� � �"�"�"�"�"� #� #� c �F � | j D ]}t j |d�� � �dS )zTeardown anything we set up.T��captureN)r r )r � excp_type�
excp_value�excp_traceback�cmds r �__exit__zEphemeralIPv4Network.__exit__i s6 � ��$� )� )�C��I�c�4�(�(�(�(�(� )� )r) c
�V � t j ddddd|�d|��d| j gd� � � d
S )z7Perform the ip command to remove the specified address.r �-family�inet�addr�del�/�devTr+ N)r r )r �addressr s r �_delete_addressz$EphemeralIPv4Network._delete_addressn sT � �� ������"�7�7�F�F�+����
� �
�
�
�
�
�
r) c � � d� | j | j � � }t � d| j || j � � t j ddddd|d| j d | j g
d
ddi�
� � t j dddddd | j dgd
�� � | j � dddddd | j dg� � | j � ddddd|d | j g� � dS # t j
$ rH}dt |j � � vr� t � d| j | j � � Y d}~dS d}~ww xY w)z1Perform the ip comands to fully setup the device.z{0}/{1}z:Attempting setup of ephemeral network on %s with %s brd %sr r3 r4 r5 �addr r8 T�LANG�C)r, �
update_env�link�set�upr+ �downr6 zFile existsz7Skip ephemeral network setup, %s already has address %sN)
r r r r" |