
    gqP                         S r SSKrSSKrSSKrSSKrSSKJr   SSKrSrSSK	J
r
  SSKrSSKJr   " S	 S
\R                  5      rg! \ a    Sr N0f = f)znetplan migrate command line    N)globFT)OrderedDict   )utilsc                   V   ^  \ rS rSrU 4S jrS rS rS rS rS r	S r
SS	 jrS
rU =r$ )NetplanMigrate$   c                 &   > [         TU ]  SSSSS9  g )Nmigratez/Migration of /etc/network/interfaces to netplanT)
command_iddescriptionleaftesting)super__init__)self	__class__s    6/usr/share/netplan/netplan_cli/cli/commands/migrate.pyr   NetplanMigrate.__init__%   s     I%V"!% 	 	'    c                 n   SU;   aT  SU;  a  0 US'   SUS   ;  a  / US   S'   US   R                  S5       H  nU(       d  M  US   S==   U/-  ss'   M     US	 SU;   aU  SU;  a  0 US'   SUS   ;  a  / US   S'   US   R                  S5       H  nU(       d  M  US   S==   U/-  ss'   M     US	 gg)zParse dns options (dns-nameservers and dns-search) from if_options
(an interface options dict) into the interface configuration if_config
Mutates the arguments in place.
zdns-nameserversnameservers	addresses z
dns-searchsearchN)split)r   
if_options	if_confignsdomains        r   parse_dns_options NetplanMigrate.parse_dns_options+   s    
 
*I-+-	-()M"::8:	-(5 !2399#>-(5"=5 ? ,-:%I-+-	-(y7757	-(2$\288=-(2vh>2	 >
 <( &r   c                 @   SU;   aX   [        US   5      nSU;   a:  US   W:X  d1  [        R                  " SXUS   5        [        R
                  " S5        WUS'   US	 gg! [         a3    [        R                  " SXS   5        [        R
                  " S5         Nf = f)zParse out the MTU. Operates the same way as parse_dns_options
iface is the name of the interface, used only to print error messages
mtuz%s: cannot parse "%s" as an MTUr   z0%s: tried to set MTU=%d, but already have MTU=%dN)int
ValueErrorloggingerrorsysexit)r   ifacer   r   r$   s        r   	parse_mtuNetplanMigrate.parse_mtuH   s    
 J*U+,
 	!)E*:c*APRW^ghm^no"Ie5!   ?SXHYZs   A   :BBc                     SU;   aS  SU;   aA  US   US   :X  d5  [         R                  " SUUS   US   5        [        R                  " S5        US   US'   US	 gg)zParse out the manually configured MAC.
Operates the same way as parse_dns_options
iface is the name of the interface, used only to print error messages
	hwaddress
macaddressz0%s: tried to set MAC %s, but already have MAC %sr   N)r'   r(   r)   r*   )r   r+   r   r   s       r   parse_hwaddressNetplanMigrate.parse_hwaddress[   so     *$y(<1HJWbLc1cPRW(5y7NP&0&=Il#;' %r   c                 >   U R                   R                  SSS9  U R                   R                  SSSS9  U R                  U l        U R	                  5         [
        (       a,  [        R                  " S5        [        R                  " S	5        U R                  5         g )
Nz
--root-dirzOSearch for and generate configuration files in this root directory instead of /)helpz	--dry-run
store_truezQPrint converted netplan configuration to stdout instead of writing/changing files)actionr4   zThe `yaml` Python package couldn't be imported, and is needed for the migrate command.
To install it on Debian or Ubuntu-based system, run `apt install python3-yaml`   )parseradd_argumentcommand_migratefunc
parse_argsNO_YAMLr'   r(   r)   r*   run_command)r   s    r   runNetplanMigrate.runj   s      &w 	! 	y  \&y 	! 	{((	7MM R SHHQKr   c           
         0 n U R                  U R                  =(       d    S5      u  p#WR                  5        GH  u  pVUR                  5        GH  u  px[        R                  " SXWU5        UW;  a-  [        R                  " SU5        [        R                  " S5        US   S:X  a  [        R                  " SU5        Ms  US   S:X  a  UR                  S	0 5      R                  S
0 5      R                  U0 5      n	U R                  US   U	5        U R                  XXS   U	5        US   (       aN  [        R                  " SUSR                  US   R                  5       5      5        [        R                  " S5        US:X  a  SU	S'   GM>  US:X  d   eSU	S'   GMN  US   S:X  Ga"  SU;   a  UR                  S5      S   nUR                  S	0 5      R                  S
0 5      R                  U0 5      n	SU	;  a  / U	S'   U R                  US   U	5        U R!                  XXS   U	5        U R                  XXS   U	5        US:X  Ga  [#        / SQ5      n
[#        / SQ5      n[#        US   R                  5       5      nX-
  nU(       a  UR%                  U5       H1  n[        R                  " SXWU5        [        R                  " S5        M3     X-
   H1  n[        R                  " SXWU5        [        R                  " S5        M3     SUS   ;  a-  [        R                  " SU5        [        R                  " S5        SUS   S   ;   a#  US   S   R                  S5      S   nUS   S   nORSUS   ;  a-  [        R                  " SU5        [        R                  " S5        US   S   nUS   S   S-   US   S   -   n [&        R(                  " U5      n [&        R,                  " US!S"9nU	S==   [        W5      S-   [        WR0                  5      -   /-  ss'   S$US   ;   a  US   S$   U	S%'   GM  GM  US:X  d   e[#        / S&Q5      n
[#        / S'Q5      n[#        US   R                  5       5      nX-
  nU(       a  UR%                  U5       H1  n[        R                  " SXWU5        [        R                  " S5        M3     X-
   H1  n[        R                  " SXWU5        [        R                  " S5        M3     SUS   ;  a-  [        R                  " SU5        [        R                  " S5        SUS   S   ;   a#  US   S   R                  S5      S   nUS   S   nORSUS   ;  a-  [        R                  " SU5        [        R                  " S5        US   S   nUS   S   S-   US   S   -   n [&        R2                  " U5      n [&        R4                  " US!S"9nU	S==   [        W5      S-   [        WR0                  5      -   /-  ss'   S$US   ;   a  US   S$   U	S*'   S+US   ;   a  US   S+   S,:X  a  S!U	S+'   GM  US   S+   S-:X  a  SU	S+'   GM  US   S+   S.:X  a0  [        R                  " S/U5        [        R                  " S5        GM@  [        R                  " S0UUS   S+   5        [        R                  " S5        GMw  GMz  [        R                  " S1XXS   5        [        R                  " S5        GM     GM     [6        R8                  R                  U R                  =(       d    SS25      nU(       a  SUS	   S3'   [:        R<                  " U5      nU R>                  (       a  [A        U5        O[6        R8                  R                  U R                  =(       d    SS45      n [6        RB                  " [6        R8                  RE                  U5      5         [I        US55       nURK                  U5        S S S 5        [        RL                  " S8U5        O[        RL                  " S95        U R>                  (       d3  [        RL                  " S:UU5        [6        RN                  " UUS;-   5        g g ! [         a@  n[        R                  " [        U5      5        [        R                  " S5         S nAGNS nAff = f! [&        R*                   a:  n[        R                  " S UUU5        [        R                  " S5         S nAGN=S nAff = f! [&        R.                   a:  n[        R                  " S#UUU5        [        R                  " S5         S nAGNxS nAff = f! [&        R*                   a:  n[        R                  " S(UUU5        [        R                  " S5         S nAGNS nAff = f! [&        R.                   a:  n[        R                  " S)UUU5        [        R                  " S5         S nAGN$S nAff = f! [F         a     GN8f = f! , (       d  f       GN$= f! [F         a1    [        R                  " S6U5        [        R                  " S75         GNaf = f)<N r   zConverting %s family %s %sz.%s: non-automatic interfaces are not supportedmethodloopbackzIgnoring loopback interface %sdhcpnetwork	ethernetsoptionsz2%s: option(s) %s are not supported for dhcp methodz, inetTdhcp4inet6dhcp6static:r   r   )addressnetmaskgateway)	broadcastmetricpointopointscopez%s: unsupported %s option "%s"z%s: unknown %s option "%s"rO   z(%s: no address supplied in static method/rP   zE%s: address does not specify prefix length, and netmask not specifiedz-%s: error parsing "%s" as an IPv4 address: %sF)strictz-%s: error parsing "%s" as an IPv4 network: %srQ   gateway4)rO   rP   rQ   	accept_ra)rS   mediaautoconfprivextrU   zpreferred-lifetimezdad-attemptszdad-intervalz-%s: error parsing "%s" as an IPv6 address: %sz-%s: error parsing "%s" as an IPv6 network: %sgateway6rY   012z(%s: netplan does not support accept_ra=2z#%s: unexpected accept_ra value "%s"z%s: method %s is not supportedzetc/network/interfacesversionzetc/netplan/10-ifupdown.yamlxzC%s already exists; remove it if you want to run the migration again   zmigration complete, wrote %sz>ifupdown does not configure any interfaces, nothing to migratez#renaming %s to %s.netplan-convertedz.netplan-converted)(parse_ifupdownroot_dirr&   r'   r(   strr)   r*   itemsdebug
setdefaultr!   r1   joinkeysr   r,   setintersection	ipaddressIPv4AddressAddressValueErrorIPv4NetworkNetmaskValueError	prefixlenIPv6AddressIPv6Networkospathyamldumpdry_runprintmakedirsdirnameFileExistsErroropenwriteinforename)r   netplan_configifacesauto_ifaceser+   family_configfamilyconfigcsupported_optsunsupported_optsoptsbad_optsunsupportedunknown	addr_specnet_specipaddraipnetr   netplan_yamldestfs                            r   r:   NetplanMigrate.command_migratex   s	   	"&"5"5dmm6Ir"JF %+LLN E"/"5"5"7:E6R+MM"RTYZHHQK(#z1MM"BEJH%/&11)R@KKKY[\gghmoqrA**6)+<a@((y0A1Ei(&Z&+TYYvi7H7M7M7O-PR'%)'
%000%)'
H%1u %C 0 3&11)R@KKKY[\gghmoqrA"!+)++**6)+<a@NN5*;Q?((y0A1E '
 *--N)O+./^+_("6)#4#9#9#;<#'#8#/7/D/DEU/V '.NPU_j k # 0W ,4+F '.JE[b c # ,G %F9,==#MM*TV[\HHQK&"3I">>(.y(9)(D(J(J3(OPQ(RI'-i'8'CH(y0AA '.u.3!5 #(.y(9)(DI'-i'8'Cc'IFS\L]^gLh'hH(%.%:%:9%EF
($-$9$9(5$QE
 +3v;+<s5???S+S*TT$y(99,29,=i,HAjM :  &000 *--[)\+. 0o ,p(  #6)#4#9#9#;<#'#8#/7/D/DEU/V '.NPU_j k # 0W ,4+F '.JE[b c # ,G %F9,==#MM*TV[\HHQK&"3I">>(.y(9)(D(J(J3(OPQ(RI'-i'8'CH(y0AA '.u.3!5 #(.y(9)(DI'-i'8'Cc'IFS\L]^gLh'hH(%.%:%:9%EF
($-$9$9(5$QE
 +3v;+<s5???S+S*TT$y(99,29,=i,HAjM&&*;;%i0=D16+!'	!2;!?3!F15+!'	!2;!?3!F '.XZ_ ` # '.SUZ.4Y.?.L!N # < MM"BERZK[\HHQKS #8 %3X GGLL!5#7OP	34N9%i099^4L||l#ww||DMM$8S:XYKK 56 dCA- )
 ;TBLLYZ||LL>	9UIIi-A!AB M  	MM#a&!HHQKK	b  ):: (#MM*Y[`bkmnoHHQKK(  ):: (#MM*Y[`bjlmnHHQKK(l  ):: (#MM*Y[`bkmnoHHQKK(  ):: (#MM*Y[`bjlmnHHQKK(N '  )&  MM"gimnHHQK s   &a: =cd3e)
f:-3h !h. -h?h. :
c5b??cd/dde&,/e!!e&)f7=/f22f7:h/hh
hh
h+&h. +h. .7i)(i)c                    S n/ n[        U5      S-   n [        US-   U-   5       n[        R                  " SUR                  5        U GH|  nUR                  5       nU(       a  UR                  S5      (       a  M3  UR                  S5      (       a  [        R                  " S5      nU" U[        R                  R                  UR                  5      U5      n	[        R                  " U	5       HO  nUR                  U5      (       d  M  X@R                  U[        R                  R                  XS U5      5      -  nMQ     M  UR                  S	5      (       aZ  [!        U" U[        R                  R                  UR                  5      U5      5       H  nX@R                  XUS 5      -  nM     GMk  UR#                  U5        GM     SSS5        U$ ! , (       d  f       U$ = f! [$         a    [        R                  " S
X5         U$ f = f)zfReturn normalized lines from ifupdown config

This resolves "source" and "source-directory" includes.
c                 l    UR                  5       S   nUR                  S5      (       a  X-   $ US-   U-   $ )Nr7   rV   )r   
startswith)rootdircurdirlineargs       r   expand_source_argCNetplanMigrate._ifupdown_lines_from_file.<locals>.expand_source_argK  s7    **,q/C~~c""}$|c))r   r7   rV   z
reading %s#zsource-directory z^[a-zA-Z0-9_-]+$Nzsource z%s/%s does not exist, ignoring)lenr   r'   rh   namestripr   recompilerv   rw   r}   listdirmatch_ifupdown_lines_from_filerj   r   appendFileNotFoundError)
r   r   rw   r   linesrootdir_lenr   r   valid_reds
             r   r   (NetplanMigrate._ifupdown_lines_from_fileF  s   
	* 'lQ&	Kgmd*+qlAFF3D::<D4??3#7#7 ':;;#%::.@#A-grwwqvv7NPTU!#AA'~~a00 %)G)GQSQXQXQ]Q]^_`l^mopQq)r r "/ 33!%&7QRQWQWAXZ^&_!`A!%C%CG{|_%]]E "a T*!  ,* + ,+*  ! 	KMM:GJ	Ks6   G' C!GC GG' 
G$G' $G' '!HHc           	         SSSSSSS.nU R                  US5      n[        5       n[        5       nSnSnU GH$  nUR                  5       n	 X)S      n
[        R
                  " SX5        SnSn[        U	5      U
S-   :w  a  [        SXS   [        U	5      S-
  4-  5      eU	S   S;   a  UR                  U	S   5        M  U	S   S:X  a  [        S5      eU	S   S:X  a  M  U	S   S:X  ag  U	S   S;  a  [        SU	S   -  5      eU	S   S;  a  [        SU	S   -  5      eU	S   nU	S   nU	S   0 S.UR                  U	S   [        5       5      U'   GM  [        SU	S   -  5      e   [        R
                  " SXE5        XE4$ ! [         aT    U(       a;  [        R
                  " SXh5        UR                  SS	9S   XF   U   S
   U	S   '    GM  [        SU	S   -  5      ef = f)u/  Parse ifupdown configuration.

Return (iface_name →  family → {method, options}, auto_ifaces: set) tuple
on successful parsing, or a ValueError when encountering an invalid file or
ifupdown features which are not supported (such as "mapping").

options is itself a dictionary option_name → value.
r7   rc   )auto
allow-autoallow-hotplugmapping
no-scriptsr+   z/etc/network/interfacesNr   z$line fields %s (expected length: %i)z$in_options %s, parsing as option: %s)maxsplitrH   zUnknown stanza type %sz0Expected %i fields for stanza type %s but got %i)r   r   r   r   zmapping stanza is not supportedr   r+   r   )rI   rK   zUnknown address family %s)rD   rM   rE   zUnsupported method %s)rC   rH   z!stanza type %s is not implementedz,final parsed interfaces: %s; auto ifaces: %s)r   r   rl   r   r'   rh   KeyErrorr&   r   addri   NotImplementedError)r   r   fieldlenr   r   r   
in_options	in_familyr   fieldsexp_lens              r   rd   NetplanMigrate.parse_ifupdownl  s4    Qq`almn ..w8QRu
	 DZZ\FK"!9-DfV!
 	 6{gk) !S")!9c&kAo!F"G H H ayCC#i' !BCCl*g%!9$55$%@6!9%LMM!9$BB$%<vay%HII#AY
"1I	TZ[\T]jlIm!!&)[];IF)*MPVWXPY*YZZO R 	DfS~C  KMM"H*[JN**^_*J`abJcF&y1)<VAYG$%=q	%IJJKs   "E99AGG)r;   )rV   )__name__
__module____qualname____firstlineno__r   r!   r,   r1   r?   r:   r   rd   __static_attributes____classcell__)r   s   @r   r   r   $   s4    '):"&(LC\$L? ?r   r   )__doc__r'   rv   r)   r   r   rx   r=   ImportErrorcollectionsr   rn   rB   r   NetplanCommandr    r   r   <module>r      s[   $ #  	 
 	 G $  GU)) G  Gs   A AA