a '¡DgŸbã@sîddlZddlZddlZddlZddlZddlZddlmZmZddl m Z m Z ddl m Z mZmZmZddlmZddlmZdZdZd Zd Ze e¡ZGd d „d ƒZGd d„dejƒZddd„Zddd„Zdd„Z ddd„Z!ddd„Z"dS)éN)ÚListÚOptional)ÚsubpÚutil)Úis_ipv6_addressÚis_ipv6_networkÚrendererÚsubnet_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@sÎeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z e dœdd„Z dd„Z dd„Zdd„Zdd„Zdd„Ze ddœdd „Zee dd!œd"d#„Zd$d%„Zd&d'„Zed(d)„ƒZd*d+„Zd,d-„ZdS).Ú NMConnectionz/Represents a NetworkManager connection profile.cCsRt d¡}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)ÚidÚuuidzautoconnect-priorityÚ connectionz%org.freedesktop.NetworkManager.originz cloud-initÚuserN)rZUUIDÚ configparserZ ConfigParserÚconfigÚstrZ optionxformZuuid5)ÚselfÚcon_idZ CI_NM_UUID©rúA/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. N©rÚ has_sectionÚ has_option©rÚsectionÚoptionÚvaluerrrÚ _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. r©rrrrrrÚ_config_option_is_setPsÿz"NMConnection._config_option_is_setcCs"| ||¡r|j||SdSdS)z] Returns the value of a config option if its set, else returns None. N©r"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}| |d¡dkrdS| |d¡dvrdSqdD]}| |dd¡q:dS)zt If for both ipv4 and ipv6, 'may-fail' is set to be False, set it to True for both of them. ©Úipv4Úipv6úmay-failÚfalseNÚmethod)ÚdhcpÚautoÚtrue)r$r%)rÚfamilyrrrÚ$_set_mayfail_true_if_both_false_dhcpksz1NMConnection._set_mayfail_true_if_both_false_dhcpc CsÈdddddddddœ}d}| |d|¡z|r6||}Wn$ty\d}d|j|d<Yn0|j|ddkrtd S|j|ddkr’|dkr’d 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_slaacúipv6_dhcpv6-statelessúipv6_dhcpv6-statefulZdhcp4r,Údisabledr+r.r)N)r3r2r1r'r*)r ÚKeyErrorr)rr/Z subnet_typeZ method_mapr+rrrÚ_set_ip_methodƒs2ø   zNMConnection._set_ip_method)ÚreturncCsL|j |¡si|j|<t d¡D]&}|›|›�}|j ||¡s |Sq dS)NéZ not_possible)rrÚ itertoolsÚcountr)rrÚ key_prefixÚindexÚkeyrrrÚ_get_next_numbered_sectionÀs    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_numberedÉs 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)rÚget)rrÚrouter=rZ numbered_keyZ route_optionsrrrÚ_add_route_optionsÑs  ÿ ýzNMConnection._add_route_optionscCs*|ddt|dƒ}| |d|¡dS)z5 Adds an ipv[46]address property. Úaddressú/ÚprefixN)rr?)rr/ÚsubnetrrrrÚ _add_addressèszNMConnection._add_addresscCsd}|ddko|d|vS)N)z::z0.0.0.0rGrÚnetworkr)rrCZ default_netsrrrÚ_is_default_routeðszNMConnection._is_default_routecCs¸t|dƒrdnd}|d›d|d›�}d|vrD|d|d›�7}d|vr~| |¡rl|d›|j|d <n|d|d›�7}| |d ¡}||j||<d |vr´| ||d |d ¡d S) z!Adds a ipv[46].route property.rJr(r'rFrGÚgatewayr@Zmetricz route-metricrCÚmtuN)rrKrr>rD)rrCr/rZ route_keyrrrÚ _add_routeôs  zNMConnection._add_routeN)Údnsr7cCs^t|ƒr dnd}|j |¡rZ| |d¡dkrZ| |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| |d¡dkr| |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)r©rrrrÚcon_uuid"szNMConnection.con_uuidcCs|j dd¡S)zN Can this be serialized into a meaningful connection profile? rÚtype)rrrVrrrÚvalid(szNMConnection.validcCs| dd¡ ¡S)z) Sanitize a MAC address. ú-ú:)ÚreplaceÚupper)ÚaddrrrrÚmac_addr.szNMConnection.mac_addrcsðdddddddœ}||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} g‰g‰|dk�r|d2�sd3D]} | | d¡�q|d2D]À} t| ƒ�r4d4nd5} | | | d¡d6| v�r^| | | ¡d7| v�rz| d7|j| d7<| d8D]} | | ¡�q‚d9| v�r¬ˆ | d9¡d:| v�rĈ | d:¡| d5k�r"d1| v�r"| d1} �q"d;|v�r*ˆ‡fdDƒ7‰ˆ‡fd?d=„|d;d@Dƒ7‰ˆ�s>|j�r>|j‰ˆ�sR|j�rR|j‰ˆD]} |  | ¡�qVˆ�rx|  ˆ¡|  ¡| du�rŽ|} | |k�s¬t   dA|dB|| ¡|| ¡D]n\}}||v�rÎ�q¸||du�rà�q¸t||tƒ�r||�rþdCndD|j||<nt||ƒ|j||<�q¸|dk�r”|dEdFu�rRtdGƒ|jddH<| du�rnt| ƒ|jdd1<|dIdu�r”| |dI¡|jddJ<|dk�rÀdK|v�rÀ| |dK¡|jddL<|dk�rü| du�rüd|jv�rêi|jd<t| ƒ|jdd1<|dk�rf|dMD]0}| |¡}| d d d¡| d d | ¡¡�q|dIdu�rf| |dI¡|jddJ<|dk�rÀ| du�rÀdN|jddO<t| ƒ|jdd1<|dIdu�rÀ| |dI¡|jddJ<|dk�sÚ|j |dJ¡�sì|dB|jd dP<dS)QzŒ Integrate information from network state interface information into the connection. Most of the work is done here. ÚethernetÚvlanÚbondÚbridgeÚ 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)ZstpÚpriorityr Zvlan_id)rbrcrar`rdrMÚsubnetsr&r(r'rErLZroutesÚdns_nameserversrSrOcsg|]}|ˆvr|‘qSrr)Ú.0rO)Úfound_nameserversrrÚ ¿sþz1NMConnection.render_interface..Z nameserverscsg|]}|ˆvr|‘qSrr)riÚsearch)Úfound_dns_searchrrrkÄsþrlzZNetwork 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-deviceÚparentZ bridge_portsZdatagramztransport-modezinterface-name)rÚcon_refr6r rIrNÚextendrhZdns_searchdomainsrRrUr0ÚLOGZwarningÚitemsÚ isinstanceÚboolrr_Úget_connr rWr)rÚifaceÚ network_staterZ _type_mapZif_typeZ slave_typeZ _prop_mapZ device_mtuZipv4_mtur/rHrCZ nameserverZnm_propr=Úportr)rmrjrÚrender_interface5s,ú  ÿ Ó0þÿÈ;           þ  þ   û  ÿ  ÿÿ     ÿÿÿzNMConnection.render_interfacecCs(t ¡}|jj|dd�d}|| ¡S)z Stringify. F)Zspace_around_delimitersz2# Generated by cloud-init. Changes will be lost. )ÚioÚStringIOrÚwriteÚgetvalue)rÚbufÚheaderrrrÚdump"szNMConnection.dump)Ú__name__Ú __module__Ú __qualname__Ú__doc__rr r"r$r%r0r6rr>r?rDrIrKrNrRrrUrWrYÚ staticmethodr_r{r‚rrrrr *s.   =   nr c@sBeZdZdZd dd„Zdd„Zdd„Zd eee dd œd d „Z dS)ÚRendererz‘Renders network information in a NetworkManager keyfile format. See https://networkmanager.dev/docs/api/latest/nm-settings-keyfile.html NcCsi|_||_dS©N)Ú connectionsr)rrrrrr3szRenderer.__init__cCs |j|Sr‰)rŠ©rrrrrrw7szRenderer.get_conncCs ||jvr|j| ¡S|SdSr‰)rŠrWr‹rrrrq:s zRenderer.con_ref)ryÚ templatesr7c Csº| ¡D](}| d¡p|d}t|dƒ|j|<q| ¡D].}| d¡pN|d}|j|}| |||¡q:|j ¡D].\}}| ¡s†qtt||ƒ}t  ||  ¡d¡qtt  t |ƒt d¡dS)NZ config_idrni€) Ziter_interfacesrBr rŠr{rtrYÚnm_conn_filenamerZ write_filer‚Úcloud_init_nm_conf_filenameÚNM_IPV6_ADDR_GEN_CONF) rryrŒÚtargetrxZconn_keyZconnrrnrrrÚrender_network_stateAs     ÿzRenderer.render_network_state)N)NN) rƒr„r…r†rrwrqr rÚdictr‘rrrrrˆ-s  üûrˆcCs&t |t¡}d|›d�}|›d|›�S)Nz cloud-init-z .nmconnectionz/system-connections/©rÚ target_pathÚ NM_RUN_DIR)rr�Útarget_con_dirÚcon_filerrrr�bs  r�cCs$t |t¡}d|›�}|›d|›�S)Nzifcfg-rF)rr”ÚIFCFG_CFG_FILE)Údevnamer�r–r—rrrÚsysconfig_conn_filenamehs  ršcCs6t|ƒ}tj |¡s"tƒr"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)r�ÚosÚpathÚisfiler rš)r™Z conn_filerrrÚ conn_filenamens ržcCst |t¡}d}|›d|›�S)Nz$30-cloud-init-ip6-addr-gen-mode.confz/conf.d/r“)r�r–Z conf_filerrrrŽƒs rŽcCs\ddlm}tjd|d�}d}|ƒrPzt gd¢¡WntjyNd}Yn0t|ƒoZ|S)Nr)Ú uses_systemdZnmcli)r�T)Z systemctlz is-enabledzNetworkManager.serviceF)Zcloudinit.distrosrŸrÚwhichZProcessExecutionErrorrv)r�rŸZ nmcli_presentZservice_activerrrÚ available‰s  r¡)N)N)N)N)#rr|r9Zloggingr›rÚtypingrrZ cloudinitrrZ cloudinit.netrrrr Zcloudinit.net.network_stater Zcloudinit.net.sysconfigr r•Z NM_LIB_DIRr˜r�Z getLoggerrƒrsr rˆr�ršržrŽr¡rrrrÚ s4   5