Viewing File: /usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyc

ó
ƒ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‹|dkrd
}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+
%
(
+

	!($RtloggingR_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	
Back to Directory File Manager