o
    w7e`7                     @   s   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 d dlmZ	 d dlm
Z
mZ d dlmZmZ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dZdS )    N)Optional)log)subputil)is_ipv6_addressrenderersubnet_is_ipv6)NetworkStatez/etc/NetworkManagerz/usr/lib/NetworkManagerz'/etc/NetworkManager/NetworkManager.confz# 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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_default?   s
   
zNMConnection._set_defaultc              	   C   s   ddddddddd}|  |dd z|| }W n ty*   d}d| j| d< Y nw | j| d dkr6d	S | j| d dkrE|dkrEd	S || j| d< |  |dd
 d	S )z}
        Ensures there's appropriate [ipv4]/[ipv6] for given family
        appropriate for given configuration type
        manualautodhcp)staticstatic6dhcp6
ipv6_slaaczipv6_dhcpv6-statelesszipv6_dhcpv6-statefuldhcp4r%   methoddisabledtruezmay-failNfalse)r"   KeyErrorr   )r   familysubnet_type
method_mapr+   r   r   r   _set_ip_methodJ   s,   zNMConnection._set_ip_methodc                 C   sB   t dD ]}| | }| j||s|| j| |<  dS qdS )z
        Adds a numbered property, such as address<n> or route<n>, ensuring
        the appropriate value gets used for <n>.
           N)	itertoolscountr   r   )r   r   
key_prefixr!   indexkeyr   r   r   _add_numberedo   s   zNMConnection._add_numberedc                 C   s*   |d d t |d  }| |d| dS )z5
        Adds an ipv[46]address<n> property.
        address/prefixNr   r:   )r   r0   subnetr!   r   r   r   _add_address{   s   zNMConnection._add_addressc                 C   sB   |d d t |d  }d|v r|d |d  }| |d| dS )z3
        Adds a ipv[46].route<n> property.
        networkr<   r=   gateway,routeNr>   )r   r0   rD   r!   r   r   r   
_add_route   s   zNMConnection._add_routec                 C   sP   t |rdnd}| |dd | |dd | j| d | d | j| d< dS )	zF
        Extends the ipv[46].dns property with a name server.
        ipv6ipv4r+   r,   dns ;N)r   r"   r   )r   rH   r0   r   r   r   _add_nameserver   s   	$zNMConnection._add_nameserverc                 C   s8   |  |dd | j| d d| d | j| d< dS )zM
        Extends the ipv[46].dns-search property with a name server.
        z
dns-searchrI   rJ   N)r"   r   join)r   r0   
dns_searchr   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   rO   r   r   r   valid   rQ   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              
   C   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}|d D ]^}	t|	rld nd!}
| |
|	d  d"|	v r| |
|	 d#|	v r|	d# | j |
 d#< |	d$ D ]}| |
| qd%|	v r|	d% D ]}| | qd&|	v r| |
|	d&  |
d!krd|	v r|	d }qd|du r|}||kst	d'|d( || || 
 D ]1\}}||vrq|| du rqt|| tr|| rd)nd*| j | |< qt|| | j | |< q|dkrE|d+ d,u r$td-| j d d.< |dur2t|| j d d< |d/ durE| |d/ | j d d0< |dkr[d1|v r[||d1 | j d d2< |dkr|d3 D ]}||}|d
dd |d
d|   qd|d/ dur| |d/ | j d d0< |dkr|durd4| j d d5< t|| j d d< |d/ dur| |d/ | j d d0< |dks| j |d0s|d( | j d
 d6< dS dS )7z
        Integrate information from network state interface information
        into the connection. Most of the work is done here.
        ethernetvlanbondbridge
infinibandN)physicalr[   r\   r]   r^   loopbackrR   zbond-masterr   z
slave-typez-mastermasterz	bond-mode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)r\   r]   r[   rZ   r^   mtusubnetsrF   rG   r;   rB   routesdns_nameserversrM   zZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.namer-   r.   	wakeonlanT@   zwake-on-lanmac_addresszmac-addresszvlan-raw-deviceparentbridge_portsdatagramztransport-modezinterface-name)r   con_refr   r3   r@   rE   rK   rN   LOGwarningitems
isinstanceboolr   rY   get_connr"   rP   r   )r   ifacer   	_type_mapif_type
slave_type	_prop_map
device_mtuipv4_mtur?   r0   rD   
nameservernm_propr9   portr   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   dumpI  s   zNMConnection.dumpN)__name__
__module____qualname____doc__r   r"   r3   r:   r@   rE   rK   rN   rP   rS   staticmethodrY   r   r   r   r   r   r   r
   $   s"    %


 r
   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 )Rendererz?Renders network information in a NetworkManager keyfile format.Nc                 C   s
   i | _ d S Nconnections)r   r   r   r   r   r   W     
zRenderer.__init__c                 C   s
   | j | S r   r   r   r   r   r   r   r   Z  r   zRenderer.get_connc                 C   s   || j v r| j |  S |S r   )r   rP   r   r   r   r   r|   ]  s   
zRenderer.con_refnetwork_state	templatesreturnc                 C   s   |  D ]}t|d | j|d < q|  D ]}| j|d  }|||  q| j D ]\}}| s4q+t||}t||	 d q+tt
|td d S )Nru   i  )iter_interfacesr
   r   r   r   rS   conn_filenamer   
write_filer   cloud_init_nm_conf_filenameNM_IPV6_ADDR_GEN_CONF)r   r   r   targetr   connr   ru   r   r   r   render_network_stated  s   	

zRenderer.render_network_stater   )NN)r   r   r   r   r   r   r|   r	   r   dictr   r   r   r   r   r   T  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$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/r   )r   r   	conf_filer   r   r   r     s   r   c                 C   sv   ddl m} tjtj| td}tjd| d}d}| r3z	tg d W n tj	y2   d}Y nw |o:t
|o:|S )	Nr   )uses_systemd)pathnmcli)r   T)	systemctlz
is-enabledzNetworkManager.serviceF)cloudinit.distrosr   osr   isfiler   r   NM_CFG_FILEwhichProcessExecutionErrorr   )r   r   config_presentnmcli_presentservice_activer   r   r   	available  s   r   r   )r   r   r5   r   r   typingr   	cloudinitr   loggingr   r   cloudinit.netr   r   r   cloudinit.net.network_stater	   r   
NM_LIB_DIRr   r   	getLoggerr   r}   r
   r   r   r   r   r   r   r   r   <module>   s,   
	  2
/
