U
    Ad9'                     @   sz  U d dl Z d dlmZmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ e eZG dd deZed	d
dZG dd deZ G dd de Z!G dd de Z"G dd de Z#G dd de Z$ddddgZ%e!e#e"e$dZ&ee'e	e  f e(d< ee' e
e'df ee	e   dddZ)d!eee'  ee' e	e  ddd Z*dS )"    N)ABCabstractmethod)DictIterableListOptionalTypeUnion)subputil	available)NetworkStatec                   @   s   e Zd ZdS )NoActivatorExceptionN)__name__
__module____qualname__ r   r   :/usr/lib/python3/dist-packages/cloudinit/net/activators.pyr      s   r   returnc                 C   sd   t d| | z*t| \}}t|r4t d| | W dS  tjk
r^   tt d|  Y dS X d S )Nz#Attempting command %s for device %sz(Running %s resulted in stderr output: %sTz#Running interface command %s failedF)LOGdebugr
   lenZwarningZProcessExecutionErrorr   Zlogexc)cmddevice_nameZ_outerrr   r   r   _alter_interface   s    r   c                   @   s   e Zd Zeedee edddZeeeedddZ	eeeeddd	Z
eee ed
ddZeeedddZeee ed
ddZeeedddZdS )NetworkActivatorNtargetr   c                 C   s
   t  dS )z>Return True if activator is available, otherwise return False.NNotImplementedErrorr    r   r   r   r   !   s    zNetworkActivator.availabler   r   c                 C   s
   t  dS )zWBring up interface.

        Return True is successful, otherwise return False
        Nr!   r   r   r   r   bring_up_interface'   s    z#NetworkActivator.bring_up_interfacec                 C   s
   t  dS )zYBring down interface.

        Return True is successful, otherwise return False
        Nr!   r%   r   r   r   bring_down_interface0   s    z%NetworkActivator.bring_down_interfacedevice_namesr   c                    s   t  fdd|D S )zjBring up specified list of interfaces.

        Return True is successful, otherwise return False
        c                 3   s   | ]}  |V  qd S N)r&   .0deviceclsr   r   	<genexpr>?   s     z7NetworkActivator.bring_up_interfaces.<locals>.<genexpr>allr/   r)   r   r.   r   bring_up_interfaces9   s    z$NetworkActivator.bring_up_interfacesnetwork_stater   c                 C   s   |  dd | D S )z\Bring up all interfaces.

        Return True is successful, otherwise return False
        c                 S   s   g | ]}|d  qS namer   r,   ir   r   r   
<listcomp>H   s     z<NetworkActivator.bring_up_all_interfaces.<locals>.<listcomp>)r4   iter_interfacesr/   r6   r   r   r   bring_up_all_interfacesA   s    z(NetworkActivator.bring_up_all_interfacesc                    s   t  fdd|D S )zlBring down specified list of interfaces.

        Return True is successful, otherwise return False
        c                 3   s   | ]}  |V  qd S r*   )r'   r+   r.   r   r   r0   Q   s     z9NetworkActivator.bring_down_interfaces.<locals>.<genexpr>r1   r3   r   r.   r   bring_down_interfacesK   s    z&NetworkActivator.bring_down_interfacesc                 C   s   |  dd | D S )z^Bring down all interfaces.

        Return True is successful, otherwise return False
        c                 S   s   g | ]}|d  qS r7   r   r9   r   r   r   r;   Z   s     z>NetworkActivator.bring_down_all_interfaces.<locals>.<listcomp>)r?   r<   r=   r   r   r   bring_down_all_interfacesS   s    z*NetworkActivator.bring_down_all_interfaces)N)r   r   r   staticmethodr   r   strboolr   r&   r'   classmethodr   r4   r   r>   r?   r@   r   r   r   r   r       s"   	r   c                   @   sN   e Zd Zed
ee edddZeeedddZeeeddd	Z	dS )IfUpDownActivatorNr   c                 C   s
   t | dS z3Return true if ifupdown can be used on this system.r#   )eni_availabler#   r   r   r   r   c   s    zIfUpDownActivator.availabler$   c                 C   s   d| g}t || S )bBring up interface using ifup.

        Return True is successful, otherwise return False
        Zifupr   r   r   r   r   r   r&   h   s    z$IfUpDownActivator.bring_up_interfacec                 C   s   d| g}t || S )rH   ZifdownrI   rJ   r   r   r   r'   q   s    z&IfUpDownActivator.bring_down_interface)N)
r   r   r   rA   r   rB   rC   r   r&   r'   r   r   r   r   rE   ^   s   rE   c                   @   sH   e Zd Zed
edddZeeedddZeeeddd	ZdS )NetworkManagerActivatorNr   c                 C   s
   t | dS )z9Return true if NetworkManager can be used on this system.r#   )nm_availabler#   r   r   r   r   |   s    z!NetworkManagerActivator.availabler$   c                 C   sb   ddl m} || }ddd|g}t|| r:dddd|g}ntdddg|  dddd	| g}t|| S )
zdBring up connection using nmcli.

        Return True is successful, otherwise return False
        r   )conn_filenamenmcliZ
connectionloadupfilenamereloadZifname)cloudinit.net.network_managerrM   r   )r   rM   rQ   r   r   r   r   r&      s    
z*NetworkManagerActivator.bring_up_interfacec                 C   s   ddd| g}t || S )zeBring down interface using nmcli.

        Return True is successful, otherwise return False
        rN   r-   Z
disconnectrI   rJ   r   r   r   r'      s    z,NetworkManagerActivator.bring_down_interface)N)	r   r   r   rA   rC   r   rB   r&   r'   r   r   r   r   rK   {   s   rK   c                   @   s   e Zd ZddgZededddZeeeddd	Zee	e ed
ddZ
eeedddZeeedddZee	e ed
ddZeeedddZdS )NetplanActivatornetplanZapplyNr   c                 C   s
   t | dS )z2Return true if netplan can be used on this system.r#   )netplan_availabler#   r   r   r   r      s    zNetplanActivator.availabler$   c                 C   s   t d ttjdS YApply netplan config.

        Return True is successful, otherwise return False
        zBCalling 'netplan apply' rather than altering individual interfacesr2   r   r   r   rT   NETPLAN_CMDr%   r   r   r   r&      s    z#NetplanActivator.bring_up_interfacer(   c                 C   s   t d ttjdS rW   rY   r)   r   r   r   r4      s    z$NetplanActivator.bring_up_interfacesr5   c                 C   s   t tjdS rX   r2   r   rT   rZ   r6   r   r   r   r>      s    z(NetplanActivator.bring_up_all_interfacesc                 C   s   t d ttjdS rW   rY   r%   r   r   r   r'      s    z%NetplanActivator.bring_down_interfacec                 C   s   t d ttjdS rW   rY   r[   r   r   r   r?      s    z&NetplanActivator.bring_down_interfacesc                 C   s   t tjdS r\   r]   r^   r   r   r   r@      s    z*NetplanActivator.bring_down_all_interfaces)N)r   r   r   rZ   rA   rC   r   rB   r&   r   r4   r   r>   r'   r?   r@   r   r   r   r   rT      s   rT   c                   @   s\   e Zd ZededddZeeedddZeeedd	d
Z	eeedddZ
dS )NetworkdActivatorNr   c                 C   s
   t | dS rF   )networkd_availabler#   r   r   r   r      s    zNetworkdActivator.availabler$   c                 C   s   dddd| g}t || S )1Return True is successful, otherwise return FalseiplinksetrP   rI   rJ   r   r   r   r&      s    z$NetworkdActivator.bring_up_interfacer5   c                 C   s   ddddg}t |dS )ra   Z	systemctlZrestartzsystemd-networkdzsystemd-resolvedr2   rI   )r6   r   r   r   r   r>      s    z)NetworkdActivator.bring_up_all_interfacesc                 C   s   dddd| g}t || S )ra   rb   rc   rd   ZdownrI   rJ   r   r   r   r'      s    z&NetworkdActivator.bring_down_interface)N)r   r   r   rA   rC   r   rB   r&   r   r>   r'   r   r   r   r   r_      s   r_   enirU   network-managernetworkd)re   rU   rf   rg   NAME_TO_ACTIVATOR)priorityr    r   c                    s>   dd | D }|rt d| dd | D } fdd|D S )Nc                 S   s   g | ]}|t kr|qS r   )DEFAULT_PRIORITYr9   r   r   r   r;     s      z$search_activator.<locals>.<listcomp>z0Unknown activators provided in priority list: %sc                 S   s   g | ]}t | qS r   )rh   )r,   r8   r   r   r   r;     s     c                    s   g | ]}|  r|qS r   r   )r,   Zactivator_clsr#   r   r   r;     s   
)
ValueError)ri   r    unknownZactivator_classesr   r#   r   search_activator  s    
rm   c                 C   s\   | d krt } t| |}|sBd}|r2|dkr2d| }td|| f |d }td||  |S )N /z in target=%szBNo available network activators found%s. Searched through list: %sr   z.Using selected activator: %s from priority: %s)rj   rm   r   r   r   )ri   r    foundZtmsgZselectedr   r   r   select_activator  s&    
  rq   )NN)+Zloggingabcr   r   typingr   r   r   r   r   r	   Z	cloudinitr
   r   Zcloudinit.net.enir   rG   Zcloudinit.net.netplanrV   rS   rL   Zcloudinit.net.network_stater   Zcloudinit.net.networkdr`   Z	getLoggerr   r   	Exceptionr   rC   r   r   rE   rK   rT   r_   rj   rh   rB   __annotations__rm   rq   r   r   r   r   <module>   sL     
>!I	 

   
 