Current File : //lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pyo
�
oB�]c@s�dZddlmZddlmZddlZddlZddlZddlZddl	Z	ddl
Z
eZdZd�Z
dd�Zd�Zd	�Zd
�Zddd�Zd�ZdS(
s�
Power State Change
------------------
**Summary:** change power state

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. The ``power_state`` config
key accepts a dict of options. If ``mode`` is any value other than
``poweroff``, ``halt``, or ``reboot``, then no action will be taken.

The system
can be shutdown before cloud-init has finished using the ``timeout`` option.
The ``delay`` key specifies a duration to be added onto any shutdown command
used. Therefore, if a 5 minute delay and a 120 second shutdown are specified,
the maximum amount of time between cloud-init starting and the system shutting
down is 7 minutes, and the minimum amount of time is 5 minutes. The ``delay``
key must have an argument in a form that the ``shutdown`` utility recognizes.
The most common format is the form ``+5`` for 5 minutes. See ``man shutdown``
for more options.

Optionally, a command can be run to determine whether or not
the system should shut down. The command to be run should be specified in the
``condition`` key. For command formatting, see the documentation for
``cc_runcmd``. The specified shutdown behavior will only take place if the
``condition`` key is omitted or the command specified by the ``condition``
key returns 0.

**Internal name:** ``cc_power_state_change``

**Module frequency:** per instance

**Supported distros:** all

**Config keys**::

    power_state:
        delay: <now/'+minutes'>
        mode: <poweroff/halt/reboot>
        message: <shutdown message>
        timeout: <seconds>
        condition: <true/false/command>
i����(tPER_INSTANCE(tutilNi�cCs�yttj�rbtjddt|�g�\}}|j�d}tjd|�}|jd�Stjd|�SWnt	k
r�dSXdS(Ntprocstats-cis\d+ (\w|\.|-)+\s+(/\w.+)is/proc/%s/cmdline(Rt
is_FreeBSDtsubptstrt
splitlinestretsearchtgroupt	load_filetIOErrortNone(tpidtoutputt_errtlinetm((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pytgivecmdlineCs$

cCs t|t�r-|r)|jd|�n|Sd|}y�tj|dt|t��}|j�|j}|dkr�|r�|j|d�ntS|dkr�|r�|j|d�nt	S|r�|j
|d|d	�nt	SWn2tk
r}|r|j
|d
|�nt	SXdS(NsStatic Condition: %sscheck_condition command (%s): tshellisexited 0. condition met.isexited 1. condition not met.sunexpected exit %s. sdo not apply change.sUnexpected error: %s(t
isinstancetbooltdebugt
subprocesstPopentlisttcommunicatet
returncodetTruetFalsetwarningt	Exception(tcondtlogtpretproctrette((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pytcheck_conditionUs2

	c
Csy6t|�\}}}|dkr5|jd�dSWn+tk
rc}|jdt|��dSX|tkr�|jd�dStj�}	t	|	�}
|
s�|jd�dSt
tjd�}|jd|	dj|�f�t
jt|	|
|||t||g�dS(Ns&no power_state provided. doing nothings%%s Not performing power state change!s3Condition was false. Will not perform state change.s5power_state: failed to get cmdline of current processtws#After pid %s ends, will execute: %st (tload_power_stateRRRRRRtostgetpidRtopentdevnulltjoinRtfork_cbtrun_after_pid_gonetexecmd(t_nametcfgt_cloudR!t_argstargsttimeoutt	conditionR%tmypidtcmdlinet
devnull_fp((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pythandless(


 cCs�|jd�}|dkrdSt|t�s=td��nidd6dd6dd6}|jd	�}||kr�td
dj|j��|f��n|jdd
�}ydt|�}Wntk
r�nX|d
krt	j
d|�rtd|��nd|||g}|jd�rB|j|jd��nyt|jdd��}Wn%tk
r�td|d��nX|jdt
�}t|tjttf�s�td��n|||fS(Ntpower_statespower_state is not a dict.s-Hthalts-Ptpoweroffs-rtreboottmodes<power_state[mode] required, must be one of: %s. found: '%s'.t,tdelaytnows+%ss\+[0-9]+s?power_state[delay] must be 'now' or '+m' (minutes). found '%s'.tshutdowntmessageR7g>@s(failed to convert timeout '%s' to float.R8s2condition type %s invalid. must be list, bool, str(NNN(tgetRRtdictt	TypeErrorR.tkeystintt
ValueErrorRtmatchtappendtfloatRtsixtstring_typesRR(R3tpstatetopt_mapRARCR6R7R8((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pyR)�s@"


cCstj|�dS(N(R*t_exit(tsysexit((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pytdoexit�scCssd}yAtj|dtjd|dtj�}|j|�|j}Wntk
rdtt�nXt|�dS(Nitstdintstdouttstderr(	RRtPIPEtSTDOUTRRRRVt	EXIT_FAIL(texe_argsRtdata_inR$R#((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pyR1�s


cswd}tj�|}�fd�}	tjtjf}
x�trtj�|kr`d|}Pny0t|�}||kr�d||f}PnWnatk
r�}|j|
kr�d|j}n|	d|�Pn!tk
r�}
|	d|
�nXtj	d�q:W|s|	d�n�r.�j
|�nyt|��sDdSWn!tk
rh}
|	d	|
�nX||�dS(
Ncs$�r�j|�ntt�dS(N(RRVR\(tmsg(R!(sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pytfatal�sstimeout reached before %s endeds cmdline changed for %s [now: %s]spidfile gone [%d]sIOError during wait: %ssUnexpected Exception: %sg�?s&Unexpected error in run_after_pid_gones0Unexpected Exception when checking condition: %s(RttimeterrnotENOENTtESRCHRRRRtsleepRR&(R
t
pidcmdlineR7R!R8tfuncR6R_tend_timeR`tknown_errnosR:tioerrR%((R!sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pyR0�s>	

(t__doc__tcloudinit.settingsRt	cloudinitRRbR*RRPRRat	frequencyR\RRR&R<R)RVR1R0(((sJ/usr/lib/python2.7/site-packages/cloudinit/config/cc_power_state_change.pyt<module>2s"			/