܍~c"zddlZddlZddlZddlmZmZddlmZeje Z dZ GddZ dS)N)subputil)ipv4_mask_to_net_prefixcR|d}|d}g}ttdD]@}|t ||t ||zAdd|DS)z Return the subnet for a given ip address and a netmask @return (str): the subnet @param ip: ip address @param netmask: netmask .c,g|]}t|S)str).0xs Q/usr/lib/python3/dist-packages/cloudinit/sources/helpers/vmware/imc/config_nic.py zgen_subnet..s,,,SVV,,,)splitlistrangeappendintjoin)ipnetmaskip_array mask_arrayresultindexs r gen_subnetrsxx}}Hs##J FeAhhEE c(5/**SE1B-C-CCDDDD 88,,V,,, - --rcVeZdZddZdZdZdZdZdZdZ d Z dd Z d Z ddZ d S)NicConfiguratorTc||_i|_d|_d|_|r|n0|jD](}|j|j|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) nicsmac2Nameipv4PrimaryGatewayipv6PrimaryGateway find_devicesnamemaclowerget_primary_nic _primaryNic)selfr!use_system_devicesnics r__init__zNicConfigurator.__init__#s  "&"&  :       y : :14 cgmmoo..//11rcd|jD}|sdSt|dkrtdd|D|dS)z| Retrieve the primary nic if it exists @return (NicBase): the primary nic if exists, None otherwise c g|] }|j | Sr )primaryr r-s rrz3NicConfigurator.get_primary_nic..=s@@@CK@@@@rNz!There can only be one primary niccg|] }|j Sr )r'r2s rrz3NicConfigurator.get_primary_nic..Cs111S111rr)r!len Exception)r+ primary_nicss rr)zNicConfigurator.get_primary_nic8sl A@ty@@@  #4    " "311L111   ? "rcTgd}tj|\}}tjdd|zdd}d}|D]g}tj||}|s|d}|ddd} | |j|<hdS) zb Create the mac2Name dictionary The mac address(es) are in the lower case )raddrshowz\n\d+:  r3Nz3link/ether (([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})):r)rrersearchgroupr(r") r+cmdoutput_errsectionsmacPatsectionmatchr'r&s rr%zNicConfigurator.find_devicesHs %$$y~~ 8Jv 66qrr:G & &GIfg..E ++a..&&((C==a((+D!%DM#    & &rcH|j}|j|}|st d|jzg}d||d}g}g}|||\}} |||| |||\}} |||| |d|i| ||r|||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: %sphysical)typer& mac_addresssubnets) r'r(r"get ValueErrorgen_ipv4extendgen_ipv6updater) r+r-r'r& nics_cfg_listcfg subnet_list route_listrKroutess r gen_one_niczNicConfigurator.gen_one_nicZs6 gmmoo}  %% J>HII I !4DD  !MM$44&7###&!!!!MM$44&7###&!!! I{+,,,S!!!  -   , , ,rci}g}|jr|ddi|j}|jdkrd}|dkr|ddi|g|fS|ddi|j}|s|g|fS|d}|jr|d |ji|jr|d |ji|jr:|j r3|j d|_ |d |j i|g|fS|j s=|j r6|d | ||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 controlautodisabledmanualstaticrIdhcpraddressrgatewayrV) onbootrQ bootProtor( ipv4_mode staticIpv4rrr1gatewaysr#r*gen_ipv4_route)r+r&r-subnetrU bootprotoaddrsv4s rrNzNicConfigurator.gen_ipv4~s : / MM9f- . . .M'')) =   J . . I  MM66* + + +Hj) ) MM68, - - - *Hj) ) 1X 5 . MM9be, - - - : 3 MM9bj1 2 2 2 ; *2; *&(k!nD # MM9d&=> ? ? ?Hj) ) BK  MM4..sBKLLM   *%%rcg}t|}|D]1}dt|||fz}||d|dd2|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/%droute') destinationrIr`metric)rrr)r+r-rerrUcidrr`rns rrfzNicConfigurator.gen_ipv4_routesy &w//  G!Z%A%A4$HHK   #.#&#     rc|jsggfSg}|j}|D]'}d|j|jd}||(g}||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 static6)rIr_r) staticIpv6rrr)r+r&r-rTrir9rgrUs rrPzNicConfigurator.gen_ipv6sx~ 8O  ' 'D!7<F   v & & & & Z((rcPg}|D] }|d|jdd!|S)Nrlrm)rIr`ro)rr`)r+r&r-rirUr9s r _genIpv6RoutezNicConfigurator._genIpv6RoutesJ   D    T\UKK    rFNc|r/td||g}|jD]*}|||+|S)z@Return the config elements that are needed to configure the nicszConfiguring the interfaces file)loggerinfo configurer!rOrW)r+ryosfamilyrRr-s rgeneratezNicConfigurator.generatesp  % KK9 : : : NN8 $ $ $ 9 8 8C  !1!1#!6!6 7 7 7 7rctdtjddgddgtjgddS)NzClearing DHCP leasespkilldhclientrr3)rcs)rmz-fz/var/lib/dhcp/*)rwrxr)r+s r clear_dhcpzNicConfigurator.clear_dhcpsR *+++ 7J'aV4444 11122222rc|r|dkrtddSd}tj|d}tj|d}tj|s4tj|rtj||gd}tj|d| | dS) 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) rwrxospathrexistsrenamer write_filer)r+rz containingDir interfaceFile originalFileliness rryzNicConfigurator.configures  8x// KKM N N N F&  ]LAA w|| C  w~~l++ 3}0M0M 3 Im\ 2 2 2     tyy/?/?@@@@ r)T)FN)N)__name__ __module__ __qualname__r.r)r%rWrNrfrPrur{rryr rrrr"s2222*### &&&$"""H0&0&0&d0)))B    333rr) loggingrr= cloudinitrrcloudinit.net.network_stater getLoggerrrwrrr rrrs ??????  8 $ $ . . . BBBBBBBBBBr