HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/cloudinit/cmd/devel/__pycache__/hotplug_hook.cpython-310.pyc
o

�Ad�#�@s,dZddlZddlZddlZddlZddlZddlmZmZm	Z	ddl
mZmZddl
mZddlmZddlmZddlmZmZdd	lmZe�e�Zd
Zddd�ZGd
d�dej�ZGdd�de�Zdeej fiZ!dd�Z"dede#fdd�Z$defdd�Z%dd�Z&edkr�e��'�Z(e&ee(�dSdS)z)Handle reconfiguration on hotplug events.�N)�log�	reporting�stages)�
EventScope�	EventType)�read_sys_net_safe)�parse_net_config_data)�events)�
DataSource�DataSourceNotFoundException)�Initzhotplug-hookcCs�|s	tjttd�}t|_|jdddddgd�|jdd	d
�}d|_|jdd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 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)	�argparse�ArgumentParser�NAME�__doc__r�add_argument�add_subparsersr�
add_parser)�parser�
subparsers�
parser_handle�r&�B/usr/lib/python3/dist-packages/cloudinit/cmd/devel/hotplug_hook.py�
get_parsersF������r(c@s`eZdZdd�Zejdd��Zeejdd���Zejde	fdd	��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__Js

zUeventHandler.__init__cC�t��r*��NotImplementedError�r0r&r&r'�applyQ�zUeventHandler.applycCr2r*r3r5r&r&r'�configUszUeventHandler.config�returncCr2r*r3r5r&r&r'�device_detectedZr7zUeventHandler.device_detectedcCsPd}|jdkr
d}n|jdkrd}ntd|j��||��kr&td|j��dS)NrTrFzUnknown action: %sz'Failed to detect %s in updated metadata)r.�
ValueErrorr:�RuntimeErrorr+)r0�detect_presencer&r&r'�detect_hotplugged_device^s

��z&UeventHandler.detect_hotplugged_devicecCs|��Sr*)r/r5r&r&r'�successlszUeventHandler.successcCs,|j�tjg�}|std|jtjf��|S)Nz&Datasource %s not updated for event %s)r,�update_metadata_if_supportedr�HOTPLUGr<)r0�resultr&r&r'�update_metadataos�
��zUeventHandler.update_metadataN)�__name__�
__module__�__qualname__r1�abc�abstractmethodr6�propertyr8�boolr:r>r?rCr&r&r&r'r)Is
r)cs>eZdZ�fdd�Zdd�Zedd��Zdefdd	�Z�Z	S)
�
NetHandlercs*ttj�|�d�}t��|||||�dS)N�address)r�os�path�basename�superr1)r0r,r-r.r/r+��	__class__r&r'r1|szNetHandler.__init__cCs�|jjj|jdd�tj�|j�}|jj��}|j	dkr+|�
|�s)td�|j���dS|j	dkr=|�
|�s?td�|j���dSdS)NF)�bring_uprzFailed to bring up device: {}rzFailed to bring down device: {})r,�distro�apply_network_configr8rMrNrOr-�network_activatorr.�bring_up_interfacer<�format�bring_down_interface)r0�interface_name�	activatorr&r&r'r6�s&�


��


��zNetHandler.applycCs|jjSr*)r,�network_configr5r&r&r'r8�szNetHandler.configr9cs<t�j�}�fdd�|��D�}t�d�j|�t|�dkS)Ncs g|]}|�d��jkr|�qS)�mac_address)�getr+)�.0�ifacer5r&r'�
<listcomp>�s��z.NetHandler.device_detected.<locals>.<listcomp>zIfaces with ID=%s : %sr)rr8�iter_interfaces�LOG�debugr+�len)r0�netstate�foundr&r5r'r:�s

�zNetHandler.device_detected)
rDrErFr1r6rIr8rJr:�
__classcell__r&r&rQr'rK{s
rKrc
CsRzt|d}Wnty}ztd�|��|�d}~wwtj|j|jtj	|d�S)N�z4hotplug-hook: cannot handle events for subsystem: {})r,�cfg�event_source_type�scope)
�SUBSYSTEM_PROPERTES_MAP�KeyError�	ExceptionrXr�update_event_enabledr,rjrrA)�hotplug_init�	subsystemrl�er&r&r'�
is_enabled�s$������rtrqrrcCsRt�d�|jdd�}|�tjg�st�d|�dSt||�s't�d|�dS|S)NzFetching datasource�trust)�existingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)rcrd�fetch�get_supported_eventsrrArt)rqrrr,r&r&r'�initialize_datasource�s

rycCs
t||�}|s	dSt|d}t�d|�|||||jd�}gd�}td�}t|�D]X\}	}
t�d||	t|��z+t�d�|��|j	sNt�d�|�
�t�d	�|��t�d
�|��WdSty�}zt�d|�t
�|
�|}WYd}~q*d}~ww|�)NrzCreating %s event handler)r,r-r.r/)ri���
�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)ryrmrcrd�_write_to_cachero�	enumeratererC�skip_hotplug_detectr>r6r?�time�sleep)rqr-rr�
udevactionr,�handler_cls�
event_handler�
wait_times�last_exception�attempt�waitrsr&r&r'�handle_hotplug�sL
��




��r�c	CsBtj|tdd�}tg|d�}|��t�|j�d|jvr&t�	|j�
d��t�d||j
|jd|vr5|jndd|vr=|jnd�|�Lz6|j
dkrmzt||j�}Wntyctd	�t�d
�Ynwt|ridnd�nt||j|j|jd
�Wn
ty�t�d��wWd�n1s�wYt�d�t��dS)NT)�reporting_enabled)�ds_deps�reporterrzh%s called with the following arguments: {hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}r�r-rz9Unable to determine hotplug state. No datasource detectedri�enabled�disabled)rqr-rrr�z*Received fatal exception handling hotplug!zExiting hotplug handler)r	�ReportEventStackrr�read_cfgr�setupLoggingrjr�update_configurationr^rcrdrrrr�r-ryr�print�sys�exitr�ro�	exception�flush_events)�name�args�hotplug_reporterrqr,r&r&r'�handle_args�sZ�
�

�����
���
r��__main__r*))rrGrrMr�r��	cloudinitrrr�cloudinit.eventrr�
cloudinit.netr�cloudinit.net.network_stater�cloudinit.reportingr	�cloudinit.sourcesr
r�cloudinit.stagesr�	getLoggerrDrcrr(�ABCr)rK�NETWORKrmrt�strryr�r��
parse_argsr�r&r&r&r'�<module>s8

22
(�(5
�