܍~c9dZddlZddlZddlmZmZmZddlmZddl m Z ddl m Z m Z mZejeZGddZGdd ZGd d ZGd d ZdS)z.Module for ephemeral network context managers N)AnyDictList)subp)NoDHCPLeaseErrormaybe_perform_dhcp_discoveryparse_static_routescZeZdZdZ d deeeffdZdZdZ dZ dZ d Z d Z dS) EphemeralIPv4NetworkaContext 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_url_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. Nconnectivity_url_datacxt||||gs%td|||| tj||_n5#t$r(}td||d}~wwxYw||_||_||_||_ ||_ ||_ g|_ dS)aXSetup 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 connectivity_url_data: Optionally, a URL to verify if a usable connection already exists. @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 ValueErrorformatnetipv4_mask_to_net_prefixprefixr interfaceip broadcastrouter static_routes cleanup_cmds) selfrrprefix_or_maskrrr res 9/usr/lib/python3/dist-packages/cloudinit/net/ephemeral.py__init__zEphemeralIPv4Network.__init__s,Ir>9=>> GNNr>9   5nEEDKK   %vayy   &;""" *')sA B#BBc2|jrAtj|jr(td|jddS||jr|dS|jr| dSdS)z>Perform ephemeral network setup if interface is not connected.z=Skip ephemeral network setup, instance has connectivity to %surlN) r rhas_url_connectivityLOGdebug_bringup_devicer_bringup_static_routesr_bringup_routerrs r __enter__zEphemeralIPv4Network.__enter__Ls  % '(BCC  .u5     #  ' ' ) ) ) ) ) [ #  " " " " " # #cF|jD]}tj|ddS)zTeardown anything we set up.TcaptureN)rr)r excp_type excp_valueexcp_tracebackcmds r__exit__zEphemeralIPv4Network.__exit__is6$ ) )C Ic4 ( ( ( ( ( ) )r)c Vtjddddd|d|d|jgd d S) z7Perform the ip command to remove the specified address.r-familyinetaddrdel/devTr+N)rr)raddressrs r_delete_addressz$EphemeralIPv4Network._delete_addressnsT "77FF+  r)c d|j|j}td|j||j tjddddd|d|jd |jg d d d i tjdddddd |jdgd |j dddddd |jdg|j ddddd|d |jgdS#tj $rH}dt|j vrtd|j|jYd}~dSd}~wwxYw)z1Perform the ip comands to fully setup the device.z{0}/{1}z:Attempting setup of ephemeral network on %s with %s brd %srr3r4r5addrr8TLANGC)r, update_envlinksetupr+downr6z File existsz7Skip ephemeral network setup, %s already has address %sN) rrrr"r#rrrrappendProcessExecutionErrorstrstderr)rcidrrs rr$z$EphemeralIPv4Network._bringup_device~s55 H N  N    ?  INN "C=    4 IN        $ $N       $ $N      G)   CMM11 III          s -C))E8=D;;Ec |jD]f\}}g}|dkrd|g}tjdddd|g|zd|jgzd |jd dddd |g|zd|jgzgdS) Nz0.0.0.0viar-4routerDr8Tr+rr6)rrrrinsert)r net_addressgatewayvia_args rr%z+EphemeralIPv4Network._bringup_static_routess%)$6   KG)## '* ItWh <$.)*        $ $tWe[9$.)*      r)c tjgdd\}}d|vr5td|j|dStjddd d |jd |jd |jg d|jd ddd d|jd |jd |jg tjddd d dd|jd |jg d|jd ddd ddd |jgdS)z>/+J+J'( K Kr))NNNN) rXrYrZrrFrrr(r1rrrprwrr\r)rrgrg8s04   $CH~     # # #111000d   KKKKKr)rgc6eZdZdZ d dedefdZdZd ZdS) EphemeralIPNetworkz9Marries together IPv4 and IPv6 ephemeral context managersFTNipv6ipv4c|||_||_||_tj|_d|_||_dS)N)rrr contextlib ExitStackstack state_msgrn)rrrrrns rrzEphemeralIPNetwork.__init__s;#  )++   r)c4 |jr3|jt|j|j|jr,|jt|jn(#t$r}|jrd|_ n|Yd}~nd}~wwxYw|S)N)rnzusing link-local ipv6) rr enter_contextrgrrnrr^rr)rrs rr(zEphemeralIPNetwork.__enter__s y  ((#DNDLIIIy O (()=dn)M)MNNN    y !8   sA-A00 B:BBc8|jdSri)rcloserds rr1zEphemeralIPNetwork.__exit__s r))FTN)rXrYrZr[boolrr(r1r\r)rrrspCC        &r)r)r[rloggingtypingrrr cloudinit.netr cloudinitrcloudinit.net.dhcprrr getLoggerrXr"r r^rgrr\r)rrsq"""""""""" g!!        D44444444DjKjKjKjKjKjKjKjKZ%%%%%%%%%%r)