summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStig Thormodsrud <stig@vyatta.com>2008-05-31 14:48:07 -0700
committerStig Thormodsrud <stig@vyatta.com>2008-05-31 14:48:07 -0700
commit18dd1b542f69528ac40ade79ee10a6ac77fed9c4 (patch)
tree194c818e0e03fa737fe759773bdcddc327f1cf6c
parent5cc881e0cc7eccd422b1dce2980bb69e8fe0f709 (diff)
downloadvyatta-op-18dd1b542f69528ac40ade79ee10a6ac77fed9c4.tar.gz
vyatta-op-18dd1b542f69528ac40ade79ee10a6ac77fed9c4.zip
Fix 88: Feature Request: Reboot Command (add confirmation)
-rw-r--r--Makefile.am1
-rw-r--r--debian/control3
-rw-r--r--scripts/vyatta-reboot.pl187
-rw-r--r--templates/reboot/.at/node.def1
-rw-r--r--templates/reboot/.at/node.tag/node.def3
-rw-r--r--templates/reboot/.cancel/node.def2
-rw-r--r--templates/reboot/node.def2
-rw-r--r--templates/show/.reboot/node.def2
8 files changed, 199 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am
index 35d6691..7499a22 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ bin_SCRIPTS += scripts/vyatta-tshark-interface-port.pl
bin_sudo_users_SCRIPTS = scripts/vyatta-identify-interface.pl
bin_sudo_users_SCRIPTS += scripts/vyatta-delete-log-file.sh
+bin_sudo_users_SCRIPTS += scripts/vyatta-reboot.pl
cpiop = find . ! -regex '\(.*~\|.*\.bak\|.*\.swp\|.*\#.*\#\)' -print0 | \
cpio -0pd
diff --git a/debian/control b/debian/control
index 757332b..7d90d2b 100644
--- a/debian/control
+++ b/debian/control
@@ -17,7 +17,8 @@ Depends: sed (>= 4.1.5),
coreutils (>= 5.97-5.3),
host,
vyatta-bash | bash (>= 3.1),
- less
+ less,
+ libio-prompt-perl
Suggests: util-linux (>= 2.13-5),
net-tools,
ncurses-bin (>= 5.5-5),
diff --git a/scripts/vyatta-reboot.pl b/scripts/vyatta-reboot.pl
new file mode 100644
index 0000000..9ed369a
--- /dev/null
+++ b/scripts/vyatta-reboot.pl
@@ -0,0 +1,187 @@
+#!/usr/bin/perl
+#
+# Module: vyatta-reboot.pl
+#
+# **** License ****
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# This code was originally developed by Vyatta, Inc.
+# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
+# All Rights Reserved.
+#
+# Author: Stig Thormodsrud
+# Date: May 2008
+# Description: Script to reboot or schedule a reboot
+#
+# **** End License ****
+#
+
+use lib "/opt/vyatta/share/perl5/";
+use Getopt::Long;
+use POSIX;
+use IO::Prompt;
+use Sys::Syslog qw(:standard :macros);
+
+use strict;
+use warnings;
+
+my $reboot_job_file = '/var/run/reboot.job';
+
+
+sub parse_at_output {
+ my @lines = @_;
+
+ foreach my $line (@lines) {
+ if ($line =~ /error/) {
+ return (1, '', '');
+ } elsif ($line =~ /job (\d+) (.*)$/) {
+ return (0, $1, $2);
+ }
+ }
+ return (1, '', '');
+}
+
+sub is_reboot_pending {
+
+ if ( ! -f $reboot_job_file) {
+ return (0, '');
+ }
+ my $job = `cat $reboot_job_file`;
+ chomp $job;
+ my $line = `atq $job`;
+ if ($line =~ /\d+\s+(.*)\sa root$/) {
+ return (1, $1);
+ } else {
+ return (0, '');
+ }
+}
+
+
+#
+# main
+#
+my ($action, $at_time);
+GetOptions("action=s" => \$action,
+ "at_time=s" => \$at_time,
+);
+
+if (! defined $action) {
+ die "no action specified";
+}
+
+openlog($0, "", LOG_USER);
+my $login = getlogin();
+
+#
+# reboot
+#
+if ($action eq "reboot") {
+
+ my ($rc, $time) = is_reboot_pending();
+ if ($rc) {
+ print "Reboot already scheduled for [$time]\n";
+ exit 1;
+ }
+
+ if (prompt("Proceed with reboot? [confirm]", -y1d=>"y")) {
+ syslog("warning", "Reboot now requested by $login");
+ exec("sudo /sbin/reboot");
+ } else {
+ print "Reboot canceled\n";
+ exit 1;
+ }
+}
+
+#
+# reboot_at
+#
+if ($action eq "reboot_at") {
+ if (! -f '/usr/bin/at') {
+ die "Package [at] not installed";
+ }
+
+ if (! defined $at_time) {
+ die "no at_time specified";
+ }
+
+ my ($rc, $rtime) = is_reboot_pending();
+ if ($rc) {
+ print "Reboot already scheduled for [$rtime]\n";
+ exit 1;
+ }
+
+ #
+ # check if the time format is valid, then
+ # remove that job
+ #
+ my @lines = `echo true | at $at_time 2>&1`;
+ my ($err, $job, $time) = parse_at_output(@lines);
+ if ($err) {
+ print "Invalid time format [$at_time]\n";
+ exit 1;
+ }
+ system("atrm $job");
+
+ print "\nReload scheduled for $time\n\n";
+ if (! prompt("Proceed with reboot schedule? [confirm]", -y1d=>"y")) {
+ print "Reboot canceled\n";
+ exit 1;
+ }
+
+ @lines = `echo sudo /sbin/reboot | at $at_time 2>&1`;
+ ($err, $job, $time) = parse_at_output(@lines);
+ if ($err) {
+ print "Error: unable to schedule reboot\n";
+ exit 1;
+ }
+ system("echo $job > $reboot_job_file");
+ print "\nReboot scheduled for $time\n";
+ syslog("warning", "Reboot scheduled for [$time] by $login");
+
+ exit 0;
+}
+
+#
+# reboot_cancel
+#
+if ($action eq "reboot_cancel") {
+
+ my ($rc, $time) = is_reboot_pending();
+ if (! $rc) {
+ print "No reboot currently scheduled\n";
+ exit 1;
+ }
+ my $job = `cat $reboot_job_file`;
+ chomp $job;
+ system("atrm $job");
+ system("rm $reboot_job_file");
+ print "Reboot canceled\n";
+ syslog("warning", "Reboot scheduled for [$time] - CANCELED by $login");
+ exit 0;
+}
+
+#
+# show_reboot
+#
+if ($action eq "show_reboot") {
+
+ my ($rc, $time) = is_reboot_pending();
+ if ($rc) {
+ print "Reboot scheduled for [$time]\n";
+ exit 0;
+ } else {
+ print "No reboot currently scheduled\n";
+ }
+ exit 1;
+}
+
+exit 1;
+
+# end of file
diff --git a/templates/reboot/.at/node.def b/templates/reboot/.at/node.def
new file mode 100644
index 0000000..9944ff7
--- /dev/null
+++ b/templates/reboot/.at/node.def
@@ -0,0 +1 @@
+help: Reboot at a specific time
diff --git a/templates/reboot/.at/node.tag/node.def b/templates/reboot/.at/node.tag/node.def
new file mode 100644
index 0000000..62f39f9
--- /dev/null
+++ b/templates/reboot/.at/node.tag/node.def
@@ -0,0 +1,3 @@
+help: Reboot the system at a future time
+allowed: echo -n '<HH:MM>' '<MMDDYY>' '<midnight>' '<noon>'
+run: sudo /opt/vyatta/bin/sudo-users/vyatta-reboot.pl --action reboot_at --at_time "$3"
diff --git a/templates/reboot/.cancel/node.def b/templates/reboot/.cancel/node.def
new file mode 100644
index 0000000..110f595
--- /dev/null
+++ b/templates/reboot/.cancel/node.def
@@ -0,0 +1,2 @@
+help: Cancel a pending reboot
+run: sudo /opt/vyatta/bin/sudo-users/vyatta-reboot.pl --action reboot_cancel
diff --git a/templates/reboot/node.def b/templates/reboot/node.def
index 57b4c9e..e65917d 100644
--- a/templates/reboot/node.def
+++ b/templates/reboot/node.def
@@ -1,2 +1,2 @@
help: Reboot the system
-run: sudo /sbin/reboot
+run: sudo /opt/vyatta/bin/sudo-users/vyatta-reboot.pl --action reboot
diff --git a/templates/show/.reboot/node.def b/templates/show/.reboot/node.def
new file mode 100644
index 0000000..882f700
--- /dev/null
+++ b/templates/show/.reboot/node.def
@@ -0,0 +1,2 @@
+help: Show scheduled reboot
+run: sudo /opt/vyatta/bin/sudo-users/vyatta-reboot.pl --action show_reboot