HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/share/netplan/netplan_cli/cli/commands/__pycache__/migrate.cpython-310.pyc
o

t��f�O�@s�dZddlZddlZddlZddlZddlmZzddlZdZWney+dZYnwddl	m
Z
ddlZddlm
Z
Gd	d
�d
e
j�ZdS)znetplan migrate command line�N)�globFT)�OrderedDict�)�utilscsVeZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	ddd�Z
�ZS)�NetplanMigratecst�jddddd�dS)N�migratez/Migration of /etc/network/interfaces to netplanT)�
command_id�description�leaf�testing)�super�__init__��self��	__class__��6/usr/share/netplan/netplan_cli/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	Cs�d|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 MTUrz0%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$)rr%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_commandrrrr�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|	�|dr�t�d|d
�
|d����t�d�|dkr�d|	d<q:|dks�J�d|	d<q:|ddk�r\|�
d	i��
d
i��
|i�}	d|	vr�g|	d<|�|d|	�|�||d|	�|�||d|	�|dk�r�tgd��}
tgd��}t|d���}||
}
|
�r5|
�|�D]}t�d|||�t�d��q|
|D]}t�d|||�t�d��q$d|dv�rGt�d|�t�d�d|ddv�rb|dd�d�d}|dd}n&d|dv�rtt�d|�t�d�|dd}|ddd|dd}zt�|�}Wn!tj�y�}zt�d|||�t�d�WYd}~nd}~wwz	tj|d d!�}Wn!tj�y�}zt�d"|||�t�d�WYd}~nd}~ww|	dt|�dt|j�g7<d#|dv�r�|dd#|	d$<q:|dk�sJ�tgd%��}
tgd&��}t|d���}||
}
|
�rK|
�|�D]}t�d|||�t�d��q%|
|D]}t�d|||�t�d��q:d|dv�r]t�d|�t�d�d|ddv�rx|dd�d�d}|dd}n&d|dv�r�t�d|�t�d�|dd}|ddd|dd}zt�|�}Wn!tj�y�}zt�d'|||�t�d�WYd}~nd}~wwz	tj|d d!�}Wn!tj�y�}zt�d(|||�t�d�WYd}~nd}~ww|	dt|�dt|j�g7<d#|dv�r|dd#|	d)<d*|dv�r[|dd*d+k�r(d |	d*<q:|dd*d,k�r6d|	d*<q:|dd*d-k�rKt�d.|�t�d�q:t�d/||dd*�t�d�q:t�d0||d�t�d�q:q1tj�
|j�ptdd1�}|�r�d|d	d2<t�|�}|j�r�t |�n`tj�
|j�p�dd3�}zt�!tj�"|��Wn
t#�y�Ynwzt$|d4��
}|�%|�Wd�n	1�s�wYWnt#�y�t�d5|�t�d6�Ynwt�&d7|�nt�'d8�|j�st�&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"rBz(%s: no address supplied in static method�/rrCzE%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: %srD�gateway4)rBrCrD�	accept_ra)rF�media�autoconf�privextrHzpreferred-lifetimezdad-attemptszdad-intervalz-%s: error parsing "%s" as an IPv6 address: %sz-%s: error parsing "%s" as an IPv6 network: %s�gateway6rL�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_dirr r!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�frrrr0xsL��
�




�
����$�
�
����$


�
���(

����
�zNetplanMigrate.command_migratec
Cs\dd�}g}t|�d}z�t|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-rI)r�
startswith)�rootdir�curdir�line�argrrr�expand_source_argGs
zCNetplanMigrate._ifupdown_lines_from_file.<locals>.expand_source_argr-rIz
reading %s�#zsource-directory z^[a-zA-Z0-9_-]+$Nzsource z%s/%s does not exist, ignoring)�lenrrr!r[�name�stripr��re�compilerirjrp�listdir�match�_ifupdown_lines_from_filer]r�append�FileNotFoundError)
rr�rjr��lines�rootdir_lenr�r��valid_re�drrrr�Bs@


"��
������z(NetplanMigrate._ifupdown_lines_from_filerIc	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|	ddkr�td��|	ddkr�q|	ddkr�|	ddvr�td|	d��|	ddvr�td|	d��|	d}|	d}|	did�|�
|	dt��|<qtd|	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-rV)�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)r8rAr9zUnsupported method %s)r7r<z!stanza type %s is not implementedz,final parsed interfaces: %s; auto ifaces: %s)r�rr_rr!r[�KeyErrorr r��addr\�NotImplementedError)rr��fieldlenr�rxr��
in_options�	in_familyr��fields�exp_lenrrrrWhsR
$�
�"zNetplanMigrate.parse_ifupdown)rI)�__name__�
__module__�__qualname__r
rr&r)r5r0r�rW�
__classcell__rrrrr$sK&r)�__doc__r!rir#r�rrkr3�ImportError�collectionsrrar6r�NetplanCommandrrrrr�<module>s �