a ahi@sdZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZeeZdZddZeed d d Zejed d dZddZddZddZddZddZGddde jZee j ffgZ!ddZ"dS)zr This file contains code used to gather the user data passed to an instance on rootbox / hyperone cloud platforms N) IPv4Address)sourcessubputil) EventScope EventTypez /etc/hostscCs.tjtdd}|r tddStddS)NT)quietz5/etc/hosts exists - setting manage_etc_hosts to FalseFz=/etc/hosts does not exists - setting manage_etc_hosts to True)rload_text_file ETC_HOSTSLOGdebug)hostsrH/usr/lib/python3.9/site-packages/cloudinit/sources/DataSourceRbxCloud.pyget_manage_etc_hostss   r)increturncCstttt||SN)strrint)addrrrrr increment_ip#sr)rcCs|t|dt|dgS)zReturn 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'srcCstdg|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|dgWqtjyv}z$td|d|d|WYd}~qd}~00qdS) Nz-S)Z almalinuxZfedoraZcentosZ cloudlinuxZrhelz-sz-c2source destinationz&Failed to arping from "%s" to "%s": %s)namerrZProcessExecutionErrorr Zwarning)itemsdistroZ source_paramitemerrorrrrgratuitous_arp=s r%c Csttdtd}|s dS|D]~}z&tj|tgdd}|rL|WSWq$ty}z|jtjkrlWYd}~q$d}~0tjyt t d|Yq$0q$t dd |dS) zAReturns False (not found or error) or a dictionary with metadata.z LABEL=CLOUDMDz LABEL=cloudmdF)ZvfatZfatZmsdosfs)devicecallbackZmtypeNz-Failed to mount %s when looking for user dataz0Did not find RbxCloud data, searched devices: %s,) setrZfind_devs_withZmount_cbread_user_data_callbackOSErrorerrnoENOENTZMountFailedErrorlogexcr r join)Zdevicesr&rbx_dataerrrrrget_mdRs6   r2cCsdddt|DdS)zGenerate network configuration @param netadps: A list of network adapter settings @returns: A dict containing network config csBg|]:\}ddt|dfdddDdqS)Zphysicalzeth{}Z macaddressc s>g|]6}d|ddddddddddqS) ZstaticaddressnetworknetmaskautogatewayZdnsZ nameservers)typer4r6Zcontrolr8Zdns_nameserversr).0ipnetadprr s    z6generate_network_config...r;)r9r Z mac_addresssubnets)formatrlower)r:irr<rr>zs   z+generate_network_config..)versionZconfig) enumerate)Znetadpsrrrgenerate_network_configqs rEc Cstjtjtj|ddd}tjtj|ddd}d|vsFd|vrVttd d S| d i d }| d i d g}d }| d i dr|d dd}t |d}||dd|ddgddd|dDddd||dg|d|di|t dd}t dt ||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.NZadditionalMetadatausernameZsshKeyspasswordZsha512Z_idr )z instance-idzlocal-hostname public-keyscSs>g|]6}|dD](}t|ddD]}|d|dq$qqS)r;r5r8r4)rr)r)r:r=r;targetrrrr>s z+read_user_data_callback..Z default_userzALL=(ALL) NOPASSWD:ALLF)r ZgecosZsudopasswdZ lock_passwdZssh_authorized_keys)Z ssh_pwauthZ disable_rootZ system_infonetwork_configZmanage_etc_hosts)userdatametadatar%cfgzreturning DATA object:) rZ load_jsonZload_binary_fileospathr/r r.r getrErr )Z mount_dirZ meta_dataZ user_datarHZssh_keyshashr5datarrrr*sR        r*c@sjeZdZdZejejejej hiZ ddZ ddZ ddZ edd Zd d Zd d ZddZddZdS)DataSourceRbxCloudZRbxCloudcCs(tj||||d|_d|_d|_dSr)r DataSource__init__seedr%rP)selfZsys_cfgr"pathsrrrrXszDataSourceRbxCloud.__init__cCstj|}d||jfS)Nz %s [seed=%s])rrW__str__rY)rZrootrrrr\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_datas    zDataSourceRbxCloud._get_datacCs |jdS)NrMrPrZrrrrMsz!DataSourceRbxCloud.network_configcCs |jdS)NrJ)rOrarrrget_public_ssh_keyssz&DataSourceRbxCloud.get_public_ssh_keyscCs|jSr)r^rarrrget_userdata_rawsz#DataSourceRbxCloud.get_userdata_rawcCs|jSrr`rarrrget_config_objsz!DataSourceRbxCloud.get_config_objcCst|j|jdSr)r%r")rZrPZis_new_instancerrractivateszDataSourceRbxCloud.activateN)__name__ __module__ __qualname__ZdsnamerZNETWORKrZBOOT_NEW_INSTANCEZBOOTZ BOOT_LEGACYZdefault_update_eventsrXr\r_propertyrMrbrcrdrerrrrrVs  rVcCs t|tSr)rZlist_from_depends datasources)Zdependsrrrget_datasource_list srk)#__doc__r,ZloggingrQZos.pathtyping ipaddressrZ cloudinitrrrZcloudinit.eventrrZ getLoggerrfr r rrrrListrrr%r2rEr*rWrVZDEP_FILESYSTEMrjrkrrrrs,     !@5