
"T`                 @   s   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z m Z e j e  Z	 e
 Z e Z Gd d   d d e  j Z Gd d   d e  Z Gd	 d
   d
 e  Z d S)    N)subp)netutilc               @   s  e  Z d  Z d Z d d   Z e d d d    Z d d e d d	 d
 d   Z e	 d d d d    Z
 e d d d    Z e d d d    Z e	 e d d d    Z d d d d  Z d d d d e d d d   Z e d d d    Z e d d  d!    Z e e d" d# d$    Z e d d% d&    Z e d d' d(    Z e d d) d*    Z e d d+ d,    Z d- d e e e d. d/ d0   Z e e d1 d2 d3    Z e e d1 d4 d5    Z e j e e d1 d6 d7     Z e e d1 d8 d9    Z e e d1 d: d;    Z  e e d1 d< d=    Z! e e d1 d> d?    Z" e j d@ d d d dA dB    Z# d- dC e	 e d dD dE dF   Z$ e j e e d1 dG dH     Z% d S)I
NetworkingzThe root of the Networking hierarchy in cloud-init.

    This is part of an ongoing refactor in the cloud-init codebase, for more
    details see "``cloudinit.net`` -> ``cloudinit.distros.networking``
    Hierarchy" in HACKING.rst for full details.
    c             C   s   d  |  _  d  S)N)blacklist_drivers)self r   >/usr/lib/python3/dist-packages/cloudinit/distros/networking.py__init__   s    zNetworking.__init__)returnc             C   s
   t  j   S)N)r   _get_current_rename_info)r   r   r   r	   r      s    z#Networking._get_current_rename_infocurrent_infoN)renamesr   c            C   s   t  j | d | S)Nr   )r   _rename_interfaces)r   r   r   r   r   r	   r       s    zNetworking._rename_interfaces)netcfgr   c             C   s   t  j |  S)N)r   apply_network_config_names)r   r   r   r   r	   r   #   s    z%Networking.apply_network_config_names)devnamec             C   s   t  j |  S)N)r   device_devid)r   r   r   r   r	   r   &   s    zNetworking.device_devidc             C   s   t  j |  S)N)r   device_driver)r   r   r   r   r	   r   )   s    zNetworking.device_driverc             C   s   t  j |  S)N)r   extract_physdevs)r   r   r   r   r	   r   ,   s    zNetworking.extract_physdevsr   c            C   s   t  j d |  S)Nr   )r   find_fallback_nic)r   r   r   r   r	   r   /   s    zNetworking.find_fallback_nicconfig_driverF)r   c            C   s   t  j d | d |  S)Nr   r   )r   generate_fallback_config)r   r   r   r   r   r	   r   2   s    	z#Networking.generate_fallback_configc             C   s
   t  j   S)N)r   get_devicelist)r   r   r   r	   r   9   s    zNetworking.get_devicelistc             C   s
   t  j   S)N)r   get_ib_hwaddrs_by_interface)r   r   r   r	   r   <   s    z&Networking.get_ib_hwaddrs_by_interface)r   ethernet_formatc             C   s   t  j | |  S)N)r   get_ib_interface_hwaddr)r   r   r   r   r   r	   r   ?   s    z"Networking.get_ib_interface_hwaddrc             C   s   t  j |  S)N)r   get_interface_mac)r   r   r   r   r	   r   D   s    zNetworking.get_interface_macc             C   s
   t  j   S)N)r   get_interfaces)r   r   r   r	   r   G   s    zNetworking.get_interfacesc             C   s   t  j d |  j  S)Nr   )r   get_interfaces_by_macr   )r   r   r   r	   r   J   s    	z Networking.get_interfaces_by_macc             C   s   t  j |  S)N)r   
get_master)r   r   r   r   r	   r    N   s    zNetworking.get_masterstrict)r   r!   r   c            C   s   t  j | d | S)Nr!   )r   interface_has_own_mac)r   r   r!   r   r   r	   r"   Q   s    z Networking.interface_has_own_mac)r   r   c             C   s   t  j |  S)N)r   is_bond)r   r   r   r   r	   r#   V   s    zNetworking.is_bondc             C   s   t  j |  S)N)r   	is_bridge)r   r   r   r   r	   r$   Y   s    zNetworking.is_bridgec             C   s   d S)z
        Is ``devname`` a physical network device?

        Examples of non-physical network devices: bonds, bridges, tunnels,
        loopback devices.
        Nr   )r   r   r   r   r	   is_physical\   s    zNetworking.is_physicalc             C   s   t  j |  S)N)r   
is_renamed)r   r   r   r   r	   r&   e   s    zNetworking.is_renamedc             C   s   t  j |  S)N)r   is_up)r   r   r   r   r	   r'   h   s    zNetworking.is_upc             C   s   t  j |  S)N)r   is_vlan)r   r   r   r   r	   r(   k   s    zNetworking.is_vlanc             C   s   t  j |  S)N)r   master_is_bridge_or_bond)r   r   r   r   r	   r)   n   s    z#Networking.master_is_bridge_or_bondexistsc            C   s   d S)a  Wait for device population in the system to complete.

        :param exists:
            An optional optimisation.  If given, only perform as much of the
            settle process as is required for the given DeviceName to be
            present in the system.  (This may include skipping the settle
            process entirely, if the device already exists.)
        :type exists: Optional[DeviceName]
        Nr   )r   r*   r   r   r	   settleq   s    zNetworking.settleT)r   r!   r   c         
   C   s6  |  j  |  } t d d   | D  } t | j    } |  j   j   } x t d d  D] } | j |  r t j d  d S| j	 |  } t j d |  xO | D]G }	 | |	 }
 d j
 |
  } t j t j | d	 |  j d
 d |
 i q W|  j   j   } q\ Wd | } t j |  | r2t |   d S)a  Wait for all the physical devices in `netcfg` to exist on the system

        Specifically, this will call `self.settle` 5 times, and check after
        each one if the physical devices are now present in the system.

        :param netcfg:
            The NetworkConfig from which to extract physical devices to wait
            for.
        :param strict:
            Raise a `RuntimeError` if any physical devices are not present
            after waiting.
        c             S   s$   g  |  ] } | d  | d f  q S)r      r   ).0Zifacer   r   r	   
<listcomp>   s   	 z0Networking.wait_for_physdevs.<locals>.<listcomp>r      z*net: all expected physical devices presentNz)net: waiting for expected net devices: %szWaiting for settle or {} existsfunckwargsr*   z-Not all expected physical devices present: %s)r   dictsetkeysr   rangeissubsetLOGdebug
differenceformatr   Zlog_timer+   ZwarningRuntimeError)r   r   r!   ZphysdevsZexpected_ifacesZexpected_macsZpresent_macs_ZmissingZmacr   msgr   r   r	   wait_for_physdevs}   s.    
	
zNetworking.wait_for_physdevsc             C   s   d S)z=Try setting the link to up explicitly and return if it is up.Nr   )r   r   r   r   r	   try_set_link_up   s    zNetworking.try_set_link_up)&__name__
__module____qualname____doc__r
   r2   r   listr   NetworkConfigr   
DeviceNamer   r   r   r   boolr   r   r   r   r   r   r   r    r"   r#   r$   abcabstractmethodr%   r&   r'   r(   r)   r+   r>   r?   r   r   r   r	   r      sH   			4r   	metaclassc               @   sa   e  Z d  Z d Z e e d d d    Z d d d d d d	   Z e e d d
 d    Z d S)BSDNetworkingz>Implementation of networking functionality shared across BSDs.)r   r   c             C   s   t     d  S)N)NotImplementedError)r   r   r   r   r	   r%      s    zBSDNetworking.is_physicalr*   N)r   c            C   s   d S)z0BSD has no equivalent to `udevadm settle`; noop.Nr   )r   r*   r   r   r	   r+      s    zBSDNetworking.settlec             C   s   t     d  S)N)rL   )r   r   r   r   r	   r?      s    zBSDNetworking.try_set_link_up)	r@   rA   rB   rC   rF   rG   r%   r+   r?   r   r   r   r	   rK      s   rK   c               @   s   e  Z d  Z d Z e e d d d    Z e e d d d    Z e e d d d    Z	 e e d d	 d
    Z
 e e d d d    Z e e d d d    Z e e d d d    Z d d d d d d   Z e e d d d    Z d S)LinuxNetworkingzCImplementation of networking functionality common to Linux distros.)r   r   c             C   s   t  j |  S)N)r   get_dev_features)r   r   r   r   r	   rN      s    z LinuxNetworking.get_dev_featuresc             C   s   t  j |  S)N)r   has_netfail_standby_feature)r   r   r   r   r	   rO      s    z+LinuxNetworking.has_netfail_standby_featurec             C   s   t  j |  S)N)r   is_netfailover)r   r   r   r   r	   rP      s    zLinuxNetworking.is_netfailoverc             C   s   t  j |  S)N)r   is_netfail_master)r   r   r   r   r	   rQ      s    z!LinuxNetworking.is_netfail_masterc             C   s   t  j |  S)N)r   is_netfail_primary)r   r   r   r   r	   rR      s    z"LinuxNetworking.is_netfail_primaryc             C   s   t  j |  S)N)r   is_netfail_standby)r   r   r   r   r	   rS      s    z"LinuxNetworking.is_netfail_standbyc             C   s   t  j j t j | d   S)NZdevice)ospathr*   r   sys_dev_path)r   r   r   r   r	   r%      s    zLinuxNetworking.is_physicalr*   N)r   c            C   s/   | d  k	 r t  j |  } t j d |  d  S)Nr*   )r   rV   r   Zudevadm_settle)r   r*   r   r   r	   r+      s    zLinuxNetworking.settlec             C   s)   t  j  d d d | d g  |  j |  S)zTry setting the link to up explicitly and return if it is up.
           Not guaranteed to bring the interface up. The caller is expected to
           add wait times before retrying.Ziplinkr3   Zup)r   r'   )r   r   r   r   r	   r?      s    zLinuxNetworking.try_set_link_up)r@   rA   rB   rC   rF   strrN   rG   rO   rP   rQ   rR   rS   r%   r+   r?   r   r   r   r	   rM      s   rM   )rH   ZloggingrT   Z	cloudinitr   r   r   Z	getLoggerr@   r7   rX   rF   r2   rE   ABCMetar   rK   rM   r   r   r   r	   <module>   s   