%PDF- %PDF-
Mini Shell

Mini Shell

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

3

�Adp.�@s�ddlZddlZddlZddlZddlZddlZddlmZddlZddl	m
Z
mZddlm
Z
mZeje�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.
    N)rr	r
rrrrr
rsrc@seZdZdZdS)�NoDHCPLeaseInterfaceErrorz7Raised when unable to find a viable interface for DHCP.N)rr	r
rrrrr
r%src@seZdZdZdS)�NoDHCPLeaseMissingDhclientErrorz$Raised when unable to find dhclient.N)rr	r
rrrrr
r)srcCsn|dkr(t�}|dkrDtjd�t��n|t�krDtjd|�t��tjd�}|sbtjd�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�tjdtj�}g}tj|�}t|�dkr6tdj|���xh|j|�D]Z}g}xB|j	d�D]4}|j
�jdd�jdd�}|sxqV|j|j	dd	��qVW|jt
|��qBW|s�td
j|���|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)cCsxtjd|�d}d}tjt��tj|�tj|�WdQRXtjdddd|d	gd
d�|dd
d|d||ddg
}tj|d
d�\}}tj	||gddd�}|r�tj
ddjdd�|D���gSd}	d}
x�tdd�D]t}tj
|�j�}yt|�}
Wntk
�rYn6Xtj|
�}	|	dk�r8tjd|
�tj|
tj�d
}
Ptjd�q�W|
�s\tjd||	d �|dk	�rp|||�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.leaseNZip�link�setZdevZupT)Zcapturez-1z-vz-lfz-pfz-sfz	/bin/true�g{�G�z�?)ZmaxwaitZnaplenz+dhclient did not produce expected files: %sz, css|]}tjj|�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$@)rr�
contextlib�suppress�FileNotFoundErrorr-�removerrZwait_for_filesZwarning�join�rangerr#�int�
ValueErrorZ
get_proc_ppid�kill�signal�SIGKILL�timeZsleep�errorr))Zdhclient_cmd_pathZ	interfacerZpid_filer'�cmd�out�errZmissingZppidZ
daemonized�_Zpid_content�pidrrr
rnsb
	



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�srGcCsT|dkrt}i}tjj|�s |Sx.tj|�D] }ttjtjj||���||<q,W|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�listdirrGrrr8)�leases_dZretZlfilerrr
�networkd_load_leases�srLcCsF|dkrt}t|d�}x*t|j��D]\}}|j|�r$||Sq$WdS)N)rK)rHrL�sorted�items�get)ZkeynamerKZleasesZ_ifindex�datarrr
�networkd_get_option_from_leases�s

rQcs�jd��dd�tjd��D�}g}�fdd�}d}�x�t|�D�]�\}}||krTq@t|�}|tdd	�kr�d
}t||d��|kr�|||t||d���|Sdj||d
|d��}	dj||d||��}
||}�n |tdd�k�rjd}t||d��|k�r$|||t||d���|Sdj||d
|d�dg�}	dj||d||��}
||}�n�|td
d�k�r�d}t||d��|k�r�|||t||d���|Sdj||d
|d�ddg�}	dj||d||��}
||}�n|td
d
�k�r�d}t||d��|k�r>|||t||d���|Sdj||d
|d�dddg�}	dj||d||��}
||}nx|dk�r�d}t||d��|k�r�|||t||d���|Sd}	dj||d
||��}
||}ntj	d|�|S|j
d|	|f|
f�q@W|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)r0�tokrrr
�
<listcomp>sz'parse_static_routes.<locals>.<listcomp>z[, .]csd|||�f}tj|�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)rr@)ZcidrZrequiredZremain�msg)�rfc3442rr
�_trunc_errorsz)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:r9rr8rr@r%)rU�tokensZ
static_routesrVZcurrent_idx�idxrRZ
net_lengthZreq_toksZnet_addressZgatewayr)rUr
�parse_static_routes�sn
	 "$


rg)NNN)N)N)N)r4Zloggingr-rr=r?�iorrFZ	cloudinitrrZ
cloudinit.netrrZ	getLoggerrrrH�	Exceptionrrrrrr)rrGrLrQrgrrrr
�<module>s,

"
[



Zerion Mini Shell 1.0