
    Vh%                        d dl 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 d dlmZ  e       Zd Z G d d	e      Z G d
 de      Zy)    )annotations)AnsibleErrorAnsibleConnectionFailure)	to_nativeto_text	is_string)
ActionBase)Displayc                :    t        | |         j                         S N)r   strip)mappingkeys     m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/action/shutdown.pyfmtr      s    WS\"((**    c                      e Zd Zy)TimedOutExceptionN)__name__
__module____qualname__ r   r   r   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	d
dZddde
ddddddd
Z fdZed        Zd Zd Zd Zd Zd Zd fd	Z xZS )ActionModuleF)msgdelaysearch_pathsNr   zShut down initiated by Ansibleshutdownz-h {delay_min} "{message}"Tpoweroffhalt)alpinevmkernel z-h +{delay_min} "{message}"z-p +{delay_sec}s "{message}"z"-y -g {delay_sec} -i 5 "{message}"z-d {delay_sec}z-Fh)
r"   voidfreebsdlinuxmacosxopenbsdsolarissunosr#   aixc                ,    t        t        | 
  |i | y r   )superr   __init__)selfargskwargs	__class__s      r   r/   zActionModule.__init__=   s    lD*D;F;r   c                :    | j                  d| j                        S )Nr   )_check_delayDEFAULT_PRE_SHUTDOWN_DELAY)r0   s    r   r   zActionModule.delay@   s      $*I*IJJr   c                r    t        | j                  j                  j                  ||            }|dk  rd}|S )z)Ensure that the value is positive or zeror   )int_taskr1   get)r0   r   defaultvalues       r   r5   zActionModule._check_delayD   s2    DJJOO''W56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)getattrr:   )r0   variable_namedistributiondefault_valueattrr<   s         r   _get_value_from_factsz"ActionModule._get_value_from_factsK   sg    t]+ <	#::HHV$ *D-0234 r   c           	     v   i }t         j                  | j                  j                   d       | j	                  |dddi      }	 |j                  dd      r%t        dt        |d	       d
t        |d             |d   d   j                         |d<   t        |d   d   j                  d      d         |d<   t        |d   d   j                               |d<   t         j                  | j                  j                   d|        |S # t        $ r!}t        d|j                  d    d      d }~ww xY w)Nz*: running setup module to get distributionzansible.legacy.setupgather_subsetmin	task_varsmodule_namemodule_argsfailedFz)Failed to determine system distribution. module_stdout, module_stderransible_factsansible_distributionr>   ansible_distribution_version.r   r?   ansible_os_familyr@   z: distribution: z1Failed to get distribution information. Missing "z" in output.)displaydebugr9   action_execute_moduler:   r   r   lowerr   splitKeyErrorr1   )r0   rK   rC   module_outputkes        r   get_distributionzActionModule.get_distributionW   sw   **++UVW,,.(%0 - 2
	m  51"%NsS`bqOrNssuvy  {H  JY  wZ  v[  $\  ]  ]#0#ABX#Y#_#_#aL &-o./MNTTUXYZ[\'^L#%,]?-KL_-`-f-f-h%iL"MMTZZ..//?~NO 	m!RSUSZSZ[\S]R^^jkll	ms   CD 	D8D33D8c           	          fd} j                  d|d      }g d} j                  j                  j                  d|      }t	        |      r|g}	 t        d |D              }t        |t              r|rt        	  |||      }	|	sHt        j                  d| d	| d
       ddg}
 |d|
      }	|	st        d| d	| d|
 d      |	d    dS  j                  d|d      } j                  } j                  j                  j                  d j                        }|j                  ||dz  |      }|	d    d| S # t        $ r d| }t        |      w xY w)Nc                    t         j                  j                  j                   d| d|  d       j	                  d|| gdd      }|d   D cg c]  }|d	   	 c}S c c}w )
Nz!: running find module looking in z to get path for ""zansible.legacy.findany)pathspatterns	file_typerJ   filespath)rW   rX   r9   rY   rZ   )commandfind_search_pathsfind_resultxr0   rK   s       r   find_commandz7ActionModule.get_shutdown_command.<locals>.find_commandl   s    MMTZZ..//PQbPccuv}u~~  A  B..#1.!(	!&	 / 	K (37';<!AfI<<<s   A#SHUTDOWN_COMMANDSDEFAULT_SHUTDOWN_COMMAND)z/sbinz	/usr/sbinz/usr/local/sbinr   c              3  4   K   | ]  }t        |         y wr   r   ).0rm   s     r   	<genexpr>z4ActionModule.get_shutdown_command.<locals>.<genexpr>   s      HaYq\!1 Hs   z='search_paths' must be a string or flat list of strings, got zUnable to find command "z" in search paths: z1, will attempt a shutdown using systemd directly.z/binz/usr/bin	systemctlzCould not find command "z' or systemctl command in search paths: z, unable to shutdown.r   z	 poweroffSHUTDOWN_COMMAND_ARGSDEFAULT_SHUTDOWN_COMMAND_ARGSr   <   )	delay_sec	delay_minmessage )rF   r9   r1   r:   r	   rd   
isinstancelist	TypeErrorr   rW   vvvr   DEFAULT_SHUTDOWN_MESSAGEformat)r0   rK   rC   rn   shutdown_bindefault_search_pathsr   incorrect_typeerr_msg	full_pathsystemctl_search_pathsr1   rx   shutdown_messageafs   ``             r   get_shutdown_commandz!ActionModule.get_shutdown_commandk   s   	= 112E|UopHzz**>;OP \"(>L	(  H< HHNlD1^ 6D !|<	 KK2<.@ST`Sa  bS  T  U&,j%9"$[2HII".|n<OP\~ ^11G0HH]_` ` $A,y11 ))*A<QpqJJ	::??..ud6S6ST[[9	RQa[bA,q%%5  	(UVbUcdGw''	(s   *E Ec                   i }i }| j                  ||      }| j                  d       	 t        j                  | j                  j
                   d       t        j                  | j                  j
                   d| d       | j                  j                  rd|d<   n| j                  || j                        }|d   dk7  r+d|d
<   d|d<   dt        |d       dt        |d       |d<   |S d|d
<   ||d<   |S # t        $ r=}t        j                  | j                  j
                   d	|        d|d<   Y d }~d }~ww xY w)NT)forcez: shutting down server...z%: shutting down server with command ''r   rc)sudoablez/: AnsibleConnectionFailure caught and handled: rN   Fr   z#Shutdown command failed. Error was stdoutrP   stderrr   shutdown_command)r   cleanuprW   r   r9   rY   rX   _play_context
check_mode_low_level_execute_commandDEFAULT_SUDOABLEr   r   )r0   rK   rC   resultshutdown_resultshutdown_command_execes          r   perform_shutdownzActionModule.perform_shutdown   sp    $ 9 9)\ R4 	&KK4::,,--FGHMMTZZ..//TUjTkklmn!!,,()%"&"A"ABWbfbwbw"A"x 4 A%#F8!&F:A#oW_B`Aaacdghw  zB  eC  dD  EF5MM x%:!" ( 	&MM::$$%%TUVTWXZ$%OD!		&s   BC= =	E3D>>Ec                   d| _         d| _        | j                  j                  dk(  r8| j                  j
                  s"d| j                  j                   d}dddd|dS |i }t        t        | +  ||      }|j                  dd      s|j                  d	d      r|S | j                  |      }| j                  ||      }|d	   r|}|S d|d
<   d|d<   |d   |d<   |S )NTlocalzRunning z7 with local connection would shutdown the control node.Fr   )changedelapsedr   rN   r   skippedrN   r   r   r   )_supports_check_mode_supports_async_connection	transportr   r   r9   rY   r.   r   runr:   r`   r   )r0   tmprK   r   r   rC   r   r3   s          r   r   zActionModule.run   s   $(!# %%0$:L:L:W:WTZZ..//fgC$QU^abbI|T.sI>::i'6::h+FM,,Y7 //	<H8$$FM!z y%45G%H!"r   )NN)r   r   r   TRANSFERS_FILES	frozenset_VALID_ARGSDEFAULT_CONNECT_TIMEOUTr6   r   rp   rv   r   ro   ru   r/   propertyr   r5   rF   r`   r   r   r   __classcell__)r3   s   @r   r   r      s    O  K #!"?)$@!  -1./075$< K K
m(6&p: r   r   N)
__future__r   ansible.errorsr   r   +ansible.module_utils.common.text.convertersr   r   'ansible.module_utils.common.collectionsr	   ansible.plugins.actionr
   ansible.utils.displayr   rW   r   	Exceptionr   r   r   r   r   <module>r      sB    # B J = - )
)+		 	B: Br   