+
    i]                     P   ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIH	t	H
t
Ht ^ RIHtHtHtHt ^ RIHtHtHtHtHtHt ^ RIHtHt RtRt]P:                  ! ]4      tR	 t R
 R lt!R t"RR lt#RR R llt$R R lt%R R lt&R R lt' ! R R]PP                  4      t(R t)R# )    N)SpooledTemporaryFile)CallableListOptional)featuressafeyamlsubputil)IPV6_DYNAMIC_TYPESSYS_CLASS_NETget_devicelistrenderershould_add_gateway_onlink_flagsubnet_is_ipv6)NET_CONFIG_TO_V2NetworkStatez/etc/netplan/50-cloud-init.yamls4  # This is the initial network config.
# It can be overwritten by cloud-init or console-conf.
network:
    version: 2
    ethernets:
        all-en:
            match:
                name: "en*"
            dhcp4: true
        all-eth:
            match:
                name: "eth*"
            dhcp4: true
c                 J   a \        V3R  lV P                  4        4       4      # )c              3   `   <"   T F#  w  rVP                  S4      '       g   K  W3x  K%  	  R # 5iN)
startswith).0keyvaluematchs   &  7/usr/lib/python3/dist-packages/cloudinit/net/netplan.py	<genexpr>,_get_params_dict_by_match.<locals>.<genexpr>-   s*      *LS>>%  	*s   ..)dictitems)configr   s   &fr   _get_params_dict_by_matchr!   ,   s#     "LLN      c                <    V ^8  d   QhR\         R\         R\        /# )   r    entryr   )r   r   )formats   "r   __annotate__r'   4   s+     VK VKt VKD VKH VKr"   c                   R#R lp. p. p. p. pV P                  R. 4      p	V	f   . p	V	 EF  p
V
P                  R4      pV
P                  R4      pVP                  R4      '       dY   VR8X  d
   VR,          pVP                  VR/4       Ve0   V R	2pVP                  V/ 4      pWR
&   VP                  W/4       K  K  V\        9   dE   VP                  RR/4       Ve,   VP                  R/ 4      pWR
&   VP                  RV/4       K  K  VR$9   g   K  RV
P                  R4      ,          pRV
9   d    VRV
P                  R4      ,          ,          pV
P                  R4      '       dl   RV
P                  R4      RR/p\	        V
R,          V4      '       d$   \
        P                  RV
R,          V4       RVR&   Ve   VVR&   VP                  V4       RV
9   d   Wt! V
P                  R. 4      4      ,          pRV
9   d   W! V
P                  R. 4      4      ,          pRV
9   dD   Rp\        V
4      '       d   RV! 4       9   d   RpVP                  VV
P                  R4      /4       V
P                  R. 4       Ft  pVP                  R4      : RVP                  R4      : 2pRVP                  R4      RV/pVP                  R4      pVe   VVR&   M	Ve   VVR&   VP                  V4       Kv  	  VP                  V4       EK  	  RV 9   dT   VP                  R4      pV'       d/   V R,          V8w  d!   \
        P                  RVV R,          V4       MV R,          VR&   \        V4      ^ 8  d   VP                  RV/4       \        V4      ^ 8  d   VP                  RV/4       \        V4      ^ 8  d   RV/pVP                  R V/4       \        V4      ^ 8  d9   VP                  R / 4      pVP                  R!V/4       VP                  R V/4       R"V 9   dF   V R",          e9   VP                  R"\        P                  ! V P                  R"4      4      /4       R# R# R# )%aj  This method parse a cloudinit.net.network_state dictionary (config) and
   maps netstate keys/values into a dictionary (entry) to represent
   netplan yaml. (config v1 -> netplan)

An example config dictionary might look like:

{'mac_address': '52:54:00:12:34:00',
 'name': 'interface0',
 'subnets': [
    {'address': '192.168.1.2/24',
     'mtu': 1501,
     'type': 'static'},
    {'address': '2001:4800:78ff:1b:be76:4eff:fe06:1000",
     'mtu': 1480,
     'netmask': 64,
     'type': 'static'}],
  'type: physical',
  'accept-ra': 'true'
}

An entry dictionary looks like:

{'set-name': 'interface0',
 'match': {'macaddress': '52:54:00:12:34:00'},
 'mtu': 1501}

After modification returns

{'set-name': 'interface0',
 'match': {'macaddress': '52:54:00:12:34:00'},
 'mtu': 1501,
 'address': ['192.168.1.2/24', '2001:4800:78ff:1b:be76:4eff:fe06:1000"],
 'ipv6-mtu': 1480}

c                v    V '       d   \        V \        4      '       g   V # W9   d   V P                  V4      # V .# )zD
Helper to convert strings to list of strings, handle single string
)
isinstancestrsplit)objtokens   &&r   _listify$_extract_addresses.<locals>._listifyY   s;     *S#..J<99U##  r"   subnetsNtypemetricdhcp4Tz
-overrideszroute-metricdhcp6zdhcp6-overridesz%saddressprefixz/%dgatewayviatodefaultzAGateway %s is not contained within subnet %s, adding on-link flagzon-linkdns_nameservers
dns_searchmtuzipv6-mturoutesnetwork/zZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.	addressesnameserverssearchz	accept-ra) )staticstatic6)getr   updater   r   LOGdebugappendr   warninglenr
   is_true)r    r%   ifnamer   r/   rC   r@   rD   searchdomainsr1   subnetsn_type	sn_metricdhcp_override_keydhcp_overrideaddr	new_routemtukeyrouteto_netroute_metric	entry_mtunss   &&&&                   r   _extract_addressesr`   4   s.   J IFKMjjB'G**V$JJx(	f%%& 3LL'4)$'.iz$:! %		*;R @09n-/?@ % **LL'4)$ %		*;R @09n-/?@	 %
 --&**Y//D6!

8 444zz)$$6::i0)	 2&2CTJJII/y)	 ,0Ii((*3Ih'i( F*x

3Db(IJJv%&**\2*F!GG!&))jHJ.F'Fffjj&789Hb1$)IIi$8%))H:MN599Y/&	
  %yy2+*6Ih'**3Ih'i( 2 T"G J IIe$	)3KK6u "%=E%L
9~k9-.
6{Qh'(
;!;'mR()
=AYY}b)
		8]+,mR()f!4!@k4<<

;0G#HIJ "Ar"   c           
          \        V P                  4        UUu. uF   w  r4VP                  R R4      V8X  g   K  VNK"  	  upp4      p\        V4      ^ 8  d   VP	                  RV/4       R# R# u uppi )zbond-masterN
interfaces)sortedr   rI   rO   rJ   )rb   r%   bond_masternamecfgbond_slave_namess   &&&   r   _extract_bond_slaves_by_namerh      sv      *//1	
1ww}d+{: D1	
 q l$456 !	
s
   A.
A.
c                    \         P                  ! V R 4      p\        P                  P	                  V4      '       g   R# \
        P                  ! V4      pV\        8w  d   R# R Uu. uF  p\         P                  ! W4      NK  	  ppV Uu. uF,  p\        P                  P	                  V4      '       g   K*  VNK.  	  pp\        P                  RVV4       V.V,            F  p\        P                  ! V4       K  	  R# u upi u upi )z etc/netplan/00-snapd-config.yamlNz9removing known config '%s' and derived existing files: %s)z-run/systemd/network/10-netplan-all-en.networkz.run/systemd/network/10-netplan-all-eth.networkz#run/systemd/generator/netplan.stamp)r	   target_pathospathisfiler
   load_binary_fileKNOWN_SNAPD_CONFIGrK   rL   unlink)targettpathcontentfderivedexistings   &     r   _clean_defaultrw      s     V%GHE77>>%  ##E*G$$

A 	#
   #87abggnnQ&77H8IIC Wx
		!   9s   &C?'D7Dc                R    V ^8  d   QhR\         R\        \         ,          R\        /# )r$   net_config_contentrq   return)r+   r   bool)r&   s   "r   r'   r'      s(     6 66%-c]6	6r"   c                    ^ RI Hp ^ RIHp \        T 4      p  \        RR7      ;_uu_ 4       pTP                  T 4       TP                  4        TP                  ^ \        P                  4       T! 4       pTP                  T4       T! 4       pTP!                  T4       T'       g%   \"        P$                  P'                  \        4      pMTpTP)                  T4       RRR4       \
        P                  R	4       R
#   \         d    \
        P                  R\        4        R# i ; i  + '       g   i     LS; i  \*         d'   p\
        P-                  R\        T4        Rp?R# Rp?ii ; i)a@  Use netplan.State._write_yaml_file to write netplan config

Where netplan python API exists, prefer to use of the private
_write_yaml_file to ensure proper permissions and file locations
are chosen by the netplan python bindings in the environment.

By calling the netplan API, allow netplan versions to change behavior
related to file permissions and treatment of sensitive configuration
under the API call to _write_yaml_file.

In future netplan releases, security-sensitive config may be written to
separate file or directory paths than world-readable configuration parts.
)Parser)Statez.No netplan python module. Fallback to write %sFwmodeNzUUnable to render network config using netplan python module. Fallback to write %s. %sz0Rendered netplan config using netplan python APIT)netplan.parserr}   netplan.stater~   ImportErrorrK   rL   CLOUDINIT_NETPLAN_FILE_maybe_strip_invalid_mtur   writeflushseekioSEEK_SET	load_yamlimport_parser_resultsrk   rl   basename_write_yaml_file	ExceptionrN   )	ry   rq   r}   r~   rt   parserstate_output_filefilees	   &&       r   netplan_api_write_yaml_filer      s#    )' 22DE!s++qGG&'GGIFF1bkk"XFQ %33F; ww''(>?..t4# ,4 II@AG  		<"	
  ,+$  ("		
 sF   C> D= B1D*D= >%D'&D'*D:	5D= :D= =E.E))E.c                $    V ^8  d   QhR\         /# )r$   ry   r+   )r&   s   "r   r'   r'   /  s       r"   c                    \         P                  '       d)   \        P                  ! RRV \        P                  R7      p V # )a  Strip invalid MTU from the netplan config.

This is a fix for https://github.com/canonical/cloud-init/issues/6239
A 0 MTU value is NOT valid, but cloud-init accepted it prior to 24.2,
so rejecting it after c465de8 is a breaking change for existing releases.
z^\s*mtu:\s*0\s*\n )flags)r   STRIP_INVALID_MTUresub	MULTILINE)ry   s   &r   r   r   /  s9     !!!  VV ,,	
 r"   c                <    V ^8  d   QhR\         R\         R\        /# )r$   cfg_filers   rz   )r+   r{   )r&   s   "r   r'   r'   F  s!     0 0 0s 0t 0r"   c                    \         P                  P                  V 4      '       g   R# \        P                  ! \        P
                  ! V 4      4      pV\        P                  ! V4      8g  # )z<Return True when new netplan config has changed vs previous.T)rk   rl   existsr
   r   load_text_file)r   rs   	prior_cfgs   && r   has_netplan_config_changedr   F  sE    77>>(##t228<=Iw///r"   c                0    V ^8  d   QhR\         R\         /# )r$   r   rs   r   )r&   s   "r   r'   r'   P  s     2 2# 2 2r"   c                    \         P                  '       d   RMRp\        P                  P	                  V 4      '       d&   \
        P                  ! V 4      pW2,          V8X  d   Tp\
        P                  ! WVR7       R# )zDWrite netplan config to cfg_file because python API was unavailable.i  i  r   N)r   NETPLAN_CONFIG_ROOT_READ_ONLYrk   rl   r   r
   get_permissions
write_file)r   rs   r   current_modes   &&  r   fallback_write_netplan_yamlr   P  sU    :::5D	ww~~h++H5,.DOOHD1r"   c                      a  ] tR tRt o RtRR.tRR.tRR ltV 3R lR	 ltRV 3R
 lR llt	V 3R lR lt
RR ltV 3R lR ltRtV tR# )Rendereri[  zBRenders network information in a /etc/netplan/network.yaml format.netplangenerateinfoNc                   V'       g   / pVP                  R \        4      V n        VP                  RR4      V n        VP                  RR4      V n        VP                  RR4      V n        VP                  R4      ;'       g    . V n        R# )netplan_pathnetplan_headerNpostcmdsFclean_defaultTr   )rI   r   r   r   	_postcmdsr   	_features)selfr    s   &&r   __init__Renderer.__init__a  so    F"JJ~7MN$jj)94@J6#ZZ>J/552r"   c                0   < V ^8  d   QhRS[ S[,          /# )r$   rz   )r   r+   )r&   __classdict__s   "r   r'   Renderer.__annotate__j  s      $s) r"   c                   V P                   '       g]    \        P                  ! V P                  R R7      w  r\        P                  ! V4      pVR,          R,          V n         V P                   # V P                   #   \        P
                   d     T P                   # \        \        3 d,   p\        P                  RT4        Rp?T P                   # Rp?ii ; i)Tcapturez
netplan.ior   z-Failed to list features from netplan info: %sN)
r   r	   NETPLAN_INFOr
   r   ProcessExecutionError	TypeErrorKeyErrorrK   rL   )r   	info_blob_errr   r   s   &    r   r   Renderer.featuresj  s    ~~~	N"&))D,=,=t"L	~~i0!%l!3J!? ~~t~~ --   ~~ x( N		I1MM~~Ns$   AA; ;CC,C-CCc                :   < V ^8  d   QhRS[ RS[S[,          RR/# )r$   network_state	templatesrz   N)r   r   r   )r&   r   s   "r   r'   r   x  s-     %1 %1#%1 D>%1
 
%1r"   c                   \         P                  P                  \        P                  ! V4      V P
                  4      p\        P                  ! \         P                  P                  V4      4       V P                  V4      pV P                  '       d   V P                  MR pVP                  R4      '       g
   VR,          pWe,           pVf   TpMTp\        WE4      p\        WWR7      '       g   \        WE4       V P                  '       d   \!        VR7       V P#                  V P$                  VR7       V P'                  V P$                  R7       R# )r   
N)rq   )runconfig_changed)r   )rk   rl   joinr	   rj   r   r
   
ensure_dirdirname_render_contentr   endswithr   r   r   r   rw   _netplan_generater   _net_setup_link)	r   r   r   rq   fpnplanrs   headertarget_netplan_config_changeds	   &&&&     r   render_network_stateRenderer.render_network_statex  s    '',,t//79J9JK01 &&}5 )-(;(;(;$$t$$dNF" >GG!;G!M*7CC'9&)/E 	 	
 	0r"   c                &   < V ^8  d   QhRS[ RS[ /# )r$   r   r   )r{   )r&   r   s   "r   r'   r     s     
7 
7T 
74 
7r"   c                    V'       g   \         P                  R 4       R# V'       g   \         P                  R4       R# \        P                  ! V P                  RR7       R# )z"netplan generate postcmds disabledNzEskipping call to `netplan generate`. reason: identical netplan configTr   )rK   rL   r	   NETPLAN_GENERATE)r   r   r   s   &&&r   r   Renderer._netplan_generate  sB    II:;II4 		$''6r"   c                &   V'       g   \         P                  R4       R# R\        P                  ! 4       9   d   \         P                  R4       R# . ROp\	        ^4       Ft  p \        4        F`  p\        P                  P                  \        V,           4      '       g   K5  \        P                  ! V\        V,           .,           RR7       Kb  	  Rp M	  X'       d   \        R4      VhR#   \        P                   d   pTp Rp?K  Rp?ii ; i)	zTo ensure device link properties are applied, we poke
udev to re-evaluate networkd .link files and call
the setup_link udev builtin command
z'netplan net_setup_link postcmd disabledNznet.ifnames=0z%Predictable interface names disabled.Tr   zQ'udevadm test-builtin net_setup_link' unable to run successfully for all devices.)udevadmztest-builtinnet_setup_link)rK   rL   r
   get_cmdlineranger   rk   rl   islinkr   r	   r   RuntimeError)r   r   	setup_lnk_ifacelast_exceptionr   s   &&     r   r   Renderer._net_setup_link  s    
 II?@ 0 0 22II=>A	 qA	#+-Eww~~me&;<<		%)>(?? .
 "&  0 ""  -- #!"#s   $:C.#1C..DDDc                &   < V ^8  d   QhRS[ RS[/# )r$   r   rz   )r   r+   )r&   r   s   "r   r'   r     s     `  ` \ ` c ` r"   c           	        VP                   ^8X  d;   \        P                  R4       \        P                  ! RVP
                  /RRR7      # / p/ p/ p/ p/ p. pVP                  P                  R. 4      pVP                  p	VP                  p
VP                  4        EF  pVP                  R4      p\        \        R VP                  4       4      4      pVP                  R4      pVR	8X  d   R
VRVP                  RR4      /pRV9   d   VR,          VR&   VR,          f3   VP                  RR4      pVe   RVP                  4       /VR&   MVR VR
 \        WWP                   4       VP#                  W/4       K  VR8X  Ed!   / p/ p\$        R,          pR+ Fc  p\'        VV4      pVP                  4        F@  w  ppVP                  VP)                  RR4      4      pVf   K-  VP#                  VV/4       KB  	  Ke  	  \+        V4      ^ 8  d   VP#                  RV/4       VP                  R4      '       d   VR,          P                  4       VR&   VP                  R4      pVR8X  d   \-        VVV4       \        VVWP                   4       VP#                  VV/4       EK	  VR8X  Ed|   VP                  R4      pVf   \        P/                  RV4       EK?  \1        \2        P2                  ! V4      4      pRV/pRp\'        VV4      p/ p\$        R,          p VP                  4        Fx  w  ppV P                  V4      pVf   K  VP#                  VV/4       VR,9   g   K9  / p!V F$  p"V"P5                  4       w  p#p$\7        V$4      V!V#&   K&  	  VP#                  VV!/4       Kz  	  \+        V4      ^ 8  d   VP#                  RV/4       VP                  R4      '       d   VR,          P                  4       VR&   \        VVWP                   4       VP#                  VV/4       EK  VR8X  g   EK  RVP                  R4      RVP                  R4      /p%VP                  RR4      pVe   VP                  4       V%R&   \        VV%WP                   4       VP#                  VV%/4       EK  	  V	'       g	   V
'       dP   R V	R!V
/p&W#WTV3 F@  p'V'P                  4        F)  w  p(p)R"V)9   g   R V)9  d   K  V)P#                  R"V&/4       K+  	  KB  	  R# p*VP9                  R$4       VV*! R%V4      ,          pVV*! R&V4      ,          pVV*! R'V4      ,          pVV*! R(V4      ,          pVV*! R)V4      ,          pR*P;                  V4      # )-r$   zV2 to V2 passthroughrA   F)explicit_startexplicit_endrb   re   c                     V ^,          RJ# )   N )its   &r   <lambda>*Renderer._render_content.<locals>.<lambda>  s    2a5+<r"   r2   physicalzset-namer   Nkeep_configurationcriticalmac_address
macaddressbondr   -
parameterszbond-slavesnonebridgebridge_portsz8Invalid config. The key'bridge_ports' is required in %s.bridge_vlanidvlan_idlinkzvlan-raw-devicerC   rE   rD   c                     V'       d6   \         P                  ! W/R R RR7      p\        P                  ! VR4      pV.# . # )FT)r   r   noaliasz    )r   dumpstextwrapindent)re   sectiondumptxts   &&  r   _render_section1Renderer._render_content.<locals>._render_section[  s?    ~~O#(!& 	 oodG4uIr"   znetwork:
    version: 2
	ethernetswifisbondsbridgesvlansr   )bond_zbond-)z	path-costzport-priority)versionrK   rL   r   r	  r    _network_staterI   r=   dns_searchdomainsiter_interfacesr   filterr   lowerr`   r   rJ   r   r!   replacerO   rh   rN   rc   copyr,   intrM   r   )+r   r   r  r  r  r  r  rs   rb   rD   rR   r    rQ   ifcfgif_typeethmacaddrr   bond_configv2_bond_mapr   bond_paramsparamr   newnameslave_interfacesr  portsr   match_prefixparams	br_configv2_bridge_mapnewvaluevalportportvalr  nscfgr  _namerf   r  s+   &&                                         r   r   Renderer._render_content  s6     A%II,->>M001$"  	"1155lBG
#33%77#335FZZ'F <fllnMNEii'G*$ UYYw5 (50&+,@&AC
Ow<'#iit<G*(4gmmo'FGL
O"5v}}E  &/F"  /v6/E";E5"IK(3(9(9(;u"-//%--S2I"J"?$#**GU+;<	 )< 0 {#a'KK{ ;<99]++).})=)C)C)ED&#(99]#; #v-0T6J"5$Ffd^,H$  %yy8'KK<
 tyy67 % 
  )25,G	 !1 :$*LLNLE5+//6G $$gu%56"@@#%#(C.1iikOT7-0\HTN $) "(('8)<= %3 y>A%MM<";<99]+++0+?+E+E+GF<("5&&--H/0F" %))I.EII&78  ))M48&)0D&"5$Ffd^,U 6\ - +xGE%eeD")--/JE3$+{#/E JJu56 #2 E
	 	34?;	::?7E22?7E22?9g66?7E22wwwr"   )r   r   r   r   r   r   )NN)F)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   __static_attributes____classdictcell__)r   s   @r   r   r   [  sS     L!:.v&L6 %1 %1N
7 
7!"F`  ` r"   r   c                  f    R .p RR.pV  F#  p\         P                  ! W!R7      '       d   K"   R# 	  R# )r   z	/usr/sbinz/sbin)rE   FT)r	   which)expectedrE   ps      r   	availablerA  q  s4    {H7#Fzz!++  r"   r   )*r  r   loggingrk   r   r
  tempfiler   typingr   r   r   	cloudinitr   r   r	   r
   cloudinit.netr   r   r   r   r   r   cloudinit.net.network_stater   r   r   ro   	getLoggerr6  rK   r!   r`   rh   rw   r   r   r   r   r   rA  r   r"   r   <module>rI     s     	  	 	  ) + + 4 4  G:    !VKr	7:6r.02S x   S lr"   