Your IP : 216.73.216.99


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

U

�Adp.�@s�ddlZddlZddlZddlZddlZddlZddlmZddlZddl	m
Z
mZddlm
Z
mZe�e�ZdZGdd�de�ZGdd	�d	e�ZGd
d�de�ZGdd
�d
e�Zddd�Zdd�Zddd�Zdd�Zddd�Zddd�Zdd�ZdS) �N)�StringIO)�subp�util)�find_fallback_nic�get_devicelistz/run/systemd/netif/leasesc@seZdZdZdS)�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.pyrsrc@seZdZdZdS)�InvalidDHCPLeaseFileErrorz�Raised when parsing an empty or invalid dhclient.lease file.

    Current uses are DataSourceAzure and DataSourceEc2 during ephemeral
    boot to scrape metadata.
    Nrr
r
r
rrsrc@seZdZdZdS)�NoDHCPLeaseInterfaceErrorz7Raised when unable to find a viable interface for DHCP.Nrr
r
r
rr%src@seZdZdZdS)�NoDHCPLeaseMissingDhclientErrorz$Raised when unable to find dhclient.Nrr
r
r
rr)srcCsn|dkr(t�}|dkrDt�d�t��n|t�krDt�d|�t��t�d�}|sbt�d�t��t|||�S)aJPerform 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�debugrrrZwhichr�dhcp_discovery)Znic�
dhcp_log_funcZtmp_dirZ
dhclient_pathr
r
r�maybe_perform_dhcp_discovery-s 

�

rcCs�t�dtj�}g}t�|�}t|�dkr6td�|���|�|�D]V}g}|�	d�D]4}|�
��dd��dd�}|stqR|�|�	dd	��qR|�t
|��q@|s�td
�|���|S)aParse 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>.*?)}\nrz&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_fileLs,	
���r*c
Csvt�d|�d}d}t�t��t�|�t�|�W5QRXtjdddd|dgd	d
�|ddd
|d||ddg
}tj|d	d
�\}}tj	||gddd�}|r�t�
dd�dd�|D���gSd}	d}
tdd�D]v}t�
|���}zt|�}
Wntk
r�Yn:Xt�|
�}	|	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, css|]}tj�|�VqdS)N)�os�path�basename)�.0�fr
r
r�	<genexpr>�sz!dhcp_discovery.<locals>.<genexpr>�unknownFri�rzkilling dhclient with pid=%szCdhclient(pid=%s, parentpid=%s) failed to daemonize after %s secondsg$@N)rr�
contextlib�suppress�FileNotFoundErrorr.�removerrZwait_for_filesZwarning�join�rangerr$�int�
ValueErrorZ
get_proc_ppid�kill�signal�SIGKILL�time�sleep�errorr*)Zdhclient_cmd_pathZ	interfacerZpid_filer(�cmd�out�errZmissingZppidZ
daemonized�_Zpid_content�pidr
r
rrnsn
�	��

�

rcCsttjt|�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'�	configobjZ	ConfigObjr)Zcontentr
r
r�networkd_parse_lease�srIcCsP|dkrt}i}tj�|�s |St�|�D] }tt�tj�||���||<q*|S)z�Return a dictionary of dictionaries representing each lease
    found in lease_d.i

    The top level key will be the filename, which is typically the ifindex.N)	�NETWORKD_LEASES_DIRr.r/�isdir�listdirrIrrr9)�leases_dZretZlfiler
r
r�networkd_load_leases�s�
rNcCsF|dkrt}t|d�}t|���D]\}}|�|�r"||Sq"dS)N)rM)rJrN�sorted�items�get)ZkeynamerMZleasesZ_ifindex�datar
r
r�networkd_get_option_from_leases�s

rScs.��d��dd�t�d��D�}g}�fdd�}d}t|�D�]�\}}||krPq<t|�}|tdd	�kr�d
}t||d��|kr�|||t||d���|Sd�||d
|d��}	d�||d||��}
||}�n4|tdd�k�rnd}t||d��|k�r(|||t||d���|Sd�||d
|d�dg�}	d�||d||��}
||}�n�|td
d�k�rd}t||d��|k�r�|||t||d���|Sd�||d
|d�ddg�}	d�||d||��}
||}�n|td
d
�k�r�d}t||d��|k�rJ|||t||d���|Sd�||d
|d�dddg�}	d�||d||��}
||}n�|dk�r�d}t||d��|k�r�|||t||d���|Sd}	d�||d
||��}
||}nt�	d|�|S|�
d|	|f|
f�q<|S)a�parse rfc3442 format and return a list containing tuple of strings.

    The tuple is composed of the network_address (including net length) and
    gateway for a parsed static route.  It can parse two formats of rfc3442,
    one from dhcpcd and one from dhclient (isc).

    @param rfc3442: string in rfc3442 format (isc or dhcpd)
    @returns: list of tuple(str, str) for all valid parsed routes until the
              first parsing error.

    E.g.
    sr=parse_static_routes("32,169,254,169,254,130,56,248,255,0,130,56,240,1")
    sr=[
        ("169.254.169.254/32", "130.56.248.255"), ("0.0.0.0/0", "130.56.240.1")
    ]

    sr2 = parse_static_routes("24.191.168.128 192.168.128.1,0 192.168.128.1")
    sr2 = [
        ("191.168.128.0/24", "192.168.128.1"), ("0.0.0.0/0", "192.168.128.1")
    ]

    Python version of isc-dhclient's hooks:
       /etc/dhcp/dhclient-exit-hooks.d/rfc3442-classless-routes
    rcSsg|]}|r|�qSr
r
)r1�tokr
r
r�
<listcomp>sz'parse_static_routes.<locals>.<listcomp>z[, .]csd|||�f}t�|�dS)Nz�RFC3442 string malformed.  Current route has CIDR of %s and requires %s significant octets, but only %s remain. Verify DHCP rfc3442-classless-static-routes value: %s)rrB)ZcidrZrequiredZremain�msg��rfc3442r
r�_trunc_errors

��z)parse_static_routes.<locals>._trunc_errorr��!�	N�.rr-����0����z0.0.0.0zSParsed invalid net length "%s".  Verify DHCP rfc3442-classless-static-routes value.z%s/%s)�rstriprr#�	enumerater;r:r r9rrBr&)rX�tokensZ
static_routesrYZcurrent_idx�idxrTZ
net_lengthZreq_toksZnet_addressZgatewayr
rWr�parse_static_routes�sp
	 "$


�rj)NNN)N)N)N)r5Zloggingr.rr>r@�iorrHZ	cloudinitrrZ
cloudinit.netrrZ	getLoggerr	rrJ�	Exceptionrrrrrr*rrIrNrSrjr
r
r
r�<module>s,

"
[