a -@=j‚ã@sêdZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZe e¡ZdZdd„Zeed œd d „Zejed œd d„Zdd„Zdd„Zdd„Zdd„Zdd„ZGdd„de jƒZee j ffgZ!dd„Z"dS)zr This file contains code used to gather the user data passed to an instance on rootbox / hyperone cloud platforms éN)Ú IPv4Address)ÚsourcesÚsubpÚutil)Ú EventScopeÚ EventTypez /etc/hostscCs.tjtdd�}|r t d¡dSt d¡dS)NT)Úquietz5/etc/hosts exists - setting manage_etc_hosts to FalseFz=/etc/hosts does not exists - setting manage_etc_hosts to True)rÚload_text_fileÚ ETC_HOSTSÚLOGÚdebug)Úhosts©rúH/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceRbxCloud.pyÚget_manage_etc_hostss   r)ÚincÚreturncCstttt|ƒƒ|ƒƒS©N)ÚstrrÚint)ÚaddrrrrrÚ increment_ip#sr)rcCs|t|dƒt|dƒgS)z–Return a list of 3 IP addresses: [addr, addr + 2, addr + 3] @param addr: an object that is passed to IPvAddress @return: list of strings éé)r)rrrrÚ get_three_ips'sýrcCst dg|¡S)z© Uses the preferred cloud-init subprocess def of subp.subp and runs arping. Breaking this to a separate function for later use in mocking and unittests Zarping)r)ÚcmdrrrÚ_sub_arp4src Cs~d}|jdvrd}|D]b}ztdd||d|dgƒWqtjyv}z$t d|d|d|¡WYd}~qd}~00qdS) Nz-S)Z almalinuxZfedoraZcentosZ cloudlinuxZrhelz-sz-cÚ2ÚsourceÚ destinationz&Failed to arping from "%s" to "%s": %s)ÚnamerrZProcessExecutionErrorr Zwarning)ÚitemsÚdistroZ source_paramÚitemÚerrorrrrÚgratuitous_arp=s ÿür%c Csºtt d¡t d¡ƒ}|s dS|D]~}z&tj|tgd¢d�}|rL|WSWq$ty€}z|jtjkrl‚WYd}~q$d}~0tjy t  t d|¡Yq$0q$t   dd   |¡¡dS) zAReturns False (not found or error) or a dictionary with metadata.z LABEL=CLOUDMDz LABEL=cloudmdF)ZvfatZfatZmsdosfs)ÚdeviceÚcallbackZmtypeNz-Failed to mount %s when looking for user dataz0Did not find RbxCloud data, searched devices: %sú,) ÚsetrZfind_devs_withZmount_cbÚread_user_data_callbackÚOSErrorÚerrnoÚENOENTZMountFailedErrorÚlogexcr r Újoin)Zdevicesr&Úrbx_dataÚerrrrrÚget_mdRs6ÿÿý ÿ  ÿr2cCsddd„t|ƒDƒdœS)z‹Generate network configuration @param netadps: A list of network adapter settings @returns: A dict containing network config écsBg|]:\}‰dd t|ƒ¡ˆd ¡‡fdd„ˆdDƒdœ‘qS)Zphysicalzeth{}Z macaddressc s>g|]6}d|dˆdddˆddˆddddœ‘qS) ZstaticÚaddressÚnetworkÚnetmaskÚautoÚgatewayZdnsZ nameservers)Útyper4r6Zcontrolr8Zdns_nameserversr)Ú.0Úip©ÚnetadprrÚ s ÷   ÿúz6generate_network_config...r;)r9r Z mac_addressÚsubnets)ÚformatrÚlower)r:Úirr<rr>zsï   õüz+generate_network_config..)ÚversionZconfig)Ú enumerate)ZnetadpsrrrÚgenerate_network_configqs íþrEc Cstjtjtj |d¡d�d�}tjtj |d¡dd�}d|vsFd|vrVt td ¡d S|  d i¡  d ¡}|  d i¡  d g¡}d }|  d i¡  d¡r¢|d dd}t |dƒ}||dd|ddgdœdd„|dDƒddd||dg|d|dœi|t ƒdœdœ}t  d¡t  |¡|S)aThis callback will be applied by util.mount_cb() on the mounted drive. @param mount_dir: String representing path of directory where mounted drive is available @returns: A dict containing userdata, metadata and cfg based on metadata. z cloud.json)Úfname)Útextz user.dataT)rFrZvmr=z(Failed to load metadata. Invalid format.NZadditionalMetadataÚusernameZsshKeysÚpasswordZsha512Z_idr )z instance-idzlocal-hostnameú public-keyscSs>g|]6}|dD](}t|ddƒD]}|d|dœ‘q$qqS)r;r5r8r4)rr)r)r:r=r;Útargetrrrr>´s ýz+read_user_data_callback..Z default_userzALL=(ALL) NOPASSWD:ALLF)r ZgecosZsudoÚpasswdZ lock_passwdZssh_authorized_keys)Z ssh_pwauthZ disable_rootZ system_infoÚnetwork_configZmanage_etc_hosts)ÚuserdataÚmetadatar%Úcfgzreturning DATA object:) rZ load_jsonZload_binary_fileÚosÚpathr/r r.r ÚgetrErr )Z mount_dirZ meta_dataZ user_datarHZssh_keysÚhashr5Údatarrrr*’sR ÿÿ    ýþúÿ òó  r*c@sjeZdZdZejejejej hiZ dd„Z dd„Z dd„Z edd „ƒZd d „Zd d „Zdd„Zdd„ZdS)ÚDataSourceRbxCloudZRbxCloudcCs(tj ||||¡d|_d|_d|_dSr)rÚ DataSourceÚ__init__Úseedr%rP)ÚselfZsys_cfgr"ÚpathsrrrrXÜszDataSourceRbxCloud.__init__cCstj |¡}d||jfS)Nz %s [seed=%s])rrWÚ__str__rY)rZÚrootrrrr\âs zDataSourceRbxCloud.__str__cCs>tƒ}|durdS|d|_|d|_|d|_|d|_dS)zw Metadata is passed to the launching instance which is used to perform instance configuration. FrNrOr%rPT)r2Ú userdata_rawrOr%rP)rZr0rrrÚ _get_dataæs    zDataSourceRbxCloud._get_datacCs |jdS)NrM©rP©rZrrrrMôsz!DataSourceRbxCloud.network_configcCs |jdS)NrJ)rOrarrrÚget_public_ssh_keysøsz&DataSourceRbxCloud.get_public_ssh_keyscCs|jSr)r^rarrrÚget_userdata_rawûsz#DataSourceRbxCloud.get_userdata_rawcCs|jSrr`rarrrÚget_config_objþsz!DataSourceRbxCloud.get_config_objcCst|j|jƒdSr)r%r")rZrPZis_new_instancerrrÚactivateszDataSourceRbxCloud.activateN)Ú__name__Ú __module__Ú __qualname__ZdsnamerZNETWORKrZBOOT_NEW_INSTANCEZBOOTZ BOOT_LEGACYZdefault_update_eventsrXr\r_ÚpropertyrMrbrcrdrerrrrrVÒs ýÿ rVcCs t |t¡Sr)rZlist_from_dependsÚ datasources)ZdependsrrrÚget_datasource_list srk)#Ú__doc__r,ZloggingrQZos.pathÚtypingÚ ipaddressrZ cloudinitrrrZcloudinit.eventrrZ getLoggerrfr r rrrrÚListrrr%r2rEr*rWrVZDEP_FILESYSTEMrjrkrrrrÚs,     !@5 ÿ