o
    x[h¢"  ã                   @   s`   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 e¡ZG dd„ dƒZdS )é    N)ÚnetÚsubpÚutil)Úipv4_mask_to_net_prefixÚipv6_mask_to_net_prefixc                   @   sŒ   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„ 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   sR   || _ || _|| _i | _|r|  ¡  n| j D ]}|j| j|j ¡ < q|  ¡ | _	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Úname_serversÚdns_suffixesÚmac2NameÚfind_devicesÚnameÚmacÚlowerÚget_primary_nicÚ_primaryNic)Úselfr   r	   r
   Úuse_system_devicesÚnic© r   úQ/usr/lib/python3/dist-packages/cloudinit/sources/helpers/vmware/imc/config_nic.pyÚ__init__   s   


zNicConfigurator.__init__c                 C   s@   dd„ | j D ƒ}|sdS t|ƒdkrtddd„ |D ƒƒ‚|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©Ú.0r   r   r   r   Ú
<listcomp>6   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   <   s    r   )r   ÚlenÚRuntimeError)r   Úprimary_nicsr   r   r   r   1   s   þzNicConfigurator.get_primary_nicc           
      C   sz   g d¢}t   |¡\}}t dd| ¡dd… }d}|D ]}t ||¡}|s&q| d¡ ¡ }| dd¡d }	|	| j|< qdS )	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   r   r   r   r   A   s   úzNicConfigurator.find_devicesc              	   C   sŽ   |j  ¡ }| j |¡}|std|j  ƒ‚i }|  |¡|  |¡|  |¡|  |¡|  	|¡|  
|¡|  |¡|  ¡ g}|D ]	}|rB| |¡ q9||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Úvaluer   r   r   Úgen_one_nic_v2S   s&   
ø

€zNicConfigurator.gen_one_nic_v2c                 C   s   dd|iiS )Nr/   Ú
macaddressr   )r   r   r   r   r   r2   o   s   zNicConfigurator.gen_matchc                 C   s   d|iS )Nzset-namer   )r   r   r   r   r   r3   r   s   zNicConfigurator.gen_set_namec                 C   s
   d|j iS )NÚ	wakeonlan)Úonboot)r   r   r   r   r   r4   u   s   
zNicConfigurator.gen_wakeonlanc                 C   sj   i }|j  ¡ }|j ¡ dkrd}|dkr,| ddi¡ | js!| jr*| dddii¡ |S | ddi¡ |S )	NÚdisabledÚmanualÚstaticÚdhcp4Tzdhcp4-overrideszuse-dnsF)Ú	bootProtor   Ú	ipv4_moder:   r	   r
   )r   r   rE   Ú	bootprotor   r   r   r5   x   s   
ÿzNicConfigurator.gen_dhcp4c                 C   s   i }|j r| ddi¡ |S )NÚdhcp6F)Ú
staticIpv6r:   )r   r   rI   r   r   r   r6   †   s   zNicConfigurator.gen_dhcp6c           	      C   sŠ   g }d}|j }|r#|d }|jrt|jƒ}|jr#| |j› d|› �¡ |j}|r=|D ]}t|jƒ}| |j› d|› �¡ q*|rCd|iS i S )Né    r   ú/Ú	addresses)Ú
staticIpv4Únetmaskr   r    ÚappendrJ   r   )	r   r   Úaddress_listÚv4_cidrÚv4_addrsÚv4Úv6_addrsÚv6Úv6_cidrr   r   r   r7   Ž   s"   

zNicConfigurator.gen_addressesc                 C   s  g }d}|j }|rG|d }|jr|jr| d|jd dœ¡ | jsG|jrG|jr,t|jƒ}|jD ]}tj|› d|› �dd�}| |› |dœ¡ q/|j	}|r‚|D ]3}	t
|	jƒ}
|jrd|	jrd| d|	jdœ¡ | js�|	jr�tj|	j› d|
› �dd�}| |› |	jdœ¡ qN|rˆd	|iS i S )
NrK   r   z	0.0.0.0/0)ÚtoÚviarL   F)Ústrictz::/0Úroutes)rN   r   ÚgatewaysrP   r   rO   r   Ú	ipaddressÚIPv4NetworkrJ   r   ÚgatewayÚIPv6Network)r   r   Ú
route_listrR   rS   rT   r_   Ú	v4_subnetrU   rV   rW   Ú	v6_subnetr   r   r   r8   ¦   s@   

ÿ
ÿÿ€zNicConfigurator.gen_routesc                 C   sx   i }g }g }| j r| j D ]}| |¡ q| jr"| jD ]}| |¡ q|r+| d|i¡ |r4| d|i¡ |r:d|iS i S )Nr'   rM   Únameservers)r
   rP   r	   r:   )r   Únameservers_dictÚsearch_listÚaddresses_listÚ
dns_suffixÚname_serverr   r   r   r9   Ð   s    

zNicConfigurator.gen_nameserversFNc                 C   s<   |rt  d¡ |  |¡ i }| jD ]
}| |  |¡¡ q|S )z@Return the config elements that are needed to configure the nicszConfiguring the interfaces file)ÚloggerÚinfoÚ	configurer   r:   r>   )r   rl   ÚosfamilyÚethernets_dictr   r   r   r   Úgenerateä   s   


zNicConfigurator.generatec                 C   s   t  d¡ tjj ¡  d S )NzClearing DHCP leases)rj   rk   r   ÚdhcpÚIscDhclientÚclear_leases)r   r   r   r   Ú
clear_dhcpñ   s   
zNicConfigurator.clear_dhcpc                 C   s†   |r|dkrt  d¡ dS d}tj |d¡}tj |d¡}tj |¡s/tj |¡r/t ||¡ g d¢}tj|d |¡d	� |  	¡  dS )
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)
rj   rk   ÚosÚpathÚjoinÚexistsÚrenamer   Ú
write_filers   )r   rm   ÚcontainingDirÚinterfaceFileÚoriginalFileÚlinesr   r   r   rl   õ   s   
ÿzNicConfigurator.configure)T)FN)N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r>   r2   r3   r4   r5   r6   r7   r8   r9   ro   rs   rl   r   r   r   r   r      s"    
ÿ*
r   )r]   Úloggingrw   r%   Ú	cloudinitr   r   r   Úcloudinit.net.network_stater   r   Ú	getLoggerr�   rj   r   r   r   r   r   Ú<module>   s   

