o
    w7e+                     @   s   d dl Z d dlZd dlZd dlmZmZmZ d dlmZ e	e
ZeZeZG dd de jdZG dd deZG d	d
 d
eZG dd deZdS )    N)netsubputil)ifconfigc                   @   s  e Zd ZdZdefddZdddeddfdd	Zej	d
e
ddfddZdefddZdefddZd
e
defddZdd ZdddefddZdefddZdefddZdedefd d!Zdefd"d#Zdefd$d%Zdefd&d'Zdefd(d)Zdd*ded+edefd,d-Zdedefd.d/Zdedefd0d1Zej	dedefd2d3Zdedefd4d5Zdedefd6d7Zdedefd8d9Z dedefd:d;Z!ej	dd<dDd=d>Z"d?d*d
e
d+eddfd@dAZ#ej	dedefdBdCZ$dS )E
Networkinga  The 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 CONTRIBUTING.rst for full details.
    returnc                 C      t  S N)r   _get_current_rename_infoself r   >/usr/lib/python3/dist-packages/cloudinit/distros/networking.pyr
         z#Networking._get_current_rename_infoNcurrent_inforenamesc                C      t j||dS )Nr   )r   _rename_interfaces)r   r   r   r   r   r   r         zNetworking._rename_interfacesnetcfgc                 C      dS )z7Read the network config and rename devices accordingly.Nr   r   r   r   r   r   apply_network_config_names       z%Networking.apply_network_config_namesdevnamec                 C   
   t |S r	   )r   device_devidr   r   r   r   r   r   #      
zNetworking.device_devidc                 C   r   r	   )r   device_driverr   r   r   r   r    &   r   zNetworking.device_driverc                 C   r   r	   )r   extract_physdevsr   r   r   r   r!   )   r   zNetworking.extract_physdevsc                 C   r   r	   )r   find_fallback_nicr   r   r   r   r"   ,   r   zNetworking.find_fallback_nicFconfig_driverr$   c                C   s   t j|dS )Nr#   )r   generate_fallback_config)r   r$   r   r   r   r%   /      z#Networking.generate_fallback_configc                 C   r   r	   )r   get_devicelistr   r   r   r   r'   2   r   zNetworking.get_devicelistc                 C   r   r	   )r   get_ib_hwaddrs_by_interfacer   r   r   r   r(   5   r   z&Networking.get_ib_hwaddrs_by_interfaceethernet_formatc                 C   s   t ||S r	   )r   get_ib_interface_hwaddr)r   r   r)   r   r   r   r*   8   s   z"Networking.get_ib_interface_hwaddrc                 C   r   r	   )r   get_interface_macr   r   r   r   r+   =   r   zNetworking.get_interface_macc                 C   r   r	   )r   get_interfacesr   r   r   r   r,   @   r   zNetworking.get_interfacesc                 C   r   r	   )r   get_interfaces_by_macr   r   r   r   r-   C   r   z Networking.get_interfaces_by_macc                 C   r   r	   )r   
get_masterr   r   r   r   r.   F   r   zNetworking.get_masterstrictr0   c                C   r   )Nr/   )r   interface_has_own_mac)r   r   r0   r   r   r   r1   I   s   z Networking.interface_has_own_macc                 C   r   r	   )r   is_bondr   r   r   r   r2   N   r   zNetworking.is_bondc                 C   r   r	   )r   	is_bridger   r   r   r   r3   Q   r   zNetworking.is_bridgec                 C   r   )z
        Is ``devname`` a physical network device?

        Examples of non-physical network devices: bonds, bridges, tunnels,
        loopback devices.
        Nr   r   r   r   r   is_physicalT   r   zNetworking.is_physicalc                 C   r   r	   )r   
is_renamedr   r   r   r   r5   ]   r   zNetworking.is_renamedc                 C   r   r	   )r   is_upr   r   r   r   r6   `   r   zNetworking.is_upc                 C   r   r	   )r   is_vlanr   r   r   r   r7   c   r   zNetworking.is_vlanc                 C   r   r	   )r   master_is_bridge_or_bondr   r   r   r   r8   f   r   z#Networking.master_is_bridge_or_bondexistsc                C   r   )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   settlei   r   zNetworking.settleTc                C   s   |  |}tdd |D }t| }|   }tddD ];}||r.td  dS |	|}td| |D ]}	||	 }
d
|
}tjtj|| jd	|
id
 q;|   }qd| }t| |rjt|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   ).0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 {} existsr:   )funckwargsz-Not all expected physical devices present: %s)r!   dictsetkeysr-   rangeissubsetLOGdebug
differenceformatr   log_timer<   warningRuntimeError)r   r   r0   physdevsexpected_ifacesexpected_macspresent_macs_missingmacr   msgr   r   r   wait_for_physdevsu   s2   





zNetworking.wait_for_physdevsc                 C   r   )z=Try setting the link to up explicitly and return if it is up.Nr   r   r   r   r   try_set_link_up   r   zNetworking.try_set_link_upr   N)%__name__
__module____qualname____doc__rD   r
   listr   abcabstractmethodNetworkConfigr   
DeviceNamer   r    r!   r"   boolr%   r'   r(   r*   r+   r,   r-   r.   r1   r2   r3   r4   r5   r6   r7   r8   r<   rX   rY   r   r   r   r   r      sd    


6r   )	metaclassc                       s   e Zd ZdZ fddZdd Zdeddfd	d
Zdede	fddZ
dede	fddZdede	fddZdede	fddZdede	fddZdddddZdede	fddZ  ZS )BSDNetworkingz>Implementation of networking functionality shared across BSDs.c                    s&   t  | _i | _|   t   d S r	   )r   Ifconfigifcifs_update_ifssuper__init__r   	__class__r   r   rl      s   
zBSDNetworking.__init__c                 C   s0   t  ddg}|d r| j|d | _d S d S )Nr   z-ar   )r   rh   parseri   )r   ifconfr   r   r   rj      s   zBSDNetworking._update_ifsr   r   Nc                 C   s   t d d S )Nz Cannot rename network interface.)rI   rJ   r   r   r   r   r      r   z(BSDNetworking.apply_network_config_namesr   c                 C      | j | jS r	   )ri   r4   r   r   r   r   r4      r&   zBSDNetworking.is_physicalc                 C   rq   r	   )ri   r2   r   r   r   r   r2      r&   zBSDNetworking.is_bondc                 C   rq   r	   )ri   r3   r   r   r   r   r3      r&   zBSDNetworking.is_bridgec                 C   rq   r	   )ri   r7   r   r   r   r   r7      r&   zBSDNetworking.is_vlanc                 C   rq   r	   )ri   upr   r   r   r   r6      r&   zBSDNetworking.is_upr9   c                C   r   )z0BSD has no equivalent to `udevadm settle`; noop.Nr   r;   r   r   r   r<      r   zBSDNetworking.settlec                 C   s   t  d|dg | |S )Try 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.r   rr   r   r6   r   r   r   r   rY      s   
zBSDNetworking.try_set_link_uprZ   )r[   r\   r]   r^   rl   rj   rb   r   rc   rd   r4   r2   r3   r7   r6   r<   rY   __classcell__r   r   rm   r   rf      s    rf   c                   @   s0   e Zd ZdeddfddZdedefddZdS )	FreeBSDNetworkingr   r   Nc                 C   s   d S r	   r   r   r   r   r   r      s   z,FreeBSDNetworking.apply_network_config_namesr   c                 C   sB   | j | jsdS tjdd|gddgd\}}|d|krdS dS )	NFdevinfoz-pr   r=   )rcszdevinfo: {}: Not found
T)ri   r4   r   rL   )r   r   rT   errr   r   r   r5      s   zFreeBSDNetworking.is_renamed)r[   r\   r]   rb   r   rc   rd   r5   r   r   r   r   rv      s    rv   c                   @   s   e Zd ZdZdeddfddZdedefdd	Zdede	fd
dZ
dede	fddZdede	fddZdede	fddZdede	fddZdede	fddZdddddZdede	fddZdS )LinuxNetworkingzCImplementation of networking functionality common to Linux distros.r   r   Nc              
   C   s>   z|  | | W dS  ty } ztd| |d}~ww )zRead the network config and rename devices accordingly.

        Renames are only attempted for interfaces of type 'physical'. It is
        expected that the network system will create other devices with the
        correct name in place.
        z(Failed to apply network config names: %sN)r   r!   rO   )r   r   er   r   r   r     s   z*LinuxNetworking.apply_network_config_namesr   c                 C   r   r	   )r   get_dev_featuresr   r   r   r   r|     r   z LinuxNetworking.get_dev_featuresc                 C   r   r	   )r   has_netfail_standby_featurer   r   r   r   r}     r   z+LinuxNetworking.has_netfail_standby_featurec                 C   r   r	   )r   is_netfailoverr   r   r   r   r~     r   zLinuxNetworking.is_netfailoverc                 C   r   r	   )r   is_netfail_masterr   r   r   r   r     r   z!LinuxNetworking.is_netfail_masterc                 C   r   r	   )r   is_netfail_primaryr   r   r   r   r     r   z"LinuxNetworking.is_netfail_primaryc                 C   r   r	   )r   is_netfail_standbyr   r   r   r   r     r   z"LinuxNetworking.is_netfail_standbyc                 C   s   t jt|dS )Ndevice)ospathr:   r   sys_dev_pathr   r   r   r   r4   "  s   zLinuxNetworking.is_physicalr9   c                C   s"   |d ur	t |}tj|d d S )Nr9   )r   r   r   udevadm_settler;   r   r   r   r<   %  s   
zLinuxNetworking.settlec                 C   s   t  ddd|dg | |S )rs   iplinkrE   rr   rt   r   r   r   r   rY   *  s   
zLinuxNetworking.try_set_link_uprZ   )r[   r\   r]   r^   rb   r   rc   strr|   rd   r}   r~   r   r   r   r4   r<   rY   r   r   r   r   rz      s    rz   )r`   loggingr   	cloudinitr   r   r   cloudinit.distros.parsersr   	getLoggerr[   rI   r   rc   rD   rb   ABCMetar   rf   rv   rz   r   r   r   r   <module>   s    
  -!