summaryrefslogtreecommitdiff
path: root/src/op_mode/powerctrl.py
diff options
context:
space:
mode:
authorDmitriyEshenko <dmitriy.eshenko@vyos.io>2020-01-09 08:19:29 +0000
committerDmitriyEshenko <dmitriy.eshenko@vyos.io>2020-01-09 08:20:57 +0000
commitba6e15cdc7059168646c9f47e1b57e0cedf08115 (patch)
treed0e96daf2d3ecc1c72f4a109fa0dff6b07e31dfa /src/op_mode/powerctrl.py
parent3863e5dc24a622090f52c13f8aa04e5c9f00341a (diff)
downloadvyos-1x-ba6e15cdc7059168646c9f47e1b57e0cedf08115.tar.gz
vyos-1x-ba6e15cdc7059168646c9f47e1b57e0cedf08115.zip
powerctr: T1842: Use systemd files for detect schedule
Diffstat (limited to 'src/op_mode/powerctrl.py')
-rwxr-xr-xsrc/op_mode/powerctrl.py38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/op_mode/powerctrl.py b/src/op_mode/powerctrl.py
index a6188ec74..54fc12be3 100755
--- a/src/op_mode/powerctrl.py
+++ b/src/op_mode/powerctrl.py
@@ -24,6 +24,7 @@ from datetime import datetime, timedelta, time as type_time, date as type_date
from subprocess import check_output, CalledProcessError, STDOUT
from vyos.util import ask_yes_no
+systemd_sched_file = "/run/systemd/shutdown/scheduled"
def parse_time(s):
try:
@@ -45,33 +46,40 @@ def parse_date(s):
def get_shutdown_status():
try:
- output = check_output(["/bin/systemctl", "status", "systemd-shutdownd.service"]).decode()
- return output
+ if os.path.exists(systemd_sched_file):
+ # Get scheduled from systemd file
+ with open(systemd_sched_file, 'r') as f:
+ data = f.read().rstrip('\n')
+ r_data = {}
+ for line in data.splitlines():
+ tmp_split = line.split("=")
+ if tmp_split[0] == "USEC":
+ # Convert USEC to human readable format
+ r_data['DATETIME'] = datetime.utcfromtimestamp(int(tmp_split[1])/1000000).strftime('%Y-%m-%d %H:%M:%S')
+ else:
+ r_data[tmp_split[0]] = tmp_split[1]
+ return r_data
+ return None
except CalledProcessError:
return None
def check_shutdown():
output = get_shutdown_status()
- if output:
- r = re.findall(r'Status: \"(.*)\"\n', output)
- if r:
- # When available, that line is like
- # Status: "Shutting down at Thu 1970-01-01 00:00:00 UTC (poweroff)..."
- print(r[0])
- else:
- # Sometimes status string is not available immediately
- # after service startup
- print("Poweroff or reboot is scheduled")
+ if output and 'MODE' in output:
+ if output['MODE'] == 'reboot':
+ print("Reboot is scheduled", output['DATETIME'])
+ elif output['MODE'] == 'poweroff':
+ print("Poweroff is scheduled", output['DATETIME'])
else:
- print("Poweroff or reboot is not scheduled")
+ print("Reboot or poweroff is not scheduled")
def cancel_shutdown():
output = get_shutdown_status()
- if output:
+ if output and 'MODE' in output:
try:
timenow = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cmd = check_output(["/sbin/shutdown","-c","--no-wall"])
- message = "Scheduled reboot or poweroff has been cancelled %s" % timenow
+ message = "Scheduled %s has been cancelled %s" % (output['MODE'], timenow)
os.system("wall %s" % message)
except CalledProcessError as e:
sys.exit("Could not cancel a reboot or poweroff: %s" % e)