HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/net/__pycache__/network_manager.cpython-310.pyc
o

�Ad=4�@s�ddlZddlZddlZddlZddlZddlmZddlmZ	ddlm
Z
mZddlm
Z
mZmZddlmZdZdZd	Ze	�e�ZGd
d�d�ZGdd
�d
ej�Zddd�Zddd�ZdS)�N)�Optional)�log)�subp�util)�is_ipv6_address�renderer�subnet_is_ipv6)�NetworkStatez/etc/NetworkManagerz/usr/lib/NetworkManagerz'/etc/NetworkManager/NetworkManager.confc@s|eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
edd��Zdd�Zdd�ZdS)�NMConnectionz/Represents a NetworkManager connection profile.cCsPt�d�}t��|_t|j_d|��tt�||��d�|jd<ddi|jd<dS)	z�
        Initializes the connection with some very basic properties,
        notably the UUID so that the connection can be referred to.
        z$a3924cb8-09e0-43e9-890b-77972a800108zcloud-init )�id�uuid�
connectionz%org.freedesktop.NetworkManager.originz
cloud-init�userN)r�UUID�configparser�ConfigParser�config�str�optionxform�uuid5)�self�con_id�
CI_NM_UUID�r�?/usr/lib/python3/dist-packages/cloudinit/net/network_manager.py�__init__s

��zNMConnection.__init__cCs:|j�|�si|j|<|j�||�s||j||<dSdS)z_
        Sets a property unless it's already set, ensuring the section
        exists.
        N)r�has_section�
has_option)r�section�option�valuerrr�_set_default5s

�zNMConnection._set_defaultcCs�dddddddd�}|�|dd�z||}Wnty)d}d|j|d<Ynw|j|ddkr5d	S|j|ddkrD|dkrDd	S||j|d<|�|dd
�d	S)z}
        Ensures there's appropriate [ipv4]/[ipv6] for given family
        appropriate for given configuration type
        �manual�auto)�static�dhcp6�
ipv6_slaaczipv6_dhcpv6-statelesszipv6_dhcpv6-stateful�dhcp4�dhcp�method�disabled�truezmay-failr(N�false)r!�KeyErrorr)r�family�subnet_type�
method_mapr)rrr�_set_ip_method@s*��zNMConnection._set_ip_methodcCsBt�d�D]}|�|��}|j�||�s||j||<dSqdS)z�
        Adds a numbered property, such as address<n> or route<n>, ensuring
        the appropriate value gets used for <n>.
        �N)�	itertools�countrr)rr�
key_prefixr �index�keyrrr�
_add_numberedds��zNMConnection._add_numberedcCs*|ddt|d�}|�|d|�dS)z5
        Adds an ipv[46]address<n> property.
        �address�/�prefixN�rr8)rr.�subnetr rrr�_add_addresspszNMConnection._add_addresscCsB|ddt|d�}d|vr|d|d}|�|d|�dS)z3
        Adds a ipv[46].route<n> property.
        �networkr:r;�gateway�,�routeNr<)rr.rBr rrr�
_add_routexszNMConnection._add_routecCsPt|�rdnd}|�|dd�|�|dd�|j|d|d|j|d<dS)	zF
        Extends the ipv[46].dns property with a name server.
        �ipv6�ipv4r)r*�dns��;N)rr!r)rrFr.rrr�_add_nameserver�s	$zNMConnection._add_nameservercCs8|�|dd�|j|dd�|�d|j|d<dS)zM
        Extends the ipv[46].dns-search property with a name server.
        z
dns-searchrGrHN)r!r�join)rr.�
dns_searchrrr�_add_dns_search�s�zNMConnection._add_dns_searchcCs|jddS)z-
        Returns the connection UUID
        r
r)r�rrrr�con_uuid��zNMConnection.con_uuidcCs|j�dd�S)zN
        Can this be serialized into a meaningful connection profile?
        r
�type)rrrMrrr�valid�rOzNMConnection.validcCs|�dd���S)z)
        Sanitize a MAC address.
        �-�:)�replace�upper)�addrrrr�mac_addr�szNMConnection.mac_addrc
Cs�ddddddd�}||d}|durdSd	|vrd}nd}||jd
d<|dur>||jd
d<|�||d�|jd
d
<i|j|<dddddddddd�	ddd�ddiiid�}|d}d}|dD]^}	t|	�rld nd!}
|�|
|	d�d"|	vr�|�|
|	�d#|	vr�|	d#|j|
d#<|	d$D]}|�|
|�q�d%|	vr�|	d%D]}|�|�q�d&|	vr�|�|
|	d&�|
d!kr�d|	vr�|	d}qd|dur�|}||ks�t�	d'|d(||�||�
�D]1\}
}||vr�q�||dur�q�t||t��r||r�d)nd*|j||
<q�t
||�|j||
<q�|dk�rE|d+d,u�r$t
d-�|jdd.<|du�r2t
|�|jdd<|d/du�rE|�|d/�|jdd0<|dk�r[d1|v�r[|�|d1�|jdd2<|dk�r�|d3D]}|�|�}|�d
dd�|�d
d
|����qd|d/du�r�|�|d/�|jdd0<|dk�r�|du�r�d4|jdd5<t
|�|jdd<|d/du�r�|�|d/�|jdd0<|dk�s�|j�|d0��s�|d(|jd
d6<dSdS)7z�
        Integrate information from network state interface information
        into the connection. Most of the work is done here.
        �ethernet�vlan�bond�bridge�
infinibandN)�physicalrYrZr[r\�loopbackrPzbond-masterr
z
slave-typez-master�masterz	bond-mode�bond_miimonzbond-xmit-hash-policyzbond-num-grat-arpzbond-downdelayzbond-updelayzbond-fail-over-maczbond-primary-reselectzbond-primary)	�mode�miimon�xmit_hash_policy�num_grat_arp�	downdelay�updelay�
fail_over_mac�primary_reselect�primary�
bridge_stp�bridge_bridgeprio)�stp�priorityr�vlan_id)rZr[rYrXr\�mtu�subnetsrDrEr9r@�routes�dns_nameserversrKzZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.�namer+r,�	wakeonlanT�@zwake-on-lan�mac_addresszmac-addresszvlan-raw-device�parent�bridge_ports�datagramztransport-modezinterface-name)r�con_refrr1r>rCrIrL�LOG�warning�items�
isinstance�boolrrW�get_connr!rNr)r�ifacer�	_type_map�if_type�
slave_type�	_prop_map�
device_mtu�ipv4_mtur=r.rB�
nameserver�nm_propr7�portrrr�render_interface�s��	
�
������	�

��

����zNMConnection.render_interfacecCs(t��}|jj|dd�d}||��S)z
        Stringify.
        F)�space_around_delimitersz2# Generated by cloud-init. Changes will be lost.

)�io�StringIOr�write�getvalue)r�buf�headerrrr�dump>szNMConnection.dumpN)�__name__�
__module__�__qualname__�__doc__rr!r1r8r>rCrIrLrNrQ�staticmethodrWr�r�rrrrr
s"$


r
c@sJeZdZdZddd�Zdd�Zdd�Z		dd	ed
ee	ddfdd
�Z
dS)�Rendererz?Renders network information in a NetworkManager keyfile format.NcCs
i|_dS�N��connections)rrrrrrL�
zRenderer.__init__cCs
|j|Sr�r��rrrrrr�Or�zRenderer.get_conncCs||jvr|j|��S|Sr�)r�rNr�rrrrzRs
zRenderer.con_ref�
network_state�	templates�returncCs�|��D]
}t|d�|j|d<q|��D]}|j|d}|�||�q|j��D]\}}|��s4q+t||�}t�||�	�d�q+dS)Nrsi�)
�iter_interfacesr
r�r�r}rQ�
conn_filenamer�
write_filer�)rr�r��targetr��connrrsrrr�render_network_stateYs	
�zRenderer.render_network_stater�)NN)r�r�r�r�rr�rzr	r�dictr�rrrrr�Is

����r�cCs&t�|t�}d|�d�}|�d|��S)Nzcloud-init-z
.nmconnectionz/system-connections/)r�target_path�
NM_RUN_DIR)rr��target_con_dir�con_filerrrr�rsr�cCsvddlm}tj�tj|td��}tjd|d�}d}|�r3z	t�gd��Wntj	y2d}Ynw|o:t
|�o:|S)	Nr)�uses_systemd)�path�nmcli)r�T)�	systemctlz
is-enabledzNetworkManager.serviceF)�cloudinit.distrosr��osr��isfilerr��NM_CFG_FILE�which�ProcessExecutionErrorr)r�r��config_present�
nmcli_present�service_activerrr�	availablexs�r�r�)rr�r3r�r�typingr�	cloudinitr�loggingrr�
cloudinit.netrrr�cloudinit.net.network_stater	r��
NM_LIB_DIRr��	getLoggerr�r{r
r�r�r�rrrr�<module>s(
0
)