3 \me @ s& U d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl m Z mZ ddlm Z ddlmZ ddlmZmZ ddlmZ dd lmZ eZd ZdZdd deegeededgdgdZeeeZ ejeZdd Zdd Ze ee e!ddddZ"dd Z#dd Z$d"ddZ%d d! Z&dS )#z&Power State Change: Change power state N)dedent)subputil)Cloud)Config) MetaSchemaget_meta_doc)ALL_DISTROS)PER_INSTANCE a[ 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. Zcc_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)idnametitledescriptionZdistros frequencyZexamplesZactivate_by_schema_keysc C sn yTt j rDtjddt| g\}}|j d }tjd|}|jdS t jd| S W n t k rh d S X d S )NZprocstatz-c z\d+ (\w|\.|-)+\s+(/\w.+) z/proc/%s/cmdline) r Z is_FreeBSDr str splitlinesresearchgroupZ load_fileIOError)pidoutputZ_errlinem r +/usr/lib/python3.6/cc_power_state_change.pygivecmdlineU s r c C s t | trtjd| | S d| }yjtj| t | t d}|j |j}|dkr`tjd| dS |dkrxtjd| d S tj d || d S W n. t k r } ztj d|| d S d }~X nX d S )NzStatic Condition: %szcheck_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) isinstanceboolLOGdebug subprocessPopenlistcommunicate returncodewarning Exception)ZcondZpreprocreter r r check_conditiong s&