3 \me!@s\ddlZddlZddlZddlmZmZmZddlmZej e Z ddZ GdddZ dS)N)netsubputil)ipv4_mask_to_net_prefixcCs`|jd}|jd}g}x2ttdD]"}|jt||t||@q&Wdjdd|DS)z Return the subnet for a given ip address and a netmask @return (str): the subnet @param ip: ip address @param netmask: netmask .cSsg|] }t|qS)str).0xrr /usr/lib/python3.6/config_nic.py szgen_subnet..)splitlistrangeappendintjoin)ipnetmaskZip_arrayZ mask_arrayresultindexrrr gen_subnets   "rc@sbeZdZdddZddZddZdd Zd d Zd d ZddZ dddZ ddZ dddZ dS)NicConfiguratorTcCsV||_i|_d|_d|_|r&|jn"x |jD]}|j|j|jj<q.W|j|_ dS)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) nicsmac2Nameipv4PrimaryGatewayZipv6PrimaryGateway find_devicesnamemaclowerget_primary_nic _primaryNic)selfrZuse_system_devicesnicrrr __init__#s  zNicConfigurator.__init__cCsFdd|jD}|sdSt|dkr:tddd|Dn|dSdS)z| Retrieve the primary nic if it exists @return (NicBase): the primary nic if exists, None otherwise cSsg|]}|jr|qSr)primary)r r$rrr r =sz3NicConfigurator.get_primary_nic..Nz!There can only be one primary niccSsg|] }|jqSr)r)r r$rrr r Csr)rlen RuntimeError)r#Z primary_nicsrrr r!8s zNicConfigurator.get_primary_nicc Csdddg}tj|\}}tjdd|dd}d}xF|D]>}tj||}|sPq:|jdj}|jd dd } | |j|<q:WdS) zb Create the mac2Name dictionary The mac address(es) are in the lower case raddrZshowz\n\d+:  r'Nz3link/ether (([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})):r)rrersearchgroupr r) r#cmdoutputZ_errZsectionsZmacPatZsectionmatchrrrrr rHs   zNicConfigurator.find_devicesc Cs|jj}|jj|}|s(td|jg}d||d}g}g}|j||\}} |j||j| |j||\}} |j||j| |jd|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 zNo known device has MACADDR: %sZphysical)typerZ mac_addresssubnets) rr rget ValueErrorgen_ipv4extendgen_ipv6updater) r#r$rr nics_cfg_listZcfg subnet_list route_listr4routesrrr gen_one_nicZs&         zNicConfigurator.gen_one_niccCsi}g}|jr|jddi|jj}|jjdkr8d}|dkrX|jddi|g|fS|jddi|j}|sz|g|fS|d}|jr|jd |ji|jr|jd |ji|jr|j r|j d|_ |jd |j i|g|fS|j o|j r |jd |j ||j |ji|g|fS) 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 ZcontrolautoZdisabledZmanualZstaticr3dhcpraddressrgatewayr>) Zonbootr:Z bootProtor Z ipv4_modeZ staticIpv4rrr&gatewaysrr"gen_ipv4_route)r#rr$subnetr=Z bootprotoaddrsZv4rrr r7~s6       zNicConfigurator.gen_ipv4cCsDg}t|}x2|D]*}dt|||f}|j|d|ddqW|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/%dZroutei') destinationr3rCZmetric)rrr)r#r$rDrr=ZcidrrCrHrrr rEs zNicConfigurator.gen_ipv4_routecCsL|jsggfSg}|j}x&|D]}d|j|jd}|j|qWg}||fS)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 Zstatic6)r3rBr)Z staticIpv6rrr)r#rr$r<rGr*rFr=rrr r9s   zNicConfigurator.gen_ipv6FNcCs@|rtjd|j|g}x|jD]}|j|j|q$W|S)z@Return the config elements that are needed to configure the nicszConfiguring the interfaces file)loggerinfo configurerr8r?)r#rKosfamilyr;r$rrr generates   zNicConfigurator.generatecCstjdtjjjdS)NzClearing DHCP leases)rIrJrrAZ IscDhclientZ clear_leases)r#rrr clear_dhcps zNicConfigurator.clear_dhcpcCs| s|dkrtjddSd}tjj|d}tjj|d}tjj| rbtjj|rbtj||ddd g}tj|d j|d |j dS) z^ Configure the /etc/network/interfaces Make a back up of the original Zdebianz3Debian 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+)Zcontent) rIrJospathrexistsrenamerZ write_filerN)r#rLZ containingDirZ interfaceFileZ originalFilelinesrrr rKs  zNicConfigurator.configure)T)FN)N) __name__ __module__ __qualname__r%r!rr?r7rEr9rMrNrKrrrr r"s $2! r)ZloggingrOr-Z cloudinitrrrZcloudinit.net.network_staterZ getLoggerrTrIrrrrrr s