܍~c RUdZddlZddlZddlZddlZddlZddlmZddlm Z ddl m Z m Z ddl mZddlmZddlmZmZdd lmZdd lmZeZd Zd Zd ddeegee de dgdgdZeed<eeZdZd"dZdedededede ddf dZ!dZ"dZ#d#d Z$d!Z%dS)$z&Power State Change: Change power stateN)Logger)dedent)subputil)Cloud)Config) MetaSchema get_meta_doc) ALL_DISTROS) PER_INSTANCEa[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_changezPower State ChangezChange 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)idnametitle descriptiondistros frequencyexamplesactivate_by_schema_keysmetacP tjrktjddt|g\}}|d}t jd|}|dStjd|zS#t$rYdSwxYw)Nprocstatz-cz\d+ (\w|\.|-)+\s+(/\w.+)z/proc/%s/cmdline) r is_FreeBSDrstr splitlinesresearchgroup load_fileIOError)pidoutput_errlinems H/usr/lib/python3/dist-packages/cloudinit/config/cc_power_state_change.py givecmdliner+Ts  ?   <!Y D#c(('CDDNVT$$&&q)D 5t<"4s":;; ; ttsA=BB B%$B%c0t|tr|r|d|z|Sd|z} tj|t|t  }||j}|dkr|r||dzdS|dkr|r||dzd S|r||d |zzd zd S#t$r(}|r||d |zzYd}~d Sd}~wwxYw) NzStatic Condition: %szcheck_condition command (%s): )shellrzexited 0. condition met.Trzexited 1. condition not met.Fzunexpected exit %s. zdo not apply change.zUnexpected error: %s) isinstancebooldebug subprocessPopenlist communicate returncodewarning Exception)condlogpreprocretes r*check_conditionr>fsj$  5 II,t3 4 4 4 *T 1C 40F0F,FGGG o !88 < # ::;;;4 AXX @ # >>???5  03669OO5   : KK4q88 9 9 9uuuuus$A%C# C# C## D-DDrcfgcloudr9argsreturnc r t||j\}}}||ddSn=#t$r0}|dt |zYd}~dSd}~wwxYw|dur|ddSt j}t|} | s|ddStt j d} |d|dd |tj t|| |||t|| gdS) 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 processwz After pid z ends, will execute:  )load_power_statedistror0r7r6rosgetpidr+opendevnulljoinrfork_cbrun_after_pid_goneexecmd) rr?r@r9rAtimeout conditionr=mypidcmdline devnull_fps r*handlerUs_%5c5<%H%H"w < II> ? ? ? F   ;c!ffDEEEE GHHH IKKE%  G  KLLLbj#&&JIIIuuuchhtnnnMNNNL   z     s05 A/%A**A/c|d}|dSt|tstdgd}|d}||jvr)tdd|d|d |||d d |d  } t|dd}n+#t$r}td|dz|d}~wwxYw|dd}t|tttfstd|||fS)Nr)NNNzpower_state is not a dict.)haltpoweroffrebootmodez,power_state[mode] required, must be one of: ,z . found: 'z'.delaynowmessage)rZr\r^rPg>@z(failed to convert timeout '%s' to float.rQTz2condition type %s invalid. must be list, bool, str) getr.dict TypeErrorshutdown_options_maprLshutdown_commandfloat ValueErrorrr3r/) r?rGpstatemodes_okrZrArPr=rQs r*rFrFsWW] # #F ~!! fd # #64555---H ::f  D 6...ixx!!!!444 )    " " jj%(( 9%% #  D  9d3344  6 9J J     ;--I i#tT!2 3 3NLMMM '9 %%s #C-- D7DDc.tj|dSN)rH_exit)sysexits r*doexitrlsHWcd} tj|tj|tj}|||j}n$#t $rttYnwxYwt|dS)Nr)stdinstdoutstderr) r1r2PIPESTDOUTr4r5r7rl EXIT_FAIL)exe_argsr&data_inr<r;s r*rOrOs C  /$     !!!o y 3KKKKKsAA A.-A.cd}tj|z}fd} tjtjf} tj|krd|z}n t |} | |kr d|d| d}nonY#t $r,} | j| vr d| jz}n| d| zYd} ~ n=d} ~ wt $r} | d | zYd} ~ nd} ~ wwxYwtjd |s | d r| t|sdSn%#t $r} | d | zYd} ~ nd} ~ wwxYw||dS) Nc^r|ttdSri)r6rlrt)msgr9s r*fatalz!run_after_pid_gone..fatals1   KK   yrmTztimeout reached before %s endedzcmdline changed for z [now: ]zpidfile gone [%d]zIOError during wait: %szUnexpected Exception: %sg?z&Unexpected error in run_after_pid_gonez0Unexpected Exception when checking condition: %s) timeerrnoENOENTESRCHr+r$r7sleepr0r>)r% pidcmdlinerPr9rQfuncrAryend_timerz known_errnosrSioerrr=s ` r*rNrNs Cy{{W$H L%+.L 9;; ! !3c9C  2!#&&G*$$$&D99D>ri)NN)&__doc__r}rHr r1r|loggingrtextwrapr cloudinitrrcloudinit.cloudrcloudinit.configrcloudinit.config.schemar r cloudinit.distrosr cloudinit.settingsr rrtMODULE_DESCRIPTIONr__annotations__r+r>rr3rUrFrlrOrNrmr*rs-,,   !!!!!!######<<<<<<<<))))))++++++   . " !%}      *!.9j> ,t  $>$ $$#($/5$=A$ $$$$N#&#&#&L     11111rm