3 cj9L@sddlZddlZddlZddlZddlZddlZddlmZmZddl m Z m Z ddl m Z mZmZmZddlmZddlmZdZdZd Zd Zd ZejeZGd d d ZGdddejZdddZdddZ ddZ!dddZ"dddZ#dS)N)ListOptional)subputil)is_ipv6_addressis_ipv6_networkrenderersubnet_is_ipv6) NetworkState)available_nm_ifcfg_rhz/etc/NetworkManagerz/usr/lib/NetworkManagerz'/etc/NetworkManager/NetworkManager.confz/etc/sysconfig/network-scriptsz# This is generated by cloud-init. Do not edit. # [.config] enable=nm-version-min:1.40 [connection.30-cloud-init-ip6-addr-gen-mode] # Select EUI64 to be used if the profile does not specify it. ipv6.addr-gen-mode=0 c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ eddddZeeddddZddZdd Zed!d"Zd#d$Zd%d&ZdS)' NMConnectionz/Represents a NetworkManager connection profile.cCsRtjd}tj|_t|j_d|ttj||dd|jd<ddi|jd<d S) z Initializes the connection with some very basic properties, notably the UUID so that the connection can be referred to. z$a3924cb8-09e0-43e9-890b-77972a800108z cloud-init Z120)iduuidzautoconnect-priority connectionz%org.freedesktop.NetworkManager.originz cloud-inituserN)rZUUID configparserZ ConfigParserconfigstrZ optionxformZuuid5)selfcon_idZ CI_NM_UUIDr%/usr/lib/python3.6/network_manager.py__init__.s  zNMConnection.__init__cCs6|jj|si|j|<|jj||s2||j||<dS)z_ Sets a property unless it's already set, ensuring the section exists. N)r has_section has_option)rsectionoptionvaluerrr _set_defaultFs  zNMConnection._set_defaultcCs|jj|o|jj||S)zf Checks if a config option is set. Returns True if it is, else returns False. )rrr)rrrrrr_config_option_is_setQsz"NMConnection._config_option_is_setcCs"|j||r|j||SdSdS)z] Returns the value of a config option if its set, else returns None. N)rr)rrrrrr_get_config_optionZs zNMConnection._get_config_optioncCs|j||r||j||<dS)z Overrides the value of a config option if its already set. Else, if the config option is not set, it does nothing. N)rr)rrrrrrr_change_set_config_optionds z&NMConnection._change_set_config_optioncCsVx4d D],}|j|ddkrdS|j|dd krdSqWxd D]}|j|dd q or route, ensuring the appropriate value gets used for . N)rr itertoolscountr)rrZ key_prefixrindexkeyrrr _add_numbereds   zNMConnection._add_numberedcCs*|ddt|d}|j|d|dS)z5 Adds an ipv[46]address property. address/prefixN)rr5)rr)subnetrrrr _add_addressszNMConnection._add_addresscCsVt|drdnd}|ddt|d}d|krD|d|d}|j|d|d S) z!Adds a ipv[46].route property.gatewayr#r"Znetworkr7r8,routeN)rrr5)rr=r)rrrr _add_routes zNMConnection._add_routeN)dnsreturncCsNt|r dnd}|jj|rJ|j|dd|j|d|d|j|d<dS)zF Extends the ipv[46].dns property with a name server. r#r"r?;N)rrrr)rr?r)rrr_add_nameservers zNMConnection._add_nameserver) dns_searchr@cCsRxLdD]D}|jj|r|j|dd|j|ddj|d|j|d<qWdS)zM Extends the ipv[46].dns-search property with a name server. r"r#z dns-searchrArBN)r"r#)rrrjoin)rrDr)rrr_add_dns_searchs  zNMConnection._add_dns_searchcCs|jddS)z- Returns the connection UUID rr)r)rrrrcon_uuidszNMConnection.con_uuidcCs|jjddS)zN Can this be serialized into a meaningful connection profile? rtype)rr)rrrrvalidszNMConnection.validcCs|jddjS)z) Sanitize a MAC address. -:)replaceupper)Zaddrrrrmac_addrszNMConnection.mac_addrc stddddddd}||d}|dkr*dSd |kr8d}nd}||jd d<|dk r|||jd d <|j||d |jd d <i|j|<dddddddddd dddddiiid}|d}d} ggx|dD]} t| rd nd!} |j| | dd"| kr |j| | d#| kr(| d#|j| d#<x| d$D]} |j| q2Wd%| kr^j| d%d&| krvj| d&| d!krd| kr| d} qWd'|kr؈fd(d)|d'd*D7fd+d)|d'd,D7 r|jr|j r|jr|jxD]} |j | q Wr.|j |j | dkrD|} | |ksbt j d-|d.|| x~||jD]n\}}||krqp||dkrqpt||tr||rd/nd0|j||<nt|||j||<qpW|dkrN|d1d2kr td3|jdd4<| dk r(t| |jdd<|d5dk rN|j|d5|jdd6<|dkrzd7|krz|j|d7|jdd8<|dkrx<|d9D]0}|j|}|jd d d|jd d |jqW|d5dk r|j|d5|jdd6<|dkrB| dk rBd:|jdd;<t| |jdd<|d5dk rB|j|d5|jdd6<|dks^|jj|d6 rp|d.|jd d<<dS)=z Integrate information from network state interface information into the connection. Most of the work is done here. ethernetvlanbondbridge infinibandN)ZphysicalrPrQrRrSZloopbackrHz bond-masterrz slave-typez-masterZmasterz bond-modeZ bond_miimonzbond-xmit-hash-policyzbond-num-grat-arpzbond-downdelayz bond-updelayzbond-fail-over-maczbond-primary-reselectz bond-primary) modeZmiimonZxmit_hash_policyZ num_grat_arpZ downdelayZupdelayZ fail_over_macZprimary_reselectZprimaryZ bridge_stpZbridge_bridgeprio)ZstpZpriorityr Zvlan_id)rQrRrPrOrSZmtuZsubnetsr#r"r6r;Zroutesdns_nameserversrDr?csg|]}|kr|qSrr).0r?)found_nameserversrr Osz1NMConnection.render_interface..Z nameserverscsg|]}|kr|qSrr)rVsearch)found_dns_searchrrrXTsrYzZNetwork config: ignoring %s device-level mtu:%s because ipv4 subnet-level mtu:%s provided.namer(r$Z wakeonlanT@z wake-on-lanZ mac_addressz mac-addresszvlan-raw-deviceparentZ bridge_portsZdatagramztransport-modezinterface-name)rcon_refr r/r:r>extendrUZdns_searchdomainsrCrFr*LOGZwarningitems isinstanceboolrrNget_connrrGr)riface network_staterZ _type_mapZif_typeZ slave_typeZ _prop_mapZ device_mtuZipv4_mtur9r)r=Z nameserverZnm_propr4Zportr)rZrWrrender_interfaces                     zNMConnection.render_interfacecCs(tj}|jj|ddd}||jS)z Stringify. F)Zspace_around_delimitersz2# Generated by cloud-init. Changes will be lost. )ioStringIOrwritegetvalue)rZbufheaderrrrdumpszNMConnection.dump)__name__ __module__ __qualname____doc__rrrr r!r*r/r5r:r>rrCrrFrGrI staticmethodrNrgrmrrrrr +s&   -     5r c@sBeZdZdZd ddZddZddZd eee dd d d Z dS)Rendererz?Renders network information in a NetworkManager keyfile format.NcCsi|_||_dS)N) connectionsr)rrrrrrszRenderer.__init__cCs |j|S)N)rt)rrrrrrdszRenderer.get_conncCs ||jkr|j|jS|SdS)N)rtrG)rrrrrr^s zRenderer.con_ref)rf templatesr@cCsx&|jD]}t|d|j|d<q Wx,|jD] }|j|d}|j|||q2Wx<|jjD].\}}|jstqbt||}tj||j dqbWtjt |t ddS)Nr[i) Ziter_interfacesr rtrgrarInm_conn_filenamerZ write_filermcloud_init_nm_conf_filenameNM_IPV6_ADDR_GEN_CONF)rrfrutargetreZconnrr[rrrrender_network_states  zRenderer.render_network_state)N)NN) rnrorprqrrdr^r rdictrzrrrrrss   rscCs&tj|t}d|d}|d|S)Nz cloud-init-z .nmconnectionz/system-connections/)r target_path NM_RUN_DIR)rrytarget_con_dircon_filerrrrvs  rvcCs$tj|t}d|}|d|S)Nzifcfg-r7)rr|IFCFG_CFG_FILE)devnameryr~rrrrsysconfig_conn_filenames  rcCs8t|}tjj| r$tr$t|}tjj|r4|SdS)a This function returns the name of the interface config file. It first checks for presence of network manager connection file. If absent and ifcfg-rh plugin for network manager is available, it returns the name of the ifcfg file if it is present. If the plugin is not present or the plugin is present but ifcfg file is not, it returns None. This function is called from NetworkManagerActivator class in activators.py. N)rvospathisfiler r)rZ conn_filerrr conn_filenames rcCstj|t}d}|d|S)Nz$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/)rr|r})ryr~Z conf_filerrrrw s rwc Cszddlm}tjjtj|td}tjd|d}d}|rjytjddd gWntj k rhd }YnX|oxt |ox|S) Nr) uses_systemd)rZnmcli)ryTZ systemctlz is-enabledzNetworkManager.serviceF) Zcloudinit.distrosrrrrrr| NM_CFG_FILEZwhichZProcessExecutionErrorrc)ryrZconfig_presentZ nmcli_presentZservice_activerrr availables  r)N)N)N)N)$rrhr1ZloggingrrZtypingrrZ cloudinitrrZ cloudinit.netrrrr Zcloudinit.net.network_stater Zcloudinit.net.sysconfigr r}Z NM_LIB_DIRrrrxZ getLoggerrnr`r rsrvrrrwrrrrr s4    0