a 'Dgb@sddlZddlZddlZddlZddlZddlZddlmZmZddl m Z m Z ddl m Z mZmZmZddlmZddlmZdZdZd Zd ZeeZGd d d ZGd ddejZdddZdddZddZ dddZ!dddZ"dS)N)ListOptional)subputil)is_ipv6_addressis_ipv6_networkrenderersubnet_is_ipv6) NetworkState)available_nm_ifcfg_rhz/etc/NetworkManagerz/usr/lib/NetworkManagerz/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 e dddZ ddZ ddZddZddZddZe dddd Zee dd!d"d#Zd$d%Zd&d'Zed(d)Zd*d+Zd,d-ZdS). NMConnectionz/Represents a NetworkManager connection profile.cCsRtd}t|_t|j_d|tt||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_UUIDrA/usr/lib/python3.9/site-packages/cloudinit/net/network_manager.py__init__-s   zNMConnection.__init__cCs6|j|si|j|<|j||s2||j||<dS)z_ Sets a property unless it's already set, ensuring the section exists. Nr has_section has_optionrsectionoptionvaluerrr _set_defaultEs  zNMConnection._set_defaultcCs|j|o|j||S)zf Checks if a config option is set. Returns True if it is, else returns False. rrrrrrr_config_option_is_setPsz"NMConnection._config_option_is_setcCs"|||r|j||SdSdS)z] Returns the value of a config option if its set, else returns None. Nr"rr!rrr_get_config_optionYs zNMConnection._get_config_optioncCs|||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. Nr#rrrr_change_set_config_optioncs z&NMConnection._change_set_config_optioncCsRdD]0}||ddkrdS||ddvrdSqdD]}||ddq:dS)zt If for both ipv4 and ipv6, 'may-fail' is set to be False, set it to True for both of them. ipv4ipv6may-failfalseNmethod)dhcpautotrue)r$r%)rfamilyrrr$_set_mayfail_true_if_both_false_dhcpksz1NMConnection._set_mayfail_true_if_both_false_dhcpc Csddddddddd}d}||d|z|r6||}Wn$ty\d}d|j|d<Yn0|j|ddkrtd S|j|ddkr|dkrd S|d vr|d dd||j|d<||dd d S) z} Ensures there's appropriate [ipv4]/[ipv6] for given family appropriate for given configuration type Zmanualr-r,)ZstaticZstatic6Zdhcp6 ipv6_slaacipv6_dhcpv6-statelessipv6_dhcpv6-statefulZdhcp4r,disabledr+r.r)N)r3r2r1r'r*)r KeyErrorr)rr/Z subnet_typeZ method_mapr+rrr_set_ip_methods2   zNMConnection._set_ip_method)returncCsL|j|si|j|<tdD]&}||}|j||s |Sq dS)NZ not_possible)rr itertoolscountr)rr key_prefixindexkeyrrr_get_next_numbered_sections    z'NMConnection._get_next_numbered_sectioncCs|||}||j||<dS)z Adds a numbered property, such as address or route, ensuring the appropriate value gets used for . N)r>r)rrr;rr=rrr _add_numbereds zNMConnection._add_numberedcCsN|d}|j||}|r2|d|d|n |d||j||<dS)a:Add route options to a given route Example: Given: section: ipv4 route: route0 key: mtu value: 500 Create line under [ipv4] section: route0_options=mtu=500 If the line already exists, then append the new key/value pair Z_options,=N)rget)rrrouter=rZ numbered_keyZ route_optionsrrr_add_route_optionss   zNMConnection._add_route_optionscCs*|ddt|d}||d|dS)z5 Adds an ipv[46]address property. address/prefixN)rr?)rr/subnetrrrr _add_addressszNMConnection._add_addresscCsd}|ddko|d|vS)N)z::z0.0.0.0rGrnetworkr)rrCZ default_netsrrr_is_default_routeszNMConnection._is_default_routecCst|drdnd}|dd|d}d|vrD|d|d7}d|vr~||rl|d|j|d <n|d|d7}||d }||j||<d |vr|||d |d d S) z!Adds a ipv[46].route property.rJr(r'rFrGgatewayr@Zmetricz route-metricrCmtuN)rrKrr>rD)rrCr/rZ route_keyrrr _add_routes  zNMConnection._add_routeN)dnsr7cCs^t|r dnd}|j|rZ||ddkrZ||dd|j|d|d|j|d<dS) zF Extends the ipv[46].dns property with a name server. r(r'r+r4rO;N)rrrr$r )rrOr/rrr_add_nameservers zNMConnection._add_nameserver) dns_searchr7cCs^dD]T}|j|r||ddkr||dd|j|dd|d|j|d<qdS)zM Extends the ipv[46].dns-search property with a name server. r&r+r4z dns-searchrPrQN)rrr$r join)rrSr/rrr_add_dns_searchs  zNMConnection._add_dns_searchcCs|jddS)z- Returns the connection UUID rr)rrrrrcon_uuid"szNMConnection.con_uuidcCs|jddS)zN Can this be serialized into a meaningful connection profile? rtype)rrrVrrrvalid(szNMConnection.validcCs|ddS)z) Sanitize a MAC address. -:)replaceupper)addrrrrmac_addr.szNMConnection.mac_addrcsddddddd}||d}|dur*dSd |vr8d}nd}||jd d<|dur|||jd d <|||d |jd d <i|j|<ddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/iiid0}|d1}d} gg|dkr|d2sd3D]} || dq|d2D]} t| r4d4nd5} || | dd6| vr^|| | d7| vrz| d7|j| d7<| d8D]} || qd9| vr| d9d:| vrĈ| d:| d5kr"d1| vr"| d1} q"d;|vr*fdD7fd?d=|d;d@D7s>|jr>|jsR|jrR|jD]} | | qVrx| | | dur|} | |kst dA|dB|| ||D]n\}}||vrΐq||durqt||tr||rdCndD|j||<nt|||j||<q|dkr|dEdFurRtdG|jddH<| durnt| |jdd1<|dIdur||dI|jddJ<|dkrdK|vr||dK|jddL<|dkr| durd|jvri|jd<t| |jdd1<|dkrf|dMD]0}||}|d d d|d d |q|dIdurf||dI|jddJ<|dkr| durdN|jddO<t| |jdd1<|dIdur||dI|jddJ<|dks|j|dJs|dB|jd dP<dS)Qz Integrate information from network state interface information into the connection. Most of the work is done here. ethernetvlanbondbridge infinibandN)ZphysicalrarbrcrdZloopbackrXz bond-masterrz slave-typez-masterZmasterz bond-modez bond-miimonzbond-xmit_hash_policyzbond-num_grat_arpzbond-downdelayz bond-updelayzbond-fail_over_maczbond-primary_reselectz bond-primaryzbond-active_slavezbond-ad_actor_sys_priozbond-ad_actor_systemzbond-ad_selectzbond-ad_user_port_keyzbond-all_slaves_activezbond-arp_all_targetszbond-arp_intervalzbond-arp_ip_targetzbond-arp_validatezbond-lacp_ratezbond-lp_intervalzbond-min_linkszbond-num_unsol_nazbond-packets_per_slavezbond-peer_notif_delayzbond-resend_igmpzbond-tlb_dynamic_lbzbond-use_carrier)modeZmiimonZxmit_hash_policyZ num_grat_arpZ downdelayZupdelayZ fail_over_macZprimary_reselectZprimaryZ active_slaveZad_actor_sys_prioZad_actor_systemZ ad_selectZad_user_port_keyZall_slaves_activeZarp_all_targetsZ arp_intervalZ arp_ip_targetZ arp_validateZ lacp_rateZ lp_intervalZ min_linksZ num_unsol_naZpackets_per_slaveZpeer_notif_delayZ resend_igmpZtlb_dynamic_lbZ use_carrierZ bridge_stpZbridge_bridgeprio)Zstppriorityr Zvlan_id)rbrcrar`rdrMsubnetsr&r(r'rErLZroutesdns_nameserversrSrOcsg|]}|vr|qSrr).0rO)found_nameserversrr sz1NMConnection.render_interface..Z nameserverscsg|]}|vr|qSrr)risearch)found_dns_searchrrrksrlzZNetwork 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_refr6r rIrNextendrhZdns_searchdomainsrRrUr0LOGZwarningitems isinstanceboolrr_get_connr rWr)riface network_staterZ _type_mapZif_typeZ slave_typeZ _prop_mapZ device_mtuZipv4_mtur/rHrCZ nameserverZnm_propr=portr)rmrjrrender_interface5s,   0;                         zNMConnection.render_interfacecCs(t}|jj|ddd}||S)z Stringify. F)Zspace_around_delimitersz2# Generated by cloud-init. Changes will be lost. )ioStringIOrwritegetvalue)rbufheaderrrrdump"szNMConnection.dump)__name__ __module__ __qualname____doc__rr r"r$r%r0r6rr>r?rDrIrKrNrRrrUrWrY staticmethodr_r{rrrrrr *s.   =   nr c@sBeZdZdZd ddZddZddZd eee dd d d Z dS)RendererzRenders network information in a NetworkManager keyfile format. See https://networkmanager.dev/docs/api/latest/nm-settings-keyfile.html NcCsi|_||_dSN) connectionsr)rrrrrr3szRenderer.__init__cCs |j|Sr)rrrrrrrw7szRenderer.get_conncCs ||jvr|j|S|SdSr)rrWrrrrrq:s zRenderer.con_ref)ry templatesr7c Cs|D](}|dp|d}t|d|j|<q|D].}|dpN|d}|j|}||||q:|jD].\}}|sqtt||}t || dqtt t |t ddS)NZ config_idrni) Ziter_interfacesrBr rr{rtrYnm_conn_filenamerZ write_filercloud_init_nm_conf_filenameNM_IPV6_ADDR_GEN_CONF) rryrtargetrxZconn_keyZconnrrnrrrrender_network_stateAs     zRenderer.render_network_state)N)NN) rrrrrrwrqr rdictrrrrrr-s  rcCs&t|t}d|d}|d|S)Nz cloud-init-z .nmconnectionz/system-connections/r target_path NM_RUN_DIR)rrtarget_con_dircon_filerrrrbs  rcCs$t|t}d|}|d|S)Nzifcfg-rF)rrIFCFG_CFG_FILE)devnamerrrrrrsysconfig_conn_filenamehs  rcCs6t|}tj|s"tr"t|}tj|r2|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)rospathisfiler r)rZ conn_filerrr conn_filenamens rcCst|t}d}|d|S)Nz$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/r)rrZ conf_filerrrrs rcCs\ddlm}tjd|d}d}|rPztgdWntjyNd}Yn0t|oZ|S)Nr) uses_systemdZnmcli)rT)Z systemctlz is-enabledzNetworkManager.serviceF)Zcloudinit.distrosrrwhichZProcessExecutionErrorrv)rrZ nmcli_presentZservice_activerrr availables  r)N)N)N)N)#rr|r9ZloggingrrtypingrrZ cloudinitrrZ cloudinit.netrrrr Zcloudinit.net.network_stater Zcloudinit.net.sysconfigr rZ NM_LIB_DIRrrZ getLoggerrrsr rrrrrrrrrr s4   5