Current File : //lib/python2.7/site-packages/cloudinit/net/dhcp.pyo
�
�jbc@sMddlZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
ddlmZ
ddlmZddlmZddlmZeje�ZdZdefd	��YZd
efd��YZdefd
��YZdd�Zd�Zd�Zd�Z dd�Z!dd�Z"d�Z#dS(i����N(tEphemeralIPv4Networktfind_fallback_nictget_devicelistthas_url_connectivity(tmask_and_ipv4_to_bcast_addr(t
temp_utils(tutil(tStringIOs/run/systemd/netif/leasestInvalidDHCPLeaseFileErrorcBseZdZRS(s�Raised when parsing an empty or invalid dhcp.leases file.

    Current uses are DataSourceAzure and DataSourceEc2 during ephemeral
    boot to scrape metadata.
    (t__name__t
__module__t__doc__(((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyRstNoDHCPLeaseErrorcBseZdZRS(s'Raised when unable to get a DHCP lease.(R	R
R(((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR$stEphemeralDHCPv4cBsMeZddd�Zd�Zd�Zd�Zd�Zd�Zd�Z	RS(cCs(||_d|_d|_||_dS(N(tifacetNonet_ephipv4tleasetconnectivity_url(tselfRR((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyt__init__*s			cCs<|jr2t|j�r2tjd|j�dSn|j�S(sUSetup sandboxed dhcp context, unless connectivity_url can already be
        reached.s:Skip ephemeral DHCP setup, instance has connectivity to %sN(RRtLOGtdebugtobtain_lease(R((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyt	__enter__0s	
cCs|j�dS(s Teardown sandboxed dhcp context.N(t
clean_network(Rt	excp_typet
excp_valuetexcp_traceback((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyt__exit__;scCs<|jrd|_n|js"dS|jjddd�dS(s@Exit _ephipv4 context to teardown of ip configuration performed.N(RRRR(R((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR?s
		cCsW|jr|jSyt|j�}Wntk
r?t��nX|sRt��n|d|_tjd|jd|jd|jd�idd6dd6dd6dd	6d
dgd6d
d6}|j|�}|d	s�t|d|d�|d	<n|drt	|d�|d<n|j
r1|j
|d<nt|�}|j�||_
|jS(s9Perform 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.
        i����s#Received dhcp lease on %s for %s/%st	interfaces
fixed-addressssubnet-masktiptprefix_or_masksbroadcast-addresst	broadcastsrfc3442-classless-static-routessclassless-static-routest
static_routestrouterstrouterR(Rtmaybe_perform_dhcp_discoveryRRRRRtextract_dhcp_options_mappingtbciptparse_static_routesRRRR(Rtleasestnmaptkwargstephipv4((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyRGs:		


	



	
	cCsbi}xU|j�D]G\}}t|t�rD|j|||�q|jj|�||<qW|S(N(titemst
isinstancetlisttget_first_option_valueRtget(RR*tresulttinternal_referencetlease_option_names((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR&qs
cCs=x6|D].}|j|�s|jj|�||<qqWdS(N(R1R(Rtinternal_mappingR4R2tdifferent_names((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR0~s
N(
R	R
RRRRRRR&R0(((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR
)s				*	
cCs�|d	kr5t�}|d	krXtjd�gSn#|t�krXtjd|�gStjd�}|s~tjd�gStjdt	dddt	��}t
|||�SWd	QXd	S(
s�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.
    @return: A list of dicts representing dhcp options for each lease obtained
        from the dhclient discovery if run, otherwise an empty list is
        returned.
    s1Skip dhcp_discovery: Unable to find fallback nic.s8Skip dhcp_discovery: nic %s not found in get_devicelist.tdhclients7Skip dhclient configuration: No dhclient command found.trmtree_ignore_errorstprefixscloud-init-dhcp-t	needs_exeN(RRRRRRtwhichRttempdirtTruetdhcp_discovery(tnict
dhclient_pathttdir((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR%�s"	


cCstjd�}g}tj|�}t|�dkrNtdj|���nx�|j|�D]�}g}x`|jd�D]O}|j	�j
dd�j
dd�}|s�qzn|j|jdd	��qzW|jt|��q^W|std
j|���n|S(sParse 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.
    slease {(?P<lease>[^}]*)}\nis&Cannot parse empty dhcp lease file {0}t;t"tsoption t is1Cannot parse dhcp lease file {0}. No leases found(
tretcompileRt	load_filetlenRtformattfindalltsplittstriptreplacetappendtdict(t
lease_filetlease_regextdhcp_leasest
lease_contentRt
lease_optionstline((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pytparse_dhcp_lease_file�s&	$c
Cs�tjd|�tjj|d�}tj||�tjj|d�}tjj|d�}tj|tj�s}|}ntj	dddd|d	gd
t
�|ddd
|d||ddg
}tj	|d
t
�tj||gdddd�}|r'tjddjd�|D���gSd}x�t
dd�D]�}	tj|�j�}
yt|
�}Wntk
r{nIXtj|�}|dkr�tjd|�tj|tj�t|�Stjd�q=Wtjd|
|d�t|�S(s�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 inteface on which to dhclient.
    @param cleandir: The directory from which to run dhclient as well as store
        dhcp leases.

    @return: A list of dicts of representing the dhcp leases parsed from the
        dhcp.leases file or empty list.
    s!Performing a dhcp discovery on %sR7sdhclient.pidsdhcp.leasesRtlinktsettdevtuptcaptures-1s-vs-lfs-pfs-sfs	/bin/truetmaxwaititnapleng{�G�z�?s+dhclient did not produce expected files: %ss, css!|]}tjj|�VqdS(N(tostpathtbasename(t.0tf((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pys	<genexpr>�stunknownii�iskilling dhclient with pid=%ssCdhclient(pid=%s, parentpid=%s) failed to daemonize after %s secondsg$@(RRR_R`tjoinRtcopytaccesstX_OKtsubpR=twait_for_filestwarningtrangeRHRMtintt
ValueErrort
get_proc_ppidtkilltsignaltSIGKILLRWttimetsleepterror(tdhclient_cmd_pathRtcleandirtsandbox_dhclient_cmdtpid_fileRQtcmdtmissingtppidt_tpid_contenttpid((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR>�sD	%		


cCsttjt|�dt��S(s�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.tlist_values(RPt	configobjt	ConfigObjRtFalse(tcontent((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pytnetworkd_parse_lease
scCsw|dkrt}ni}tjj|�s1|Sx?tj|�D].}ttjtjj	||���||<qAW|S(s�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(
RtNETWORKD_LEASES_DIRR_R`tisdirtlistdirR�RRHRe(tleases_dtrettlfile((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pytnetworkd_load_leasess	)cCsb|dkrt}ntd|�}x7t|j��D]#\}}|j|�r7||Sq7WdS(NR�(RR�R�tsortedR-R1(tkeynameR�R)t_ifindextdata((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pytnetworkd_get_option_from_leases*s	cs��jd��gtjd��D]}|r"|^q"}g}�fd�}d}x^t|�D]P\}}||kr�qbnt|�}|tdd�kr$d}t||�|kr�|||t||��|Sdj||d	|d
!�}	dj||d
||!�}
||}nq|tdd�kr�d}t||�|krs|||t||��|Sdj||d	|d
!dg�}	dj||d
||!�}
||}n�|tdd�kred}t||�|kr|||t||��|Sdj||d	|d!ddg�}	dj||d||!�}
||}n0|td	d�kr
d}t||�|kr�|||t||��|Sdj||d	|d!dddg�}	dj||d||!�}
||}n�|dkr�d
}t||�|krP|||t||��|Sd}	dj||d	||!�}
||}ntj	d|�|S|j
d|	|f|
f�qbW|S(s� 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
    RBs[, .]cs'd|||�f}tj|�dS(Ns�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(RRu(tcidrtrequiredtremaintmsg(trfc3442(s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyt_trunc_errorRsiii!i	t.iiiiit0iiiis0.0.0.0sSParsed invalid net length "%s".  Verify DHCP rfc3442-classless-static-routes value.s%s/%s(trstripRFRLt	enumerateRmRlRIReRRuRO(R�ttokttokensR"R�tcurrent_idxtidxt
net_lengthtreq_tokstnet_addresstgateway((R�s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR(4sl+
%
(
+

	!($R�tloggingR_RFRqRst
cloudinit.netRRRRtcloudinit.net.network_stateRR't	cloudinitRRtsixRt	getLoggerR	RR�t	ExceptionRRtobjectR
RR%RWR>R�R�R�R((((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyt<module>s,"	\		J