Your IP : 216.73.217.13


Current Path : /usr/lib/python3/dist-packages/cloudinit/cmd/devel/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/cloudinit/cmd/devel/__pycache__/hotplug_hook.cpython-38.pyc

U

�Ad�#�@s(dZddlZddlZddlZddlZddlZddlmZmZm	Z	ddl
mZmZddl
mZddlmZddlmZddlmZmZdd	lmZe�e�Zd
Zddd�ZGd
d�dej�ZGdd�de�Zdeej fiZ!dd�Z"ee#d�dd�Z$ed�dd�Z%dd�Z&edk�r$e��'�Z(e&ee(�dS)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�|stjttd�}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.)rZhandlezHandle 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_argumentZadd_subparsersrZ
add_parser)�parserZ
subparsersZ
parser_handle�r!�B/usr/lib/python3/dist-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)r*r!r!r"�__init__Js
zUeventHandler.__init__cCs
t��dSr%��NotImplementedError�r+r!r!r"�applyQszUeventHandler.applycCs
t��dSr%r-r/r!r!r"�configUszUeventHandler.config��returncCs
t��dSr%r-r/r!r!r"�device_detectedZszUeventHandler.device_detectedcCsPd}|jdkrd}n|jdkr$d}ntd|j��||��krLtd|j��dS)NrTrFzUnknown action: %sz'Failed to detect %s in updated metadata)r)�
ValueErrorr4�RuntimeErrorr&)r+Zdetect_presencer!r!r"�detect_hotplugged_device^s

�z&UeventHandler.detect_hotplugged_devicecCs|��Sr%)r*r/r!r!r"�successlszUeventHandler.successcCs,|j�tjg�}|s(td|jtjf��|S)Nz&Datasource %s not updated for event %s)r'Zupdate_metadata_if_supportedr�HOTPLUGr6)r+�resultr!r!r"�update_metadataos�
��zUeventHandler.update_metadataN)�__name__�
__module__�__qualname__r,�abc�abstractmethodr0�propertyr1�boolr4r7r8r;r!r!r!r"r$Is
r$cs>eZdZ�fdd�Zdd�Zedd��Zed�dd	�Z�Z	S)
�
NetHandlercs*ttj�|�d�}t��|||||�dS)NZaddress)r�os�path�basename�superr,)r+r'r(r)r*r&��	__class__r!r"r,|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_configr1rDrErFr(Znetwork_activatorr)Zbring_up_interfacer6�formatZbring_down_interface)r+Zinterface_nameZ	activatorr!r!r"r0�s �


�


�zNetHandler.applycCs|jjSr%)r'Znetwork_configr/r!r!r"r1�szNetHandler.configr2cs<t�j�}�fdd�|��D�}t�d�j|�t|�dkS)Ncs g|]}|�d��jkr|�qS)Zmac_address)�getr&)�.0Zifacer/r!r"�
<listcomp>�s�z.NetHandler.device_detected.<locals>.<listcomp>zIfaces with ID=%s : %sr)rr1Ziter_interfaces�LOG�debugr&�len)r+Znetstate�foundr!r/r"r4�s

�zNetHandler.device_detected)
r<r=r>r,r0rAr1rBr4�
__classcell__r!r!rHr"rC{s

rCrc
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_PROPERTES_MAP�KeyError�	ExceptionrJrZupdate_event_enabledr'rTrr9)�hotplug_init�	subsystemrU�er!r!r"�
is_enabled�s ����r\)rYrZcCsRt�d�|jdd�}|�tjg�s4t�d|�dSt||�sNt�d|�dS|S)NzFetching datasourceZtrust)�existingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)rNrOZfetchZget_supported_eventsrr9r\)rYrZr'r!r!r"�initialize_datasource�s

r^)rYcCst||�}|sdSt|d}t�d|�|||||jd�}dddddg}td	�}t|�D]�\}	}
t�d
||	t|��zZt�d�|��|j	s�t�d�|�
�t�d
�|��t�d�|��W�qWqZtk
�r}zt�d|�t
�|
�|}W5d}~XYqZXqZ|�dS)NrzCreating %s event handler)r'r(r)r*rS���
�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)r^rVrNrOZ_write_to_cacherX�	enumeraterPr;Zskip_hotplug_detectr7r0r8�time�sleep)rYr(rZ�
udevactionr'Zhandler_clsZ
event_handlerZ
wait_timesZlast_exceptionZattempt�waitr[r!r!r"�handle_hotplug�sH
��




rhc	Cs8tj|tdd�}tg|d�}|��t�|j�d|jkrLt�	|j�
d��t�d||j
|jd|krj|jndd|krz|jnd�|��zn|j
dkr�zt||j�}Wn&tk
r�td	�t�d
�YnXt|r�dnd�nt||j|j|jd
�Wn"tk
�rt�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}rfr(rz9Unable to determine hotplug state. No datasource detectedrS�enabledZdisabled)rYr(rZrfz*Received fatal exception handling hotplug!zExiting hotplug handler)r	ZReportEventStackrrZread_cfgrZsetupLoggingrTrZupdate_configurationrKrNrOrrZrfr(r^r�print�sys�exitrhrXZ	exceptionZflush_events)�name�argsZhotplug_reporterrYr'r!r!r"�handle_args�sV�
�

���


ro�__main__)N))rr?rrDrkrdZ	cloudinitrrrZcloudinit.eventrrZ
cloudinit.netrZcloudinit.net.network_staterZcloudinit.reportingr	Zcloudinit.sourcesr
rZcloudinit.stagesrZ	getLoggerr<rNrr#�ABCr$rCZNETWORKrVr\�strr^rhro�
parse_argsrnr!r!r!r"�<module>s6

22(�(5