jbc@sMddlZddlZddlZddlZddlZddlZddlmZmZm Z m Z ddl m Z ddlmZddlmZddlmZejeZdZdefd YZd efd YZd efd YZddZdZdZdZ ddZ!ddZ"dZ#dS(iN(tEphemeralIPv4Networktfind_fallback_nictget_devicelistthas_url_connectivity(tmask_and_ipv4_to_bcast_addr(t temp_utils(tutil(tStringIOs/run/systemd/netif/leasestInvalidDHCPLeaseFileErrorcBseZdZRS(sRaised 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 $stEphemeralDHCPv4cBsMeZdddZdZdZdZdZdZdZ 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|jr2tjd|jdSn|jS(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|jdS(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|jjddddS(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?tnX|sRtn|d|_tjd|jd|jd|jdidd6dd6dd6dd 6d d gd 6d d6}|j|}|d st|d|d|d [^}]*)}\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_filet lease_regext dhcp_leasest lease_contentRt lease_optionstline((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pytparse_dhcp_lease_files& $c Cstjd|tjj|d}tj||tjj|d}tjj|d}tj|tjs}|}ntj dddd|d gd t |d d d |d||ddg }tj |d t tj ||gdddd}|r'tj ddjd|DgSd}xt ddD]} tj|j} yt| } Wntk r{nIXtj| }|dkrtjd| tj| tjt|Stjdq=Wtjd| |dt|S(sRun 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.pids dhcp.leasesRtlinktsettdevtuptcaptures-1s-vs-lfs-pfs-sfs /bin/truetmaxwaititnapleng{Gz?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 stunknowniiiskilling 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_t pid_contenttpid((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyR>sD  %      cCsttjt|dtS(sParse 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.t list_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 ||||RRRR((((s6/usr/lib/python2.7/site-packages/cloudinit/net/dhcp.pyts,      " \   J