
    x[h-                        S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	J
r
Jr  SSKJr  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  \R<                  " \5      r Sr!SS jr" " S S\RF                  5      r$ " S S\$5      r%S\%\RL                  40r'S r(S\S\)4S jr*S\SS4S jr+SS jr,S\S\-4S jr.S r/\S:X  a  \"" 5       Ra                  5       r1\/" \!\15        gg)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-hookc                 j   U (       d  [         R                  " [        [        S9n [        U l        U R                  SSSSS/S9  U R                  SS	S
9nSUl        UR                  SSS9  UR                  SSS9nUR                  SSSSSS9  UR                  SSSSSS/S9  UR                  SSS9  U $ )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   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enablez%Enable hotplug for a given subsystem.)	argparseArgumentParserNAME__doc__r   add_argumentadd_subparsersr   
add_parser)parser
subparsersparser_handles      B/usr/lib/python3/dist-packages/cloudinit/cmd/devel/hotplug_hook.py
get_parserr,      s    ((dH F
"   &&%5 ' J JH   ))2 * M .   &!   >   M    c                       \ rS rSrS r\R                  S 5       r\\R                  S 5       5       r	\R                  S\
4S j5       rS rS rS	 rS
rg)UeventHandlerQ   c                 @    Xl         X l        X0l        X@l        XPl        g N)id
datasourcedevpathaction
success_fn)selfr3   r4   r5   r6   r7   s         r+   __init__UeventHandler.__init__R   s    &0$r-   c                     [        5       er2   NotImplementedErrorr8   s    r+   applyUeventHandler.applyY       !##r-   c                     [        5       er2   r<   r>   s    r+   configUeventHandler.config]   s     "##r-   returnc                     [        5       er2   r<   r>   s    r+   device_detectedUeventHandler.device_detectedb   rA   r-   c                     S nU R                   S:X  a  SnO+U R                   S:X  a  SnO[        SU R                   -  5      eXR                  5       :w  a  [        SU R                  -  5      eg )Nr   Tr   FzUnknown action: %sz'Failed to detect %s in updated metadata)r6   
ValueErrorrG   RuntimeErrorr3   )r8   detect_presences     r+   detect_hotplugged_device&UeventHandler.detect_hotplugged_devicef   sk    ;;%"O[[H$#O1DKK?@@22449DGGC  5r-   c                 "    U R                  5       $ r2   )r7   r>   s    r+   successUeventHandler.successt   s      r-   c                     U R                   R                  [        R                  /5      nU(       d+  [	        SU R                   < S[        R                  < 35      eU$ )NzDatasource z not updated for event )r4   update_metadata_if_supportedr   HOTPLUGrK   )r8   results     r+   update_metadataUeventHandler.update_metadataw   sO    ==
 ??I$5$57  r-   )r6   r4   r5   r3   r7   N)__name__
__module____qualname____firstlineno__r9   abcabstractmethodr?   propertyrC   boolrG   rM   rP   rV   __static_attributes__ r-   r+   r/   r/   Q   ss    % 	$ $ $  $ 	$ $ $!	r-   r/   c                   L   ^  \ rS rSrU 4S jrS r\S 5       rS\4S jr	Sr
U =r$ )
NetHandler   c                 z   > [        [        R                  R                  U5      S5      n[        TU ]  XQX#U5        g )Naddress)r
   ospathbasenamesuperr9   )r8   r4   r5   r6   r7   r3   	__class__s         r+   r9   NetHandler.__init__   s/    rww//8)D*Er-   c                 &   U R                   R                  R                  U R                  SS9  [        R
                  R                  U R                  5      nU R                   R                  R                  5       nU R                  S:X  a;  UR                  U5      (       d$  [        SR                  U R                  5      5      eg U R                  S:X  a;  UR                  U5      (       d$  [        SR                  U R                  5      5      eg g )NF)bring_upr   zFailed to bring up device: {}r   zFailed to bring down device: {})r4   distroapply_network_configrC   rg   rh   ri   r5   network_activatorr6   bring_up_interfacerK   formatbring_down_interface)r8   interface_name	activators      r+   r?   NetHandler.apply   s    33KK 	4 	
 ))$,,7OO**<<>	;;%//??"3::4<<H  @ [[H$11.AA"5<<T\\J  B %r-   c                 .    U R                   R                  $ r2   )r4   network_configr>   s    r+   rC   NetHandler.config   s    ---r-   rE   c                    [        U R                  5      nUR                  5        Vs/ s H&  nUR                  S5      U R                  :X  d  M$  UPM(     nn[
        R                  SU R                  U5        [        U5      S:  $ s  snf )Nmac_addresszIfaces with ID=%s : %sr   )r   rC   iter_interfacesgetr3   LOGdebuglen)r8   netstateifacefounds       r+   rG   NetHandler.device_detected   sz    (5 "113
3yy'4772 3 	 

 			*DGGU;5zA~
s   #BBra   )rX   rY   rZ   r[   r9   r?   r^   rC   r_   rG   r`   __classcell__)rk   s   @r+   rc   rc      s3    F
$ . .  r-   rc   r   c                      [         U   S   n[        R
                  " U R                  U R                  [        R                  US9$ ! [         a   n[        SR                  U5      5      UeS nAff = f)N   z4hotplug-hook: cannot handle events for subsystem: {})r4   cfgevent_source_typescope)
SUBSYSTEM_PROPERTIES_MAPKeyErrorrK   rs   r   update_event_enabledr4   r   r   rT   )hotplug_init	subsystemr   es       r+   
is_enabledr      s~    (3A6 &&**#++	   BII
 		s   A 
A1A,,A1r   r   c                    [         R                  S5        U R                  SS9nUR                  [        R
                  /5      (       d  [         R                  SU5        g [        X5      (       d  [         R                  SU5        g U$ )NzFetching datasourcetrustexistingz*hotplug not supported for event of type %sz(hotplug not enabled for event of type %s)r   r   fetchget_supported_eventsr   rT   r   )r   r   r4   s      r+   initialize_datasourcer      sq    II#$##W#5J**I,=,=+>??		>	Jl..		<iHr-   rE   c                 j   [        X5      nU(       d  g [        U   S   n[        R                  SU5        U" UUUU R                  S9n[
        R
                  " 5       nUR                  R                  (       d  [        X&U5        g [
        R
                  " 5       U-
  UR                  R                  :  a~  [        X&U5        [        R                  S5        [
        R                  " UR                  R                  5        [
        R
                  " 5       U-
  UR                  R                  :  a  M}  g g )Nr   zCreating %s event handler)r4   r5   r6   r7   z>Gathering network configuration again due to IMDS limitations.)r   r   r   r   _write_to_cachetimehotplug_retry_settingsforce_retrytry_hotplugsleep_totalsleepsleep_period)r   r5   r   
udevactionr4   handler_clsevent_handlerstarts           r+   handle_hotplugr      s    &|?J*95a8KII)95#.//	$M IIKE,,88Ij9
))+

 A A M M
MIj9		L	
 	

:44AAB ))+

 A A M M
Mr-   c           	      h   / SQn[        S5      n[        U5       H  u  pV[        R                  SU U[	        U5      5         [        R                  S5        UR                  5         UR                  (       d%  [        R                  S5        UR                  5         [        R                  S5        UR                  5         [        R                  S5        UR                  5           g    Ue! [          a:  n[        R                  SU5        [        R                  " U5        Un S nAGM  S nAff = f)	N)r         
      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)	Exception	enumerater   r   r   rV   skip_hotplug_detectrM   r?   rP   r   r   )r   r   r4   
wait_timeslast_exceptionattemptwaitr   s           r+   r   r      s    "JDEN":.		/
O		
	II+,))+11		@A668II./!II&'!!## /.   	IIDaHJJtN	s   B%C--
D17.D,,D1c                    U R                  SS9nU(       d  g[        U   S   n[        R                  UR	                  [        R                  /5      R                  U[        5       5      ;   nU(       d  [        SU 3[        R                  S9  g[        R                  " U R                  5      nUR                  US   ;   a  [        SU S	3[        R                  S9  g
US   R                  UR                  5        [        R                  " U R                  R!                  S5      ["        R$                  " U5      SSS9  ['        US
U R(                  S9  g
)Nr   r   Fr   z#hotplug not supported for event of )filescopesz)Not installing hotplug for event of type z. Reason: Already done.Tzhotplug.enabledwi  )omodemode)network_hotplug_enabledr   )r   r   r   rT   r   r~   setprintsysstderrr   read_hotplug_enabled_filepathsvalueappend
write_file	get_cpathjsondumpsr   r   )r   r   r4   r   hotplug_supportedhotplug_enabled_files         r+   enable_hotplugr     s9   ##W#5J$Y/2E!))''):):(;<@@N 1)=CJJ	
 99,:L:LM{{*8447	{ C% %	

 "))%++6OO$$%67

'(	 Dl6F6F r-   c           	         [         R                  " U [        SS9n[        / US9nUR	                  5         [
        R                  " UR                  5        SUR                  ;   a/  [        R                  " UR                  R                  S5      5        [        R                  SU UR                  UR                  SU;   a  UR                  OS SU;   a  UR                   OS 5        U    UR                  S:X  a+   [#        X1R                  5      n['        W(       a  SOS5        OUR                  S:X  a+  [-        UUR                   UR                  UR                  S9  O[.        R0                  " 5       S:w  a5  [(        R2                  R5                  S5        [(        R*                  " S
5        [7        X1R                  S9(       d  [(        R*                  " S
5        ['        SUR                   35         S S S 5        [        R                  S5        [        R<                  " 5         g ! [$         a%    ['        S	5        [(        R*                  " S
5         GNFf = f! [8         a    [        R;                  S5        e f = f! , (       d  f       N= f)NT)reporting_enabled)ds_depsreporterr   zh%s called with the following arguments: {hotplug_action: %s, subsystem: %s, udevaction: %s, devpath: %s}r   r5   r   z9Unable to determine hotplug state. No datasource detectedr   enableddisabledr   )r   r5   r   r   r   z9Root is required. Try prepending your command with sudo.
)r   r   z)Enabled cloud-init hotplug for subsystem=z*Received fatal exception handling hotplug!zExiting hotplug handler)r   ReportEventStackr$   r   read_cfgr	   setup_loggingr   r   update_configurationr~   r   r   r   r   r   r5   r   r   r   r   exitr   rg   getuidr   writer   r   	exceptionflush_events)nameargshotplug_reporterr   r4   s        r+   handle_argsr   '  s    ..g -=>L,**+l&&&&&|'7'7';';K'HIII	J'4/T!T)t 
&	""g-	 !6$nn"J :i:>$$0!- LL"nn#	 99;!#JJ$$# HHQK%!- HHQK!!% 02A 
R II'(G 3  # HHQK <  	MMFG	K 
	sa   (I>*I;H(I$I>%:II> BI4I>(+IIII!I;;I>>
J__main__r2   )rE   N)2r$   r\   r!   r   loggingrg   r   r   	cloudinitr   r   r   #cloudinit.config.cc_install_hotplugr   cloudinit.eventr   r   cloudinit.logr	   cloudinit.netr
   cloudinit.net.network_stater   cloudinit.reportingr   cloudinit.sourcesr   r   cloudinit.stagesr   	getLoggerrX   r   r#   r,   ABCr/   rc   NETWORKr   r   strr   r   r   r_   r   r   
parse_argsr   ra   r-   r+   <module>r      s   0 
    	 
  - - ? 1 ! + = & E !!3l/CGG /d$ $P 
J
**+ 
$  C C$ C0:   T  FBJ z<""$Dd r-   