summaryrefslogtreecommitdiff
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
parent0a9bcf9a7706e4bc7f4884419790c8e3f1601d39 (diff)
downloadvyos-1x-ce35b9830e538bce50b42272bb457201dfae102e.tar.gz
vyos-1x-ce35b9830e538bce50b42272bb457201dfae102e.zip
T689: Added support for specifying date on poweroff and reboot
-rw-r--r--op-mode-definitions/poweroff.xml24
-rw-r--r--op-mode-definitions/reboot.xml16
-rwxr-xr-xsrc/op_mode/powerctrl.py58
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>&lt;Minutes&gt;</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>&lt;DDMMYYYY&gt; &lt;DD/MM/YYYY&gt; &lt;DD.MM.YYYY&gt; &lt;DD:MM:YYYY&gt;</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>&lt;Minutes&gt;</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>&lt;DDMMYYYY&gt; &lt;DD/MM/YYYY&gt; &lt;DD.MM.YYYY&gt; &lt;DD:MM:YYYY&gt;</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",