a 'Dgm+@s`dZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z ddlmZmZddlmZddlmZddlmZdd lmZdd lmZmZdd lmZeeZ d Z!d"d dZ"Gdddej#Z$Gddde$Z%de%ej&fiZ'ddZ(ee)dddZ*edddZ+ee,dddZ-dd Z.ed!kr\e"/Z0e.e!e0dS)#z)Handle reconfiguration on hotplug events.N) reportingstagesutil)install_hotplug) EventScope EventType)loggers)read_sys_net_safe)parse_net_config_data)events) DataSourceDataSourceNotFoundException)Initz hotplug-hookcCs|stjttd}t|_|jdddddgd|jdd d }d|_|jd d d |jddd }|jdddddd|jddddddgd|jddd |S)aBuild or extend an arg parser for hotplug-hook utility. @param parser: Optional existing ArgumentParser instance representing the subcommand which will be extended to support the args of this utility. @returns: ArgumentParser with proper argument configuration. )prog descriptionz-sz --subsystemTzsubsystem to act onnet)requiredhelpchoiceszHotplug Actionhotplug_action)titledestqueryz0Query if hotplug is enabled for given subsystem.)rhandlezHandle the hotplug event.z-dz --devpathPATHzSysfs path to hotplugged device)rmetavarrz-uz --udevactionzSpecify action to take.addremoveenablez%Enable hotplug for a given subsystem.) argparseArgumentParserNAME__doc__r add_argumentadd_subparsersr add_parser)parserZ subparsersZ parser_handler'D/usr/lib/python3.9/site-packages/cloudinit/cmd/devel/hotplug_hook.py get_parsersLr)c@s`eZdZddZejddZeejddZeje ddd Z d d Z d d Z ddZ dS) UeventHandlercCs"||_||_||_||_||_dSN)id datasourcedevpathaction success_fn)selfr,r-r.r/r0r'r'r(__init__Rs zUeventHandler.__init__cCs tdSr+NotImplementedErrorr1r'r'r(applyYszUeventHandler.applycCs tdSr+r3r5r'r'r(config]szUeventHandler.configreturncCs tdSr+r3r5r'r'r(device_detectedbszUeventHandler.device_detectedcCsPd}|jdkrd}n|jdkr$d}ntd|j||krLtd|jdS)NrTrFzUnknown action: %sz'Failed to detect %s in updated metadata)r/ ValueErrorr: RuntimeErrorr,)r1Zdetect_presencer'r'r(detect_hotplugged_devicefs   z&UeventHandler.detect_hotplugged_devicecCs|Sr+)r0r5r'r'r(successtszUeventHandler.successcCs,|jtjg}|s(td|jtjf|S)Nz&Datasource %s not updated for event %s)r-Zupdate_metadata_if_supportedrHOTPLUGr<)r1resultr'r'r(update_metadataws zUeventHandler.update_metadataN)__name__ __module__ __qualname__r2abcabstractmethodr6propertyr7boolr:r=r>rAr'r'r'r(r*Qs  r*cs>eZdZfddZddZeddZeddd ZZ S) NetHandlercs*ttj|d}t|||||dS)Naddress)r ospathbasenamesuperr2)r1r-r.r/r0r, __class__r'r(r2szNetHandler.__init__cCs||jjj|jddtj|j}|jj}|j dkrT| |sxt d |jn$|j dkrx| |sxt d |jdS)NF)Zbring_uprzFailed to bring up device: {}rzFailed to bring down device: {})r-ZdistroZapply_network_configr7rKrLrMr.Znetwork_activatorr/Zbring_up_interfacer<formatZbring_down_interface)r1Zinterface_nameZ activatorr'r'r(r6s        zNetHandler.applycCs|jjSr+)r-Znetwork_configr5r'r'r(r7szNetHandler.configr8cs<tj}fdd|D}tdj|t|dkS)Ncs g|]}|djkr|qS)Z mac_address)getr,).0Zifacer5r'r( sz.NetHandler.device_detected..zIfaces with ID=%s : %sr)r r7Ziter_interfacesLOGdebugr,len)r1Znetstatefoundr'r5r(r:s   zNetHandler.device_detected) rBrCrDr2r6rGr7rHr: __classcell__r'r'rOr(rIs   rIrc Cs^zt|d}Wn4tyD}ztd||WYd}~n d}~00tj|j|jtj |dS)Nz4hotplug-hook: cannot handle events for subsystem: {})r-cfgZevent_source_typescope) SUBSYSTEM_PROPERTIES_MAPKeyErrorr<rQrZupdate_event_enabledr-r[rr?) hotplug_init subsystemr\er'r'r( is_enableds rbr_r`cCsRtd|jdd}|tjgs4td|dSt||sNtd|dS|S)NzFetching datasourcetrustexistingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)rUrVfetchget_supported_eventsrr?rb)r_r`r-r'r'r(initialize_datasources     ri)r_c Cst||}|sdSt|d}td||||||jd}gd}td}t|D]\} } td|| t|zZtd||j std| td | td | WqWqTty } z&td | t | | }WYd} ~ qTd} ~ 00qT|dS) NrzCreating %s event handler)r-r.r/r0)rZ z#Bug while processing hotplug event.z!subsystem=%s update attempt %s/%szRefreshing metadataz$Detecting device in updated metadatazApplying config changezUpdating cachez,Exception while processing hotplug event. %s)rir]rUrVZ_write_to_cache Exception enumeraterWrAZskip_hotplug_detectr=r6r>timesleep) r_r.r` udevactionr-Z handler_clsZ event_handlerZ wait_timesZlast_exceptionZattemptwaitrar'r'r(handle_hotplugsH          rt)r_r9cCs|jdd}|sdSt|d}tj|tjg|tv}|sZtd|tj ddSt |j }|j |dvrtd|d tj dd S|d|j t j|j d t|d d dt|d |jdd S)NrdreFrZz#hotplug not supported for event of )fileZscopesz)Not installing hotplug for event of type z. Reason: Already done.Tzhotplug.enabledwi)Zomodemode)Znetwork_hotplug_enabledr[)rgr]rr?rhrRsetprintsysstderrrZread_hotplug_enabled_filepathsvalueappendZ write_fileZ get_cpathjsondumpsrr[)r_r`r-r\Zhotplug_supportedZhotplug_enabled_filer'r'r(enable_hotplugs<      rc Cstj|tdd}tg|d}|t|jd|jvrLt |j dt d||j |jd|vrj|jndd|vrz|jnd|z|j dkrzt||j}Wn$tytd td Yn0t|rd nd np|j d krt||j|j|jdnNtdkr tjdtd t||jds:td td|jWn tylt dYn0Wdn1s0Yt dtdS)NT)Zreporting_enabled)Zds_depsZreporterrzh%s called with the following arguments: {hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}rrr.rz9Unable to determine hotplug state. No datasource detectedrZZenabledZdisabledr)r_r.r`rrrz9Root is required. Try prepending your command with sudo. rcz)Enabled cloud-init hotplug for subsystem=z*Received fatal exception handling hotplug!zExiting hotplug handler)r ZReportEventStackr"rZread_cfgrZ setup_loggingr[rZupdate_configurationrRrUrVrr`rrr.rir ryrzexitrtrKgetuidr{writerrn exceptionZ flush_events)nameargsZhotplug_reporterr_r-r'r'r( handle_argssn          ( r__main__)N)1r"rErrZloggingrKrzrpZ cloudinitrrrZ#cloudinit.config.cc_install_hotplugrZcloudinit.eventrrZ cloudinit.logrZ cloudinit.netr Zcloudinit.net.network_stater Zcloudinit.reportingr Zcloudinit.sourcesr r Zcloudinit.stagesrZ getLoggerrBrUr!r)ABCr*rIZNETWORKr]rbstrrirtrHrr parse_argsrr'r'r'r(s>        62( (#E