summaryrefslogtreecommitdiff
path: root/src/op_mode
diff options
context:
space:
mode:
authorRunar Borge <runar@borge.nu>2018-08-17 00:22:16 +0200
committerRunar Borge <runar@borge.nu>2018-08-17 00:22:16 +0200
commitce35b9830e538bce50b42272bb457201dfae102e (patch)
treed6358a8526171754b8453cedbfc171217b15fa3a /src/op_mode
parent0a9bcf9a7706e4bc7f4884419790c8e3f1601d39 (diff)
downloadvyos-1x-ce35b9830e538bce50b42272bb457201dfae102e.tar.gz
vyos-1x-ce35b9830e538bce50b42272bb457201dfae102e.zip
T689: Added support for specifying date on poweroff and reboot
Diffstat (limited to 'src/op_mode')
-rwxr-xr-xsrc/op_mode/powerctrl.py58
1 files changed, 49 insertions, 9 deletions
diff --git a/src/op_mode/powerctrl.py b/src/op_mode/powerctrl.py
index 0200a09e8..f73d6c005 100755
--- a/src/op_mode/powerctrl.py
+++ b/src/op_mode/powerctrl.py
@@ -23,10 +23,25 @@ def yn(msg, default=False):
def valid_time(s):
try:
- a = datetime.strptime(s, "%H:%M")
- return True
+ return datetime.strptime(s, "%H:%M").time()
except ValueError:
- return False
+ return None
+
+
+def valid_date(s):
+ try:
+ return datetime.strptime(s, "%d%m%Y").date()
+ except ValueError:
+ try:
+ return datetime.strptime(s, "%d/%m/%Y").date()
+ except ValueError:
+ try:
+ return datetime.strptime(s, "%d.%m.%Y").date()
+ except ValueError:
+ try:
+ return datetime.strptime(s, "%d:%m:%Y").date()
+ except ValueError:
+ return None
def check_shutdown():
@@ -51,11 +66,36 @@ def execute_shutdown(time, reboot = True, ask=True):
if not yn("Are you sure you want to %s this system?" % action):
sys.exit(0)
- if not (time.isdigit() or valid_time(time) or time.lower() == "now"):
- sys.exit("minutes (45), valid time (12:34) or 'now' needs to be specified")
-
action = "-r" if reboot else "-P"
- cmd = check_output(["/sbin/shutdown",action,time],stderr=STDOUT)
+
+ if len(time) == 0:
+ cmd = check_output(["/sbin/shutdown",action,"now"],stderr=STDOUT)
+ print(cmd.decode().split(",",1)[0])
+ return
+
+ # Try to extract date from the first argument
+ if len(time) == 1:
+ time = time[0].split(" ",1)
+
+ if len(time) == 1:
+ ts=valid_time(time[0])
+ if time[0].isdigit() or valid_time(time[0]):
+ cmd = check_output(["/sbin/shutdown",action,time[0]],stderr=STDOUT)
+ else:
+ sys.exit("Timestamp needs to be in format of 12:34")
+
+ elif len(time) == 2:
+ ts = valid_time(time[0])
+ ds = valid_date(time[1])
+ if ts and ds:
+ t = datetime.combine(ds, ts)
+ td = t-datetime.now()
+ t2 = 1+int(td.total_seconds())//60 # Get total minutes
+ cmd = check_output(["/sbin/shutdown",action,str(t2)],stderr=STDOUT)
+ else:
+ sys.exit("Timestamp needs to be in format of 12:34\nDatestamp in the format of DD.MM.YY")
+ else:
+ sys.exit("Could not decode time and date")
print(cmd.decode().split(",",1)[0])
@@ -68,12 +108,12 @@ def main():
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument("--reboot", "-r",
help="Reboot the system",
- nargs="?",
+ nargs="*",
metavar="Minutes|HH:MM")
action.add_argument("--poweroff", "-p",
help="Poweroff the system",
- nargs="?",
+ nargs="*",
metavar="Minutes|HH:MM")
action.add_argument("--cancel", "-c",