
    쑛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	 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j0                  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)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_conditionr7   :   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*   r1   r0   r   osgetpidr$   opendevnulljoinr   fork_cbrun_after_pid_goneexecmd)r8   r9   r:   r;   arg_listtimeout	conditionr6   mypidcmdline
devnull_fps              r#   handlerP   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)rU   rW   rY   rK   g      >@z(failed to convert timeout '%s' to float.rL   Tz2condition type %s invalid. must be list, bool, str)getr'   dict	TypeErrorshutdown_options_maprF   shutdown_commandfloat
ValueErrorr   r-   r(   )	r9   rA   pstatemodes_okrU   r;   rK   r6   rL   s	            r#   r@   r@   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)rB   _exit)sysexits    r#   doexitrg      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.   r/   r1   rg   	EXIT_FAIL)exe_argsr   data_inr5   r4   s        r#   rI   rI      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 rd   )r)   r0   rg   ro   )msgs    r#   fatalz!run_after_pid_gone.<locals>.fatal   s    Cyrh   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   r1   sleepr)   r*   r7   )r   
pidcmdlinerK   rL   funcr;   rt   end_timeru   known_errnosrN   ioerrr6   s                r#   rH   rH      sA    C~~')H LL%++.L
>>h&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__ry   loggingrB   r   r+   rw   	cloudinitr   r   cloudinit.cloudr   cloudinit.configr   cloudinit.config.schemar   cloudinit.distrosr   cloudinit.settingsr	   r   ro   r   __annotations__	getLogger__name__r)   r$   r7   r   r-   rP   r@   rg   rI   rH    rh   r#   <module>r      s    -   	 	     ! # . ) +		 "} -	j  g!*0! !6 !% !t ! !H#&L /rh   