
    VhV                        d dl m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mZ d dlmZmZ d dlmZ d dlmZ  e       Z G d	 d
e      Z G d de      Zy)    )annotationsN)datetime	timedeltatimezone)AnsibleErrorAnsibleConnectionFailure)	to_nativeto_text)check_type_listcheck_type_str)
ActionBase)Displayc                      e Zd Zy)TimedOutExceptionN)__name__
__module____qualname__     M/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/action/reboot.pyr   r      s    r   r   c                      e Zd ZdZ ed      ZdZdZdZdZ	dZ
dZdZd	Zd
ZdZi ZddddddddZdddZdddeddddddd
ZdddZ fdZed        Zed        Zd Zd Zd  Zd! Zd" Zd# Zd$ Zd% Z d& Z!d+d'Z"d( Z#d,d)Z$d, fd*	Z% xZ&S )-ActionModuleF)	boot_time_commandconnect_timeoutmsgpost_reboot_delaypre_reboot_delayreboot_commandreboot_timeoutsearch_pathstest_commandiX  Nr   whoamiz#cat /proc/sys/kernel/random/boot_idzReboot initiated by Ansibleshutdownz-r {delay_min} "{message}"Tz/sbin/sysctl kern.boottimezwho -bz/grep booted /var/log/vmksummary.log | tail -n 1)freebsdopenbsdmacosxsolarissunosvmkernelaixreboot)alpiner)    z-r +{delay_min} "{message}"z-r +{delay_sec}s "{message}"z"-y -g {delay_sec} -i 6 "{message}"z-d {delay_sec}z-Fr)
r,   voidr$   linuxr&   r%   r'   r(   r)   r*   who)r'   r)   c                ,    t        t        | 
  |i | y N)superr   __init__)selfargskwargs	__class__s      r   r4   zActionModule.__init__U   s    lD*D;F;r   c                :    | j                  d| j                        S )Nr   )_check_delayDEFAULT_PRE_REBOOT_DELAYr5   s    r   r   zActionModule.pre_reboot_delayX   s      !3T5R5RSSr   c                :    | j                  d| j                        S )Nr   )r:   DEFAULT_POST_REBOOT_DELAYr<   s    r   r   zActionModule.post_reboot_delay\   s      !4d6T6TUUr   c           	         t        | j                  j                  j                  || j                  j                  j                  |dz   |                  }|dk  rd}|S )z)Ensure that the value is positive or zero_secr   )int_taskr6   get)r5   keydefaultvalues       r   r:   zActionModule._check_delay`   sK    DJJOO''TZZ__-@-@vw-WXY19Er   c                    t        | |      }|j                  |d   |d   z   |j                  |d   |j                  |d   t        | |                        }|S )zXGet dist+version specific args first, then distribution, then family, lastly use defaultnameversionfamily)getattrrC   )r5   variable_namedistributiondefault_valueattrrF   s         r   _get_value_from_factsz"ActionModule._get_value_from_factsg   sg    t]+ <	#::HHV$ *D-0234 r   c                   | j                   j                  j                  d      }|$	 t        |d      }	 |j                  dd      d   S | j                  d|d	      }| j                  d
z  }| j                   j                  j                  d| j                        }|j                  | j                  ||      S # t        $ r}t        dt        |      z        d }~ww xY w# t        $ r Y yw xY w)Nr   Fallow_conversion-Invalid value given for 'reboot_command': %s.    r-   SHUTDOWN_COMMAND_ARGSDEFAULT_SHUTDOWN_COMMAND_ARGS<   r   )	delay_sec	delay_minmessage)rB   r6   rC   r   	TypeErrorr   r	   split
IndexErrorrP   r   DEFAULT_REBOOT_MESSAGEformat)r5   rM   r   er6   r[   reboot_messages          r   get_shutdown_command_argsz&ActionModule.get_shutdown_command_argss   s    ,,-=>%c!/QV!W
%++C3A66 --.E|UtuD --3I!ZZ__008S8STN;;)>)>)]k;ll  c"#RU^_`Ua#abbc  s(   B< C$ <	C!CC!$	C0/C0c           	        i }t         j                  dj                  | j                  j                               | j                  |dddi      }	 |j                  dd      rOt        d	j                  t        |d
         j                         t        |d         j                                     |d   d   j                         |d<   t        |d   d   j                  d      d         |d<   t        |d   d   j                               |d<   t         j                  dj                  | j                  j                  |             |S # t        $ r,}t        dj                  |j                  d               d }~ww xY w)Nz2{action}: running setup module to get distributionactionzansible.legacy.setupgather_subsetmin	task_varsmodule_namemodule_argsfailedFz1Failed to determine system distribution. {0}, {1}module_stdoutmodule_stderransible_factsansible_distributionrH   ansible_distribution_version.r   rI   ansible_os_familyrJ   z{action}: distribution: {dist})rg   distz@Failed to get distribution information. Missing "{0}" in output.)displaydebugra   rB   rg   _execute_modulerC   r   r	   striplowerr
   r^   KeyErrorr6   )r5   rk   rM   module_outputkes        r   get_distributionzActionModule.get_distribution   s   JQQY]YcYcYjYjQkl,,.(%0 - 2	v  51"#V#]#]mO<=CCEmO<=CCE$G H H $1#ABX#Y#_#_#aL &-mO.LMk.l.r.rsv.wxy.z&{L#%,]?-KL_-`-f-f-h%iL"MM:AAIZIZamAno 	vahhikipipqristuu	vs   C=E 	F'FFc                    | j                   j                  j                  d      }|$	 t        |d      }|j                  dd      d   }n| j                  d|d	      }|d   d
k(  r|S g d}| j                   j                  j                  d|      }	 t        |      }t        j                  dj                  | j                   j                  ||             | j                  |d||gdd      }	|	d   D 
cg c]  }
|
d   	 }}
|st        dj                  ||            |d   S # t        $ r}t        dt        |      z        d }~ww xY w# t        $ r d}t        |j                  |            w xY wc c}
w )Nr   FrR   rT   rU   rV   r   SHUTDOWN_COMMANDSDEFAULT_SHUTDOWN_COMMAND/)z/sbinz/binz	/usr/sbinz/usr/binz/usr/local/sbinr    z@'search_paths' must be a string or flat list of strings, got {0}zL{action}: running find module looking in {paths} to get path for "{command}")rg   commandpathszansible.legacy.findany)r   patterns	file_typerj   filespathz1Unable to find command "{0}" in search paths: {1})rB   r6   rC   r   r]   r   r	   r^   rP   r   ra   rw   rx   rg   ry   )r5   rk   rM   r   rb   shutdown_bindefault_search_pathsr    err_msgfind_resultx	full_paths               r   get_shutdown_commandz!ActionModule.get_shutdown_command   s   ,,-=>%c!/QV!W *//Q7:L556I<YstL?c!#` ::??..~?STLA.|<
 MMhoozz(($" p $ %
 ..#1)!-!&	 / 	K -8,@Aq6AIA"#V#]#]^jlx#yzzQ<M  c"#RU^_`Ua#abbc  A\"7>>,#?@@A( Bs)   D* E :E;*	E3E

E&E8c                
   | j                   j                         D ]f  \  }}| j                  j                  j	                  |      ,t
        j                  dj                  ||| j                  j                               h y )NzGSince Ansible {version}, {arg} is no longer a valid option for {action})rI   argrg   )	DEPRECATED_ARGSitemsrB   r6   rC   rw   warningra   rg   )r5   r   rI   s      r   deprecated_argszActionModule.deprecated_args   sp     00668 	/LCzz""3'3 i p p#::,, !q !. /	/r   c           
     D   | j                  d|d      }| j                  j                  j                  d      r3| j                  j                  j                  d      }	 t	        |d       t        j                  dj                  | j                  j                  |             | j                  || j                  	      }|d
   dk7  rQ|d   }|d   }t        dj                  | j                  j                  |d
   t        |      t        |                  t        j                  dj                  | j                  j                  |d   j                                      |d   j                         S # t
        $ r}t        dt        |      z        d }~ww xY w)NBOOT_TIME_COMMANDSDEFAULT_BOOT_TIME_COMMANDr   FrR   z0Invalid value given for 'boot_time_command': %s.z5{action}: getting boot time with command: '{command}'rg   r   sudoablercr   stdoutstderrzS{action}: failed to get host boot time info, rc: {rc}, stdout: {out}, stderr: {err})rg   r   outerrz {action}: last boot time: {boot})rg   boot)rP   rB   r6   rC   r   r]   r   r	   rw   rx   ra   rg   _low_level_execute_commandDEFAULT_SUDOABLErz   )r5   rM   r   rb   command_resultr   r   s          r   get_system_boot_timez!ActionModule.get_system_boot_time   s    667K\[vw::??23 $

 3 34G Hf05I 	MTT\`\f\f\m\m  xIT  J  	K889JUYUjUj8k$1$#H-F#H-Ft{{&*jj&7&7"0"6#,V#4#,V#4	  |  6 7 7
 	8??tzzGXGX_mnv_w_}_}_?  A  	Bh'--//  f"#UXabcXd#deefs   E: :	FFFc                8   t         j                  dj                  | j                  j                               | j                  j
                  j                  d| j                  j
                  j                  d| j                              }|rq	 t         j                  dj                  | j                  j                  |             | j                  j                  d|       | j                  j                          	 | j                  |      }t#        |      d
k(  s||k(  rt%        d      y # t        $ rK 	 | j                  j                  d|       n*# t        t        f$ r t         j                  d	       Y nw xY wY w xY w# t         $ r}|d }~ww xY w)Nz,{action}: attempting to get system boot timerf   r   connect_timeout_secz,{action}: setting connect_timeout to {value}rg   rF   connection_timeouttimeoutzHConnection plugin does not allow the connection timeout to be overriddenr   zboot time has not changed)rw   vvvra   rB   rg   r6   rC   DEFAULT_CONNECT_TIMEOUTrx   _connection
set_optionr   AttributeErrorr   resetr   	Exceptionlen
ValueError)r5   rM   previous_boot_timer   current_boot_timerb   s         r   check_boot_timezActionModule.check_boot_time   s   BIIQUQ[Q[QbQbIcd**//--.?ATATUjlp  mI  mI  BJ  K pLSS[_[e[e[l[l  uDS  E  F  ++,@/R ""$	 $ 9 9, G  !Q&*;?Q*Q899 +R!   pp$$//	?K$n5 pOO$nopp  	G	sO   AD2 F	 2	F<EF$F =F?F  FF		FFFc                   | j                   j                  j                  d| j                  d|d            }t        j                  dj                  | j                   j                               t        j                  dj                  | j                   j                  |             	 | j                  || j                        }|d	   d
k7  r6dj                  t        |d         t        |d               }t!        |      t        j                  dj                  | j                   j                               y # t        $ r, 	 | j                  j                           # t        $ r Y  w xY ww xY w)Nr!   TEST_COMMANDSDEFAULT_TEST_COMMANDz-{action}: attempting post-reboot test commandrf   z9{action}: attempting post-reboot test command '{command}'r   r   r   r   z Test command failed: {err} {out}r   r   )r   r   z&{action}: system successfully rebooted)rB   r6   rC   rP   rw   r   ra   rg   rx   r   r   r   r   r   r   r	   RuntimeError)r5   rM   r7   r!   r   r   s         r   run_test_commandzActionModule.run_test_command  sj   zz**>4;U;UVegs  vL  <M  NCJJRVR\R\RcRcJdeQXX`d`j`j`q`q  |HX  I  	J		!<<\TXTiTi<jN $1$4;;nX67nX67 < 9C s##<CC4::K\K\C]^  	  &&(  " 	s0   ,E 	E6E&%E6&	E2/E61E22E6c                   t        j                  t        j                        t	        |      z   }|i }d}d}d}	t        j                  t        j                        |k  rH	  |dd|i| |r:t
        j                  dj                  | j                  j                  |             y |	r/d|	 }t
        j                  |       t
        j)                  |       t/        dj                  ||            # t        $ r}
t        |
t              r+	 | j                  j                          n# t        $ r Y nw xY wt        j                   d      dz  }d	|z  |z   }||kD  r||z   }|r	 t#        |
      j%                         d
   }n!# t&        $ r}
t#        |
      }Y d }
~
nd }
~
ww xY w| j                  j                   d| d| d}	|	 d|dd}t
        j                  |       t
        j)                  |       |dz  }t+        j,                  |       Y d }
~
nd }
~
ww xY wt        j                  t        j                        |k  r)N)secondsr      r-   rM   z{action}: {desc} success)rg   desci     z: z fail ''z, retrying in z.4fz seconds...rV   z2Last error message before the timeout exception - z0Timed out waiting for {desc} (timeout={timeout}))r   r   r   )r   nowr   utcr   rw   rx   ra   rB   rg   r   
isinstancer   r   r   secrets	randbelowr
   
splitlinesr_   r   timesleepr   )r5   rg   r   action_descrM   action_kwargsmax_end_time
fail_countmax_fail_sleeplast_error_msgrb   
random_int
fail_sleeperrorr   s                  r   do_until_success_or_timeoutz(ActionModule.do_until_success_or_timeout  s   ||HLL1In4UU M
ll8<<(<7'BLBMBMM"<"C"C4::K\K\cn"C"op4 F~FVWCMM#KK R Y Y_j  uC Y  !D  E  	E;  'a!9:((..03  %..t4t;
*_z9
.!/*!<J+ '
 5 5 7 ;% + '
+(,

(9(9':"[MQVPWWX%YN+,N:c:J+VCMM#&KK$a


:&&/' ll8<<(<7sh   "AC6 6
H HD,+H,	D85H7D88/H(FH	F#FHF##A/HHc           	     h   i }i }| j                  ||      }| j                  |      }dj                  ||      }	 t        j	                  dj                  | j
                  j                               t        j                  dj                  | j
                  j                  |             | j                  || j                        }t        j                  t        j                        |d<   |d
   d	k7  rVd|d<   d|d<   dj                  t!        |d   j#                               t!        |d   j#                                     |d<   |S d|d<   |S # t        $ rR}t        j                  dj                  | j
                  j                  t        |                   d	|d
<   Y d }~d }~ww xY w)Nz{0} {1}z{action}: rebooting server...rf   z3{action}: rebooting server with command '{command}'r   r   z>{action}: AnsibleConnectionFailure caught and handled: {error}rg   r   r   r   startTrn   Frebootedz6Reboot command failed. Error was: '{stdout}, {stderr}'r   r   )r   r   r   )r   rd   ra   rw   r   rB   rg   rx   r   r   r   r
   r   r   r   r   r	   rz   )	r5   rk   rM   resultreboot_resultshutdown_commandshutdown_command_argsr   rb   s	            r   perform_rebootzActionModule.perform_rebootK  s   44YM $ > >| L"))*:<QR	$KK7>>djjFWFW>XYMMOVV^b^h^h^o^o  zHV  I  J ;;NUYUjUj;kM #,,x||4w!##F8!&F:T[[ x!8!>!>!@A x!8!>!>!@A \ CF5M M x! ( 	$MMZaaimisisiziz  CJ  KL  CMa  N  O"#M$	$s   BE 	F1AF,,F1c           	        t         j                  dj                  | j                  j                               i }	 t        | j                  j                  j                  d| j                  j                  j                  d| j                                    }| j                  | j                  d|||       	 | j                  j                  d      }||k7  rr	 t         j                  dj                  | j                  j                  |	             	 | j                  j                  d|       | j                  j!                          | j                  | j(                  d|||       d|d<   d|d<   |S # t        $ r/ 	 | j                  j                  d
|       n# t        $ r  w xY wY }w xY w# t        t"        f$ rM}t         j                  dj                  | j                  j                  t%        |                   Y d }~d }~ww xY w# t&        $ r/ 	 | j                  j                  d
      }n# t&        $ r Y nw xY wY w xY w# t*        $ r$}d|d<   d|d<   t%        |      |d<   |cY d }~S d }~ww xY w)Nz{action}: validating rebootrf   r   reboot_timeout_seczlast boot time check)rg   r   r   rM   r   r   zK{action}: setting connect_timeout/timeout back to original value of {value}r   r   zE{action}: failed to reset connection_timeout back to default: {error}r   zpost-reboot test commandTr   changedrn   r   )rw   r   ra   rB   rg   rA   r6   rC   DEFAULT_REBOOT_TIMEOUTr   r   r   
get_optionrx   r   r   r   r   r
   r|   r   r   )	r5   rM   original_connection_timeoutr   r   r   r   rb   toexs	            r   validate_rebootzActionModule.validate_rebooth  s   188

@Q@Q8RS5	 !4!45EtzzGZGZ[oqu  rM  rM  HN  "O  PN,,++2-)+ - -H"&"2"2"="=>R"S //AH&s&z&z  CG  CM  CM  CT  CT B] '{ '^ _& ,,778LNij ((..0 ,,,,6-)+ - - "&F: $F9 ;  , && $ 0 0 ; ;IGb c#/ & %& !d& ).9 H&m&t&t  }A  }G  }G  }N  }N |C  DE  |F 'u 'G H HH'  &*&6&6&A&A)&LO F ! 	#F8!%F:#DMF5MM		s   A=I ;H I :F4 E9 3F4 *I 9	F1F F1 F++F1.F4 0F11F4 4HAHI HI 	IH98I9	IIIII 
II 	I;I60I;6I;c                d   d| _         | j                  j                  dk(  r-dj                  | j                  j
                        }dddd|dS | j                  j                  rddddS |i }| j                          t        t        | +  ||      }|j                  dd      s|j                  d	d      r|S | j                  |      }	 | j                  |      }d }t         j#                  dj                  | j                  j
                  |             	 | j                  j%                  d      }| j)                  ||      }	|	d	   r<|	}t+        j,                  t.        j0                        |	d   z
  }
|
j2                  |d<   |S | j4                  dk7  rt         j#                  dj                  | j                  j
                  | j4                               t         j7                  dj                  | j                  j
                  | j4                               t9        j:                  | j4                         | j=                  ||d|i      }t+        j,                  t.        j0                        |	d   z
  }
|
j2                  |d<   |S # t        $ r$}d|d	<   d|d
<   t        |      |d<   |cY d }~S d }~ww xY w# t&        $ ri 	 | j                  j%                  d      }nH# t&        $ r< t         j#                  dj                  | j                  j
                               Y nw xY wY w xY w)NTlocalz@Running {0} with local connection would reboot the control node.Fr   )r   elapsedr   rn   r   )r   r   r   skippedrn   r+   r   z6{action}: saving original connect_timeout of {timeout})rg   r   r   r   z<{action}: connect_timeout connection option has not been setrf   r   r   z/{action}: waiting an additional {delay} seconds)rg   delayr   )r   )_supports_check_moder   	transportra   rB   rg   
check_moder   r3   r   runrC   r   r   r   r
   rw   rx   r   r|   r   r   r   r   r   r   r   r   r   r   r   )r5   tmprk   r   r   rM   r   rb   r   r   r   r8   s              r   r   zActionModule.run  s    $(! %%0T[[\`\f\f\m\mnC$QU^abb::  #tDDI|T.sI>::i'6::h+FM,,Y7	!%!:!:<!H '+#NUU]a]g]g]n]n  yTU  U  	V	*.*:*:*E*EFZ*[' ++I|D""Fll8<<0=3IIG 'F9M!!Q&MMKRRZ^ZdZdZkZksw  tJ  tJR  K  LKKIPPX\XbXbXiXiqu  rH  rHP  I  JJJt--. %%l4O`t  wI  `J%  K,,x||,}W/EE#OOyM  	#F8$F8#AJF5MM		  	.2.>.>.I.I).T+ \cckokukuk|k|c}~	sU   J J= 	J:J5/J:5J:=	L/K#"L/#AL(%L/'L((L/.L/r2   )NN)'r   r   r   TRANSFERS_FILES	frozenset_VALID_ARGSr   r   r;   r>   r   r   r`   r   rX   r   r   r   r   rW   r   r4   propertyr   r   r:   rP   rd   r   r   r   r   r   r   r   r   r   r   __classcell__)r8   s   @r   r   r      s@   O 
 
K !"  !# E:)$@!O 0/E  -1./075$ M
< T T V V
m*v*+ Z/00:8_.,E\:;z@ @r   r   )
__future__r   r   r   r   r   r   ansible.errorsr   r   +ansible.module_utils.common.text.convertersr	   r
   &ansible.module_utils.common.validationr   r   ansible.plugins.actionr   ansible.utils.displayr   rw   r   r   r   r   r   r   <module>r      sF   
 #   2 2 A J R - )
)		 	L: Lr   