o x[hg@sdZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z mZeeZdZddZd ed efd d Zd ejefd 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}|rtddStddS)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)hostsrF/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceRbxCloud.pyget_manage_etc_hostss   rincreturncCstttt||SN)strrint)addrrrrr increment_ip#srcCs|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 arping)r)cmdrrr_sub_arp4src Cs|d}|jdvr d}|D]0}ztdd||d|dgWq tjy;}ztd|d|d|WYd}~q d}~wwdS) Nz-S)fedoracentosrhelz-sz-c2source destinationz&Failed to arping from "%s" to "%s": %s)namerrProcessExecutionErrorr warning)itemsdistro source_paramitemerrorrrrgratuitous_arp=s$ r,c Csttdtd}|sdS|D]>}ztj|tgdd}|r&|WSWqty@}z |jtjkr6WYd}~qd}~wtjyPt t d|Yqwt dd |dS) zAReturns False (not found or error) or a dictionary with metadata.z LABEL=CLOUDMDz LABEL=cloudmdF)vfatfatmsdosfs)devicecallbackmtypeNz-Failed to mount %s when looking for user dataz0Did not find RbxCloud data, searched devices: %s,) setrfind_devs_withmount_cbread_user_data_callbackOSErrorerrnoENOENTMountFailedErrorlogexcr r join)devicesr0rbx_dataerrrrrget_mdRs>    rAcCsdddt|DdS)zGenerate network configuration @param netadps: A list of network adapter settings @returns: A dict containing network config csBg|]\}ddt|dfdddDdqS)physicalzeth{} macaddressc s>g|]}d|ddddddddddqS) staticaddressnetworknetmaskautogatewaydns nameservers)typerFrHcontrolrJdns_nameserversr).0ipnetadprr s    z6generate_network_config...rQ)rMr$ mac_addresssubnets)formatrlower)rPirrRrrTzs    z+generate_network_config..)versionconfig) enumerate)netadpsrrrgenerate_network_configqs r^c Cstjtjtj|ddd}tjtj|ddd}d|vs#d|vr+ttd d S| d i d }| d i d g}d }| d i drQ|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)r_rvmrSz(Failed to load metadata. Invalid format.NadditionalMetadatausernamesshKeyspasswordsha512_idr$)z instance-idzlocal-hostname public-keyscSs>g|]}|dD]}t|ddD] }|d|dqqqS)rQrGrJrF)r"r#)r)rPrSrQtargetrrrrTs z+read_user_data_callback.. default_userzALL=(ALL) NOPASSWD:ALLF)r$gecossudopasswd lock_passwdssh_authorized_keys) ssh_pwauth disable_root system_infonetwork_configmanage_etc_hosts)userdatametadatar,cfgzreturning DATA object:) r load_jsonload_binary_fileospathr=r r<r getr^rr ) mount_dir meta_data user_datarcssh_keyshashrGdatarrrr7sR        r7c@sjeZdZdZejejejej hiZ ddZ ddZ ddZ edd Zd d Zd d ZddZddZdS)DataSourceRbxCloudRbxCloudcCs(tj||||d|_d|_d|_dSr)r DataSource__init__seedr,rw)selfsys_cfgr(pathsrrrrs zDataSourceRbxCloud.__init__cCstj|}d||jfS)Nz %s [seed=%s])rr__str__r)rrootrrrrs zDataSourceRbxCloud.__str__cCs>t}|dur dS|d|_|d|_|d|_|d|_dS)zw Metadata is passed to the launching instance which is used to perform instance configuration. Frurvr,rwT)rA userdata_rawrvr,rw)rr?rrr _get_datas    zDataSourceRbxCloud._get_datacC |jdS)Nrsrwrrrrrss z!DataSourceRbxCloud.network_configcCr)Nrh)rvrrrrget_public_ssh_keyss z&DataSourceRbxCloud.get_public_ssh_keyscC|jSr)rrrrrget_userdata_rawz#DataSourceRbxCloud.get_userdata_rawcCrrrrrrrget_config_objrz!DataSourceRbxCloud.get_config_objcCst|j|jdSr)r,r()rrwis_new_instancerrractivateszDataSourceRbxCloud.activateN)__name__ __module__ __qualname__dsnamerNETWORKrBOOT_NEW_INSTANCEBOOT BOOT_LEGACYdefault_update_eventsrrrpropertyrsrrrrrrrrrs"  rcCs t|tSr)rlist_from_depends datasources)dependsrrrget_datasource_list s r)#__doc__r9loggingrzos.pathtyping ipaddressr cloudinitrrrcloudinit.eventrr getLoggerrr r rrrrListrrr,rAr^r7rrDEP_FILESYSTEMrrrrrrs.     !@ 5