%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /snap/core/17212/usr/lib/python3/dist-packages/cloudinit/net/__pycache__/
Upload File :
Create Path :
Current File : //snap/core/17212/usr/lib/python3/dist-packages/cloudinit/net/__pycache__/dhcp.cpython-35.pyc



"�T`�=�@sxddlZddlZddlZddlZddlZddlZddlmZddlm	Z	m
Z
mZmZddl
mZddlmZddlmZddlmZeje�ZdZGd	d
�d
e�ZGdd�de�ZGd
d�de�Zdddd�Zdd�Zddd�Zdd�Z ddd�Z!ddd�Z"dd�Z#dS)�N)�StringIO)�EphemeralIPv4Network�find_fallback_nic�get_devicelist�has_url_connectivity)�mask_and_ipv4_to_bcast_addr)�
temp_utils)�subp)�utilz/run/systemd/netif/leasesc@seZdZdZdS)�InvalidDHCPLeaseFileErrorz�Raised when parsing an empty or invalid dhcp.leases file.

    Current uses are DataSourceAzure and DataSourceEc2 during ephemeral
    boot to scrape metadata.
    N)�__name__�
__module__�__qualname__�__doc__�rr�4/usr/lib/python3/dist-packages/cloudinit/net/dhcp.pyrsrc@seZdZdZdS)�NoDHCPLeaseErrorz'Raised when unable to get a DHCP lease.N)rr
rrrrrrr$src@smeZdZddddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dS)�EphemeralDHCPv4NcCs1||_d|_d|_||_||_dS)N)�iface�_ephipv4�lease�
dhcp_log_func�connectivity_url)�selfrrrrrr�__init__)s
				zEphemeralDHCPv4.__init__cCs9|jr/t|j�r/tjd|j�dS|j�S)zUSetup sandboxed dhcp context, unless connectivity_url can already be
        reached.z:Skip ephemeral DHCP setup, instance has connectivity to %sN)rr�LOG�debug�obtain_lease)rrrr�	__enter__0s	
zEphemeralDHCPv4.__enter__cCs|j�dS)z Teardown sandboxed dhcp context.N)�
clean_network)rZ	excp_typeZ
excp_valueZexcp_tracebackrrr�__exit__;szEphemeralDHCPv4.__exit__cCs9|jrd|_|jsdS|jjddd�dS)z@Exit _ephipv4 context to teardown of ip configuration performed.N)rrr )rrrrr?s
			zEphemeralDHCPv4.clean_networkcCsb|jr|jSyt|j|j�}Wn1tk
r\}zt�|�WYdd}~XnX|slt��|d|_tjd|jd|jd|jd�ddddddd	d
ddd
gddi}|j|�}|d	st	|d|d�|d	<|dr&t
|d�|d<|jr<|j|d<t|�}|j
�||_|jS)a9Perform dhcp discovery in a sandboxed environment if possible.

        @return: A dict representing dhcp options on the most recent lease
            obtained from the dhclient discovery if run, otherwise an error
            is raised.

        @raises: NoDHCPLeaseError if no leases could be obtained.
        N�z#Received dhcp lease on %s for %s/%s�	interfacez
fixed-addresszsubnet-mask�ipZprefix_or_maskZ	broadcastzbroadcast-address�
static_routeszrfc3442-classless-static-routeszclassless-static-routesZrouterZroutersr���)r�maybe_perform_dhcp_discoveryrrrrrr�extract_dhcp_options_mapping�bcip�parse_static_routesrrrr)r�leases�e�nmap�kwargsZephipv4rrrrGs>			
	

	

	zEphemeralDHCPv4.obtain_leasecCsbi}xU|j�D]G\}}t|t�rD|j|||�q|jj|�||<qW|S)N)�items�
isinstance�list�get_first_option_valuer�get)rr,�resultZinternal_reference�lease_option_namesrrrr'rs
z,EphemeralDHCPv4.extract_dhcp_options_mappingcCs:x3|D]+}|j|�s|jj|�||<qWdS)N)r2r)rZinternal_mappingr4r3Zdifferent_namesrrrr1s
z&EphemeralDHCPv4.get_first_option_value)
rr
rrrr rrr'r1rrrrr(s+
rcCs�|dkr5t�}|dkrXtjd�gSn#|t�krXtjd|�gStjd�}|s~tjd�gStjdddd	d
d��}t||||�SWdQRXdS)aPerform 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.
    @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.Zrmtree_ignore_errorsT�prefixzcloud-init-dhcp-Z	needs_exe)	rrrrr	ZwhichrZtempdir�dhcp_discovery)ZnicrZ
dhclient_pathZtdirrrrr&�s"
	


r&cCs�tjd�}g}tj|�}t|�dkrKtdj|���x�|j|�D]}g}x]|jd�D]L}|j	�j
dd�j
dd�}|s�qw|j|jdd	��qwW|jt|��q[W|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 � r!z1Cannot parse dhcp lease file {0}. No leases found)
�re�compiler
�	load_file�lenr�format�findall�split�strip�replace�append�dict)�
lease_fileZlease_regexZdhcp_leasesZ
lease_contentrZ
lease_options�linerrr�parse_dhcp_lease_file�s&	$rIcCs tjd|�tjj|d�}tj||�tjj|d�}tjj|d�}tj|tj�sz|}t	j	dddd|d	gd
d�|dd
d|d||ddg
}t	j	|d
d�\}}	tj
||gdddd�}
|
r/tjddjdd�|
D���gSd}d}x�tdd�D]�}
tj
|�j�}yt|�}Wntk
r�YnFXtj|�}|dkr�tjd|�tj|tj�d}Ptjd�qKW|s�tjd ||d"�|d!k	r|||	�t|�S)#aRun 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 inteface on which to dhclient.
    @param cleandir: The directory from which to run dhclient as well as store
        dhcp leases.
    @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
        dhcp.leases file or empty list.
    z!Performing a dhcp discovery on %sr5zdhclient.pidzdhcp.leasesr#�link�setZdevZupZcaptureTz-1z-vz-lfz-pfz-sfz	/bin/trueZmaxwait�Znapleng{�G�z�?z+dhclient did not produce expected files: %sz, css!|]}tjj|�VqdS)N)�os�path�basename)�.0�frrr�	<genexpr>�sz!dhcp_discovery.<locals>.<genexpr>�unknownFri�r!zkilling dhclient with pid=%szCdhclient(pid=%s, parentpid=%s) failed to daemonize after %s secondsNg$@)rrrMrN�joinr
�copy�access�X_OKr	Zwait_for_filesZwarning�ranger>rC�int�
ValueErrorZ
get_proc_ppid�kill�signal�SIGKILL�timeZsleep�errorrI)Zdhclient_cmd_pathr"ZcleandirrZsandbox_dhclient_cmdZpid_filerG�cmd�out�errZmissingZppidZ
daemonized�_Zpid_content�pidrrrr7�sN
%		


r7cCsttjt|�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.Zlist_valuesF)rF�	configobjZ	ConfigObjr)Zcontentrrr�networkd_parse_leasesrfcCst|dkrt}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_DIRrMrN�isdir�listdirrfr
r>rT)�leases_dZretZlfilerrr�networkd_load_leases"s)rkcCs_|dkrt}td|�}x7t|j��D]#\}}|j|�r4||Sq4WdS)Nrj)rgrk�sortedr.r2)Zkeynamerjr*Z_ifindex�datarrr�networkd_get_option_from_leases4srncs�jd��dd�tjd��D�}g}�fdd�}d}x�t|�D]�\}}||krtqYt|�}|tdd	�kr*d
}t||d��|kr�|||t||d���|Sdj||d
|d��}	dj||d||��}
||}n�|tdd�kr�d}t||d��|kr�|||t||d���|Sdj||d
|d�dg�}	dj||d||��}
||}n|td
d�kr�d}t||d��|kr6|||t||d���|Sdj||d
|d�ddg�}	dj||d||��}
||}nQ|td
d
�krFd}t||d��|kr�|||t||d���|Sdj||d
|d�dddg�}	dj||d||��}
||}n�|dkr�d}t||d��|kr�|||t||d���|Sd}	dj||d
||��}
||}ntj	d|�|S|j
d|	|f|
f�qYW|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
    r8cSsg|]}|r|�qSrr)rP�tokrrr�
<listcomp>Ys	z'parse_static_routes.<locals>.<listcomp>z[, .]cs'd|||�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)�rfc3442rr�_trunc_error\sz)parse_static_routes.<locals>._trunc_errorr��!�	N�.r!rL����0����z0.0.0.0zSParsed invalid net length "%s".  Verify DHCP rfc3442-classless-static-routes value.z%s/%s)�rstripr<rB�	enumeraterYrXr?rTrr_rE)rr�tokensr$rsZcurrent_idx�idxroZ
net_lengthZreq_toksZnet_addressZgatewayr)rrrr)>sl !!
 (!
 +!
 .!
 !
	!r))$reZloggingrMr<r\r^�iorZ
cloudinit.netrrrrZcloudinit.net.network_staterr(Z	cloudinitrr	r
Z	getLoggerrrrg�	Exceptionrr�objectrr&rIr7rfrkrnr)rrrr�<module>s."^!Q

Zerion Mini Shell 1.0