o íî~föOã@s”dZddlZddlZddlZddlZddlmZzddlZdZWn ey+dZYnwddl m Z ddl Z ddl m mZGdd„dejƒZdS) znetplan migrate command lineéN)ÚglobFT)Ú OrderedDictcsVeZdZ‡fdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z ddd„Z ‡Z S)ÚNetplanMigratecstƒjddddddS)NÚmigratez/Migration of /etc/network/interfaces to netplanT)Ú command_idÚ descriptionÚleafÚtesting)ÚsuperÚ__init__©Úself©Ú __class__©ú2/usr/share/netplan/netplan/cli/commands/migrate.pyr %s  ýzNetplanMigrate.__init__cCsÔd|vr3d|vr i|d<d|dvrg|dd<|d d¡D]}|s$q|dd|g7<q|d=d|vrhd|vr?i|d<d|dvrKg|dd<|d d¡D]}|sWqR|dd|g7<qR|d=dSdS)zÂParse 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. zdns-nameserversÚ nameserversÚ addressesú z dns-searchÚsearchN)Úsplit)r Ú if_optionsÚ if_configÚnsÚdomainrrrÚparse_dns_options+s*     öz NetplanMigrate.parse_dns_optionsc Csd|vrFzt|dƒ}Wnty#t d||d¡t d¡Ynwd|vr=|d|ks=t d|||d¡t d¡||d<|d=dSdS)z”Parse 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 MTUéz0%s: tried to set MTU=%d, but already have MTU=%dN)ÚintÚ ValueErrorÚloggingÚerrorÚsysÚexit)r ÚifacerrrrrrÚ parse_mtuHs þ  ôzNetplanMigrate.parse_mtucCs\d|vr,d|vr!|d|dks!t d||d|d¡t d¡|d|d<|d=dSdS)z°Parse 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 %srN)r r!r"r#)r r$rrrrrÚparse_hwaddress[s ÿ   ùzNetplanMigrate.parse_hwaddresscCsV|jjddd|jjdddd|j|_| ¡tr%t d¡t  d ¡|  ¡dS) 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)Úactionr)z¥The `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_commandr rrrÚrunjsÿ ÿ   zNetplanMigrate.runc Cs i}z | |jp d¡\}}Wnty,}zt t|ƒ¡t d¡WYd}~nd}~ww| ¡D]9\}}| ¡D]/\}}t  d|||¡||vrVt d|¡t d¡|ddkrct  d|¡q:|ddkr³|  d i¡  d i¡  |i¡} |  |d | ¡|  ||d | ¡|d rŸt d |d   |d  ¡¡¡t d¡|dkr¨d| d<q:|dks®J‚d| d<q:|ddkr\|  d i¡  d i¡  |i¡} d| vrÐg| d<|  |d | ¡| ||d | ¡|  ||d | ¡|dkrþtgd¢ƒ} tgd¢ƒ} t|d  ¡ƒ} | | } | r5|  | ¡D]}t d|||¡t d¡q| | D]}t d|||¡t d¡q$d|d vrGt d|¡t d¡d|d dvrb|d d d¡d}|d d}n&d|d vrtt d|¡t d¡|d d}|d dd|d d}zt |¡}Wn!tjy°}zt d|||¡t d¡WYd}~nd}~wwz tj|d d!}Wn!tjyÛ}zt d"|||¡t d¡WYd}~nd}~ww| dt|ƒdt|jƒg7<d#|d vrý|d d#| d$<q:|dksJ‚tgd%¢ƒ} tgd&¢ƒ} t|d  ¡ƒ} | | } | rK|  | ¡D]}t d|||¡t d¡q%| | D]}t d|||¡t d¡q:d|d vr]t d|¡t d¡d|d dvrx|d d d¡d}|d d}n&d|d vrŠt d|¡t d¡|d d}|d dd|d d}zt |¡}Wn!tjyÆ}zt d'|||¡t d¡WYd}~nd}~wwz tj|d d!}Wn!tjyñ}zt d(|||¡t d¡WYd}~nd}~ww| dt|ƒdt|jƒg7<d#|d vr|d d#| d)<d*|d vr[|d d*d+kr(d | d*<q:|d d*d,kr6d| d*<q:|d d*d-krKt d.|¡t d¡q:t d/||d d*¡t d¡q:t d0||d¡t d¡q:q1tj  |jptdd1¡}|réd|d d2<t |¡}|jrŽt |ƒn`tj  |jp–dd3¡}z t !tj "|¡¡Wn t#y®Ynwzt$|d4ƒ }| %|¡Wdƒn 1sÆwYWnt#yát d5|¡t d6¡Ynwt &d7|¡nt 'd8¡|jst &d9||¡t (||d:¡dSdS);NÚrzConverting %s family %s %sz.%s: non-automatic interfaces are not supportedÚmethodÚloopbackzIgnoring loopback interface %sÚdhcpÚnetworkÚ ethernetsÚoptionsz2%s: option(s) %s are not supported for dhcp methodz, ÚinetTÚdhcp4Úinet6Údhcp6Ústaticr)ÚaddressÚnetmaskÚgateway)Ú broadcastÚmetricÚ pointopointÚscopez%s: unsupported %s option "%s"z%s: unknown %s option "%s"rAz(%s: no address supplied in static methodú/rrBzE%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: %srCÚgateway4)rArBrCÚ accept_ra)rEÚmediaÚautoconfÚprivextrGzpreferred-lifetimez dad-attemptsz dad-intervalz-%s: error parsing "%s" as an IPv6 address: %sz-%s: error parsing "%s" as an IPv6 network: %sÚgateway6rKÚ0Ú1Ú2z(%s: netplan does not support accept_ra=2z#%s: unexpected accept_ra value "%s"z%s: method %s is not supportedzetc/network/interfacesÚversionzetc/netplan/10-ifupdown.yamlÚxzC%s already exists; remove it if you want to run the migration againézmigration complete, wrote %sz>ifupdown does not configure any interfaces, nothing to migratez#renaming %s to %s.netplan-convertedz.netplan-converted))Úparse_ifupdownÚroot_dirrr r!Ústrr"r#ÚitemsÚdebugÚ setdefaultrr(ÚjoinÚkeysr%ÚsetÚ intersectionrÚ ipaddressÚ IPv4AddressÚAddressValueErrorÚ IPv4NetworkÚNetmaskValueErrorÚ prefixlenÚ IPv6AddressÚ IPv6NetworkÚosÚpathÚyamlÚdumpÚdry_runÚprintÚmakedirsÚdirnameÚFileExistsErrorÚopenÚwriteÚinfoÚwarningÚ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Úfrrrr/xsL€þ    ÿ          ÿ  €þ€þ$€     ÿ  €þ€þ$     ÿ € Ú(   ÿ  ÿ€ þ þzNetplanMigrate.command_migratec Cs\dd„}g}t|ƒd}zt|d|ƒ}}t d|j¡|D]i}| ¡}|r,| d¡r-q| d¡rat d¡}||t j   |j¡|ƒ} t   | ¡D]}|  |¡r_|| |t j  | |d …|¡¡7}qGq| d ¡rƒt||t j   |j¡|ƒƒD]}|| |||d …¡7}qsq| |¡qWd ƒW|S1s•wYW|Sty­t d ||¡Y|Sw) zvReturn normalized lines from ifupdown config This resolves "source" and "source-directory" includes. cSs*| ¡d}| d¡r||S|d|S)Nr,rH)rÚ startswith)ÚrootdirÚcurdirÚlineÚargrrrÚexpand_source_argGs   zCNetplanMigrate._ifupdown_lines_from_file..expand_source_argr,rHz reading %sú#zsource-directory z^[a-zA-Z0-9_-]+$Nzsource z%s/%s does not exist, ignoring)Úlenrqr rZÚnameÚstriprŒÚreÚcompilerhriroÚlistdirÚmatchÚ_ifupdown_lines_from_filer\rÚappendÚFileNotFoundError) r rrir‘ÚlinesÚ rootdir_lenr‹rÚvalid_reÚdrrrršBs@    "€þ ÿ ð þë þþz(NetplanMigrate._ifupdown_lines_from_filerHc CsÊdddddddœ}| |d¡}tƒ}tƒ}d}d}|D]¾}| ¡} z|| d} t d| | ¡d}d}Wn,ty`|rXt d||¡|jdd d|||d | d<Yqtd | dƒ‚wt| ƒ| dkrxtd | | dt| ƒdfƒ‚| dd vr†|  | d¡q| ddkrtdƒ‚| ddkr—q| ddkrÒ| ddvr«td| dƒ‚| ddvr¹td| dƒ‚| d}| d}| didœ|  | dtƒ¡|<qt d| dƒ‚t d||¡||fS)uWParse 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. r,rU)Úautoú allow-autoú allow-hotplugÚmappingú no-scriptsr$z/etc/network/interfacesNrz$line fields %s (expected length: %i)z$in_options %s, parsing as option: %s)Úmaxsplitr;zUnknown stanza type %sz0Expected %i fields for stanza type %s but got %i)r¡r¢r£r¤zmapping stanza is not supportedr¥r$r)r<r>zUnknown address family %s)r7r@r8zUnsupported method %s)r6r;z!stanza type %s is not implementedz,final parsed interfaces: %s; auto ifaces: %s) ršrr^rr rZÚKeyErrorrr“Úaddr[ÚNotImplementedError) r rÚfieldlenrrwr¡Ú in_optionsÚ in_familyrÚfieldsÚexp_lenrrrrVhsR   $ù ÿ      "zNetplanMigrate.parse_ifupdown)rH) Ú__name__Ú __module__Ú __qualname__r rr%r(r4r/ršrVÚ __classcell__rrrrr$s K&r)Ú__doc__r rhr"r–rrjr2Ú ImportErrorÚ collectionsrr`Únetplan.cli.utilsÚcliÚutilsÚNetplanCommandrrrrrÚs   ÿ