global hosts actions

This commit is contained in:
Paolo Asperti 2016-09-27 15:59:16 +02:00
parent eccdbb68d4
commit d425ad55a2
11 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,20 @@
#!/usr/bin/perl -w
package esmith;
use strict;
use Errno;
use esmith::DB::db;
use IO::File;
use English;
my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n");
my @items = $db_hosts->get_all('remote');
foreach my $item (@items) {
my $hostName = $item->key;
system("/usr/bin/sudo -u amgmt /usr/bin/ssh -o StrictHostKeyChecking=no amgmt\@$hostName /usr/bin/sudo /sbin/reboot &") == 0
or die "Error rebooting $hostName";
}
exit 0;

View File

@ -0,0 +1,20 @@
#!/usr/bin/perl -w
package esmith;
use strict;
use Errno;
use esmith::DB::db;
use IO::File;
use English;
my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n");
my @items = $db_hosts->get_all('remote');
foreach my $item (@items) {
my $hostName = $item->key;
system("/usr/bin/sudo -u amgmt /usr/bin/ssh -o StrictHostKeyChecking=no amgmt\@$hostName /usr/bin/sudo /sbin/poweroff &") == 0
or die "Error shutting down $hostName";
}
exit 0;

View File

@ -0,0 +1,21 @@
#!/usr/bin/perl -w
package esmith;
use strict;
use Errno;
use esmith::DB::db;
use IO::File;
use English;
my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n");
my @items = $db_hosts->get_all('remote');
foreach my $item (@items) {
my $hostName = $item->key;
my $mac = $item->prop('MacAddress');
system("/sbin/ether-wake -i lan0 $mac &") == 0
or die "Error waking up $hostName";
}
exit 0;

View File

@ -0,0 +1 @@
../actions/doraemon-reboot-all

View File

@ -0,0 +1 @@
../actions/doraemon-shutdown-all

View File

@ -0,0 +1 @@
../actions/doraemon-wake-all

View File

@ -8,3 +8,6 @@ $L['MacAddress_label'] = 'MAC Address';
$L['shutdown_label'] = 'Shutdown';
$L['reboot_label'] = 'Reboot';
$L['wake_label'] = 'Wake Up';
$L['shutdown-all_label'] = 'Shutdown all';
$L['reboot-all_label'] = 'Reboot all';
$L['wake-all_label'] = 'Wake Up all';

View File

@ -8,3 +8,6 @@ $L['MacAddress_label'] = 'MAC Address';
$L['shutdown_label'] = 'Spegni';
$L['reboot_label'] = 'Riavvia';
$L['wake_label'] = 'Accendi';
$L['shutdown-all_label'] = 'Spegni tutti';
$L['reboot-all_label'] = 'Riavvia tutti';
$L['wake-all_label'] = 'Accendi tutti';

View File

@ -24,6 +24,9 @@ class Hosts extends \Nethgui\Controller\TableController
->addRowAction(new \NethServer\Module\Doraemon\Hosts\TogglePower('shutdown'))
->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('delete'))
->addTableAction(new \NethServer\Module\Doraemon\Hosts\Modify('create'))
->addTableAction(new \NethServer\Module\Doraemon\Hosts\TogglePowerAll('wake-all'))
->addTableAction(new \NethServer\Module\Doraemon\Hosts\TogglePowerAll('reboot-all'))
->addTableAction(new \NethServer\Module\Doraemon\Hosts\TogglePowerAll('shutdown-all'))
->addTableAction(new \Nethgui\Controller\Table\Help('Help'))
;

View File

@ -0,0 +1,34 @@
<?php
namespace NethServer\Module\Doraemon\Hosts;
use Nethgui\System\PlatformInterface as Validate;
/**
* Toggle host power status
*
* Fires events
* - doraemon-wake-all
* - doraemon-reboot-all
* - doraemon-shutdown-all
*/
class TogglePowerAll extends \Nethgui\Controller\Table\AbstractAction
{
public function __construct($identifier = NULL)
{
if ($identifier !== 'wake-all' && $identifier !== 'reboot-all' && $identifier !== 'shutdown-all') {
throw new \InvalidArgumentException(sprintf('%s: module identifier must be one of "wake-all" or "reboot-all" or "shutdown-all".', get_class($this)), 1325579395);
}
parent::__construct($identifier);
}
public function process()
{
if ( ! $this->getRequest()->isMutation()) {
return;
}
$this->getPlatform()->signalEvent(sprintf('doraemon-%s@post', $this->getIdentifier()));
}
}

View File

@ -0,0 +1,21 @@
<?php
$view->requireFlag($view::INSET_DIALOG);
if ($view->getModule()->getIdentifier() == 'shutdown-all') {
$headerText = $T('Shutdown all hosts');
$panelText = $T('Proceed with all hosts shutdown?');
} elseif ($view->getModule()->getIdentifier() == 'reboot-all') {
$headerText = $T('Reboot all hosts');
$panelText = $T('Proceed with all hosts reboot?');
} else {
$headerText = $T('Wakeup all hosts');
$panelText = $T('Proceed with all hosts wakeup?');
}
echo $view->header()->setAttribute('template', $headerText);
// TODO: add a text label here
// echo $view->textLabel()->setAttribute('template', $panelText);
echo $view->buttonList()
->insert($view->button('Yes', $view::BUTTON_SUBMIT))
->insert($view->button('No', $view::BUTTON_CANCEL)->setAttribute('value', $view['Cancel']))
;