
    x[h"                         d dl Z 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	m
Z
  ej                  e      Z G d d      Zy)    N)netsubputil)ipv4_mask_to_net_prefixipv6_mask_to_net_prefixc                   n    e Zd Z	 ddZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZd ZddZy)NicConfiguratorc                    || _         || _        || _        i | _        |r| j	                          nB| j                   D ]3  }|j
                  | j                  |j                  j                         <   5 | j                         | _	        y)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name_serversdns_suffixesmac2Namefind_devicesnamemaclowerget_primary_nic_primaryNic)selfr   r   r   use_system_devicesnics         Q/usr/lib/python3/dist-packages/cloudinit/sources/helpers/vmware/imc/config_nic.py__init__zNicConfigurator.__init__   ss     	((yy :14cggmmo.:  //1    c                     | j                   D cg c]  }|j                  s| }}|syt        |      dkD  r$t        d|D cg c]  }|j                   c}      |d   S c c}w c c}w )z|
        Retrieve the primary nic if it exists
        @return (NicBase): the primary nic if exists, None otherwise
        N   z!There can only be one primary nicr   )r   primarylenRuntimeErrorr   )r   r   primary_nicss      r   r   zNicConfigurator.get_primary_nic1   sm    
 (,yy@CKK@@"3$01S1 
  ?" A 2s   A!A!A&
c                 >   g d}t        j                   |      \  }}t        j                  dd|z         dd }d}|D ]^  }t        j                  ||      }|s|j	                  d      j                         }|j                  dd      d   }	|	| j                  |<   ` y)	zb
        Create the mac2Name dictionary
        The mac address(es) are in the lower case
        )ipaddrshowz\n\d+: 
r   Nz3link/ether (([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})):r   )r   resplitsearchgroupr   r   )
r   cmdoutput_errsectionsmacPatsectionmatchr   r   s
             r   r   zNicConfigurator.find_devicesA   s    
 %yy~88Jv6qr:G 	&GIIfg.E++a.&&(C==a(+D!%DMM#	&r   c           	         |j                   j                         }| j                  j                  |      }|st	        d|j                   z        i }| j                  |      | j                  |      | j                  |      | j                  |      | j                  |      | j                  |      | j                  |      | j                         g}|D ]  }|s|j                  |        ||iS )z
        Return the config dict needed to configure a nic
        @return (dict): the config dict to configure the nic
        @param nic (NicBase): the nic to configure
        zNo known device has MACADDR: %s)r   r   r   get
ValueError	gen_matchgen_set_namegen_wakeonlan	gen_dhcp4	gen_dhcp6gen_addresses
gen_routesgen_nameserversupdate)r   r   r   r   nic_config_dict
generatorsvalues          r   gen_one_nic_v2zNicConfigurator.gen_one_nic_v2S   s     ggmmo}}  %>HIINN3d#s#NN3NN3s#OOC   "	

   	.E&&u-	. o&&r   c                     dd|iiS )Nr1   
macaddress )r   r   s     r   r5   zNicConfigurator.gen_matcho   s    ,,--r   c                 
    d|iS )Nzset-namerD   )r   r   s     r   r6   zNicConfigurator.gen_set_namer   s    D!!r   c                     d|j                   iS )N	wakeonlan)onboot)r   r   s     r   r7   zNicConfigurator.gen_wakeonlanu   s    SZZ((r   c                 0   i }|j                   j                         }|j                  j                         dk(  rd}|dk7  rB|j                  ddi       | j                  s| j
                  r|j                  dddii       |S |j                  ddi       |S )	Ndisabledmanualstaticdhcp4Tzdhcp4-overrideszuse-dnsF)	bootProtor   	ipv4_moder=   r   r   )r   r   rM   	bootprotos       r   r8   zNicConfigurator.gen_dhcp4x   s    MM'')	== J. I LL'4)  D$5$5/)U1CDE  LL'5)*r   c                 H    i }|j                   r|j                  ddi       |S )Ndhcp6F)
staticIpv6r=   )r   r   rR   s      r   r9   zNicConfigurator.gen_dhcp6   s&    >>LL'5)* r   c                 n   g }d}|j                   }|rR|d   }|j                  rt        |j                        }|j                  r |j	                  |j                   d|        |j
                  }|r<|D ]7  }t        |j                        }|j	                  |j                   d|        9 |rd|iS i S )N    r   /	addresses)
staticIpv4netmaskr   r"   appendrS   r   )	r   r   address_listv4_cidrv4_addrsv4v6_addrsv6v6_cidrs	            r   r:   zNicConfigurator.gen_addresses   s     >>!Bzz1"**=uu##ruugQwi$89>> :1"**=##ruugQwi$89: ..Ir   c                 &   g }d}|j                   }|r|d   }|j                  r-|j                  r!|j                  d|j                  d   d       | j                  so|j                  rc|j
                  rt        |j
                        }|j                  D ]3  }t        j                  | d| d      }|j                  | |d       5 |j                  }|r|D ]  }	t        |	j
                        }
|j                  r*|	j                  r|j                  d|	j                  d       | j                  r[|	j                  sht        j                  |	j                   d|
 d      }|j                  | |	j                  d        |rd	|iS i S )
NrU   r   z	0.0.0.0/0)toviarV   F)strictz::/0routes)rX   r   gatewaysrZ   r   rY   r   	ipaddressIPv4NetworkrS   r   gatewayIPv6Network)r   r   
route_listr\   r]   r^   rj   	v4_subnetr_   r`   ra   	v6_subnets               r   r;   zNicConfigurator.gen_routes   sp   
 >>!B{{r{{!!R[[^"LM##::5bjjAG!{{ NG ) 5 5")1WI.u!I %%	{G&LM	N >> 1"**=;;2::%%VBJJ&GH''BJJ ) 5 5::,ay1%!I %%"+bjjA j))Ir   c                 *   i }g }g }| j                   r"| j                   D ]  }|j                  |        | j                  r"| j                  D ]  }|j                  |        |r|j                  d|i       |r|j                  d|i       |rd|iS i S )Nr)   rW   nameservers)r   rZ   r   r=   )r   nameservers_dictsearch_listaddresses_list
dns_suffixname_servers         r   r<   zNicConfigurator.gen_nameservers   s    "// /
"":./#00 3%%k23##X{$;<##[.$AB!#344Ir   Nc                     |r&t         j                  d       | j                  |       i }| j                  D ]"  }|j	                  | j                  |             $ |S )z@Return the config elements that are needed to configure the nicszConfiguring the interfaces file)loggerinfo	configurer   r=   rA   )r   ry   osfamilyethernets_dictr   s        r   generatezNicConfigurator.generate   sW    KK9:NN8$99 	<C!!$"5"5c":;	< r   c                 ~    t         j                  d       t        j                  j                  j                          y )NzClearing DHCP leases)rw   rx   r   dhcpIscDhclientclear_leases)r   s    r   
clear_dhcpzNicConfigurator.clear_dhcp   s%    *+))+r   c                    |r|dk7  rt         j                  d       yd}t        j                  j	                  |d      }t        j                  j	                  |d      }t        j                  j                  |      s5t        j                  j                  |      rt        j                  ||       g d}t        j                  |dj	                  |      	       | j                          y)
z^
        Configure the /etc/network/interfaces
        Make a back up of the original
        debianz3Debian OS not detected. Skipping the configure stepNz/etc/network
interfacesz&interfaces.before_vmware_customization)zH# DO NOT EDIT THIS FILE BY HAND -- AUTOMATICALLY GENERATED BY cloud-initz"source /etc/network/interfaces.d/*z*source-directory /etc/network/interfaces.dr%   )content)
rw   rx   ospathjoinexistsrenamer   
write_filer   )r   rz   containingDirinterfaceFileoriginalFileliness         r   ry   zNicConfigurator.configure   s     8x/KKMN&]LAww||C
 ww~~l+}0MIIm\2
 	tyy/?@r   )T)FN)N)__name__
__module____qualname__r   r   r   rA   r5   r6   r7   r8   r9   r:   r;   r<   r|   r   ry   rD   r   r   r	   r	      sQ    CG2.# &$'8.")0(T(,r   r	   )rh   loggingr   r'   	cloudinitr   r   r   cloudinit.net.network_stater   r   	getLoggerr   rw   r	   rD   r   r   <module>r      s>      	 	 % %
 
		8	$y yr   