diff options
-rw-r--r-- | op-mode-definitions/poweroff.xml | 24 | ||||
-rw-r--r-- | op-mode-definitions/reboot.xml | 16 | ||||
-rwxr-xr-x | src/op_mode/powerctrl.py | 58 |
3 files changed, 79 insertions, 19 deletions
diff --git a/op-mode-definitions/poweroff.xml b/op-mode-definitions/poweroff.xml index 725d10225..bb2eedcb1 100644 --- a/op-mode-definitions/poweroff.xml +++ b/op-mode-definitions/poweroff.xml @@ -16,31 +16,41 @@ <leafNode name="cancel"> <properties> - <help>Cancel a pending reboot</help> + <help>Cancel a pending poweroff</help> </properties> <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --cancel</command> </leafNode> <tagNode name="in"> <properties> - <help>Reboot in X minutes</help> + <help>Poweroff in X minutes</help> <completionHelp> - <list>Minutes</list> + <list><Minutes></list> </completionHelp> </properties> - <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --poweroff '$3'</command> + <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --poweroff $3 $4</command> </tagNode> <tagNode name="at"> <properties> - <help>Reboot at a specific time</help> + <help>Poweroff at a specific time</help> <completionHelp> <list>HH:MM</list> </completionHelp> </properties> - <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --poweroff '$3'</command> - + <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --poweroff $3</command> + <children> + <tagNode name="date"> + <properties> + <help>Poweroff at a specific date</help> + <completionHelp> + <list><DDMMYYYY> <DD/MM/YYYY> <DD.MM.YYYY> <DD:MM:YYYY></list> + </completionHelp> + </properties> + <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --poweroff $3 $5</command> + </tagNode> + </children> </tagNode> </children> diff --git a/op-mode-definitions/reboot.xml b/op-mode-definitions/reboot.xml index 214a39633..703304ff8 100644 --- a/op-mode-definitions/reboot.xml +++ b/op-mode-definitions/reboot.xml @@ -25,10 +25,10 @@ <properties> <help>Reboot in X minutes</help> <completionHelp> - <list>Minutes</list> + <list><Minutes></list> </completionHelp> </properties> - <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --reboot $3</command> + <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --reboot $3 $4</command> </tagNode> @@ -40,7 +40,17 @@ </completionHelp> </properties> <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --reboot $3</command> - + <children> + <tagNode name="date"> + <properties> + <help>Reboot at a specific date</help> + <completionHelp> + <list><DDMMYYYY> <DD/MM/YYYY> <DD.MM.YYYY> <DD:MM:YYYY></list> + </completionHelp> + </properties> + <command>sudo ${vyos_op_scripts_dir}/powerctrl.py --yes --reboot $3 $5</command> + </tagNode> + </children> </tagNode> </children> 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", |