summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2019-12-09 23:48:23 +0100
committerDaniil Baturin <daniil@baturin.org>2019-12-09 23:48:23 +0100
commit596ede052e49b81e8fee6ed1cfd8ff350f4c6837 (patch)
treee4d48f2d13fd1e9d8fdf331b644498e76cf394d8
parent209029dcf216a3e30cbdd394c205ac9908cce313 (diff)
downloadvyatta-op-596ede052e49b81e8fee6ed1cfd8ff350f4c6837.tar.gz
vyatta-op-596ede052e49b81e8fee6ed1cfd8ff350f4c6837.zip
T1855: use the new script for "show reboot/poweroff".
-rw-r--r--Makefile.am2
-rwxr-xr-xscripts/vyatta-poweroff.pl189
-rwxr-xr-xscripts/vyatta-reboot.pl245
-rw-r--r--templates/show/poweroff/node.def2
-rw-r--r--templates/show/reboot/node.def2
5 files changed, 2 insertions, 438 deletions
diff --git a/Makefile.am b/Makefile.am
index 2b5950c..dc204fe 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -47,8 +47,6 @@ sbin_SCRIPTS += scripts/vyos-openvpn-remoteconfig.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
-bin_sudo_users_SCRIPTS += scripts/vyatta-poweroff.pl
all-local:
./gen-unpriv-commands.sh
diff --git a/scripts/vyatta-poweroff.pl b/scripts/vyatta-poweroff.pl
deleted file mode 100755
index 9103900..0000000
--- a/scripts/vyatta-poweroff.pl
+++ /dev/null
@@ -1,189 +0,0 @@
-#!/usr/bin/perl
-#
-# Module: vyatta-poweroff.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.
-#
-# Based on the original vyatta-reboot script by Stig Thormodsrud
-#
-# Author: Alex Harpin
-# Date: Jan 2015
-# Description: Script to shutdown or schedule a shutdown
-#
-# **** 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 $poweroff_job_file = '/var/run/poweroff.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_poweroff_pending {
- if ( ! -f $poweroff_job_file) {
- return (0, '');
- }
- my $job = `cat $poweroff_job_file`;
- chomp $job;
- my $line = `atq $job`;
- if ($line =~ /\d+\s+(.*)\sa root$/) {
- return (1, $1);
- } else {
- return (0, '');
- }
-}
-
-sub do_poweroff {
- my $login = shift;
-
- syslog("warning", "Poweroff now requested by $login");
- if (!system("sudo /sbin/shutdown -h now")) {
- exec("sudo /usr/bin/killall sshd");
- }
-}
-
-sub cancel_poweroff {
- my ($login, $time) = @_;
-
- my $job = `cat $poweroff_job_file`;
- chomp $job;
- system("atrm $job");
- system("rm $poweroff_job_file");
- syslog("warning", "Poweroff scheduled for [$time] - CANCELED by $login");
-}
-
-my ($action, $at_time, $now);
-GetOptions("action=s" => \$action,
- "at_time=s" => \$at_time,
- "now!" => \$now,
-);
-
-if (! defined $action) {
- die "no action specified";
-}
-
-openlog($0, "", LOG_USER);
-my $login = getlogin() || getpwuid($<) || "unknown";
-
-if ($action eq "poweroff") {
-
- my ($rc, $time) = is_poweroff_pending();
- if ($rc) {
- if (defined $now) {
- cancel_poweroff($login, $time);
- do_poweroff($login);
- } else {
- print "Poweroff already scheduled for [$time]\n";
- exit 1;
- }
- }
-
- if (defined $now) {
- do_poweroff($login);
- } else {
- if (prompt("Proceed with poweroff? (Yes/No) [No] ", -ynd=>"n")) {
- do_poweroff($login);
- } else {
- print "Poweroff canceled\n";
- exit 1;
- }
- }
-}
-
-if ($action eq "poweroff_at") {
- if (! -f '/usr/bin/at') {
- die "Package [at] not installed";
- }
-
- if (! defined $at_time) {
- die "no at_time specified";
- }
-
- my ($rc, $rtime) = is_poweroff_pending();
- if ($rc) {
- print "Poweroff already scheduled for [$rtime]\n";
- exit 1;
- }
-
- 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 "\nPoweroff scheduled for $time\n\n";
- if (!prompt("Proceed with poweroff schedule? [confirm] ", -y1d=>"y")) {
- print "Poweroff canceled\n";
- exit 1;
- }
-
- @lines = `echo "sudo /sbin/shutdown -h now && sudo /usr/bin/killall sshd" | at $at_time 2>&1`;
- ($err, $job, $time) = parse_at_output(@lines);
- if ($err) {
- print "Error: unable to schedule poweroff\n";
- exit 1;
- }
- system("echo $job > $poweroff_job_file");
- print "\nPoweroff scheduled for $time\n";
- syslog("warning", "Poweroff scheduled for [$time] by $login");
-
- exit 0;
-}
-
-if ($action eq "poweroff_cancel") {
-
- my ($rc, $time) = is_poweroff_pending();
- if (! $rc) {
- print "No poweroff currently scheduled\n";
- exit 1;
- }
- cancel_poweroff($login, $time);
- print "Poweroff canceled\n";
- exit 0;
-}
-
-if ($action eq "show_poweroff") {
-
- my ($rc, $time) = is_poweroff_pending();
- if ($rc) {
- print "Poweroff scheduled for [$time]\n";
- exit 0;
- } else {
- print "No poweroff currently scheduled\n";
- }
- exit 1;
-}
-
-exit 1;
diff --git a/scripts/vyatta-reboot.pl b/scripts/vyatta-reboot.pl
deleted file mode 100755
index 93cda4f..0000000
--- a/scripts/vyatta-reboot.pl
+++ /dev/null
@@ -1,245 +0,0 @@
-#!/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';
-my $confirm_job_file = '/var/run/confirm.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 {
- ### that won't exist anymore but may be called somewhere else
- if ( -f $reboot_job_file )
- {
- my $job = `cat $reboot_job_file`;
- chomp $job;
- my $line = `atq $job`;
- if ($line =~ /\d+\s+(.*)\sa root$/)
- {
- return (1, $1);
- }
- else
- {
- return (0, '');
- }
- }
-
- #### comit-confirm creates an at job as well
- if ( -f $confirm_job_file)
- {
- my $job = `cat $confirm_job_file`;
- chomp $job;
- my $line = `atq $job`;
- if ($line =~ /\d+\s+(.*)\sa root$/)
- {
- return (1, $1);
- }
- else
- {
- return (0, '');
- }
- }
- #### an now the systemd job from powrctrl.py
- my $line = `systemctl status systemd-shutdownd.service`;
- if ($line =~ /Active: active/)
- {
- if($line =~ m/Status:(.*)/)
- {
- return (1, $1);
- }
- }
- else
- {
- return (0, '');
- }
-}
-
-sub do_reboot {
- my $login = shift;
-
- syslog("warning", "Reboot now requested by $login");
- if (!system("sudo /sbin/reboot")) {
- exec("sudo /usr/bin/killall sshd");
- }
-}
-
-sub cancel_reboot {
- my ($login, $time) = @_;
-
- my $job = `cat $reboot_job_file`;
- chomp $job;
- system("atrm $job");
- system("rm $reboot_job_file");
- syslog("warning", "Reboot scheduled for [$time] - CANCELED by $login");
-}
-
-#
-# main
-#
-my ($action, $at_time, $now);
-GetOptions("action=s" => \$action,
- "at_time=s" => \$at_time,
- "now!" => \$now,
-);
-
-if (! defined $action) {
- die "no action specified";
-}
-
-openlog($0, "", LOG_USER);
-my $login = getlogin() || getpwuid($<) || "unknown";
-
-#
-# reboot
-#
-if ($action eq "reboot") {
-
- my ($rc, $time) = is_reboot_pending();
- if ($rc) {
- if (defined $now) {
- cancel_reboot($login, $time);
- do_reboot($login);
- } else {
- print "Reboot already scheduled for [$time]\n";
- exit 1;
- }
- }
-
- if (defined $now) {
- do_reboot($login);
- } else {
- if (defined($ENV{VYATTA_PROCESS_CLIENT} && $ENV{VYATTA_PROCESS_CLIENT} eq 'gui2_rest') ||
- prompt("Proceed with reboot? (Yes/No) [No] ", -ynd=>"n")) {
- do_reboot($login);
- } 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 (!defined($ENV{VYATTA_PROCESS_CLIENT}) || $ENV{VYATTA_PROCESS_CLIENT} ne 'gui2_rest') {
- if (! prompt("Proceed with reboot schedule? [confirm] ", -y1d=>"y")) {
- print "Reboot canceled\n";
- exit 1;
- }
- }
-
- @lines = `echo "sudo /sbin/reboot && sudo /usr/bin/killall sshd" | 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;
- }
- cancel_reboot($login, $time);
- print "Reboot canceled\n";
- 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/show/poweroff/node.def b/templates/show/poweroff/node.def
index a65f4e6..1505800 100644
--- a/templates/show/poweroff/node.def
+++ b/templates/show/poweroff/node.def
@@ -1,2 +1,2 @@
help: Show scheduled poweroff
-run: sudo /opt/vyatta/bin/sudo-users/vyatta-poweroff.pl --action show_poweroff
+run: sudo /usr/libexec/vyos/op_mode/powerctrl.py --check
diff --git a/templates/show/reboot/node.def b/templates/show/reboot/node.def
index 882f700..dfb00d4 100644
--- a/templates/show/reboot/node.def
+++ b/templates/show/reboot/node.def
@@ -1,2 +1,2 @@
help: Show scheduled reboot
-run: sudo /opt/vyatta/bin/sudo-users/vyatta-reboot.pl --action show_reboot
+run: /usr/libexec/vyos/op_mode/powerctrl.py --check