
    x[h"                         S SK r S SKrS SKrS SKrS SKJrJrJr  S SKJ	r	J
r
  \R                  " \5      r " S S5      rg)    N)netsubputil)ipv4_mask_to_net_prefixipv6_mask_to_net_prefixc                   |    \ rS rSr SS jrS rS rS rS rS r	S r
S	 rS
 rS rS rS rSS jrS rSS jrSrg)NicConfigurator   c                    Xl         X l        X0l        0 U l        U(       a  U R	                  5         ODU R                    H4  nUR
                  U R                  UR                  R                  5       '   M6     U R                  5       U l	        g)z
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__NicConfigurator.__init__   sh     	((yy14cggmmo. !  //1    c                     U R                    Vs/ s H  oR                  (       d  M  UPM     nnU(       d  g[        U5      S:  a&  [        SU Vs/ s H  oR                  PM     sn5      eUS   $ s  snf s  snf )zd
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   NicConfigurator.get_primary_nic1   sn    
 (,yy@yKKy@"3$01LSL1 
  ?" A 2s   A1A1A6
c                 N   / SQn[         R                   " U5      u  p#[        R                  " SSU-   5      SS nSnU Hd  n[        R                  " XV5      nU(       d  M"  UR	                  S5      R                  5       nUR                  SS5      S   n	XR                  U'   Mf     g)	zJ
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   NicConfigurator.find_devicesA   s    
 %yy~88Jv6qr:GGIIf.E++a.&&(C==a(+D!%MM#  r   c           	         UR                   R                  5       nU R                  R                  U5      nU(       d  [	        SUR                   -  5      e0 nU R                  U5      U R                  U5      U R                  U5      U R                  U5      U R                  U5      U R                  U5      U R                  U5      U R                  5       /nU H  nU(       d  M  UR                  U5        M     X40$ )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_v2NicConfigurator.gen_one_nic_v2S   s     ggmmo}}  %>HIINN3d#s#NN3NN3s#OOC   "	

  Eu&&u-   &&r   c                     SSU00$ )Nr4   
macaddress )r   r   s     r   r9   NicConfigurator.gen_matcho   s    ,,--r   c                 
    SU0$ )Nzset-namerI   )r   r   s     r   r:   NicConfigurator.gen_set_namer   s    D!!r   c                     SUR                   0$ )N	wakeonlan)onboot)r   r   s     r   r;   NicConfigurator.gen_wakeonlanu   s    SZZ((r   c                 H   0 nUR                   R                  5       nUR                  R                  5       S:X  a  SnUS:w  aL  UR                  SS05        U R                  (       d  U R
                  (       a  UR                  SSS005        U$ UR                  SS05        U$ )	Ndisabledmanualstaticdhcp4Tzdhcp4-overrideszuse-dnsF)	bootProtor   	ipv4_moderA   r   r   )r   r   rU   	bootprotos       r   r<   NicConfigurator.gen_dhcp4x   s    MM'')	== J. I LL'4)  D$5$5/)U1CDE  LL'5)*r   c                 R    0 nUR                   (       a  UR                  SS05        U$ )Ndhcp6F)
staticIpv6rA   )r   r   r[   s      r   r=   NicConfigurator.gen_dhcp6   s&    >>LL'5)* r   c                    / nSnUR                   nU(       a\  US   nUR                  (       a  [        UR                  5      nUR                  (       a   UR	                  UR                   SU 35        UR
                  nU(       a>  U H8  n[        UR                  5      nUR	                  UR                   SU 35        M:     U(       a  SU0$ 0 $ )N    r   /	addresses)
staticIpv4netmaskr   r%   appendr\   r   )	r   r   address_listv4_cidrv4_addrsv4v6_addrsv6v6_cidrs	            r   r>   NicConfigurator.gen_addresses   s     >>!Bzz1"**=uu##ruugQwi$89>>1"**=##ruugQwi$89  ..Ir   c                    / nSnUR                   nU(       a  US   nUR                  (       a2  UR                  (       a!  UR                  SUR                  S   S.5        U R                  (       dy  UR                  (       ah  UR
                  (       a  [        UR
                  5      nUR                   H2  n[        R                  " U SU 3SS9nUR                  U US.5        M4     UR                  nU(       a  U H  n	[        U	R
                  5      n
UR                  (       a/  U	R                  (       a  UR                  SU	R                  S.5        U R                  (       a  Mk  U	R                  (       d  M~  [        R                  " U	R                   SU
 3SS9nUR                  U U	R                  S.5        M     U(       a  S	U0$ 0 $ )
Nr_   r   z	0.0.0.0/0)toviar`   F)strictz::/0routes)rb   r   gatewaysrd   r   rc   r   	ipaddressIPv4Networkr\   r   gatewayIPv6Network)r   r   
route_listrf   rg   rh   ru   	v4_subnetri   rj   rk   	v6_subnets               r   r?   NicConfigurator.gen_routes   si   
 >>!B{{r{{!!R[[^"LM##::5bjjAG!{{G ) 5 5")1WI.u!I %%	{G&LM	  + >>1"**=;;2::%%VBJJ&GH'''BJJJ ) 5 5::,ay1%!I %%"+bjjA  j))Ir   c                 d   0 n/ n/ nU R                   (       a$  U R                    H  nUR                  U5        M     U R                  (       a$  U R                   H  nUR                  U5        M     U(       a  UR                  SU05        U(       a  UR                  SU05        U(       a  SU0$ 0 $ )Nr,   ra   nameservers)r   rd   r   rA   )r   nameservers_dictsearch_listaddresses_list
dns_suffixname_servers         r   r@   NicConfigurator.gen_nameservers   s    "//
"":. 0#00%%k2  1##X{$;<##[.$AB!#344Ir   Nc                     U(       a&  [         R                  S5        U R                  U5        0 nU R                   H#  nUR	                  U R                  U5      5        M%     U$ )z@Return the config elements that are needed to configure the nicszConfiguring the interfaces file)loggerinfo	configurer   rA   rE   )r   r   osfamilyethernets_dictr   s        r   generateNicConfigurator.generate   sR    KK9:NN8$99C!!$"5"5c":;  r   c                 ~    [         R                  S5        [        R                  R                  R                  5         g )NzClearing DHCP leases)r   r   r   dhcpIscDhclientclear_leases)r   s    r   
clear_dhcpNicConfigurator.clear_dhcp   s%    *+))+r   c                    U(       a  US:w  a  [         R                  S5        gSn[        R                  R	                  US5      n[        R                  R	                  US5      n[        R                  R                  U5      (       d:  [        R                  R                  U5      (       a  [        R                  " X45        / SQn[        R                  " USR	                  U5      S	9  U R                  5         g)
zF
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)
r   r   ospathjoinexistsrenamer   
write_filer   )r   r   containingDirinterfaceFileoriginalFileliness         r   r   NicConfigurator.configure   s     8x/KKMN&]LAww||C
 ww~~l++}0M0MIIm2
 	tyy/?@r   )r   r   r   r   r   )T)FN)N)__name__
__module____qualname____firstlineno__r   r   r   rE   r9   r:   r;   r<   r=   r>   r?   r@   r   r   r   __static_attributes__rI   r   r   r	   r	      sQ    CG2.# &$'8.")0(T(,r   r	   )rs   loggingr   r*   	cloudinitr   r   r   cloudinit.net.network_stater   r   	getLoggerr   r   r	   rI   r   r   <module>r      s<      	 	 % %
 
		8	$y yr   