
    f              
       X   U 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 ddl	m
Z
mZ ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ eZd
ZdZdddeege ed       ed      gdgdZeed<    ee      Z  ej8                  e      Zd Zd Z de!dedede"ddf
dZ#d Z$d Z%d dZ&d Z'y)!z&Power State Change: Change power state    N)dedent)subputil)Cloud)Config)
MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCE   a[  This module handles shutdown/reboot after all config modules have been run. By
default it will take no action, and the system will keep running unless a
package installation/upgrade requires a system reboot (e.g. installing a new
kernel) and ``package_reboot_if_required`` is true.

Using this module ensures that cloud-init is entirely finished with
modules that would be executed.

An example to distinguish delay from timeout:

If you delay 5 (5 minutes) and have a timeout of
120 (2 minutes), then the max time until shutdown will be 7 minutes, though
it could be as soon as 5 minutes. Cloud-init will invoke 'shutdown +5' after
the process finishes, or when 'timeout' seconds have elapsed.

.. note::
    With Alpine Linux any message value specified is ignored as Alpine's halt,
    poweroff, and reboot commands do not support broadcasting a message.

cc_power_state_changezPower State ChangezChange power statez            power_state:
                delay: now
                mode: poweroff
                message: Powering off
                timeout: 2
                condition: true
            z            power_state:
                delay: 30
                mode: reboot
                message: Rebooting machine
                condition: test -f /var/tmp/reboot_me
            power_state)idnametitledescriptiondistros	frequencyexamplesactivate_by_schema_keysmetac                 6   	 t        j                         r^t        j                  ddt        |       g      \  }}|j	                         d   }t        j                  d|      }|j                  d      S t        j                  d| z        S # t        $ r Y y w xY w)Nprocstatz-c   z\d+ (\w|\.|-)+\s+(/\w.+)   z/proc/%s/cmdline)
r   
is_FreeBSDr   str
splitlinesresearchgroupload_text_fileIOError)pidoutput_errlinems        H/usr/lib/python3/dist-packages/cloudinit/config/cc_power_state_change.pygivecmdliner*   U   s     ??!YY
D#c('CDNVT$$&q)D		5t<A771:&&'9C'?@@ s   A1B 4B 	BBc                    t        | t              rt        j                  d|        | S d| z  }	 t	        j
                  | t        | t                     }|j                          |j                  }|dk(  rt        j                  d|       y|dk(  rt        j                  d|       y	t        j                  d
||       y	# t        $ r!}t        j                  d||       Y d }~y	d }~ww xY w)NzStatic Condition: %szcheck_condition command (%s): )shellr   z%sexited 0. condition met.Tr   z%sexited 1. condition not met.Fz*%sunexpected exit %s. do not apply change.z%sUnexpected error: %s)
isinstanceboolLOGdebug
subprocessPopenlistcommunicate
returncodewarning	Exception)condpreprocretes        r)   check_conditionr=   g   s    $		($/
*T
1C
40F,FGoo!8II2C8AXII6<KKDc3O ,c15s$   AC C )C 	C+
C&&C+r   cfgcloudargsreturnc           
      B   	 t        ||j                        \  }}}|t        j                  d       y 	 |du rt        j                  d       y t        j                         }t        |      }|st        j                  d       y t        t        j                  d      }	t        j                  d|dj                  |             t        j                  t        ||||t         ||	g       y # t        $ r)}t        j                  dt        |             Y d }~y d }~ww xY w)	Nz&no power_state provided. doing nothingz%%s Not performing power state change!Fz3Condition was false. Will not perform state change.z5power_state: failed to get cmdline of current processwz#After pid %s ends, will execute: %s )load_power_statedistror/   r0   r7   r6   r   osgetpidr*   opendevnulljoinr   fork_cbrun_after_pid_goneexecmd)
r   r>   r?   r@   timeout	conditionr<   mypidcmdline
devnull_fps
             r)   handlerT      s    %5c5<<%H"w	<II>?  E		GHIIKE% GKLbjj#&JII3UCHHTNKLL	z'  ;SVDs   1C, ,	D5DDc                 F   | j                  d      }|yt        |t              st        d      g d}|j                  d      }||j                  vr!t        ddj                  |      d|d	      |j                  ||j                  d
d      |j                  d            }	 t        |j                  dd            }|j                  dd      }t        |t        t        t        f      st        d      |||fS # t        $ r}t        d|d   z        |d }~ww xY w)Nr   )NNNzpower_state is not a dict.)haltpoweroffrebootmodez,power_state[mode] required, must be one of: ,z
. found: 'z'.delaynowmessage)rY   r[   r]   rO   g      >@z(failed to convert timeout '%s' to float.rP   Tz2condition type %s invalid. must be list, bool, str)getr-   dict	TypeErrorshutdown_options_maprK   shutdown_commandfloat
ValueErrorr   r3   r.   )	r>   rF   pstatemodes_okrY   r@   rO   r<   rP   s	            r)   rE   rE      s*    WW]#F~!fd#455-H::fD6...xx!4)
 	

 ""jj%(

9% # D

9d34 

;-Ii#tT!23LMM'9%%  6	9JJ
	s   (D   	D 	DD c                 .    t        j                  |        y N)rG   _exit)sysexits    r)   doexitrk      s    HHW    c                     d}	 t        j                  | t         j                  |t         j                        }|j	                  |       |j
                  }t        |       y # t        $ r t        t               Y &w xY w)Nr   )stdinstdoutstderr)	r1   r2   PIPESTDOUTr4   r5   r7   rk   	EXIT_FAIL)exe_argsr%   data_inr;   r:   s        r)   rN   rN      sm    
C
//$$	
 	!oo 3K  ys   AA" "A=<A=c                 x   d }t        j                          |z   }d }t        j                  t        j                  f}		 t        j                          |kD  rd| z  }n2	 t	        |       }
|
|k7  r
d| d|
d}n	 t        j                  d	       O|s |d
       t        j                  |       	 t        |      sy 	  ||  y # t
        $ r3}|j                  |	v rd|j                  z  }n |d|z         Y d }~jd }~wt        $ r} |d|z         Y d }~d }~ww xY w# t        $ r} |d|z         Y d }~zd }~ww xY w)Nc                 L    t         j                  |        t        t               y rh   )r/   r6   rk   rs   )msgs    r)   fatalz!run_after_pid_gone.<locals>.fatal   s    Cyrl   ztimeout reached before %s endedzcmdline changed for z [now: ]zpidfile gone [%d]zIOError during wait: %szUnexpected Exception: %sg      ?z&Unexpected error in run_after_pid_gonez0Unexpected Exception when checking condition: %s)timeerrnoENOENTESRCHr*   r#   r7   sleepr/   r0   r=   )r$   
pidcmdlinerO   rP   funcr@   rx   end_timery   known_errnosrR   ioerrr<   s                r)   rM   rM      s?    Cyy{W$H LL%++.L
99;!3c9C	2!#&G*$<?I % 	

4+ . 67IIcNFy) *
 	$K/  	{{l*)EKK7/%78 	2,q011	2  F@1DEEFs<   C  -D  	D	)C77DDD	D9$D44D9)NN)(__doc__r|   loggingrG   r   r1   r{   textwrapr   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   r	   cloudinit.distrosr
   cloudinit.settingsr   r   rs   MODULE_DESCRIPTIONr   __annotations__	getLogger__name__r/   r*   r=   r   r3   rT   rE   rk   rN   rM    rl   r)   <module>r      s    -   	 	      ! # < ) +		 . " !%}		
 		
* !.9j > t
g!$0! !6 !% !t ! !H#&L /rl   