HEX
Server: nginx/1.24.0
System: Linux localhost 5.15.0-46-generic #49-Ubuntu SMP Thu Aug 4 18:03:25 UTC 2022 x86_64
User: www (1000)
PHP: 8.3.27
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: //usr/share/netplan/netplan/cli/commands/__pycache__/migrate.cpython-310.pyc
o

�u�b�O�@s�dZddlZddlZddlZddlZddlmZzddlZdZWney+dZYnwddl	m
Z
ddlZddlm
mZGdd�dej�ZdS)	znetplan migrate command line�N)�globFT)�OrderedDictcsVeZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	ddd�Z
�ZS)�NetplanMigratecst�jddddd�dS)NZmigratez/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-nameserversZnameservers�	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 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
        Z	hwaddress�
macaddressz0%s: tried to set MAC %s, but already have MAC %srN)rrr 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_YAMLrrr 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, �inetTZdhcp4�inet6Zdhcp6�staticr)�address�netmask�gateway)Z	broadcast�metricZpointopoint�scopez%s: unsupported %s option "%s"z%s: unknown %s option "%s"r<z(%s: no address supplied in static method�/rr=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: %sr>Zgateway4)r<r=r>�	accept_ra)r?ZmediaZautoconfZprivextr@zpreferred-lifetimezdad-attemptszdad-intervalz-%s: error parsing "%s" as an IPv6 address: %sz-%s: error parsing "%s" as an IPv6 network: %sZgateway6rC�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_dirrrr�strr r!�items�debug�
setdefaultrr%�join�keysr#�set�intersectionr�	ipaddressZIPv4AddressZAddressValueErrorZIPv4NetworkZNetmaskValueErrorZ	prefixlenZIPv6AddressZIPv6Network�os�path�yaml�dump�dry_run�print�makedirs�dirname�FileExistsError�open�write�info�rename)rZnetplan_config�ifacesZauto_ifaces�er"Z
family_configZfamily�config�cZsupported_optsZunsupported_opts�optsZbad_optsZunsupported�unknownZ	addr_specZnet_specZipaddr�aZipnetrZnetplan_yaml�dest�frrrr,xsL��
�



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


�
���(

����
�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)rA)r�
startswith)�rootdir�curdir�line�argrrr�expand_source_argGs
zCNetplanMigrate._ifupdown_lines_from_file.<locals>.expand_source_argr)rAz
reading %s�#zsource-directory z^[a-zA-Z0-9_-]+$Nzsource z%s/%s does not exist, ignoring)�lenr^rrN�name�striprk�re�compilerUrVr\�listdir�match�_ifupdown_lines_from_filerPr�append�FileNotFoundError)
rrlrVrp�linesZrootdir_lenrjrnZvalid_re�drrrryBs@


"��
������z(NetplanMigrate._ifupdown_lines_from_filerAc	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)rI)�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)�maxsplitr8zUnknown stanza type %sz0Expected %i fields for stanza type %s but got %i)r~rr�r�zmapping stanza is not supportedr�r"r)r9r:zUnknown address family %s)r4r;r5zUnsupported method %s)r3r8z!stanza type %s is not implementedz,final parsed interfaces: %s; auto ifaces: %s)ryrrRrrrN�KeyErrorrrr�addrO�NotImplementedError)rrlZfieldlenr|rbr~Z
in_optionsZ	in_familyrn�fieldsZexp_lenrrrrJhsR
$�
�"zNetplanMigrate.parse_ifupdown)rA)�__name__�
__module__�__qualname__r
rr#r%r1r,ryrJ�
__classcell__rrr
rr$sK&r)�__doc__rrUr rurrWr/�ImportError�collectionsrrT�netplan.cli.utils�cli�utils�NetplanCommandrrrrr�<module>s �