File: //usr/lib/python3/dist-packages/cloudinit/net/__pycache__/dhcp.cpython-310.pyc
o
�Adp. � @ s� d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlZd dl m
Z
mZ d dlm
Z
mZ e�e�ZdZG dd� de�ZG dd � d e�ZG d
d� de�ZG dd
� d
e�Zddd�Zdd� Zddd�Zdd� Zddd�Zddd�Zdd� ZdS )� N)�StringIO)�subp�util)�find_fallback_nic�get_devicelistz/run/systemd/netif/leasesc @ � e Zd ZdZdS )�NoDHCPLeaseErrorz'Raised when unable to get a DHCP lease.N��__name__�
__module__�__qualname__�__doc__� r r �4/usr/lib/python3/dist-packages/cloudinit/net/dhcp.pyr � r c @ r )�InvalidDHCPLeaseFileErrorz�Raised when parsing an empty or invalid dhclient.lease file.
Current uses are DataSourceAzure and DataSourceEc2 during ephemeral
boot to scrape metadata.
Nr r r r r r r r c @ r )�NoDHCPLeaseInterfaceErrorz7Raised when unable to find a viable interface for DHCP.Nr r r r r r % r r c @ r )�NoDHCPLeaseMissingDhclientErrorz$Raised when unable to find dhclient.Nr r r r r r ) r r c C sn | du rt � } | du rt�d� t� �n| t� vr"t�d| � t� �t�d�}|s1t�d� t� �t|| |�S )aJ Perform dhcp discovery if nic valid and dhclient command exists.
If the nic is invalid or undiscoverable or dhclient command is not found,
skip dhcp_discovery and return an empty dict.
@param nic: Name of the network interface we want to run dhclient on.
@param dhcp_log_func: A callable accepting the dhclient output and error
streams.
@param tmp_dir: Tmp dir with exec permissions.
@return: A list of dicts representing dhcp options for each lease obtained
from the dhclient discovery if run, otherwise an empty list is
returned.
Nz1Skip dhcp_discovery: Unable to find fallback nic.z8Skip dhcp_discovery: nic %s not found in get_devicelist.�dhclientz7Skip dhclient configuration: No dhclient command found.) r �LOG�debugr r r �whichr �dhcp_discovery)�nic�
dhcp_log_func�tmp_dir�
dhclient_pathr r r �maybe_perform_dhcp_discovery- s
�
�
r c C s� t �dt j�}g }t�| �}t|�dkrtd�| ���|�|�D ]+}g }|� d�D ]}|�
� �dd��dd�}|s:q)|�|� dd �� q)|�t
|�� q |sUtd
�| ���|S )a Parse the given dhcp lease file for the most recent lease.
Return a list of dicts of dhcp options. Each dict contains key value pairs
a specific lease in order from oldest to newest.
@raises: InvalidDHCPLeaseFileError on empty of unparseable leasefile
content.
zlease {(?P<lease>.*?)}\nr z&Cannot parse empty dhcp lease file {0}�;�"� zoption � � z1Cannot parse dhcp lease file {0}. No leases found)�re�compile�DOTALLr � load_file�lenr �format�findall�split�strip�replace�append�dict)�
lease_file�lease_regex�dhcp_leases�
lease_content�lease�
lease_options�liner r r �parse_dhcp_lease_fileL s,
���r6 c
C s� t �d|� d}d}t�t�� t�|� t�|� W d � n1 s$w Y tjdddd|d gd
d� | dd
d|d||ddg
}tj|d
d�\}}tj ||gddd�}|rgt �
dd�dd� |D ��� g S d} d}
tdd�D ]8}t�
|��� }zt|�}
W n ty� Y nw t�|
�} | dkr�t �d|
� t�|
tj� d
}
nt�d� qp|
s�t �d|| d � |dur�|||� t|�S )!a� Run dhclient on the interface without scripts or filesystem artifacts.
@param dhclient_cmd_path: Full path to the dhclient used.
@param interface: Name of the network interface on which to dhclient.
@param dhcp_log_func: A callable accepting the dhclient output and error
streams.
@return: A list of dicts of representing the dhcp leases parsed from the
dhclient.lease file or empty list.
z!Performing a dhcp discovery on %sz/run/dhclient.pidz/run/dhclient.leaseN�ip�link�set�dev�upT)�capturez-1z-vz-lfz-pfz-sfz /bin/true� g{�G�z�?)�maxwait�naplenz+dhclient did not produce expected files: %sz, c s s � | ] }t j�|�V qd S �N)�os�path�basename)�.0�fr r r � <genexpr>� s � z!dhcp_discovery.<locals>.<genexpr>�unknownFr i� r"