HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/cloudinit/cmd/devel/__pycache__/hotplug_hook.cpython-38.pyc
U

��0h�-�@spdZddlZddlZddlZddlZddlZddlZddlZddlm	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*edd�dd�Z+dd�dd�Z,ee-d�dd �Z.d!d"�Z/ed#k�rle"��0�Z1e/e!e1�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_argumentZadd_subparsersrZ
add_parser)�parserZ
subparsersZ
parser_handle�r%�B/usr/lib/python3/dist-packages/cloudinit/cmd/devel/hotplug_hook.py�
get_parsersT�������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-r.r%r%r&�__init__Rs
zUeventHandler.__init__cCs
t��dSr)��NotImplementedError�r/r%r%r&�applyYszUeventHandler.applycCs
t��dSr)r1r3r%r%r&�config]szUeventHandler.config��returncCs
t��dSr)r1r3r%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-�
ValueErrorr8�RuntimeErrorr*)r/Zdetect_presencer%r%r&�detect_hotplugged_devicefs

�z&UeventHandler.detect_hotplugged_devicecCs|��Sr))r.r3r%r%r&�successtszUeventHandler.successcCs,|j�tjg�}|s(td|jtjf��|S)Nz&Datasource %s not updated for event %s)r+Zupdate_metadata_if_supportedr�HOTPLUGr:)r/�resultr%r%r&�update_metadataws�
��zUeventHandler.update_metadataN)�__name__�
__module__�__qualname__r0�abc�abstractmethodr4�propertyr5�boolr8r;r<r?r%r%r%r&r(Qs
r(cs>eZdZ�fdd�Zdd�Zedd��Zed�dd	�Z�Z	S)
�
NetHandlercs*ttj�|�d�}t��|||||�dS)NZaddress)r	�os�path�basename�superr0)r/r+r,r-r.r*��	__class__r%r&r0�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_configr5rHrIrJr,Znetwork_activatorr-Zbring_up_interfacer:�formatZbring_down_interface)r/Zinterface_nameZ	activatorr%r%r&r4�s �


�


�zNetHandler.applycCs|jjSr))r+Znetwork_configr3r%r%r&r5�szNetHandler.configr6cs<t�j�}�fdd�|��D�}t�d�j|�t|�dkS)Ncs g|]}|�d��jkr|�qS)Zmac_address)�getr*)�.0Zifacer3r%r&�
<listcomp>�s�z.NetHandler.device_detected.<locals>.<listcomp>zIfaces with ID=%s : %sr)r
r5Ziter_interfaces�LOG�debugr*�len)r/Znetstate�foundr%r3r&r8�s

�zNetHandler.device_detected)
r@rArBr0r4rEr5rFr8�
__classcell__r%r%rLr&rG�s

rGrc
Cs\zt|d}Wn2tk
rB}ztd�|��|�W5d}~XYnXtj|j|jtj	|d�S)N�z4hotplug-hook: cannot handle events for subsystem: {})r+�cfgZevent_source_type�scope)
�SUBSYSTEM_PROPERTIES_MAP�KeyErrorr:rNrZupdate_event_enabledr+rXrr=)�hotplug_init�	subsystemrY�er%r%r&�
is_enabled�s ����r_�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)rRrS�fetch�get_supported_eventsrr=r_)r\r]r+r%r%r&�initialize_datasource�s

rf)r\r7cCs�t||�}|sdSt|d}t�d|�|||||jd�}t��}|jjs\t|||�dSt��||jj	kr�t|||�t�d�t�
|jj�q\dS)NrzCreating %s event handler)r+r,r-r.z>Gathering network configuration again due to IMDS limitations.)rfrZrRrSZ_write_to_cache�timeZhotplug_retry_settingsZforce_retry�try_hotplugZsleep_total�sleepZsleep_period)r\r,r]�
udevactionr+Zhandler_cls�
event_handler�startr%r%r&�handle_hotplug�s*
��rmr6cCs�dddddg}td�}t|�D]�\}}t�d||t|��zXt�d�|��|jsft�d	�|��t�d
�|��t�d�|�	�Wq�Wqtk
r�}zt�d|�t
�|�|}W5d}~XYqXq|�dS)
NrW���
�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)�	Exception�	enumeraterRrSrTr?Zskip_hotplug_detectr;r4r<rgri)r]rkr+Z
wait_timesZlast_exceptionZattempt�waitr^r%r%r&rh�s2�





rhcCs�|jdd�}|sdSt|d}tj|�tjg��|t��k}|sZtd|��tj	d�dSt
�|j�}|j
|dkr�td|�d	�tj	d�d
S|d�|j
�t
j|j�d�t�|�dd
d�t|d
|jd�d
S)NrarbFrWz#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_enabledrX)rdrZrr=rerO�set�print�sys�stderrrZread_hotplug_enabled_file�paths�value�appendZ
write_fileZ	get_cpath�json�dumpsrrX)r\r]r+rYZhotplug_supportedZhotplug_enabled_filer%r%r&�enable_hotplugsB��
�
��r�c	Cs�tj|tdd�}tg|d�}|��t�|j�d|jkrLt�	|j�
d��t�d||j
|jd|krj|jndd|krz|jnd�|��z�|j
dkr�zt||j�}Wn&tk
r�td	�t�d
�YnXt|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"tk
�rpt�d��YnXW5QRXt�d�t��dS)NT)Zreporting_enabled)Zds_depsZreporterrzh%s called with the following arguments: {hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}rjr,rz9Unable to determine hotplug state. No datasource detectedrW�enabledZdisabledr)r\r,r]rjrz9Root is required. Try prepending your command with sudo.
r`z)Enabled cloud-init hotplug for subsystem=z*Received fatal exception handling hotplug!zExiting hotplug handler)rZReportEventStackr"rZread_cfgrZ
setup_loggingrXrZupdate_configurationrOrRrSrr]rjr,rfr
ryrz�exitrmrH�getuidr{�writer�rrZ	exceptionZflush_events)�name�argsZhotplug_reporterr\r+r%r%r&�handle_args'sr�
�

��
��
�

�

r��__main__)N)2r"rCrrZloggingrHrzrgZ	cloudinitrrrZ#cloudinit.config.cc_install_hotplugrZcloudinit.eventrrZ
cloudinit.logrZ
cloudinit.netr	Zcloudinit.net.network_stater
Zcloudinit.reportingrZcloudinit.sourcesrr
Zcloudinit.stagesrZ	getLoggerr@rRr!r'�ABCr(rGZNETWORKrZr_�strrfrmrhrFr�r��
parse_argsr�r%r%r%r&�<module>sB

62(�#E