U 'Dg@sUdZddlZddlZddlZddlZddlZddlZddlmZm Z ddl m Z ddl m Z ddlmZddlmZddlmZeZd Zd eged gd Zeed <eeZddZddZee e eddddZddZ ddZ!dddZ"ddZ#dS)z&Power State Change: Change power stateN)subputil)Cloud)Config) MetaSchema) ALL_DISTROS) PER_INSTANCEZcc_power_state_change power_state)idZdistros frequencyZactivate_by_schema_keysmetacCstzXtrFtddt|g\}}|d}td|}|dWStd|WSWnt k rnYdSXdS)NZprocstatz-cz\d+ (\w|\.|-)+\s+(/\w.+)z/proc/%s/cmdline) rZ is_FreeBSDrstr splitlinesresearchgroupZload_text_fileIOError)pidoutputZ_errlinemrH/usr/lib/python3/dist-packages/cloudinit/config/cc_power_state_change.py givecmdline%s   rc Cst|trtd||Sd|}zptj|t|t d}||j}|dkrbtd|WdS|dkr|td|Wd St d ||Wd SWn6t k r}zt d ||WYd Sd}~XYnXdS) NzStatic Condition: %szcheck_condition command (%s): )shellrz%sexited 0. condition met.Trz%sexited 1. condition not met.Fz*%sunexpected exit %s. do not apply change.z%sUnexpected error: %s) isinstanceboolLOGdebug subprocessPopenlist communicate returncodewarning Exception)ZcondZpreprocreterrrcheck_condition7s&     r,)namecfgcloudargsreturnc Csz.t||j\}}}|dkr,tdWdSWn8tk rf}ztdt|WYdSd}~XYnX|dkr~tddSt}t |} | stddSt tj d} td|d |t 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 %s ends, will execute: %s )load_power_statedistror r!r(r'rosgetpidropendevnulljoinrZfork_cbrun_after_pid_goneexecmd) r-r.r/r0arg_listtimeout conditionr+ZmypidcmdlineZ devnull_fprrrhandleOs6     rAc Cs|d}|dkrdSt|ts(tddddg}|d}||jkr\tdd ||f|j||d d |d d }zt|dd}Wn4tk r}ztd|d|W5d}~XYnX|dd}t|t t t fstd|||fS)Nr )NNNzpower_state is not a dict.ZhaltZpoweroffZrebootmodezg>@z(failed to convert timeout '%s' to float.r?Tz2condition type %s invalid. must be list, bool, str) getrdict TypeErrorZshutdown_options_mapr:Zshutdown_commandfloat ValueErrorrr$r) r.r5ZpstateZmodes_okrBr0r>r+r?rrrr4ss<         r4cCst|dSN)r6_exit)ZsysexitrrrdoexitsrMcCsXd}z*tj|tj|tjd}|||j}Wntk rJttYnXt|dS)Nr)stdinstdoutstderr) r"r#PIPEZSTDOUTr%r&r(rM EXIT_FAIL)Zexe_argsrZdata_inr*r)rrrr<s  r<c CsLd}t|}dd}tjtjf} t|kr:d|}qz$t|} | |kr\d|| f}WqWnxtk r} z.| j| krd| j}n |d| WYqW5d} ~ XYn.tk r} z|d| W5d} ~ XYnXtdq$|s|d t |zt |s WdSWn0tk r>} z|d | W5d} ~ XYnX||dS) NcSst|ttdSrK)r r'rMrR)msgrrrfatals z!run_after_pid_gone..fatalztimeout reached before %s endedz cmdline changed for %s [now: %s]zpidfile gone [%d]zIOError during wait: %szUnexpected Exception: %sg?z&Unexpected error in run_after_pid_gonez0Unexpected Exception when checking condition: %s) time monotonicerrnoZENOENTZESRCHrrr(sleepr r!r,) rZ pidcmdliner>r?funcr0rSZend_timerTZ known_errnosr@Zioerrr+rrrr;s:           r;)NN)$__doc__rWZloggingr6rr"rUZ cloudinitrrZcloudinit.cloudrZcloudinit.configrZcloudinit.config.schemarZcloudinit.distrosrZcloudinit.settingsrr rRr __annotations__Z getLogger__name__r rr,rr$rAr4rMr<r;rrrrs6      $&