܍~cU"dZddlZddlZddlZddlZddlmZddlmZ ddlm Z m Z m Z ddl mZmZe jeZdZdZd ed efd Zd ejefd Zd ZdZdZdZdZGdde jZ e e j!ffgZ"dZ#dS)zr This file contains code used to gather the user data passed to an instance on rootbox / hyperone cloud platforms N) IPv4Address)log)sourcessubputil) EventScope EventTypez /etc/hostsctjtd}|rtddStddS)NT)quietz5/etc/hosts exists - setting manage_etc_hosts to FalseFz=/etc/hosts does not exists - setting manage_etc_hosts to True)r load_file ETC_HOSTSLOGdebug)hostss F/usr/lib/python3/dist-packages/cloudinit/sources/DataSourceRbxCloud.pyget_manage_etc_hostsrsN N9D 1 1 1E  IJJJuIIMNNN 4increturnc ttttt||zSN)strrint)addrrs r increment_ipr#s- {3{40011C788 9 99rcD|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)rs r get_three_ipsr's+ T1T1 rc2tjdg|zS)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)cmds r_sub_arpr#4s 9hZ#% & &&rc d}|jdvrd}|D]h} tdd||d|dg$#tj$r3}td|d|d|Yd}~ad}~wwxYwdS) Nz-S)fedoracentosrhelz-sz-c2source destinationz&Failed to arping from "%s" to "%s": %s)namer#rProcessExecutionErrorrwarning)itemsdistro source_paramitemerrors rgratuitous_arpr3=sL {222   sL$x.$}:MN    )    KK8X]#           s 4A6)A11A6cttjdtjdz}|sdS|D]} tj|tgd}|r|cS(#t $r }|jt jkrYd}~Md}~wtj$rtj td|Y|wxYwt 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_callbackOSErrorerrnoENOENTMountFailedErrorlogexcrrjoin)devicesr8rbx_dataerrs rget_mdrIRs? O,,  o . . /G u }0000H      yEL(()(((($    KDf       II:CHHWz6generate_network_config...sn   !)#%i=#))#4Y#?#)#))#4Y#?+1)+.zs   &6##s1vv..%l399;;    %Tl         r)versionconfig) enumerate)netadpss rgenerate_network_configriqs<  &'w//'     rc tjtjtj|dd}tjtj|dd}d|vsd |vrtjtd d S|d id }|d idg}d }|d idr|d dd}t|d }||dd|ddgdd|d Dddd||dg|d|di|tdd}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.jsonF)fnamedecode)textz user.dataT)rkr vmr^z(Failed to load metadata. Invalid format.NadditionalMetadatausernamesshKeyspasswordsha512_idr+)z instance-idzlocal-hostname public-keyscvg|]6}|dD]+}t|ddD] }|d|d,7S)r]rRrUrQ)r)r*)r)r\r^r]targets rr_z+read_user_data_callback..sx   Tl  'y(9)(DEE   )}V < <     r default_userzALL=(ALL) NOPASSWD:ALL)r+gecossudopasswd lock_passwdssh_authorized_keys) ssh_pwauth disable_root system_infonetwork_configmanage_etc_hosts)userdatametadatar3cfgzreturning DATA object:) r load_jsonr ospathrErDrgetrirr) mount_dir meta_data user_datarpssh_keyshashrRdatas rr?r?s ^',,y,77   I gll9k22$I 9 9 9 CCDDDt}}1266:::FFH}}1266::9bIIH D}})2..22:>>E-.z:8D%i&9::G$T?51'of5    #H-    $%56"#(+3 !! & 4 6 6    D>II&'''IIdOOO KrceZdZdZejejejej hiZ dZ dZ dZ edZdZdZdZd Zd S) DataSourceRbxCloudRbxCloudcXtj||||d|_dSr)r DataSource__init__seed)selfsys_cfgr/pathss rrzDataSourceRbxCloud.__init__s*##D'65AAA rcZtj|}|d|jdS)Nz [seed=])rr__str__r)rroots rrzDataSourceRbxCloud.__str__s-!))$//!%tyyy11rct}|durdS|d|_|d|_|d|_|d|_dS)zw Metadata is passed to the launching instance which is used to perform instance configuration. Frrr3rT)rI userdata_rawrr3r)rrGs r _get_datazDataSourceRbxCloud._get_datasT 88 u  5$Z0 , &'78E?trc|jdS)Nrrrs rrz!DataSourceRbxCloud.network_configsx())rc|jdS)Nru)rrs rget_public_ssh_keysz&DataSourceRbxCloud.get_public_ssh_keyss}]++rc|jSr)rrs rget_userdata_rawz#DataSourceRbxCloud.get_userdata_raws   rc|jSrrrs rget_config_objz!DataSourceRbxCloud.get_config_objs xrc:t|j|jdSr)r3r/)rris_new_instances ractivatezDataSourceRbxCloud.activatest*DK88888rN)__name__ __module__ __qualname__dsnamerNETWORKr BOOT_NEW_INSTANCEBOOT BOOT_LEGACYdefault_update_eventsrrrpropertyrrrrrr[rrrrs F  ' N  ! 222   **X*,,,!!!99999rrc6tj|tSr)rlist_from_depends datasources)dependss rget_datasource_listr s  $Wk : ::r)$__doc__rAros.pathtyping ipaddressr cloudinitrloggingrrrcloudinit.eventrr getLoggerrrr rrrrListrr#r3rIrir?rrDEP_FILESYSTEMrrr[rrrs  !!!!!!$$$$$$))))))))))11111111g!!  :C:C:::: 6;s+    '''*>B???D.9.9.9.9.9+.9.9.9f'023 ;;;;;r