name : hotplug_hook.cpython-39.pyc
a

'�Dgm+�@s`dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
mZddlm
Z
ddlmZmZddlmZddlmZddlmZdd	lmZdd
lmZmZddlmZe�e�Z dZ!d"d
d�Z"Gdd�dej#�Z$Gdd�de$�Z%de%ej&fiZ'dd�Z(ee)d�dd�Z*ed�dd�Z+ee,d�dd�Z-dd �Z.ed!k�r\e"��/�Z0e.e!e0�dS)#z)Handle reconfiguration on hotplug events.�N)�	reporting�stages�util)�install_hotplug)�
EventScope�	EventType)�loggers)�read_sys_net_safe)�parse_net_config_data)�events)�
DataSource�DataSourceNotFoundException)�Initzhotplug-hookcCs�|stjttd�}t|_|jdddddgd�|jdd	d
�}d|_|jdd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 on�net)�required�help�choiceszHotplug Action�hotplug_action)�title�dest�queryz0Query if hotplug is enabled for given subsystem.)r�handlezHandle the hotplug event.z-dz	--devpath�PATHzSysfs path to hotplugged device)r�metavarrz-uz--udevactionzSpecify action to take.�add�remove�enablez%Enable hotplug for a given subsystem.)	�argparse�ArgumentParser�NAME�__doc__r�add_argument�add_subparsersr�
add_parser)�parserZ
subparsersZ
parser_handle�r'�D/usr/lib/python3.9/site-packages/cloudinit/cmd/devel/hotplug_hook.py�
get_parsersL�������r)c@s`eZdZdd�Zejdd��Zeejdd���Zeje	d�dd	��Z
d
d�Zdd
�Zdd�Z
dS)�
UeventHandlercCs"||_||_||_||_||_dS�N)�id�
datasource�devpath�action�
success_fn)�selfr,r-r.r/r0r'r'r(�__init__Rs
zUeventHandler.__init__cCs
t��dSr+��NotImplementedError�r1r'r'r(�applyYszUeventHandler.applycCs
t��dSr+r3r5r'r'r(�config]szUeventHandler.config��returncCs
t��dSr+r3r5r'r'r(�device_detectedbszUeventHandler.device_detectedcCsPd}|jdkrd}n|jdkr$d}ntd|j��||��krLtd|j��dS)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,|j�tjg�}|s(td|jtjf��|S)Nz&Datasource %s not updated for event %s)r-Zupdate_metadata_if_supportedr�HOTPLUGr<)r1�resultr'r'r(�update_metadataws�
��zUeventHandler.update_metadataN)�__name__�
__module__�__qualname__r2�abc�abstractmethodr6�propertyr7�boolr:r=r>rAr'r'r'r(r*Qs
r*cs>eZdZ�fdd�Zdd�Zedd��Zed�dd	�Z�Z	S)
�
NetHandlercs*ttj�|�d�}t��|||||�dS)N�address)r	�os�path�basename�superr2)r1r-r.r/r0r,��	__class__r'r(r2�szNetHandler.__init__cCs||jjj|jdd�tj�|j�}|jj��}|j	dkrT|�
|�sxtd�|j���n$|j	dkrx|�
|�sxtd�|j���dS)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(r6�s �


�


�zNetHandler.applycCs|jjSr+)r-Znetwork_configr5r'r'r(r7�szNetHandler.configr8cs<t�j�}�fdd�|��D�}t�d�j|�t|�dkS)Ncs g|]}|�d��jkr|�qS)Zmac_address)�getr,)�.0Zifacer5r'r(�
<listcomp>�s�z.NetHandler.device_detected.<locals>.<listcomp>zIfaces with ID=%s : %sr)r
r7Ziter_interfaces�LOG�debugr,�len)r1Znetstate�foundr'r5r(r:�s

�zNetHandler.device_detected)
rBrCrDr2r6rGr7rHr:�
__classcell__r'r'rOr(rI�s

rIrc
Cs^zt|d}Wn4tyD}ztd�|��|�WYd}~n
d}~00tj|j|jtj	|d�S)N�z4hotplug-hook: cannot handle events for subsystem: {})r-�cfgZevent_source_type�scope)
�SUBSYSTEM_PROPERTIES_MAP�KeyErrorr<rQrZupdate_event_enabledr-r[rr?)�hotplug_init�	subsystemr\�er'r'r(�
is_enabled�s ����rb�r_r`cCsRt�d�|jdd�}|�tjg�s4t�d|�dSt||�sNt�d|�dS|S)NzFetching datasource�trust��existingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)rUrV�fetch�get_supported_eventsrr?rb)r_r`r-r'r'r(�initialize_datasource�s

ri)r_cCst||�}|sdSt|d}t�d|�|||||jd�}gd�}td�}t|�D]�\}	}
t�d||	t|��zZt�d�|��|j	s�t�d�|�
�t�d	�|��t�d
�|��W�qWqTt�y
}z&t�d|�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>�time�sleep)r_r.r`�
udevactionr-Zhandler_clsZ
event_handlerZ
wait_timesZlast_exceptionZattempt�waitrar'r'r(�handle_hotplug�sH
��




rt)r_r9cCs�|jdd�}|sdSt|d}tj|�tjg��|t��v}|sZtd|��tj	d�dSt
�|j�}|j
|dvr�td|�d	�tj	d�d
S|d�|j
�t
j|j�d�t�|�dd
d�t|d
|jd�d
S)NrdreFrZz#hotplug not supported for event of )�fileZscopesz)Not installing hotplug for event of type z. Reason: Already done.Tzhotplug.enabled�wi�)Zomode�mode)Znetwork_hotplug_enabledr[)rgr]rr?rhrR�set�print�sys�stderrrZread_hotplug_enabled_file�paths�value�appendZ
write_fileZ	get_cpath�json�dumpsrr[)r_r`r-r\Zhotplug_supportedZhotplug_enabled_filer'r'r(�enable_hotplug�s<��
�
��r�c	Cs�tj|tdd�}tg|d�}|��t�|j�d|jvrLt�	|j�
d��t�d||j
|jd|vrj|jndd|vrz|jnd�|��z�|j
dkr�zt||j�}Wn$ty�td	�t�d
�Yn0t|r�dnd�np|j
d
kr�t||j|j|jd�nNt��dk�r tj�d�t�d
�t||jd��s:t�d
�td|j���Wn t�ylt�d��Yn0Wd�n1�s�0Yt�d�t��dS)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)rZReportEventStackr"rZread_cfgrZ
setup_loggingr[rZupdate_configurationrRrUrVrr`rrr.rir
ryrz�exitrtrK�getuidr{�writer�rn�	exceptionZflush_events)�name�argsZhotplug_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.reportingrZcloudinit.sourcesrr
Zcloudinit.stagesrZ	getLoggerrBrUr!r)�ABCr*rIZNETWORKr]rb�strrirtrHr�r��
parse_argsr�r'r'r'r(�<module>s>

62(
�(#E



© 2025 Cubjrnet7