
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me!                 @   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 j	e
Zdd ZG dd dZdS )    N)netsubputil)ipv4_mask_to_net_prefixc             C   s`   | j d}|j d}g }x2ttdD ]"}|jt|| t|| @  q&W djdd |D S )z
    Return the subnet for a given ip address and a netmask
    @return (str): the subnet
    @param ip: ip address
    @param netmask: netmask
    .   c             S   s   g | ]}t |qS  )str).0xr   r    /usr/lib/python3.6/config_nic.py
<listcomp>   s    zgen_subnet.<locals>.<listcomp>)splitlistrangeappendintjoin)ipnetmaskZip_arrayZ
mask_arrayresultindexr   r   r   
gen_subnet   s    

"r   c               @   sb   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dddZ
dd ZdddZdS )NicConfiguratorTc             C   sV   || _ i | _d| _d| _|r&| j  n"x | j D ]}|j| j|jj < q.W | j | _	dS )a  
        Initialize the Nic Configurator
        @param nics (list) an array of nics to configure
        @param use_system_devices (Bool) Get the MAC names from the system
        if this is True. If False, then mac names will be retrieved from
         the specified nics.
        N)
nicsmac2Nameipv4PrimaryGatewayZipv6PrimaryGatewayfind_devicesnamemaclowerget_primary_nic_primaryNic)selfr   Zuse_system_devicesnicr   r   r   __init__#   s    
zNicConfigurator.__init__c             C   sF   dd | j D }|sdS t|dkr:tddd |D n|d S dS )z|
        Retrieve the primary nic if it exists
        @return (NicBase): the primary nic if exists, None otherwise
        c             S   s   g | ]}|j r|qS r   )primary)r
   r$   r   r   r   r   =   s    z3NicConfigurator.get_primary_nic.<locals>.<listcomp>N   z!There can only be one primary nicc             S   s   g | ]
}|j qS r   )r   )r
   r$   r   r   r   r   C   s    r   )r   lenRuntimeError)r#   Zprimary_nicsr   r   r   r!   8   s    zNicConfigurator.get_primary_nicc       
      C   s   dddg}t j |\}}tjdd| dd }d}xF|D ]>}tj||}|sPq:|jdj }|jd	dd
 }	|	| j|< q:W dS )zb
        Create the mac2Name dictionary
        The mac address(es) are in the lower case
        r   addrZshowz\n\d+: 
r'   Nz3link/ether (([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})):r   )r   rer   searchgroupr    r   )
r#   cmdoutputZ_errZsectionsZmacPatZsectionmatchr   r   r   r   r   r   H   s    

zNicConfigurator.find_devicesc       
      C   s   |j j }| jj|}|s(td|j  g }d||d}g }g }| j||\}}	|j| |j|	 | j||\}}	|j| |j|	 |jd|i |j	| |r|j| |S )z
        Return the config list needed to configure a nic
        @return (list): the subnets and routes list to configure the nic
        @param nic (NicBase): the nic to configure
        zNo known device has MACADDR: %sZphysical)typer   Zmac_addresssubnets)
r   r    r   get
ValueErrorgen_ipv4extendgen_ipv6updater   )
r#   r$   r   r   nics_cfg_listZcfgsubnet_list
route_listr4   routesr   r   r   gen_one_nicZ   s&    






zNicConfigurator.gen_one_nicc             C   s  i }g }|j r|jddi |jj }|jj dkr8d}|dkrX|jddi |g|fS |jddi |j}|sz|g|fS |d }|jr|jd	|ji |jr|jd
|ji |jr|j	r|j	d | _
|jd| j
i |g|fS | j o|j	r|jd| j||j	|ji |g|fS )a+  
        Return the set of subnets and routes needed to configure the
        IPv4 settings of a nic
        @return (set): the set of subnet and routes to configure the gateways
        @param name (str): subnet and route list for the nic
        @param nic (NicBase): the nic to configure
        ZcontrolautoZdisabledZmanualZstaticr3   dhcpr   addressr   gatewayr>   )Zonbootr:   Z	bootProtor    Z	ipv4_modeZ
staticIpv4r   r   r&   gatewaysr   r"   gen_ipv4_route)r#   r   r$   subnetr=   Z	bootprotoaddrsZv4r   r   r   r7   ~   s6    	



zNicConfigurator.gen_ipv4c             C   sD   g }t |}x2|D ]*}dt|||f }|j|d|dd qW |S )z
        Return the routes list needed to configure additional Ipv4 route
        @return (list): the route list to configure the gateways
        @param nic (NicBase): the nic to configure
        @param gateways (str list): the list of gateways
        z%s/%dZroutei'  )destinationr3   rC   Zmetric)r   r   r   )r#   r$   rD   r   r=   ZcidrrC   rH   r   r   r   rE      s    
zNicConfigurator.gen_ipv4_routec             C   sL   |j sg g fS g }|j }x&|D ]}d|j|jd}|j| qW g }||fS )a  
        Return the set of subnets and routes needed to configure the
        gateways for a nic
        @return (set): the set of subnets and routes to configure the gateways
        @param name (str): name of the nic
        @param nic (NicBase): the nic to configure
        Zstatic6)r3   rB   r   )Z
staticIpv6r   r   r   )r#   r   r$   r<   rG   r*   rF   r=   r   r   r   r9      s    	

zNicConfigurator.gen_ipv6FNc             C   s@   |rt jd | j| g }x| jD ]}|j| j| q$W |S )z@Return the config elements that are needed to configure the nicszConfiguring the interfaces file)loggerinfo	configurer   r8   r?   )r#   rK   osfamilyr;   r$   r   r   r   generate   s    

zNicConfigurator.generatec             C   s   t jd tjjj  d S )NzClearing DHCP leases)rI   rJ   r   rA   ZIscDhclientZclear_leases)r#   r   r   r   
clear_dhcp   s    
zNicConfigurator.clear_dhcpc             C   s   | s|dkrt jd dS d}tjj|d}tjj|d}tjj| rbtjj|rbtj|| ddd	g}tj|d
j|d | j	  dS )z^
        Configure the /etc/network/interfaces
        Make a back up of the original
        Zdebianz3Debian OS not detected. Skipping the configure stepNz/etc/networkZ
interfacesz&interfaces.before_vmware_customizationzH# DO NOT EDIT THIS FILE BY HAND -- AUTOMATICALLY GENERATED BY cloud-initz"source /etc/network/interfaces.d/*z*source-directory /etc/network/interfaces.dr+   )Zcontent)
rI   rJ   ospathr   existsrenamer   Z
write_filerN   )r#   rL   ZcontainingDirZinterfaceFileZoriginalFilelinesr   r   r   rK      s    
zNicConfigurator.configure)T)FN)N)__name__
__module____qualname__r%   r!   r   r?   r7   rE   r9   rM   rN   rK   r   r   r   r   r   "   s   
$2!
r   )ZloggingrO   r-   Z	cloudinitr   r   r   Zcloudinit.net.network_stater   Z	getLoggerrT   rI   r   r   r   r   r   r   <module>   s   
