3 \me#@s0dZddlZddlZddlZddlZddlZddlZddlmZm Z m Z ddl m Z m Z ddlmZddlmZddlmZddlmZmZdd lmZejeZd Zdd d ZGd ddejZGdddeZ de e j!fiZ"ddZ#ee$dddZ%edddZ&ddZ'edkr,ej(Z)e'ee)dS)z)Handle reconfiguration on hotplug events.N)log reportingstages) EventScope EventType)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|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.)rZhandlezHandle the hotplug event.z-dz --devpathPATHzSysfs path to hotplugged device)rmetavarrz-uz --udevactionzSpecify action to take.addremove) argparseArgumentParserNAME__doc__r add_argumentZadd_subparsersrZ add_parser)parserZ subparsersZ parser_handler!"/usr/lib/python3.6/hotplug_hook.py get_parsers:     r#c@s`eZdZddZejddZeejddZeje ddd Z d d Z d d Z ddZ dS) UeventHandlercCs"||_||_||_||_||_dS)N)id datasourcedevpathaction success_fn)selfr%r&r'r(r)r!r!r"__init__Ks zUeventHandler.__init__cCs tdS)N)NotImplementedError)r*r!r!r"applyRszUeventHandler.applycCs tdS)N)r,)r*r!r!r"configVszUeventHandler.config)returncCs tdS)N)r,)r*r!r!r"device_detected[szUeventHandler.device_detectedcCsPd}|jdkrd}n|jdkr$d}ntd|j||jkrLtd|jdS)NrTrFzUnknown action: %sz'Failed to detect %s in updated metadata)r( ValueErrorr0 RuntimeErrorr%)r*Zdetect_presencer!r!r"detect_hotplugged_device_s   z&UeventHandler.detect_hotplugged_devicecCs|jS)N)r))r*r!r!r"successmszUeventHandler.successcCs,|jjtjg}|s(td|jtjf|S)Nz&Datasource %s not updated for event %s)r&Zupdate_metadata_if_supportedrHOTPLUGr2)r*resultr!r!r"update_metadataps zUeventHandler.update_metadataN)__name__ __module__ __qualname__r+abcabstractmethodr-propertyr.boolr0r3r4r7r!r!r!r"r$Jsr$cs>eZdZfddZddZeddZeddd ZZ S) NetHandlercs*ttjj|d}tj|||||dS)NZaddress)rospathbasenamesuperr+)r*r&r'r(r)r%) __class__r!r"r+}szNetHandler.__init__cCs||jjj|jddtjj|j}|jjj}|j dkrT|j |sxt dj |jn$|j dkrx|j |sxt dj |jdS)NF)Zbring_uprzFailed to bring up device: {}rzFailed to bring down device: {})r&ZdistroZapply_network_configr.r@rArBr'Znetwork_activatorr(Zbring_up_interfacer2formatZbring_down_interface)r*Zinterface_nameZ activatorr!r!r"r-s     zNetHandler.applycCs|jjS)N)r&Znetwork_config)r*r!r!r"r.szNetHandler.config)r/cs<tj}fdd|jD}tjdj|t|dkS)Ncs g|]}|jdjkr|qS)Z mac_address)getr%).0Ziface)r*r!r" sz.NetHandler.device_detected..zIfaces with ID=%s : %sr)rr.Ziter_interfacesLOGdebugr%len)r*Znetstatefoundr!)r*r"r0s    zNetHandler.device_detected) r8r9r:r+r-r=r.r>r0 __classcell__r!r!)rDr"r?|s  r?rcCs\yt|d}Wn2tk rB}ztdj||WYdd}~XnXtj|j|jtj |dS)Nz4hotplug-hook: cannot handle events for subsystem: {})r&cfgZevent_source_typescope) SUBSYSTEM_PROPERTES_MAPKeyErrorr2rErZupdate_event_enabledr&rOrr5) hotplug_init subsystemrPer!r!r" is_enabledsrV)rSrTcCsRtjd|jdd}|jtjgs4tjd|dSt||sNtjd|dS|S)NzFetching datasourceZtrust)Zexistingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)rIrJZfetchZget_supported_eventsrr5rV)rSrTr&r!r!r"initialize_datasources     rW)rSc Cst||}|sdSt|d}tjd||||||jd}dddddg}td }xt|D]\} } tjd || t|yTtjd |j|j stjd |j tjd |j tjd|j PWq\tk r } z tjd| t j| | }WYdd} ~ Xq\Xq\W|dS)NrzCreating %s event handler)r&r'r(r)rN 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)rWrQrIrJZ_write_to_cache Exception enumeraterKr7Zskip_hotplug_detectr3r-r4timeZsleep) rSr'rT udevactionr&Z handler_clsZ event_handlerZ wait_timesZlast_exceptionZattemptwaitrUr!r!r"handle_hotplugsD           racCs8tj|tdd}tg|d}|jtj|jd|jkrLtj |jj dt j d||j |jd|krj|jndd|krz|jnd|yn|j dkryt||j}Wn&tk rtd tjd YnXt|rd nd nt||j|j|jd Wn"tk rt jdYnXWdQRXt j dtjdS)NT)Zreporting_enabled)Zds_depsZreporterrzh%s called with the following arguments: {hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}r_r'rz9Unable to determine hotplug state. No datasource detectedrNZenabledZdisabled)rSr'rTr_z*Received fatal exception handling hotplug!zExiting hotplug handler)r ZReportEventStackrr Zread_cfgrZ setup_loggingrOrZupdate_configurationrFrIrJrrTr_r'rWr printsysexitrar\Z exceptionZ flush_events)nameargsZhotplug_reporterrSr&r!r!r" handle_argssF       rg__main__)N)*rr;rZloggingr@rcr^Z cloudinitrrrZcloudinit.eventrrZ cloudinit.netrZcloudinit.net.network_staterZcloudinit.reportingr Zcloudinit.sourcesr r Zcloudinit.stagesr Z getLoggerr8rIrr#ABCr$r?ZNETWORKrQrVstrrWrarg parse_argsrfr!r!r!r"s4      22((5