a '¡DgVã@sîdZddlZddlZddlmZddlmZmZmZm Z m Z m Z m Z ddl mZddlmZddlmZmZddlmZddlmZmZe e¡ZGdd „d ƒZGd d „d ƒZGd d „d ƒZGdd„dƒZe ee efe e dœdd„Z!dS)z.Module for ephemeral network context managers éN)Úpartial)ÚAnyÚCallableÚDictÚListÚLiteralÚOptionalÚTuple)ÚNoDHCPLeaseErrorÚmaybe_perform_dhcp_discovery)ÚProcessExecutionError)ÚUrlErrorÚ wait_for_urlc@sHeZdZdZdedœdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)ÚEphemeralIPv4NetworkaÏContext manager which sets up temporary static network configuration. No operations are performed if the provided interface already has the specified configuration. This can be verified with the connectivity_urls_data. If unconnected, bring up the interface with valid ip, prefix and broadcast. If router is provided setup a default route for that interface. Upon context exit, clean up the interface leaving no configuration behind. N)Úinterface_addrs_before_dhcpc Cs¼t||||gƒs$td ||||¡ƒ‚zt |¡|_Wn4tyh} ztd | ¡ƒ| ‚WYd} ~ n d} ~ 00||_||_||_||_ ||_ g|_ ||_ |j›d|j›�|_ | |ji¡|_dS)aäSetup context manager and validate call signature. @param interface: Name of the network interface to bring up. @param ip: IP address to assign to the interface. @param prefix_or_mask: Either netmask of the format X.X.X.X or an int prefix. @param broadcast: Broadcast address for the IPv4 network. @param router: Optionally the default gateway IP. @param static_routes: Optionally a list of static routes from DHCP z5Cannot init network on {0} with {1}/{2} and bcast {3}z4Cannot setup network, invalid prefix or netmask: {0}Nú/)ÚallÚ ValueErrorÚformatÚnetZipv4_mask_to_net_prefixÚprefixÚ interfaceÚipÚ broadcastÚrouterÚ static_routesÚ cleanup_cmdsÚdistroÚcidrÚgetr) ÚselfrrrÚprefix_or_maskrrrrÚe©r#ú;/usr/lib/python3.9/site-packages/cloudinit/net/ephemeral.pyÚ__init__s6ÿÿÿÿýÿzEphemeralIPv4Network.__init__c Csšzrz | ¡WnBtyP}z*dt|jƒvrŒsz8EphemeralIPv4Network._bringup_device..Úipv4cSsg|]}| d¡‘qSr2r3r4r#r#r$r6�sÿz1Skip adding ip address: %s already has address %sz9Skip bringing up network link: interface %s is already upZinet)ZfamilyzLNot queueing link down: link [%s] was up prior before receiving a dhcp leasezSNot queueing address removal: address %s was assigned before receiving a dhcp leaseN)ÚLOGÚdebugrrrÚnetinfoÚ netdev_inforrrrÚnet_opsZadd_addrÚlink_uprÚappendrZ link_downZdel_addr)r Zinterface_addrs_after_dhcpZhas_linkZhad_linkZhas_ipZhad_ipr#r#r$r&wsl üÿ    ÿ  þý ÿþýýÿýÿÿz$EphemeralIPv4Network._bringup_devicec CsJ|jD]>\}}|jj |j||¡|j dt|jjj|j||d�¡qdS)Nr©Úgateway) rrr<Z append_routerrÚinsertrÚ del_route)r Z net_addressr@r#r#r$r)Àsÿüþz+EphemeralIPv4Network._bringup_static_routesc Cs¦|jj ¡}d|vr,t d|j| ¡¡dS|jjj|j|j|j d�|j   dt |jjj |j|j|j d�¡|jjj|jd|jd�|j   dt |jjj |jdƒ¡dS)zriÚerror)r Ú exceptionsZephemeral_obtainedZimds_urlZipv4_ephemeral_obtainedZipv4_exceptionZipv6_ephemeral_obtainedZipv6_exceptionr#r#r$r,¯sB ÿþ  ÿ  ÿ ÿzEphemeralIPNetwork.__enter__)r7rd)rjÚreturnc Cs¬zf|dkr$|j t|j|jd�¡n.|dkrD|j t|j|jƒ¡ntd|›�ƒ‚t d|j|¡WdSt t fy¦}z$t d|j|¡d|fWYd }~Sd }~00d S) aÓ Attempt to bring up an ephemeral network for the specified IP version. Args: ip_version (str): The IP version to bring up ("ipv4" or "ipv6"). Returns: Tuple: A tuple containing: - a boolean indicating whether an ephemeral network was successfully obtained - an optional exception if ephemeral network setup failed or None if successful r7)rrSrdzUnsupported IP version: z7Successfully brought up %s for ephemeral %s networking.)TNz2Failed to bring up %s for ephemeral %s networking.FN) rhÚ enter_contextrOrrrKrr8r9r r )r rjr"r#r#r$rkãs:þÿþÿýýz3EphemeralIPNetwork._perform_ephemeral_network_setupcGs|j ¡dSrR)rhÚcloserMr#r#r$r+szEphemeralIPNetwork.__exit__)FTN)rFrGrHrIÚboolrrrr'rr%r,rr Ú Exceptionrkr+r#r#r#r$rc‹s úú 6ý 1rc)rQrnc s¤‡fdd„}ˆst d¡dStdd„ˆDƒƒp2d}z$tdd „ˆDƒ||d d d �\}}Wn0tyˆ}zt d |¡WYd}~n d}~00|sœt d¡dS|SdS)a± Perform a connectivity check to the provided URLs to determine if the ephemeral network setup is necessary. This function attempts to reach one of the provided URLs and returns the URL that was successfully reached. If none of the URLs can be reached, it returns None. The timeout for the request is determined by the highest timeout value provided in the connectivity URLs data. If no timeout is provided, a default timeout of 5 seconds is used. Args: connectivity_urls_data: A list of dictionaries, each containing the following keys: - "url" (str): The URL to check connectivity for. - "headers" (dict, optional): Headers to include in the request. - "timeout" (int, optional): Timeout for the request in seconds. Returns: Optional[str]: The URL that was successfully reached, or None if no connectivity was established. cs‡fdd„ˆDƒd}|S)z‘ Helper function to get headers for a given URL from the connectivity URLs data provided to _check_connectivity_to_imds. cs"g|]}|dˆkr| d¡‘qS©ÚurlZheadersr3©r5Zurl_data©rtr#r$r68s þzD_check_connectivity_to_imds.._headers_cb..rr#rsrPrvr$Ú _headers_cb3s  þüz0_check_connectivity_to_imds.._headers_cbzZNo connectivity URLs provided. Skipping connectivity check before ephemeral network setup.Ncss|]}| dd¡VqdS)ÚtimeoutrNr3rur#r#r$Ú Hóz._check_connectivity_to_imds..écSsg|] }|d‘qSrvr#rur#r#r$r6Nrzz/_check_connectivity_to_imds..Fr)ZurlsZ headers_cbrxZconnect_synchronouslyZmax_waitz8Failed to reach IMDS without ephemeral network setup: %sz5Failed to reach IMDS without ephemeral network setup.)r8r9Úmaxrr )rQrwrxZurl_that_workedÚ_r"r#rPr$rWs6 ÿþ ûþ rW)"rIrfZloggingÚ functoolsrÚtypingrrrrrrr Z cloudinit.netrZcloudinit.netinfor:Zcloudinit.net.dhcpr r Zcloudinit.subpr Zcloudinit.url_helperr rZ getLoggerrFr8rrKrOrcr'rWr#r#r#r$Ús& $    f sþ