
    x[h              
          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m	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ eZd	Zd
egedgdZeed<    ej2                  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)signal_handlersubputil)Cloud)Config)
MetaSchema)ALL_DISTROS)PER_INSTANCE   cc_power_state_changepower_state)iddistros	frequencyactivate_by_schema_keysmetac                 <   	 t        j                         rat        j                  ddt        |       g      \  }}|j	                         d   }t        j                  d|      }|r|j                  d      S y 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%   %   s     ??!YY
D#c('CDNVT$$&q)D		5t<Awwqz!&&'9C'?@@ s   A3B 7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_conditionr8   :   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+name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*   r+   r2   r1   r   osgetpidr%   opendevnulljoinr   fork_cbrun_after_pid_goneexecmd)r9   r:   r;   r<   arg_listtimeout	conditionr7   mypidcmdline
devnull_fps              r$   handlerQ   R   s    )9#u||)L&7III>?  E		GHIIKE% GKLbjj#&JII3UCHHX<NOLL	:'  ;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)rV   rX   rZ   rL   g      >@z(failed to convert timeout '%s' to float.rM   Tz2condition type %s invalid. must be list, bool, str)getr(   dict	TypeErrorshutdown_options_maprG   shutdown_commandfloat
ValueErrorr   r.   r)   )	r:   rB   pstatemodes_okrV   r<   rL   r7   rM   s	            r$   rA   rA   v   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)rC   _exit)sysexits    r$   doexitrh      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)	r,   r-   PIPESTDOUTr/   r0   r2   rh   	EXIT_FAIL)exe_argsr    data_inr6   r5   s        r$   rJ   rJ      sm    
C
//$$	
 	!oo 3K  ys   AA" "A=<A=c                    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 	 t        j                         5   ||  d d d        y # t        $ r3}|j                  |	v rd|j                  z  }n |d|z         Y d }~d }~wt        $ r} |d|z         Y d }~d }~ww xY w# t        $ r} |d|z         Y d }~d }~ww xY w# 1 sw Y   y xY w)Nc                 L    t         j                  |        t        t               y re   )r*   r1   rh   rp   )msgs    r$   fatalz!run_after_pid_gone.<locals>.fatal   s    Cyri   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	monotonicerrnoENOENTESRCHr%   r   r2   sleepr*   r+   r8   r   suspend_crash)r   
pidcmdlinerL   rM   funcr<   ru   end_timerv   known_errnosrO   ioerrr7   s                r$   rI   rI      sg    C~~')H LL%++.L
>>h&3c9C	2!#&G*$<?I % 	

4+ . 67IIcNFy) * 
	%	%	' d 3  	{{l*)EKK7/%78 	2,q011	2  F@1DEEF
 sH   C -D8 E	D5&)DD5 D00D58	EEEE")NN)%__doc__rz   loggingrC   r   r,   rx   	cloudinitr   r   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.distrosr	   cloudinit.settingsr
   r   rp   r   __annotations__	getLogger__name__r*   r%   r8   r   r.   rQ   rA   rh   rJ   rI    ri   r$   <module>r      s    -   	 	   0 0 ! # . ) +		 "} -	j  g!*0! !6 !% !t ! !H#&L 2ri   