o nnhV@sddlmZddlZddlZddlmZmZmZddlmZm Z ddl m Z m Z ddl mZmZddlmZddlmZeZGd d d eZGd d d eZdS) ) annotationsN)datetime timedeltatimezone) AnsibleErrorAnsibleConnectionFailure) to_nativeto_text)check_type_listcheck_type_str) ActionBase)Displayc@s eZdZdS)TimedOutExceptionN)__name__ __module__ __qualname__rrH/usr/local/lib/python3.10/dist-packages/ansible/plugins/action/reboot.pyrsrc s eZdZdZedZdZdZdZdZ dZ dZ dZ d Z d Zd ZiZd d d d d dd dZdddZdddeddddddd ZdddZfddZeddZedd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Z d1d2Z!d;d3d4Z"d5d6Z#d)r0 variable_name distribution default_valueattrrArrr_get_value_from_factsgs z"ActionModule._get_value_from_factsc Cs|jjd}|durr TypeErrorrrsplit IndexErrorrJrDEFAULT_REBOOT_MESSAGEformat)r0rGrer1rTreboot_messagerrrget_shutdown_command_argsss"  z&ActionModule.get_shutdown_command_argsc Csi}tdj|jjd|j|dddid}zN|ddr2td t|d  t|d  |d d  |d<t |d d dd|d<t |d d |d<tdj|jj|d|WSt y{}z td|jdd}~ww)Nz2{action}: running setup module to get distributionactionzansible.legacy.setup gather_subsetmin task_vars module_name module_argsfailedFz1Failed to determine system distribution. {0}, {1} module_stdout module_stderr ansible_factsansible_distributionrBansible_distribution_version.rrCansible_os_familyrDz{action}: distribution: {dist})r_distz@Failed to get distribution information. Missing "{0}" in output.)displaydebugrZr=r__execute_moduler>rrstriplowerr rWKeyErrorr1)r0rcrG module_outputkerrrget_distributions, zActionModule.get_distributionc Cs(|jjd}|dur1zt|dd}Wnty'}ztdt|d}~ww|ddd}n|d|d }|dd kr@|Sgd }|jjd |}zt |}Wntybd }t| |wt dj |jj ||d|j|d||gddd} dd| dD} | std ||| dS)NrFrKrMrNrOrSHUTDOWN_COMMANDSDEFAULT_SHUTDOWN_COMMAND/)z/sbinz/binz /usr/sbinz/usr/binz/usr/local/sbinrz@'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}")r_commandpathszansible.legacy.findany)r|patterns file_typerbcSsg|]}|dqS)pathr).0xrrr sz5ActionModule.get_shutdown_command..filesz1Unable to find command "{0}" in search paths: {1})r=r1r>r rVrrrWrJr rZrorpr_rq) r0rcrGrr[ shutdown_bindefault_search_pathsrerr_msg find_result full_pathrrrget_shutdown_commandsJ    z!ActionModule.get_shutdown_commandcCsD|jD]\}}|jj|durtdj|||jjdqdS)NzGSince Ansible {version}, {arg} is no longer a valid option for {action})rCargr_) DEPRECATED_ARGSitemsr=r1r>rowarningrZr_)r0rrCrrrdeprecated_argsszActionModule.deprecated_argsc Cs|d|d}|jjdr2|jjd}zt|ddWnty1}ztdt|d}~wwt dj |jj |d|j ||j d }|d d krg|d }|d }tdj |jj |d t|t|dt dj |jj |d d|d S)NBOOT_TIME_COMMANDSDEFAULT_BOOT_TIME_COMMANDrFrKz0Invalid value given for 'boot_time_command': %s.z5{action}: getting boot time with command: '{command}'r_r{sudoablercrstdoutstderrzS{action}: failed to get host boot time info, rc: {rc}, stdout: {out}, stderr: {err})r_routerrz {action}: last boot time: {boot})r_boot)rJr=r1r>r rVrrrorprZr__low_level_execute_commandDEFAULT_SUDOABLErr)r0rGrr[command_resultrrrrrget_system_boot_times,   z!ActionModule.get_system_boot_timec Cstdj|jjd|jjd|jjd|j}|rZztdj|jj|d|j d|Wn#t yTz |j d|Wnt t fyQt d YnwYnw|j z||}Wntyo}z|d}~wwt|d ksz||kr~td dS) Nz,{action}: attempting to get system boot timer^rconnect_timeout_secz,{action}: setting connect_timeout to {value}r_rAconnection_timeouttimeoutzHConnection plugin does not allow the connection timeout to be overriddenrzboot time has not changed)rovvvrZr=r_r1r>DEFAULT_CONNECT_TIMEOUTrp _connection set_optionrAttributeErrorrresetr Exceptionlen ValueError)r0rGprevious_boot_timercurrent_boot_timer[rrrcheck_boot_times0  zActionModule.check_boot_timec Ks|jjd|d|d}tdj|jjdtdj|jj|dz |j ||j d}Wnt yFz|j WtyEYww|d d kr`d jt|d t|d d}t|tdj|jjddS)Nr TEST_COMMANDSDEFAULT_TEST_COMMANDz-{action}: attempting post-reboot test commandr^z9{action}: attempting post-reboot test command '{command}'rrrrz Test command failed: {err} {out}rr)rrz&{action}: system successfully rebooted)r=r1r>rJrorrZr_rprrrrrrr RuntimeError)r0rGr2rrrrrrrun_test_commands*      zActionModule.run_test_commandc Csttjt|d}|duri}d}d}d} ttj|krz|dd|i||r7tdj|jj |dWdSt y} z~t | t rWz|j Wn t yVYnwtddd} d || } | |krm|| } |rz t| d } Wnty} z t| } WYd} ~ nd} ~ ww|jj d |d | d } | d| dd}t|t||d7}t| WYd} ~ nd} ~ wwttj|ks| rd| }t|t|tdj||d)N)secondsr r)rGz{action}: {desc} success)r_desciz: z fail ''z, retrying in z.4fz seconds...rOz2Last error message before the timeout exception - z0Timed out waiting for {desc} (timeout={timeout}))rrr)rnowrutcrrorprZr=r_r isinstancerrrrandomrandintr splitlinesrXrtimesleepr)r0r_r action_descrG action_kwargs max_end_time fail_countmax_fail_sleeplast_error_msgr[ random_int fail_sleeperrorrrrrdo_until_success_or_timeoutsX        z(ActionModule.do_until_success_or_timeoutc Csi}i}|||}||}d||}z!tdj|jjdtdj|jj|d|j||j d}Wn$t yZ}ztdj|jjt |dd |d <WYd}~nd}~wwt tj|d <|d d krd |d <d|d<djt|dt|dd|d<|Sd|d <|S)Nz{0} {1}z{action}: rebooting server...r^z3{action}: rebooting server with command '{command}'rrz>{action}: AnsibleConnectionFailure caught and handled: {error}r_rrrstartTrfFrebootedz6Reboot command failed. Error was: '{stdout}, {stderr}'rr)rrr)rr]rZrorr=r_rprrrr rrrrrrr) r0rcrGresult reboot_resultshutdown_commandshutdown_command_argsrr[rrrperform_rebootKs2     zActionModule.perform_rebootc Cstdj|jjdi}zt|jjd|jjd|j}|j |j d|||dz|j d}Wnt yMz|j d}Wn t yJYnwYn`w||krz8td j|jj|d z |j d|Wntyz |j d|WntywYnw|j Wn"ttfy}ztd j|jjt|d WYd}~nd}~ww|j |jd |||dd|d<d|d<W|Sty}zd|d<d|d<t||d<|WYd}~Sd}~ww)Nz{action}: validating rebootr^rreboot_timeout_seczlast boot time check)r_rrrGrrrzK{action}: setting connect_timeout/timeout back to original value of {value}rzE{action}: failed to reset connection_timeout back to default: {error}rzpost-reboot test commandTrchangedrfr)rorrZr=r_r<r1r>DEFAULT_REBOOT_TIMEOUTrrr get_optionrtrprrrrr rr) r0rGoriginal_connection_timeoutrrrrr[toexrrrvalidate_reboothsx"      zActionModule.validate_rebootc sd|_|jjdkrd|jj}dddd|dS|jjr"ddddS|dur(i}|tt | ||}| ddsA| d drC|S| |}z| |}Wn"tyq}zd|d <d|d <t||d <|WYd}~Sd}~wwd}td j|jj|d z|jd}Wn&tyz|jd}Wntytdj|jjdYnwYnw|||} | d r| }ttj| d} | j|d<|S|jdkrtdj|jj|jdtdj|jj|jdt|j|j||d|id}ttj| d} | j|d<|S)NTlocalz@Running {0} with local connection would reboot the control node.Fr)relapsedrrfr)rrrskippedrfr'rz6{action}: saving original connect_timeout of {timeout})r_rrrz<{action}: connect_timeout connection option has not been setr^rrz/{action}: waiting an additional {delay} seconds)r_delayr)r)_supports_check_moder transportrZr=r_ check_moderr.rrunr>rwrrr rorprrtrrrrrrrrrrr) r0tmprcrrrGrr[rrrr3rrrs`           zActionModule.runr-)NN)'rrrTRANSFERS_FILES frozenset _VALID_ARGSrrr7r:rrrYryrQrrrrxrPrr/propertyrrr6rJr]rwrrrrrrrrr __classcell__rrr3rrsr      - . =r) __future__rrrrrransible.errorsrr+ansible.module_utils.common.text.convertersrr &ansible.module_utils.common.validationr r ansible.plugins.actionr ansible.utils.displayr rorrrrrrrs