o
    fwX                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZ d dlmZmZmZmZ d dlmZ d dlmZ dZdZd	Zd
ZeeZG dd dZG dd dejZdddZdddZdd Z dddZ!dddZ"dS )    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   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
defddZdd Zdd Zdd Zdd ZdeddfddZdee ddfd d!Zd"d# Zd$d% Zed&d' Zd(d) Zd*d+ ZdS ),NMConnectionz/Represents a NetworkManager connection profile.c                 C   sR   t d}t | _t| j_d| tt ||dd| jd< ddi| jd< 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-77972a800108zcloud-init 120)iduuidzautoconnect-priority
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   

zNMConnection.__init__c                 C   s:   | j |si | j |< | j ||s|| j | |< dS dS )z_
        Sets a property unless it's already set, ensuring the section
        exists.
        Nr   has_section
has_optionr   sectionoptionvaluer   r   r   _set_defaultE   s
   
zNMConnection._set_defaultc                 C   s   | j |o| j ||S )zf
        Checks if a config option is set. Returns True if it is,
        else returns False.
        r   r   r#   r$   r   r   r   _config_option_is_setP   s   z"NMConnection._config_option_is_setc                 C   s   |  ||r| j| | S dS )z]
        Returns the value of a config option if its set,
        else returns None.
        Nr(   r   r'   r   r   r   _get_config_optionY   s   zNMConnection._get_config_optionc                 C   s"   |  ||r|| j| |< dS dS )z
        Overrides the value of a config option if its already set.
        Else, if the config option is not set, it does nothing.
        Nr)   r"   r   r   r   _change_set_config_optionc   s   z&NMConnection._change_set_config_optionc                 C   sR   dD ]}|  |ddkr dS |  |ddvr dS qdD ]	}| |dd qdS )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   familyr   r   r   $_set_mayfail_true_if_both_false_dhcpk   s   
z1NMConnection._set_mayfail_true_if_both_false_dhcpc              	   C   s   ddddddddd}d}|  |d| z|r|| }W n ty.   d}d| j| d< Y nw | j| d dkr:d	S | j| d dkrI|dkrId	S |d
v rT|  ddd || j| d< |  |dd d	S )z}
        Ensures there's appropriate [ipv4]/[ipv6] for given family
        appropriate for given configuration type
        manualr3   r2   )staticstatic6dhcp6
ipv6_slaacipv6_dhcpv6-statelessipv6_dhcpv6-statefuldhcp4r2   disabledr1   r4   r/   N)r=   r<   r;   r-   r0   )r&   KeyErrorr   )r   r5   subnet_type
method_mapr1   r   r   r   _set_ip_method   s6   zNMConnection._set_ip_methodreturnc                 C   sL   | j |si | j |< tdD ]}| | }| j ||s#|  S qdS )N   not_possible)r   r    	itertoolscountr!   )r   r#   
key_prefixindexkeyr   r   r   _get_next_numbered_section   s   
z'NMConnection._get_next_numbered_sectionc                 C   s   |  ||}|| j| |< dS )z
        Adds a numbered property, such as address<n> or route<n>, ensuring
        the appropriate value gets used for <n>.
        N)rL   r   )r   r#   rI   r%   rK   r   r   r   _add_numbered   s   zNMConnection._add_numberedc                 C   sN   | d}| j | |}|r| d| d| n| d| | j | |< dS )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
        _options,=N)r   get)r   r#   routerK   r%   numbered_keyroute_optionsr   r   r   _add_route_options   s   
zNMConnection._add_route_optionsc                 C   s*   |d d t |d  }| |d| dS )z5
        Adds an ipv[46]address<n> property.
        address/prefixN)r   rM   )r   r5   subnetr%   r   r   r   _add_address   s   zNMConnection._add_addressc                 C   s   t |d rdnd}|d  d|d  }d|v r"|d|d  7 }| |d}|| j| |< d	|v r?| ||d	|d	  d
S d
S )z!Adds a ipv[46].route<n> property.networkr.   r-   rW   rX   gatewayrO   rR   mtuN)r   rL   r   rU   )r   rR   r5   r%   	route_keyr   r   r   
_add_route   s   zNMConnection._add_routednsNc                 C   sf   t |rdnd}| j|r/| |ddkr1| |dd | j| d | d | j| d< dS dS dS )	zF
        Extends the ipv[46].dns property with a name server.
        r.   r-   r1   r?   r`    ;N)r   r   r    r*   r&   )r   r`   r5   r   r   r   _add_nameserver   s   
$zNMConnection._add_nameserver
dns_searchc                 C   s^   dD ]*}| j |r,| |ddkr,| |dd | j | d d| d | j | d< qdS )zM
        Extends the ipv[46].dns-search property with a name server.
        r,   r1   r?   z
dns-searchra   rb   N)r   r    r*   r&   join)r   rd   r5   r   r   r   _add_dns_search	  s   
zNMConnection._add_dns_searchc                 C   s   | j d d S )z-
        Returns the connection UUID
        r   r   )r   r   r   r   r   con_uuid     zNMConnection.con_uuidc                 C   s   | j ddS )zN
        Can this be serialized into a meaningful connection profile?
        r   type)r   r!   rg   r   r   r   valid  ri   zNMConnection.validc                 C   s   |  dd S )z)
        Sanitize a MAC address.
        -:)replaceupper)addrr   r   r   mac_addr%  s   zNMConnection.mac_addrc              
      s  ddddddd}||d  }|du rdS d	|v rd}nd}|| j d
 d< |dur>|| j d
 d< |||d  | j d
 d< i | j |< dddddddddd	dddddii i d}|d }d}	g g  |dkrw|d swd D ]}
| |
d qn|d D ]W}t|rd!nd"}
| |
|d  d#|v r| |
| d$|v r|d$ | j |
 d$< |d% D ]}| | qd&|v r|d&  d'|v rƈ |d'  |
d"krd|v r|d }	q{d(|v rfd)d*|d( d+ D 7   fd,d*|d( d- D 7  s|jr|j s|jr|j D ]}| 	| q	 r| 
  |   |	du r%|}	|	|ks4td.|d/ ||	 ||  D ]8\}}||vrEq:|| du rNq:t|| trf|| r]d0nd1| j | |< q:t|| | j | |< q:|dkr|d2 d3u rtd4| j d d5< |	durt|	| j d d< |d6 dur| |d6 | j d d7< |dkrd8|v r||d8 | j d d9< |dkr|	durd| j vri | j d< t|	| j d d< |dkr|d: D ]}||}|d
dd |d
d|   q|d6 dur| |d6 | j d d7< |dkr?|	dur?d;| j d d<< t|	| j d d< |d6 dur?| |d6 | j d d7< |dksL| j |d7sW|d/ | j d
 d=< dS dS )>z
        Integrate information from network state interface information
        into the connection. Most of the work is done here.
        ethernetvlanbondbridge
infinibandN)physicalrs   rt   ru   rv   loopbackrj   zbond-masterr   z
slave-typez-mastermasterz	bond-modezbond-miimonzbond-xmit_hash_policyzbond-num_grat_arpzbond-downdelayzbond-updelayzbond-fail_over_maczbond-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)rt   ru   rs   rr   rv   r]   subnetsr,   r.   r-   rV   r\   routesdns_nameserversrd   r`   c                       g | ]}| vr|qS r   r   ).0r`   )found_nameserversr   r   
<listcomp>  
    z1NMConnection.render_interface.<locals>.<listcomp>nameserversc                    r   r   r   )r   search)found_dns_searchr   r   r     r   r   zZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.namer4   r0   	wakeonlanT@   zwake-on-lanmac_addresszmac-addresszvlan-raw-deviceparentbridge_portsdatagramztransport-modezinterface-name)r   con_refrC   r	   rZ   r_   extendr   dns_searchdomainsrc   rf   r6   LOGwarningitems
isinstanceboolr   rq   get_connr&   rh   r!   )r   ifacenetwork_stater   	_type_mapif_type
slave_type	_prop_map
device_mtuipv4_mtur5   rY   rR   
nameservernm_proprK   portr   )r   r   r   render_interface,  s
  	







	





zNMConnection.render_interfacec                 C   s(   t  }| jj|dd d}||  S )z
        Stringify.
        F)space_around_delimitersz2# Generated by cloud-init. Changes will be lost.

)ioStringIOr   writegetvalue)r   bufheaderr   r   r   dump  s   zNMConnection.dump)__name__
__module____qualname____doc__r   r&   r(   r*   r+   r6   rC   r   rL   rM   rU   rZ   r_   rc   r   rf   rh   rk   staticmethodrq   r   r   r   r   r   r   r   *   s.    	
=	
 Fr   c                   @   sJ   e Zd ZdZdddZdd Zdd Z		dd	ed
ee	 ddfddZ
dS )RendererzRenders network information in a NetworkManager keyfile format.

    See https://networkmanager.dev/docs/api/latest/nm-settings-keyfile.html
    Nc                 C   s   i | _ || _d S N)connectionsr   )r   r   r   r   r   r     s   
zRenderer.__init__c                 C   s
   | j | S r   )r   r   r   r   r   r   r     s   
zRenderer.get_connc                 C   s   || j v r| j |  S |S r   )r   rh   r   r   r   r   r   	  s   
zRenderer.con_refr   	templatesrD   c                 C   s   |  D ]}t|d | j|d < q|  D ]}| j|d  }||||  q| j D ]\}}| s5q,t||}t||	 d q,tt
|td d S )Nr   i  )iter_interfacesr   r   r   r   rk   nm_conn_filenamer   
write_filer   cloud_init_nm_conf_filenameNM_IPV6_ADDR_GEN_CONF)r   r   r   targetr   connr   r   r   r   r   render_network_state  s   	

zRenderer.render_network_stater   )NN)r   r   r   r   r   r   r   r
   r   dictr   r   r   r   r   r     s    

r   c                 C   s&   t |t}d|  d}| d| S )Nzcloud-init-z.nmconnectionz/system-connections/r   target_path
NM_RUN_DIR)r   r   target_con_dircon_filer   r   r   r   /  s   r   c                 C   s$   t |t}d|  }| d| S )Nzifcfg-rW   )r   r   IFCFG_CFG_FILE)devnamer   r   r   r   r   r   sysconfig_conn_filename5  s   
r   c                 C   s6   t | }tj|st rt| }tj|r|S dS )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isfiler   r   )r   	conn_filer   r   r   conn_filename;  s   r   c                 C   s   t | t}d}| d| S )Nz$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/r   )r   r   	conf_filer   r   r   r   P  s   r   c                 C   s\   ddl m} tjd| d}d}| r(z	tg d W n tjy'   d}Y nw t|o-|S )Nr   )uses_systemdnmcli)r   T)	systemctlz
is-enabledzNetworkManager.serviceF)cloudinit.distrosr   r   whichProcessExecutionErrorr   )r   r   nmcli_presentservice_activer   r   r   	availableV  s   r   r   )#r   r   rG   loggingr   r   typingr   r   	cloudinitr   r   cloudinit.netr   r   r   r	   cloudinit.net.network_stater
   cloudinit.net.sysconfigr   r   
NM_LIB_DIRr   r   	getLoggerr   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s4   
	   U
3

