
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
\me#                 @   s0  d Z ddlZddlZddlZddlZddlZddlZddlmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ ejeZd
ZdddZG dd dejZG dd deZ de ej!fiZ"dd Z#ee$dddZ%edddZ&dd Z'edkr,e j( Z)e'ee) dS )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-hookc             C   s   | st jttd} t| _| jdddddgd | jdd	d
}d|_|jddd |jddd}|jdddddd |jddddddgd | S )a  Build 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--subsystemTzsubsystem to act onnet)requiredhelpchoiceszHotplug Actionhotplug_action)titledestqueryz0Query if hotplug is enabled for given subsystem.)r   ZhandlezHandle the hotplug event.z-dz	--devpathPATHzSysfs path to hotplugged device)r   metavarr   z-uz--udevactionzSpecify action to take.addremove)	argparseArgumentParserNAME__doc__r   add_argumentZadd_subparsersr   Z
add_parser)parserZ
subparsersZparser_handle r!   "/usr/lib/python3.6/hotplug_hook.py
get_parser   s:    



r#   c               @   s`   e Zd Zdd Zejdd Zeejdd Zeje	ddd	Z
d
d Zdd Zdd ZdS )UeventHandlerc             C   s"   || _ || _|| _|| _|| _d S )N)id
datasourcedevpathaction
success_fn)selfr%   r&   r'   r(   r)   r!   r!   r"   __init__K   s
    zUeventHandler.__init__c             C   s
   t  d S )N)NotImplementedError)r*   r!   r!   r"   applyR   s    zUeventHandler.applyc             C   s
   t  d S )N)r,   )r*   r!   r!   r"   configV   s    zUeventHandler.config)returnc             C   s
   t  d S )N)r,   )r*   r!   r!   r"   device_detected[   s    zUeventHandler.device_detectedc             C   sP   d }| j dkrd}n| j dkr$d}ntd| j  || j krLtd| j d S )Nr   Tr   FzUnknown action: %sz'Failed to detect %s in updated metadata)r(   
ValueErrorr0   RuntimeErrorr%   )r*   Zdetect_presencer!   r!   r"   detect_hotplugged_device_   s    

z&UeventHandler.detect_hotplugged_devicec             C   s   | j  S )N)r)   )r*   r!   r!   r"   successm   s    zUeventHandler.successc             C   s,   | j jtjg}|s(td| j tjf |S )Nz&Datasource %s not updated for event %s)r&   Zupdate_metadata_if_supportedr   HOTPLUGr2   )r*   resultr!   r!   r"   update_metadatap   s    
zUeventHandler.update_metadataN)__name__
__module____qualname__r+   abcabstractmethodr-   propertyr.   boolr0   r3   r4   r7   r!   r!   r!   r"   r$   J   s   r$   c                   s>   e Zd Z fddZdd Zedd Zeddd	Z  Z	S )

NetHandlerc                s*   t tjj|d}t j||||| d S )NZaddress)r   ospathbasenamesuperr+   )r*   r&   r'   r(   r)   r%   )	__class__r!   r"   r+   }   s    zNetHandler.__init__c             C   s|   | j jj| jdd tjj| j}| j jj }| j	dkrT|j
|sxtdj| jn$| j	dkrx|j|sxtdj| jd S )NF)Zbring_upr   zFailed to bring up device: {}r   zFailed to bring down device: {})r&   ZdistroZapply_network_configr.   r@   rA   rB   r'   Znetwork_activatorr(   Zbring_up_interfacer2   formatZbring_down_interface)r*   Zinterface_nameZ	activatorr!   r!   r"   r-      s    



zNetHandler.applyc             C   s   | j jS )N)r&   Znetwork_config)r*   r!   r!   r"   r.      s    zNetHandler.config)r/   c                s<   t  j} fdd|j D }tjd j| t|dkS )Nc                s    g | ]}|j d  jkr|qS )Zmac_address)getr%   ).0Ziface)r*   r!   r"   
<listcomp>   s   z.NetHandler.device_detected.<locals>.<listcomp>zIfaces with ID=%s : %sr   )r   r.   Ziter_interfacesLOGdebugr%   len)r*   Znetstatefoundr!   )r*   r"   r0      s
    

zNetHandler.device_detected)
r8   r9   r:   r+   r-   r=   r.   r>   r0   __classcell__r!   r!   )rD   r"   r?   |   s   r?   r   c             C   s\   yt | d }W n2 tk
rB } ztdj||W Y d d }~X nX tj| j| jtj	|dS )N   z4hotplug-hook: cannot handle events for subsystem: {})r&   cfgZevent_source_typescope)
SUBSYSTEM_PROPERTES_MAPKeyErrorr2   rE   r   Zupdate_event_enabledr&   rO   r   r5   )hotplug_init	subsystemrP   er!   r!   r"   
is_enabled   s    rV   )rS   rT   c             C   sR   t jd | jdd}|jtjgs4t jd| d S t| |sNt jd| d S |S )NzFetching datasourceZtrust)Zexistingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)rI   rJ   ZfetchZget_supported_eventsr   r5   rV   )rS   rT   r&   r!   r!   r"   initialize_datasource   s    

rW   )rS   c             C   s  t | |}|sd S t| d }tjd| ||||| jd}dddddg}td	}xt|D ]\}	}
tjd
||	t| yTtjd |j  |j	stjd |j
  tjd |j  tjd |j  P W q\ tk
r
 } z tjd| tj|
 |}W Y d d }~X q\X q\W |d S )Nr   zCreating %s event handler)r&   r'   r(   r)   rN         
      z#Bug while processing hotplug event.z!subsystem=%s update attempt %s/%szRefreshing metadataz$Detecting device in updated metadatazApplying config changezUpdating cachez,Exception while processing hotplug event. %s)rW   rQ   rI   rJ   Z_write_to_cache	Exception	enumeraterK   r7   Zskip_hotplug_detectr3   r-   r4   timeZsleep)rS   r'   rT   
udevactionr&   Zhandler_clsZevent_handlerZ
wait_timesZlast_exceptionZattemptwaitrU   r!   r!   r"   handle_hotplug   sD    







ra   c             C   s8  t j| tdd}tg |d}|j  tj|j d|jkrLtj	|jj
d tjd| |j|jd|krj|jnd d|krz|jnd  | yn|jdkryt||j}W n& tk
r   td	 tjd
 Y nX t|rdnd nt||j|j|jd W n" tk
r   tjd  Y nX W d Q R X tjd tj  d S )NT)Zreporting_enabled)Zds_depsZreporterr   zh%s called with the following arguments: {hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}r_   r'   r   z9Unable to determine hotplug state. No datasource detectedrN   ZenabledZdisabled)rS   r'   rT   r_   z*Received fatal exception handling hotplug!zExiting hotplug handler)r	   ZReportEventStackr   r   Zread_cfgr   Zsetup_loggingrO   r   Zupdate_configurationrF   rI   rJ   r   rT   r_   r'   rW   r   printsysexitra   r\   Z	exceptionZflush_events)nameargsZhotplug_reporterrS   r&   r!   r!   r"   handle_args   sF    



rg   __main__)N)*r   r;   r   Zloggingr@   rc   r^   Z	cloudinitr   r   r   Zcloudinit.eventr   r   Zcloudinit.netr   Zcloudinit.net.network_stater   Zcloudinit.reportingr	   Zcloudinit.sourcesr
   r   Zcloudinit.stagesr   Z	getLoggerr8   rI   r   r#   ABCr$   r?   ZNETWORKrQ   rV   strrW   ra   rg   
parse_argsrf   r!   r!   r!   r"   <module>   s4   

22((5

