
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
cj9L                 @   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dZej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/NetworkManager/NetworkManager.confz/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d Zdd Zdd ZeddddZee dd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 jd}tj | _t| j_d| tt j||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 Z120)iduuidzautoconnect-priority
connectionz%org.freedesktop.NetworkManager.originz
cloud-inituserN)r   ZUUIDconfigparserZConfigParserconfigstrZoptionxformZuuid5)selfcon_idZ
CI_NM_UUID r   %/usr/lib/python3.6/network_manager.py__init__.   s    

zNMConnection.__init__c             C   s6   | j j|si | j |< | j j||s2|| j | |< 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_defaultF   s    
zNMConnection._set_defaultc             C   s   | j j|o| j 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   r   r   _config_option_is_setQ   s    z"NMConnection._config_option_is_setc             C   s"   | j ||r| j| | S dS dS )z]
        Returns the value of a config option if its set,
        else returns None.
        N)r   r   )r   r   r   r   r   r   _get_config_optionZ   s    zNMConnection._get_config_optionc             C   s   | j ||r|| j| |< 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.
        N)r   r   )r   r   r   r   r   r   r   _change_set_config_optiond   s    z&NMConnection._change_set_config_optionc             C   sV   x4d
D ],}| j |ddkrdS | j |ddkrdS qW xdD ]}| j|dd	 q<W 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ipv6zmay-failfalseNmethoddhcpautotrue)r"   r#   )r&   r'   )r"   r#   )r    r!   )r   familyr   r   r   $_set_mayfail_true_if_both_false_dhcpl   s    

 
z1NMConnection._set_mayfail_true_if_both_false_dhcpc             C   s   ddddddddd}| j |dd y|| }W n& tk
rV   d}d| j| d< Y nX | j| d dkrnd	S | j| d dkr|dkrd	S |dkr| j ddd || j| d< | j |dd d	S )z}
        Ensures there's appropriate [ipv4]/[ipv6] for given family
        appropriate for given configuration type
        Zmanualr'   r&   )ZstaticZstatic6Zdhcp6
ipv6_slaaczipv6_dhcpv6-statelesszipv6_dhcpv6-statefulZdhcp4r&   r%   Zdisabledr(   zmay-failNipv6_dhcpv6-statefulipv6_dhcpv6-statelessr+   r"   r$   )r,   r-   r+   )r   KeyErrorr   )r   r)   Zsubnet_typeZ
method_mapr%   r   r   r   _set_ip_method   s2      zNMConnection._set_ip_methodc             C   sX   | j j|si | j |< x<tjdD ].}| | }| j j||s"|| j | |< P q"W dS )z
        Adds a numbered property, such as address<n> or route<n>, ensuring
        the appropriate value gets used for <n>.
           N)r   r   	itertoolscountr   )r   r   Z
key_prefixr   indexkeyr   r   r   _add_numbered   s    
zNMConnection._add_numberedc             C   s*   |d d t |d  }| j|d| dS )z5
        Adds an ipv[46]address<n> property.
        address/prefixN)r   r5   )r   r)   subnetr   r   r   r   _add_address   s    zNMConnection._add_addressc             C   sV   t |d rdnd}|d d t|d  }d|krD|d |d  }| j|d| d	S )
z!Adds a ipv[46].route<n> property.gatewayr#   r"   Znetworkr7   r8   ,routeN)r   r   r5   )r   r=   r)   r   r   r   r   
_add_route   s
    zNMConnection._add_routeN)dnsreturnc             C   sN   t |rdnd}| jj|rJ| j|dd | j| d | d | j| d< dS )zF
        Extends the ipv[46].dns property with a name server.
        r#   r"   r?    ;N)r   r   r   r   )r   r?   r)   r   r   r   _add_nameserver   s    zNMConnection._add_nameserver)
dns_searchr@   c             C   sR   xLdD ]D}| j j|r| j|dd | j | d dj| d | j | d< qW dS )zM
        Extends the ipv[46].dns-search property with a name server.
        r"   r#   z
dns-searchrA   rB   N)r"   r#   )r   r   r   join)r   rD   r)   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   s    zNMConnection.con_uuidc             C   s   | j jddS )zN
        Can this be serialized into a meaningful connection profile?
        r   type)r   r   )r   r   r   r   valid   s    zNMConnection.validc             C   s   | j ddj S )z)
        Sanitize a MAC address.
        -:)replaceupper)Zaddrr   r   r   mac_addr   s    zNMConnection.mac_addrc          
      st  ddddddd}||d  }|dkr*dS d	|kr8d}nd}|| j d
 d< |dk	r||| j d
 d< |j||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  x|d D ]}
t|
rd nd!}| j||
d  d"|
kr| j||
 d#|
kr(|
d# | j | d#< x|
d$ D ]}| j| q2W d%|
kr^j|
d%  d&|
krv j|
d&  |d!krd|
kr|
d }	qW d'|kr؈fd(d)|d' d* D 7   fd+d)|d' d, D 7   r|jr|j  r|jr|j xD ]}| j	| q
W  r.| j
  | j  |	dkrD|}	|	|ksbtjd-|d. ||	 x~|| j D ]n\}}||krqp|| dkrqpt|| tr|| rd/nd0| j | |< nt|| | j | |< qpW |dkrN|d1 d2krtd3| j d d4< |	dk	r(t|	| j d d< |d5 dk	rN| j|d5 | j d d6< |dkrzd7|krz|j|d7 | j d d8< |dkrx<|d9 D ]0}|j|}|jd
dd |jd
d| j  qW |d5 dk	r| j|d5 | j d d6< |dkrB|	dk	rBd:| j d d;< t|	| j d d< |d5 dk	rB| j|d5 | j d d6< |dks^| j j|d6 rp|d. | j d
 d<< 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)ZphysicalrP   rQ   rR   rS   ZloopbackrH   zbond-masterr   z
slave-typez-masterZ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)	modeZmiimonZxmit_hash_policyZnum_grat_arpZ	downdelayZupdelayZfail_over_macZprimary_reselectZprimaryZ
bridge_stpZbridge_bridgeprio)ZstpZpriorityr   Zvlan_id)rQ   rR   rP   rO   rS   ZmtuZsubnetsr#   r"   r6   r;   Zroutesdns_nameserversrD   r?   c                s   g | ]}| kr|qS r   r   ).0r?   )found_nameserversr   r   
<listcomp>O  s   z1NMConnection.render_interface.<locals>.<listcomp>Znameserversc                s   g | ]}| kr|qS r   r   )rV   search)found_dns_searchr   r   rX   T  s   rY   zZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.namer(   r$   Z	wakeonlanT@   zwake-on-lanZmac_addresszmac-addresszvlan-raw-deviceparentZbridge_portsZdatagramztransport-modezinterface-name)r   con_refr	   r/   r:   r>   extendrU   Zdns_searchdomainsrC   rF   r*   LOGZwarningitems
isinstanceboolr   rN   get_connr   rG   r   )r   ifacenetwork_stater   Z	_type_mapZif_typeZ
slave_typeZ	_prop_mapZ
device_mtuZipv4_mtur9   r)   r=   Z
nameserverZnm_propr4   Zportr   )rZ   rW   r   render_interface   s    
















zNMConnection.render_interfacec             C   s(   t j }| jj|dd d}||j  S )z
        Stringify.
        F)Zspace_around_delimitersz2# Generated by cloud-init. Changes will be lost.

)ioStringIOr   writegetvalue)r   Zbufheaderr   r   r   dump  s    zNMConnection.dump)__name__
__module____qualname____doc__r   r   r   r    r!   r*   r/   r5   r:   r>   r   rC   r   rF   rG   rI   staticmethodrN   rg   rm   r   r   r   r   r   +   s&   	
-
	 5r   c               @   sB   e Zd ZdZdddZdd Zdd Zdeee	 dd	d
dZ
dS )Rendererz?Renders network information in a NetworkManager keyfile format.Nc             C   s   i | _ || _d S )N)connectionsr   )r   r   r   r   r   r     s    zRenderer.__init__c             C   s
   | j | S )N)rt   )r   r   r   r   r   rd     s    zRenderer.get_connc             C   s    || j kr| j | j S |S d S )N)rt   rG   )r   r   r   r   r   r^     s    
zRenderer.con_ref)rf   	templatesr@   c             C   s   x&|j  D ]}t|d | j|d < q
W x,|j  D ] }| j|d  }|j|||  q2W x<| jj D ].\}}|j stqbt||}tj||j	 d qbW tjt
|td d S )Nr[   i  )Ziter_interfacesr   rt   rg   ra   rI   nm_conn_filenamer   Z
write_filerm   cloud_init_nm_conf_filenameNM_IPV6_ADDR_GEN_CONF)r   rf   ru   targetre   Zconnr   r[   r   r   r   render_network_state  s    	
zRenderer.render_network_state)N)NN)rn   ro   rp   rq   r   rd   r^   r
   r   dictrz   r   r   r   r   rs     s   

 
rs   c             C   s&   t j|t}d|  d}| d| S )Nzcloud-init-z.nmconnectionz/system-connections/)r   target_path
NM_RUN_DIR)r   ry   target_con_dircon_filer   r   r   rv     s    rv   c             C   s$   t j|t}d|  }| d| S )Nzifcfg-r7   )r   r|   IFCFG_CFG_FILE)devnamery   r~   r   r   r   r   sysconfig_conn_filename  s    
r   c             C   s8   t | }tjj| r$t r$t| }tjj|r4|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)rv   ospathisfiler   r   )r   Z	conn_filer   r   r   conn_filename  s    r   c             C   s   t j| t}d}| d| S )Nz$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/)r   r|   r}   )ry   r~   Z	conf_filer   r   r   rw   	  s    rw   c             C   sz   ddl m} tjjtj| td}tjd| d}d}| rjytjddd	g W n tj	k
rh   d
}Y nX |oxt
|ox|S )Nr   )uses_systemd)r   Znmcli)ry   TZ	systemctlz
is-enabledzNetworkManager.serviceF)Zcloudinit.distrosr   r   r   r   r   r|   NM_CFG_FILEZwhichZProcessExecutionErrorrc   )ry   r   Zconfig_presentZnmcli_presentZservice_activer   r   r   	available  s    
r   )N)N)N)N)$r   rh   r1   Zloggingr   r   Ztypingr   r   Z	cloudinitr   r   Zcloudinit.netr   r   r   r	   Zcloudinit.net.network_stater
   Zcloudinit.net.sysconfigr   r}   Z
NM_LIB_DIRr   r   rx   Z	getLoggerrn   r`   r   rs   rv   r   r   rw   r   r   r   r   r   <module>	   s4   	
   0


