| Current Path : /usr/lib/python3/dist-packages/cloudinit/net/__pycache__/ |
| Current File : //usr/lib/python3/dist-packages/cloudinit/net/__pycache__/dhcp.cpython-38.pyc |
U
�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 @ s 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 s r c @ s e Zd ZdZdS )�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 s r c @ s e Zd ZdZdS )�NoDHCPLeaseInterfaceErrorz7Raised when unable to find a viable interface for DHCP.Nr r
r
r
r r % s r c @ s e Zd ZdZdS )�NoDHCPLeaseMissingDhclientErrorz$Raised when unable to find dhclient.Nr r
r
r
r r ) s r c C sn | dkr(t � } | dkrDt�d� t� �n| t� krDt�d| � t� �t�d�}|sbt�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.Zdhclientz7Skip dhclient configuration: No dhclient command found.) r �LOG�debugr r r Zwhichr �dhcp_discovery)Znic�
dhcp_log_funcZtmp_dirZ
dhclient_pathr
r
r �maybe_perform_dhcp_discovery- s
�
r c C s� t �dt j�}g }t�| �}t|�dkr6td�| ���|�|�D ]V}g }|� d�D ]4}|�
� �dd��dd�}|stqR|�|� dd �� qR|�t
|�� q@|s�td
�| ���|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_fileZlease_regexZdhcp_leasesZ
lease_contentZleaseZ
lease_options�liner
r
r �parse_dhcp_lease_fileL s,
���r* c
C sv t �d|� d}d}t�t�� t�|� t�|� W 5 Q R X tjdddd|dgd d
� | ddd
|d||ddg
}tj|d d
�\}}tj ||gddd�}|r�t �
dd�dd� |D ��� g S d} d}
tdd�D ]v}t�
|��� }zt|�}
W n tk
r� Y n:X t�|
�} | dk�r8t �d|
� t�|
tj� d }
�qDt�d� q�|
�sZt �d|| d� |d k �rn|||� 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.leaseZip�link�setZdevZupT)Zcapturez-1z-vz-lfz-pfz-sfz /bin/true� g{�G�z�?)ZmaxwaitZnaplenz+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 zkilling dhclient with pid=%szCdhclient(pid=%s, parentpid=%s) failed to daemonize after %s secondsg $@N)r r �
contextlib�suppress�FileNotFoundErrorr. �remover r Zwait_for_filesZwarning�join�ranger r$ �int�
ValueErrorZ
get_proc_ppid�kill�signal�SIGKILL�time�sleep�errorr* )Zdhclient_cmd_pathZ interfacer Zpid_filer( �cmd�out�errZmissingZppidZ
daemonized�_Zpid_content�pidr
r
r r n sn
� ��
�
r c C s t tjt| �dd��S )z�Parse a systemd lease file content as in /run/systemd/netif/leases/
Parse this (almost) ini style file even though it says:
# This is private data. Do not parse.
Simply return a dictionary of key/values.F)Zlist_values)r'