This commit is contained in:
Paolo Asperti 2024-04-28 09:44:40 +02:00
parent 74114da7a5
commit 78038181ee
Signed by: paspo
GPG Key ID: 06D46905D19D5182
18 changed files with 656 additions and 0 deletions

View File

@ -0,0 +1,177 @@
Summary: Helps client to join domain and maintain itself
Name: doraemon
Version: 2.1.0
Release: 4.ns6
URL: https://github.com/bglug-it/doraemon/
License: GPLv2+
Packager: Paolo Asperti <paolo@asperti.com>
Group: System Environment/Daemons
BuildRoot: %{_tmppath}/%{name}-root
BuildRequires: nethserver-devtools > 1.0.1
BuildRequires: hunspell-en
Requires: httpd, php, sudo, php-xml, php-mcrypt
Requires: nethserver-base, nethserver-php
Requires: net-tools
Requires: upstart
Requires: ansible
Source0: %{name}-%{version}-%{release}.tar.gz
Source1: createlinks.pl
BuildArch: noarch
%description
Helps client on a domain network to get information for its maintenance.
%prep
%setup -n %{name}-%{version}-%{release}
%build
%{makedocs}
perl createlinks.pl
%install
rm -Rf $RPM_BUILD_ROOT
(cd root ; find . -depth -print | cpio -dump %{buildroot})
rm -f %{name}-%{version}-%{release}-filelist
%{genfilelist} %{buildroot} > %{name}-%{version}-%{release}-filelist
mkdir -p %{buildroot}%{_localstatedir}/log/doraemon
mkdir -p %{buildroot}%{_localstatedir}/cache/doraemon
%pre
#if [ "$1" = 1 ]; then
# #installation
#fi
if [ "$1" = 2 ]; then
#upgrade
/sbin/service %{name} stop >/dev/null 2>&1 || :
/sbin/stop %{name} >/dev/null 2>&1 || :
DATABASE=/var/lib/doraemon/doraemon.db
INI=/etc/doraemon.ini
if [ -f $INI ]; then
DATABASE=$(awk -F "=" '/Database/ {print $2}' $INI)
PORT=$(awk -F "=" '/Port/ {print $2}' $INI)
BASE=$(awk -F "=" '/Base/ {print $2}' $INI)
ROLE=$(awk -F "=" '/Role/ {print $2}' $INI)
DIGITS=$(awk -F "=" '/Digits/ {print $2}' $INI)
DOMAIN=$(awk -F "=" '/Domain/ {print $2}' $INI)
MGMTKEY=$(awk -F "=" '/MgmtKey/ {print $2}' $INI)
VAULTPASSFILE=$(awk -F "=" '/VaultPassFile/ {print $2}' $INI)
rm $INI
/sbin/e-smith/db configuration set %{name} service \
status enabled \
TCPPort $PORT \
access private \
DefaultRole $ROLE \
DomainFile $DOMAIN \
ManagementKeyFile $MGMTKEY \
NamingBase $BASE \
NamingDigits $DIGITS \
VaultPassFile $VAULTPASSFILE
else
/sbin/e-smith/db configuration set %{name} service \
status enabled \
TCPPort 3000 \
access private \
DefaultRole client \
DomainFile /etc/domain.yml \
ManagementKeyFile /home/amgmt/.ssh/id_rsa.pub \
NamingBase lab \
NamingDigits 2 \
VaultPassFile /home/amgmt/.ansible/vault.txt
fi
if [ -f $DATABASE ]; then
sqlite3 -separator " " $DATABASE \
"select * from client" | while read id hostname mac role; do
/sbin/e-smith/db hosts set $hostname local \
MacAddress $mac Role $role
done
rm $DATABASE
fi
fi
%post
/etc/e-smith/events/actions/initialize-default-databases
[ -e /usr/sbin/doraemon ] && [ ! -L /usr/sbin/doraemon ] && rm /usr/sbin/doraemon
[ ! -L /usr/sbin/doraemon ] && ln -s httpd /usr/sbin/doraemon
if [ "$1" = 1 ]; then
#installation
/sbin/e-smith/db configuration set %{name} service status enabled TCPPort 3000 access private DefaultRole client DomainFile /etc/domain.yml ManagementKeyFile /home/amgmt/.ssh/id_rsa.pub ManagementPrivateKeyFile /home/amgmt/.ssh/id_rsa NamingBase lab NamingDigits 2 VaultPassFile /home/amgmt/.ansible/vault.txt
/sbin/e-smith/expand-template /etc/sudoers
/sbin/e-smith/expand-template /etc/httpd/doraemon/httpd.conf
/sbin/start %{name} >/dev/null 2>&1 || :
/sbin/e-smith/signal-event runlevel-adjust
/sbin/e-smith/signal-event firewall-adjust
fi
if [ "$1" = 2 ]; then
#upgrade
# TODO: this won't be needed in the next version
/sbin/e-smith/expand-template /etc/sudoers
/sbin/e-smith/expand-template /etc/httpd/doraemon/httpd.conf
/sbin/start %{name} >/dev/null 2>&1 || :
/sbin/e-smith/signal-event runlevel-adjust
/sbin/e-smith/signal-event firewall-adjust
fi
%preun
#if [ "$1" = 1 ]; then
# #upgrade
#fi
if [ "$1" = 0 ]; then
#uninstallation
/sbin/stop %{name} >/dev/null 2>&1 || :
fi
%postun
#if [ "$1" = 1 ]; then
# #upgrade
#fi
if [ "$1" = 0 ]; then
#uninstallation
/sbin/e-smith/db configuration delete %{name}
/sbin/e-smith/expand-template /etc/sudoers
/sbin/e-smith/signal-event runlevel-adjust
/sbin/e-smith/signal-event firewall-adjust
fi
%clean
rm -rf %{buildroot}
%files -f %{name}-%{version}-%{release}-filelist
%defattr(0644,root,root,755)
%doc README.md LICENSE.txt
%attr(0644,srvmgr,srvmgr) %ghost %{_localstatedir}/log/doraemon/access_log
%attr(0644,srvmgr,srvmgr) %ghost %{_localstatedir}/log/doraemon/error_log
%changelog
* Sun Oct 8 2017 Paolo Asperti <paolo@asperti.com> - 2.0.2-1
- FIX: bug in WOL
- FIX: small language typos
* Wed Nov 9 2016 Paolo Asperti <paolo@asperti.com> - 2.0.1-1
- PHP porting
- Added a basic UI
* Tue Oct 27 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.2.1-2.ns6
- Packing corrections to rpm to fix upgrading issue
* Tue Oct 27 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.2.1-1.ns6
- Packing new minor correction on how packages are extracted from db
* Mon Sep 11 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.2.0-1.ns6
- Packing new version with ansible dynamic inventory support, epoptes server role support.
* Mon Sep 07 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.1.1-1.ns6
- Packing new version with correct roles.
* Sun Sep 06 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.1.0-1.ns6
- Packing new version with new functionalities.
* Wed Jul 15 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.0.0-2.ns6
- Added logrotate file.
* Mon Jul 13 2015 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 1.0.0-1.ns6
- Unique binary for both ansiblehelper and mac2hostname.

View File

@ -0,0 +1,13 @@
<?php
/* /usr/share/nethesis/NethServer/Language/en/NethServer_Module_Doraemon.php */
$L['Doraemon_Title'] = 'Doraemon';
$L['hostname_label'] = 'Hostname';
$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

@ -0,0 +1,9 @@
<?php
/* /usr/share/nethesis/NethServer/Language/en/NethServer_Module_Doraemon_Hosts.php */
$L['Hosts_Title'] = 'Hosts';
$L['Create_Host_Header'] = 'Create a new host';
$L['Update_Host_Header'] = 'Update host "${0}"';
$L['Role_label'] = 'Role';
$L['Key_label'] = 'Hostname';

View File

@ -0,0 +1,9 @@
<?php
/* /usr/share/nethesis/NethServer/Language/en/NethServer_Module_Doraemon_Rooms.php */
$L['Rooms_Title'] = 'Rooms';
$L['Create_Room_Header'] = 'Create a new room';
$L['Update_Room_Header'] = 'Update room "${0}"';
$L['Name_label'] = 'Name';
$L['Key_label'] = 'Room';

View File

@ -0,0 +1,13 @@
<?php
/* /usr/share/nethesis/NethServer/Language/it/NethServer_Module_Doraemon.php */
$L['Doraemon_Title'] = 'Doraemon';
$L['hostname_label'] = 'Hostname';
$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

@ -0,0 +1,9 @@
<?php
/* /usr/share/nethesis/NethServer/Language/it/NethServer_Module_Doraemon_Hosts.php */
$L['Hosts_Title'] = 'Hosts';
$L['Create_Host_Header'] = 'Crea un nuovo host';
$L['Update_Host_Header'] = 'Modifica il nome host "${0}"';
$L['Role_label'] = 'Ruolo';
$L['Key_label'] = 'Hostname';

View File

@ -0,0 +1,9 @@
<?php
/* /usr/share/nethesis/NethServer/Language/it/NethServer_Module_Doraemon_Rooms.php */
$L['Rooms_Title'] = 'Stanze';
$L['Create_Room_Header'] = 'Crea una nuova stanza';
$L['Update_Room_Header'] = 'Modifica il nome stanza "${0}"';
$L['Name_label'] = 'Nome';
$L['Key_label'] = 'Stanza';

View File

@ -0,0 +1,17 @@
<?php
namespace NethServer\Module;
class Doraemon extends \Nethgui\Controller\TabsController
{
protected function initializeAttributes(\Nethgui\Module\ModuleAttributesInterface $base)
{
return \Nethgui\Module\SimpleModuleAttributesProvider::extendModuleAttributes($base, 'Administration', 50);
}
public function initialize()
{
parent::initialize();
$this->loadChildrenDirectory();
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace NethServer\Module\Doraemon;
class Hosts extends \Nethgui\Controller\TableController
{
public function initialize()
{
$columns = array(
'Key',
'MacAddress',
'IpAddress',
'Role',
'LastSeen',
'LastConfig',
'Actions',
);
$this
->setTableAdapter($this->getPlatform()->getTableAdapter('hosts', 'local'))
->setColumns($columns)
->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('update'))
// ->addRowAction(new \NethServer\Module\Doraemon\Hosts\Modify('adopt'))
->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 \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'))
;
parent::initialize();
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace NethServer\Module\Doraemon\Hosts;
use Nethgui\System\PlatformInterface as Validate;
class Modify extends \Nethgui\Controller\Table\Modify
{
public function initialize()
{
$parameterSchema = array(
// was: Validate::HOSTNAME_FQDN, but we need hostname
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),
);
$this->setSchema($parameterSchema);
parent::initialize();
}
public function validate(\Nethgui\Controller\ValidationReportInterface $report)
{
if ($this->getIdentifier() === 'delete') {
$v = $this->createValidator()->platform('host-delete');
if ( ! $v->evaluate($this->parameters['hostname'])) {
$report->addValidationError($this, 'Key', $v);
}
}
parent::validate($report);
}
protected function onParametersSaved($parameters)
{
$actionName = $this->getIdentifier();
if ($actionName === 'update') {
$actionName = 'modify';
}
$this->getPlatform()->signalEvent(sprintf('host-%s &', $actionName));
}
public function prepareView(\Nethgui\View\ViewInterface $view)
{
parent::prepareView($view);
$templates = array(
'create' => 'NethServer\Template\Doraemon\Hosts',
'update' => 'NethServer\Template\Doraemon\Hosts',
'delete' => 'Nethgui\Template\Table\Delete',
);
$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,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,28 @@
<?php
namespace NethServer\Module\Doraemon;
class Rooms extends \Nethgui\Controller\TableController
{
public function initialize()
{
$columns = array(
'Key',
'Actions',
);
$this
->setTableAdapter($this->getPlatform()->getTableAdapter('rooms', 'room'))
->setColumns($columns)
->addRowAction(new \NethServer\Module\Doraemon\Rooms\Modify('update'))
->addRowAction(new \NethServer\Module\Doraemon\Rooms\Modify('delete'))
->addTableAction(new \NethServer\Module\Doraemon\Rooms\Modify('create'))
->addTableAction(new \Nethgui\Controller\Table\Help('Help'))
;
parent::initialize();
}
}

View File

@ -0,0 +1,89 @@
<?php
namespace NethServer\Module\Doraemon\Rooms;
use Nethgui\System\PlatformInterface as Validate;
use Nethgui\Controller\Table\Modify as Table;
class Modify extends \Nethgui\Controller\Table\Modify
{
public function initialize()
{
$parameterSchema = array(
// was: Validate::HOSTNAME_FQDN, but we need hostname
array('Name', Validate::ANYTHING, Table::KEY),
array('AclRead', Validate::USERNAME_COLLECTION, Table::FIELD, 'AclRead', ','), // ACL
array('AclWrite', Validate::USERNAME_COLLECTION, Table::FIELD, 'AclWrite', ','), // ACL
// 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),
);
$this->setSchema($parameterSchema);
parent::initialize();
}
public function bind(\Nethgui\Controller\RequestInterface $request)
{
parent::bind($request);
if($request->isMutation()) {
// save the old values for later usage:
$this->originalAclRead = $this->getPlatform()->getDatabase('accounts')->getProp($this->parameters['ibay'], 'AclRead');
$this->originalAclWrite = $this->getPlatform()->getDatabase('accounts')->getProp($this->parameters['ibay'], 'AclWrite');
}
}
public function validate(\Nethgui\Controller\ValidationReportInterface $report)
{
if ($this->getIdentifier() === 'delete') {
$v = $this->createValidator()->platform('room-delete');
if ( ! $v->evaluate($this->parameters['name'])) {
$report->addValidationError($this, 'Key', $v);
}
}
parent::validate($report);
}
protected function onParametersSaved($parameters)
{
$actionName = $this->getIdentifier();
if ($actionName === 'update') {
$actionName = 'modify';
}
$this->getPlatform()->signalEvent(sprintf('room-%s &', $actionName));
}
public function prepareView(\Nethgui\View\ViewInterface $view)
{
parent::prepareView($view);
$templates = array(
'create' => 'NethServer\Template\Doraemon\Rooms',
'update' => 'NethServer\Template\Doraemon\Rooms',
'delete' => 'Nethgui\Template\Table\Delete',
);
$view->setTemplate($templates[$this->getIdentifier()]);
$owners = array(array('locals', $view->translate('locals_group_label')));
$subjects = array(array('locals', $view->translate('locals_group_label')));
foreach ($this->getPlatform()->getDatabase('accounts')->getAll('group') as $keyName => $props) {
$entry = array($keyName, sprintf("%s (%s)", isset($props['Description']) ? $props['Description'] : $keyName, $keyName));
$owners[] = $entry;
$subjects[] = $entry;
}
$view['OwningGroupDatasource'] = $owners;
foreach ($this->getPlatform()->getDatabase('accounts')->getAll('user') as $keyName => $props) {
$entry = array($keyName, sprintf("%s (%s)", trim($props['FirstName'] . ' ' . $props['LastName']), $keyName));
$subjects[] = $entry;
}
$view['AclSubjects'] = $subjects;
}
}

View File

@ -0,0 +1,22 @@
<?php
/* @var $view \Nethgui\Renderer\Xhtml */
if ($view->getModule()->getIdentifier() == 'update') {
// disable updating of hostname
// the hostname is the key of the db item, if you change it, bad things happens
$keyFlags = $view::STATE_DISABLED;
$template = 'Update_Host_Header';
} else {
$keyFlags = 0;
$template = 'Create_Host_Header';
}
echo $view->header('hostname')->setAttribute('template', $T($template));
echo $view->panel()
->insert($view->textInput('hostname', $keyFlags))
->insert($view->textInput('MacAddress'))
->insert($view->textInput('Role'))
;
echo $view->buttonList($view::BUTTON_SUBMIT | $view::BUTTON_CANCEL | $view::BUTTON_HELP);

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']))
;

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']))
;

View File

@ -0,0 +1,40 @@
<?php
/* @var $view \Nethgui\Renderer\Xhtml */
$view->requireFlag($view::INSET_FORM);
if ($view->getModule()->getIdentifier() == 'update') {
// disable updating of hostname
// the hostname is the key of the db item, if you change it, bad things happens
$keyFlags = $view::STATE_DISABLED;
$template = 'Update_Room_Header';
} else {
$keyFlags = 0;
$template = 'Create_Room_Header';
}
echo $view->header('doraemon')->setAttribute('template', $view->translate($template));
echo $view->panel()
->insert($view->textInput('ibay', $keyFlags))
->insert($view->textInput('Description'))
->insert($view->selector('OwningGroup', $view::SELECTOR_DROPDOWN))
->insert($view->checkBox('GroupAccess', 'rw')->setAttribute('uncheckedValue', 'r'))
->insert($view->checkBox('OtherAccess', 'r')->setAttribute('uncheckedValue', ''))
->insert($view->objectPicker()
->setAttribute('objects', 'AclSubjects')
->setAttribute('objectLabel', 1)
->insert($view->checkBox('AclRead', FALSE, $view::STATE_CHECKED))
->insert($view->checkBox('AclWrite', FALSE))
);
echo $view->buttonList($view::BUTTON_SUBMIT | $view::BUTTON_CANCEL | $view::BUTTON_HELP);
$actionId = $view->getUniqueId();
$view->includeJavascript("
jQuery(function($){
$('#${actionId}').on('nethguishow', function () {
$(this).find('.Tabs').tabs('select', 0);
});
});
");