Merge pull request #4 from syntaxerrormmm/php_rewrite_alone

Port to PHP, serious fixes to core and events
This commit is contained in:
Emiliano Vavassori 2018-06-17 02:14:35 +02:00 committed by GitHub
commit 45a944090b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 32 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.sh *.sh
*.tar.* *.tar.*
*.rpm

View File

@ -1,6 +1,6 @@
Summary: Helps client to join domain and maintain itself Summary: Helps client to join domain and maintain itself
Name: doraemon Name: doraemon
Version: 2.0.1 Version: 2.0.3
Release: 1.ns6 Release: 1.ns6
URL: https://github.com/bglug-it/doraemon/ URL: https://github.com/bglug-it/doraemon/
License: GPLv2+ License: GPLv2+
@ -145,6 +145,14 @@ rm -rf %{buildroot}
%changelog %changelog
* Sun Jun 17 2018 Emiliano Vavassori <syntaxerrormmm-AT-gmail.com> - 2.0.3-1
- FIX: whatsmyhostname correctly parse role variable
- Minor aesthetic syntax fixes
* 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 * Wed Nov 9 2016 Paolo Asperti <paolo@asperti.com> - 2.0.1-1
- PHP porting - PHP porting
- Added a basic UI - Added a basic UI

View File

@ -10,9 +10,9 @@ use English;
my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n"); my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n");
my $event = $ARGV [0]; my $event = $ARGV[0];
defined $ARGV[1] || die "No host specied"; defined $ARGV[1] || die "No host specified";
my $host = $ARGV[1]; my $host = $ARGV[1];
reboot_host($host); reboot_host($host);
@ -26,7 +26,7 @@ sub reboot_host
# Reboot the host # Reboot the host
#------------------------------------------------------------ #------------------------------------------------------------
my $h = $db_hosts->get($hostName) or die "No host record for user $hostName"; my $h = $db_hosts->get($hostName) or die "No host record for $hostName";
system("/usr/bin/sudo -u amgmt /usr/bin/ssh -o StrictHostKeyChecking=no amgmt\@$hostName /usr/bin/sudo /sbin/reboot") == 0 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"; or die "Error rebooting $hostName";

View File

@ -10,9 +10,9 @@ use English;
my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n"); my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n");
my $event = $ARGV [0]; my $event = $ARGV[0];
defined $ARGV[1] || die "No host specied"; defined $ARGV[1] || die "No host specified";
my $host = $ARGV[1]; my $host = $ARGV[1];
shutdown_host($host); shutdown_host($host);
@ -26,7 +26,7 @@ sub shutdown_host
# Shutdown the host # Shutdown the host
#------------------------------------------------------------ #------------------------------------------------------------
my $h = $db_hosts->get($hostName) or die "No host record for user $hostName"; my $h = $db_hosts->get($hostName) or die "No host record for $hostName";
system("/usr/bin/sudo -u amgmt /usr/bin/ssh -o StrictHostKeyChecking=no amgmt\@$hostName /usr/bin/sudo /sbin/poweroff") == 0 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"; or die "Error shutting down $hostName";

View File

@ -10,9 +10,9 @@ use English;
my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n"); my $db_hosts = esmith::DB::db->open('hosts') || die("Could not open e-smith db (" . esmith::DB::db->error . ")\n");
my $event = $ARGV [0]; my $event = $ARGV[0];
defined $ARGV[1] || die "No host specied"; defined $ARGV[1] || die "No host specified";
my $host = $ARGV[1]; my $host = $ARGV[1];
wake_host($host); wake_host($host);
@ -26,10 +26,10 @@ sub wake_host
# Wake up the host # Wake up the host
#------------------------------------------------------------ #------------------------------------------------------------
my $h = $db_hosts->get($hostName) or die "No host record for user $hostName"; my @h = $db_hosts->get($hostName) or die "No host record for $hostName";
my $client = $h[0]; my $client = $h[0];
my $mac = $client->prop('MacAddress'); my $mac = $client->prop('MacAddress');
system("/sbin/ether-wake -i lan0 $mac") == 0 system("/sbin/ether-wake -b -i lan0 $mac") == 0
or die "Error waking up $hostName"; or die "Error waking up $hostName";
} }

View File

@ -14,7 +14,7 @@ my @items = $db_hosts->get_all('local');
foreach my $item (@items) { foreach my $item (@items) {
my $hostName = $item->key; my $hostName = $item->key;
my $mac = $item->prop('MacAddress'); my $mac = $item->prop('MacAddress');
system("/sbin/ether-wake -i lan0 $mac &") == 0 system("/sbin/ether-wake -b -i lan0 $mac &") == 0
or die "Error waking up $hostName"; or die "Error waking up $hostName";
} }

View File

@ -53,7 +53,8 @@ function getClient($mac=false) {
global $hosts_db; global $hosts_db;
if (!$mac) { if (!$mac) {
$mac = getClientMac(); $mac = getClientMac();
if (!$mac) return false; if (!$mac)
return false;
} }
$hosts = $hosts_db->getAllByProp('MacAddress',$mac); $hosts = $hosts_db->getAllByProp('MacAddress',$mac);
if (count($hosts)>0) { if (count($hosts)>0) {
@ -68,7 +69,8 @@ function getClientMac($ip=false) {
$ip = $_SERVER['REMOTE_ADDR']; $ip = $_SERVER['REMOTE_ADDR'];
} }
$result = pingHost($ip,1); $result = pingHost($ip,1);
if (!$result) return false; if (!$result)
return false;
$macAddr=false; $macAddr=false;
$arptable=`arp -n $ip`; $arptable=`arp -n $ip`;
$lines=explode("\n", $arptable); $lines=explode("\n", $arptable);
@ -82,11 +84,10 @@ function getClientMac($ip=false) {
} }
function newHostname($mac, $base, $role) { function newHostname($mac, $base, $role) {
global $config_db; global $config_db, $hosts_db;
global $hosts_db;
if(!preg_match('/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/', $mac)) { if(!preg_match('/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/', $mac)) {
echo "invalid mac address"; echo "invalid mac address";
return; return false;
} }
if ($client=getClient($mac)) { if ($client=getClient($mac)) {
@ -122,8 +123,6 @@ function pingHost($host, $timeout=1) {
return (0 == $retvar); return (0 == $retvar);
} }
function ROUTE_domain() { function ROUTE_domain() {
global $config_db; global $config_db;
$theFile = $config_db->getProp(CONFIG_KEY,'DomainFile'); $theFile = $config_db->getProp(CONFIG_KEY,'DomainFile');
@ -139,6 +138,7 @@ function ROUTE_mgmtkey() {
function ROUTE_epoptes_srv() { function ROUTE_epoptes_srv() {
global $hosts_db; global $hosts_db;
$hosts = $hosts_db->getAllByProp('Role','docenti'); $hosts = $hosts_db->getAllByProp('Role','docenti');
ksort($hosts);
if (count($hosts) == 0) { if (count($hosts) == 0) {
echo 'none'; echo 'none';
} else { } else {
@ -160,8 +160,7 @@ function ROUTE_ansible_host() {
} }
function ROUTE_hosts() { function ROUTE_hosts() {
global $hosts_db; global $hosts_db, $get_params;
global $get_params;
if (isset($get_params['role'])) { if (isset($get_params['role'])) {
$items = $hosts_db->getAllByProp('Role', $get_params['role']); $items = $hosts_db->getAllByProp('Role', $get_params['role']);
} else { } else {
@ -204,10 +203,12 @@ function ROUTE_vaultpass() {
exec('/usr/bin/sudo /bin/cat ' . $theFile, $oArr, $exitCode); exec('/usr/bin/sudo /bin/cat ' . $theFile, $oArr, $exitCode);
$content = trim(implode("\n", $oArr)); $content = trim(implode("\n", $oArr));
if (false === $content) { if (false === $content) {
echo 'no file'; return; echo 'no file';
return false;
} }
if (!$client = getClient()) { if (!$client = getClient()) {
echo 'no client'; return; echo 'no client';
return false;
} }
$key = md5($client['name'], true); $key = md5($client['name'], true);
$padlenght = 16 - (strlen($content) % 16); $padlenght = 16 - (strlen($content) % 16);
@ -217,26 +218,33 @@ function ROUTE_vaultpass() {
} }
function ROUTE_whatsmyhostname() { function ROUTE_whatsmyhostname() {
global $config_db; global $config_db, $get_params;
if (isset($get_params['role'])) {
$role = $get_params['role'];
} else {
$role = $config_db->getProp(CONFIG_KEY,'DefaultRole');
}
if (isset($get_params['base'])) {
$base = $get_params['base'];
} else {
$base = $config_db->getProp(CONFIG_KEY,'NamingBase');
}
$mac = getClientMac(); $mac = getClientMac();
if (!$mac) { if (!$mac) {
echo 'no-mac-address'; echo 'no-mac-address';
return false; return false;
} }
$base = $config_db->getProp(CONFIG_KEY,'NamingBase');
$role = $config_db->getProp(CONFIG_KEY,'DefaultRole');
newHostname($mac, $base, $role); newHostname($mac, $base, $role);
} }
function ROUTE_mac2hostname() { function ROUTE_mac2hostname() {
global $config_db; global $config_db, $get_params;
global $get_params; if (isset($get_params['mac'])) {
$mac = getClientMac(); $mac = $get_params['mac'];
if (!$mac) { } else {
echo 'Usage: GET /mac2hostname?mac=XX_XX_XX_XX_XX_XX[&base=YYY][&role=ZZZ]'; echo 'Usage: GET /mac2hostname?mac=XX_XX_XX_XX_XX_XX[&base=YYY][&role=ZZZ]';
return; return false;
} }
if (isset($get_params['role'])) { if (isset($get_params['role'])) {
$role = $get_params['role']; $role = $get_params['role'];
} else { } else {