name : network_manager.cpython-39.pyc
a

'�Dg�b�@s�ddlZddlZddlZddlZddlZddlZddlmZmZddl	m
Z
mZddlm
Z
mZmZmZddlmZddlmZdZdZd	Zd
Ze�e�ZGdd�d�ZGd
d�dej�Zddd�Zddd�Zdd�Z ddd�Z!ddd�Z"dS)�N)�List�Optional)�subp�util)�is_ipv6_address�is_ipv6_network�renderer�subnet_is_ipv6)�NetworkState)�available_nm_ifcfg_rhz/etc/NetworkManagerz/usr/lib/NetworkManagerz/etc/sysconfig/network-scriptsz�# This is generated by cloud-init. Do not edit.
#
[.config]
  enable=nm-version-min:1.40
[connection.30-cloud-init-ip6-addr-gen-mode]
  # Select EUI64 to be used if the profile does not specify it.
  ipv6.addr-gen-mode=0

c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
ed�dd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zedd�dd �Zeedd!�d"d#�Zd$d%�Zd&d'�Zed(d)��Zd*d+�Zd,d-�ZdS).�NMConnectionz/Represents a NetworkManager connection profile.cCsRt�d�}t��|_t|j_d|��tt�||��dd�|jd<ddi|jd<d	S)
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 Z120)�id�uuidzautoconnect-priority�
connectionz%org.freedesktop.NetworkManager.originz
cloud-init�userN)rZUUID�configparserZConfigParser�config�strZoptionxformZuuid5)�self�con_idZ
CI_NM_UUID�r�A/usr/lib/python3.9/site-packages/cloudinit/net/network_manager.py�__init__-s

��zNMConnection.__init__cCs6|j�|�si|j|<|j�||�s2||j||<dS)z_
        Sets a property unless it's already set, ensuring the section
        exists.
        N�r�has_section�
has_option�r�section�option�valuerrr�_set_defaultEs
zNMConnection._set_defaultcCs|j�|�o|j�||�S)zf
        Checks if a config option is set. Returns True if it is,
        else returns False.
        r�rrrrrr�_config_option_is_setPs�z"NMConnection._config_option_is_setcCs"|�||�r|j||SdSdS)z]
        Returns the value of a config option if its set,
        else returns None.
        N�r"rr!rrr�_get_config_optionYszNMConnection._get_config_optioncCs|�||�r||j||<dS)z�
        Overrides the value of a config option if its already set.
        Else, if the config option is not set, it does nothing.
        Nr#rrrr�_change_set_config_optioncsz&NMConnection._change_set_config_optioncCsRdD]0}|�|d�dkrdS|�|d�dvrdSqdD]}|�|dd�q:dS)zt
        If for both ipv4 and ipv6, 'may-fail' is set to be False,
        set it to True for both of them.
        ��ipv4�ipv6�may-fail�falseN�method)�dhcp�auto�true)r$r%)r�familyrrr�$_set_mayfail_true_if_both_false_dhcpksz1NMConnection._set_mayfail_true_if_both_false_dhcpc	Cs�ddddddddd�}d}|�|d|�z|r6||}Wn$ty\d}d|j|d<Yn0|j|ddkrtd	S|j|ddkr�|dkr�d	S|d
vr�|�ddd�||j|d<|�|dd�d	S)
z}
        Ensures there's appropriate [ipv4]/[ipv6] for given family
        appropriate for given configuration type
        Zmanualr-r,)ZstaticZstatic6Zdhcp6�
ipv6_slaac�ipv6_dhcpv6-stateless�ipv6_dhcpv6-statefulZdhcp4r,�disabledr+r.r)N)r3r2r1r'r*)r �KeyErrorr)rr/Zsubnet_typeZ
method_mapr+rrr�_set_ip_method�s2�zNMConnection._set_ip_method)�returncCsL|j�|�si|j|<t�d�D]&}|�|��}|j�||�s |Sq dS)N�Znot_possible)rr�	itertools�countr)rr�
key_prefix�index�keyrrr�_get_next_numbered_section�s

z'NMConnection._get_next_numbered_sectioncCs|�||�}||j||<dS)z�
        Adds a numbered property, such as address<n> or route<n>, ensuring
        the appropriate value gets used for <n>.
        N)r>r)rrr;rr=rrr�
_add_numbered�szNMConnection._add_numberedcCsN|�d�}|j|�|�}|r2|�d|�d|��n|�d|��|j||<dS)a:Add route options to a given route

        Example:
        Given:
          section: ipv4
          route: route0
          key: mtu
          value: 500

        Create line under [ipv4] section:
            route0_options=mtu=500

        If the line already exists, then append the new key/value pair
        Z_options�,�=N)r�get)rr�router=rZnumbered_keyZ
route_optionsrrr�_add_route_options�s
��zNMConnection._add_route_optionscCs*|ddt|d�}|�|d|�dS)z5
        Adds an ipv[46]address<n> property.
        �address�/�prefixN)rr?)rr/�subnetrrrr�_add_address�szNMConnection._add_addresscCsd}|ddko|d|vS)N)z::z0.0.0.0rGr�networkr)rrCZdefault_netsrrr�_is_default_route�szNMConnection._is_default_routecCs�t|d�rdnd}|d�d|d��}d|vrD|d|d��7}d|vr~|�|�rl|d�|j|d	<n|d|d��7}|�|d
�}||j||<d|vr�|�||d|d�dS)
z!Adds a ipv[46].route<n> property.rJr(r'rFrG�gatewayr@Zmetriczroute-metricrC�mtuN)rrKrr>rD)rrCr/rZ	route_keyrrr�
_add_route�s
zNMConnection._add_routeN)�dnsr7cCs^t|�rdnd}|j�|�rZ|�|d�dkrZ|�|dd�|j|d|d|j|d<dS)	zF
        Extends the ipv[46].dns property with a name server.
        r(r'r+r4rO��;N)rrrr$r )rrOr/rrr�_add_nameservers
��zNMConnection._add_nameserver)�
dns_searchr7cCs^dD]T}|j�|�r|�|d�dkr|�|dd�|j|dd�|�d|j|d<qdS)zM
        Extends the ipv[46].dns-search property with a name server.
        r&r+r4z
dns-searchrPrQN)rrr$r �join)rrSr/rrr�_add_dns_searchs
�����zNMConnection._add_dns_searchcCs|jddS)z-
        Returns the connection UUID
        rr)r�rrrr�con_uuid"szNMConnection.con_uuidcCs|j�dd�S)zN
        Can this be serialized into a meaningful connection profile?
        r�type)rrrVrrr�valid(szNMConnection.validcCs|�dd���S)z)
        Sanitize a MAC address.
        �-�:)�replace�upper)�addrrrr�mac_addr.szNMConnection.mac_addrcs�ddddddd�}||d}|dur*dSd	|vr8d}nd}||jd
d<|dur|||jd
d<|�||d�|jd
d
<i|j|<ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*�d+d,d-�d.d/iiid0�}|d1}d}	g�g�|dk�r|d2�sd3D]}
|�|
d��q|d2D]�}t|��r4d4nd5}
|�|
|d�d6|v�r^|�|
|�d7|v�rz|d7|j|
d7<|d8D]}|�|��q�d9|v�r���|d9�d:|v�rĈ�|d:�|
d5k�r"d1|v�r"|d1}	�q"d;|v�r*��fd<d=�|d;d>D�7���fd?d=�|d;d@D�7���s>|j�r>|j���sR|j�rR|j��D]}
|�	|
��qV��rx|�
��|��|	du�r�|}	|	|k�s�t�
dA|dB||	�||��D]n\}}||v�rΐq�||du�r�q�t||t��r||�r�dCndD|j||<nt||�|j||<�q�|dk�r�|dEdFu�rRtdG�|jddH<|	du�rnt|	�|jdd1<|dIdu�r�|�|dI�|jddJ<|dk�r�dK|v�r�|�|dK�|jddL<|dk�r�|	du�r�d|jv�r�i|jd<t|	�|jdd1<|dk�rf|dMD]0}|�|�}|�d
dd�|�d
d
|����q|dIdu�rf|�|dI�|jddJ<|dk�r�|	du�r�dN|jddO<t|	�|jdd1<|dIdu�r�|�|dI�|jddJ<|dk�s�|j�|dJ��s�|dB|jd
dP<dS)Qz�
        Integrate information from network state interface information
        into the connection. Most of the work is done here.
        �ethernet�vlan�bond�bridge�
infinibandN)ZphysicalrarbrcrdZloopbackrXzbond-masterrz
slave-typez-masterZmasterz	bond-modezbond-miimonzbond-xmit_hash_policyzbond-num_grat_arpzbond-downdelayzbond-updelayzbond-fail_over_maczbond-primary_reselectzbond-primaryzbond-active_slavezbond-ad_actor_sys_priozbond-ad_actor_systemzbond-ad_selectzbond-ad_user_port_keyzbond-all_slaves_activezbond-arp_all_targetszbond-arp_intervalzbond-arp_ip_targetzbond-arp_validatezbond-lacp_ratezbond-lp_intervalzbond-min_linkszbond-num_unsol_nazbond-packets_per_slavezbond-peer_notif_delayzbond-resend_igmpzbond-tlb_dynamic_lbzbond-use_carrier)�modeZmiimonZxmit_hash_policyZnum_grat_arpZ	downdelayZupdelayZ
fail_over_macZprimary_reselectZprimaryZactive_slaveZad_actor_sys_prioZad_actor_systemZ	ad_selectZad_user_port_keyZall_slaves_activeZarp_all_targetsZarp_intervalZ
arp_ip_targetZarp_validateZ	lacp_rateZlp_intervalZ	min_linksZnum_unsol_naZpackets_per_slaveZpeer_notif_delayZresend_igmpZtlb_dynamic_lbZuse_carrierZ
bridge_stpZbridge_bridgeprio)Zstp�priorityr
Zvlan_id)rbrcrar`rdrM�subnetsr&r(r'rErLZroutes�dns_nameserversrSrOcsg|]}|�vr|�qSrr)�.0rO)�found_nameserversrr�
<listcomp>�s�z1NMConnection.render_interface.<locals>.<listcomp>Znameserverscsg|]}|�vr|�qSrr)ri�search)�found_dns_searchrrrk�s�rlzZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.�namer.r*Z	wakeonlanT�@zwake-on-lanZmac_addresszmac-addresszvlan-raw-device�parentZbridge_portsZdatagramztransport-modezinterface-name)r�con_refr6r	rIrN�extendrhZdns_searchdomainsrRrUr0�LOGZwarning�items�
isinstance�boolrr_�get_connr rWr)r�iface�
network_staterZ	_type_mapZif_typeZ
slave_typeZ	_prop_mapZ
device_mtuZipv4_mtur/rHrCZ
nameserverZnm_propr=�portr)rmrjr�render_interface5s,�	
�
�0���;





�
�


�	
�

��

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

)�io�StringIOr�write�getvalue)r�buf�headerrrr�dump"szNMConnection.dump)�__name__�
__module__�__qualname__�__doc__rr r"r$r%r0r6rr>r?rDrIrKrNrRrrUrWrY�staticmethodr_r{r�rrrrr*s.	
=	
nrc@sBeZdZdZddd�Zdd�Zdd�Zd
eee	dd	�d
d�Z
dS)�Rendererz�Renders network information in a NetworkManager keyfile format.

    See https://networkmanager.dev/docs/api/latest/nm-settings-keyfile.html
    NcCsi|_||_dS�N)�connectionsr)rrrrrr3szRenderer.__init__cCs
|j|Sr�)r��rrrrrrw7szRenderer.get_conncCs ||jvr|j|��S|SdSr�)r�rWr�rrrrq:s
zRenderer.con_ref)ry�	templatesr7c	Cs�|��D](}|�d�p|d}t|d�|j|<q|��D].}|�d�pN|d}|j|}|�|||�q:|j��D].\}}|��s�qtt||�}t�	||�
�d�qtt�	t|�td�dS)NZ	config_idrni�)
Ziter_interfacesrBrr�r{rtrY�nm_conn_filenamerZ
write_filer��cloud_init_nm_conf_filename�NM_IPV6_ADDR_GEN_CONF)	rryr��targetrxZconn_keyZconnrrnrrr�render_network_stateAs	


�zRenderer.render_network_state)N)NN)r�r�r�r�rrwrqr
r�dictr�rrrrr�-s

��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�bsr�cCs$t�|t�}d|��}|�d|��S)Nzifcfg-rF)rr��IFCFG_CFG_FILE)�devnamer�r�r�rrr�sysconfig_conn_filenamehs
r�cCs6t|�}tj�|�s"t�r"t|�}tj�|�r2|SdS)a�
    This function returns the name of the interface config file.
    It first checks for presence of network manager connection file.
    If absent and ifcfg-rh plugin for network manager is available,
    it returns the name of the ifcfg file if it is present. If the
    plugin is not present or the plugin is present but ifcfg file is
    not, it returns None.
    This function is called from NetworkManagerActivator class in
    activators.py.
    N)r��os�path�isfilerr�)r�Z	conn_filerrr�
conn_filenamensr�cCst�|t�}d}|�d|��S)Nz$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/r�)r�r�Z	conf_filerrrr��sr�cCs\ddlm}tjd|d�}d}|�rPzt�gd��WntjyNd}Yn0t|�oZ|S)Nr)�uses_systemdZnmcli)r�T)Z	systemctlz
is-enabledzNetworkManager.serviceF)Zcloudinit.distrosr�r�whichZProcessExecutionErrorrv)r�r�Z
nmcli_presentZservice_activerrr�	available�s
r�)N)N)N)N)#rr|r9Zloggingr�r�typingrrZ	cloudinitrrZ
cloudinit.netrrrr	Zcloudinit.net.network_stater
Zcloudinit.net.sysconfigrr�Z
NM_LIB_DIRr�r�Z	getLoggerr�rsrr�r�r�r�r�r�rrrr�<module>	s4	
5




© 2025 Cubjrnet7