host actions

This commit is contained in:
Paolo Asperti 2016-07-28 01:10:06 +02:00
parent 3d81fd12e3
commit 34457e9692
13 changed files with 207 additions and 24 deletions

View File

@ -9,6 +9,7 @@ BuildRoot: %{_tmppath}/%{name}-root
BuildRequires: nethserver-devtools > 1.0.1
Requires: httpd, php, sudo, php-xml, php-mcrypt
Requires: nethserver-base, nethserver-php
Requires: net-tools
Requires: upstart
Source0: doraemon-1.3.0.tar.gz
BuildArch: noarch

View File

@ -0,0 +1,34 @@
#!/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 $event = $ARGV [0];
defined $ARGV[1] || die "No host specied";
my $host = $ARGV[1];
reboot_host($host);
exit 0;
sub reboot_host
{
my ($hostName) = @_;
#------------------------------------------------------------
# Reboot the host
#------------------------------------------------------------
my $h = $db_hosts->get($hostName) or die "No host record for user $hostName";
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";
}

View File

@ -0,0 +1,34 @@
#!/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 $event = $ARGV [0];
defined $ARGV[1] || die "No host specied";
my $host = $ARGV[1];
shutdown_host($host);
exit 0;
sub shutdown_host
{
my ($hostName) = @_;
#------------------------------------------------------------
# Shutdown the host
#------------------------------------------------------------
my $h = $db_hosts->get($hostName) or die "No host record for user $hostName";
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";
}

View File

@ -0,0 +1,35 @@
#!/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 $event = $ARGV [0];
defined $ARGV[1] || die "No host specied";
my $host = $ARGV[1];
wake_host($host);
exit 0;
sub wake_host
{
my ($hostName) = @_;
#------------------------------------------------------------
# Wake up the host
#------------------------------------------------------------
my $h = $db_hosts->get($hostName) or die "No host record for user $hostName";
my $client = $h[0];
my $mac = $client->prop('MacAddress');
system("/sbin/ether-wake -i lan0 $mac") == 0
or die "Error waking up $hostName";
}

View File

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

View File

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

View File

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

View File

@ -1,12 +1,15 @@
<?php
<?php
/* /usr/share/nethesis/NethServer/Language/en/NethServer_Module_Doraemon.php */
$L['Doraemon_Title'] = 'Doraemon';
$L['Key_label'] = 'Hostname';
$L['hostname_label'] = 'Hostname';
$L['Role_label'] = 'Role';
$L['MacAddress_label'] = 'MAC Address';
$L['Hosts_title'] = 'Hosts';
$L['Create_Host_Header'] = 'Create a new host';
$L['Update_Host_Header'] = 'Update host "${0}"';
$L['Doraemon_Title'] = 'Doraemon';
$L['Key_label'] = 'Hostname';
$L['hostname_label'] = 'Hostname';
$L['Role_label'] = 'Role';
$L['MacAddress_label'] = 'MAC Address';
$L['Hosts_Title'] = 'Hosts';
$L['Create_Host_Header'] = 'Create a new host';
$L['Update_Host_Header'] = 'Update host "${0}"';
$L['shutdown_label'] = 'Shutdown';
$L['reboot_label'] = 'Reboot';
$L['wake_label'] = 'Wake Up';

View File

@ -1,12 +1,15 @@
<?php
<?php
/* /usr/share/nethesis/NethServer/Language/it/NethServer_Module_Doraemon.php */
$L['Doraemon_Title'] = 'Doraemon';
$L['Key_label'] = 'Hostname';
$L['hostname_label'] = 'Hostname';
$L['Role_label'] = 'Ruolo';
$L['MacAddress_label'] = 'MAC Address';
$L['Hosts_title'] = 'Hosts';
$L['Create_Host_Header'] = 'Crea un nuovo host';
$L['Update_Host_Header'] = 'Modifica il nome host "${0}"';
$L['Doraemon_Title'] = 'Doraemon';
$L['Key_label'] = 'Hostname';
$L['hostname_label'] = 'Hostname';
$L['Role_label'] = 'Ruolo';
$L['MacAddress_label'] = 'MAC Address';
$L['Hosts_Title'] = 'Hosts';
$L['Create_Host_Header'] = 'Crea un nuovo host';
$L['Update_Host_Header'] = 'Modifica il nome host "${0}"';
$L['shutdown_label'] = 'Spegni';
$L['reboot_label'] = 'Rivvia';
$L['wake_label'] = 'Accendi';

View File

@ -18,11 +18,10 @@ class Hosts extends \Nethgui\Controller\TableController
$this
->setTableAdapter($this->getPlatform()->getTableAdapter('hosts', 'remote'))
->setColumns($columns)
// TODO:
// ->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('wake'))
// ->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('reboot'))
// ->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('shutdown'))
->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('update'))
->addRowAction(new \NethServer\Module\Doraemon\Hosts\TogglePower('wake'))
->addRowAction(new \NethServer\Module\Doraemon\Hosts\TogglePower('reboot'))
->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 \Nethgui\Controller\Table\Help('Help'))

View File

@ -12,7 +12,7 @@ class Modify extends \Nethgui\Controller\Table\Modify
{
$parameterSchema = array(
// was: Validate::HOSTNAME_FQDN, but we need hostname
array('hostname', Validate::ANYTHING, \Nethgui\Controller\Table\Modify::KEY),
array('hostname', Validate::HOSTNAME_SIMPLE, \Nethgui\Controller\Table\Modify::KEY),
array('MacAddress', Validate::MACADDRESS, \Nethgui\Controller\Table\Modify::FIELD),
array('Role', Validate::ANYTHING, \Nethgui\Controller\Table\Modify::FIELD),
// array('Role', Validate::ROLES_COLLECTION ??? TODO!!!, \Nethgui\Controller\Table\Modify::FIELD),
@ -52,4 +52,4 @@ class Modify extends \Nethgui\Controller\Table\Modify
$view->setTemplate($templates[$this->getIdentifier()]);
}
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace NethServer\Module\Doraemon\Hosts;
use Nethgui\System\PlatformInterface as Validate;
/**
* Toggle host power status
*
* Fires events
* - doraemon-wake
* - doraemon-reboot
* - doraemon-shutdown
*/
class TogglePower extends \Nethgui\Controller\Table\AbstractAction
{
public function __construct($identifier = NULL)
{
if ($identifier !== 'wake' && $identifier !== 'reboot' && $identifier !== 'shutdown') {
throw new \InvalidArgumentException(sprintf('%s: module identifier must be one of "wake" or "reboot" or "shutdown".', get_class($this)), 1325579395);
}
parent::__construct($identifier);
}
public function bind(\Nethgui\Controller\RequestInterface $request)
{
$this->declareParameter('hostname', Validate::HOSTNAME_SIMPLE);
parent::bind($request);
$hostname = \Nethgui\array_end($request->getPath());
if ( ! $hostname) {
throw new \Nethgui\Exception\HttpException('Not found', 404, 1322148400);
}
$this->parameters['hostname'] = $hostname;
}
public function process()
{
if ( ! $this->getRequest()->isMutation()) {
return;
}
$this->getPlatform()->signalEvent(sprintf('doraemon-%s@post', $this->getIdentifier()), array($this->parameters['hostname']));
}
}

View File

@ -0,0 +1,23 @@
<?php
$view->requireFlag($view::INSET_DIALOG);
if ($view->getModule()->getIdentifier() == 'shutdown') {
$headerText = $T('Shutdown host `${0}`');
$panelText = $T('Proceed with host `${0}` shutdown?');
} elseif ($view->getModule()->getIdentifier() == 'reboot') {
$headerText = $T('Reboot host `${0}`');
$panelText = $T('Proceed with host `${0}` reboot?');
} else {
$headerText = $T('Wakeup host `${0}`');
$panelText = $T('Proceed with host `${0}` wakeup?');
}
echo $view->panel()
->insert($view->header('hostname')->setAttribute('template', $headerText))
->insert($view->textLabel('hostname')->setAttribute('template', $panelText))
;
echo $view->buttonList()
->insert($view->button('Yes', $view::BUTTON_SUBMIT))
->insert($view->button('No', $view::BUTTON_CANCEL)->setAttribute('value', $view['Cancel']))
;