searching for " negative one"

Overnumerousness :)
This commit is contained in:
Paolo Asperti 2016-07-23 12:48:58 +02:00
parent b1502803ff
commit eee2c83d90

View File

@ -79,6 +79,42 @@ function getClientMac($ip=false) {
} }
} }
function newHostname($mac, $base, $role) {
global $config_db;
global $hosts_db;
if(!preg_match('/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/', $mac)) {
echo "invalid mac address";
return;
}
if ($client=getClient($mac)) {
echo $client['name'];
return;
}
$domainName = (string)$config_db->getKeyValue('DomainName');
$digits = $config_db->getProp(CONFIG_KEY,'NamingDigits');
$formatstring = '%s-%0'.$digits.'d';
$nr=0;
$hostname = '';
do {
$nr++;
$hostname = sprintf($formatstring, $base, $nr);
# TODO: we'll use the hostname in future, maybe
# if (trim($domainName) != '') {
# $hostname .= '.' . $domainName;
# }
} while ($hosts_db->getKey($hostname));
$hosts_db->setKey($hostname, 'remote', array(
'MacAddress' => $mac,
'Role' => $role,
'Description' => ''
));
echo $hostname;
}
function pingHost($host, $timeout=1) { function pingHost($host, $timeout=1) {
$pingresult = exec("/bin/ping -c 1 -t 2 -W $timeout $host", $output, $retvar); $pingresult = exec("/bin/ping -c 1 -t 2 -W $timeout $host", $output, $retvar);
return (0 == $retvar); return (0 == $retvar);
@ -146,7 +182,6 @@ function ROUTE_hosts() {
echo json_encode($results); echo json_encode($results);
} }
function ROUTE_ansible_list() { function ROUTE_ansible_list() {
global $get_params; global $get_params;
if (isset($get_params['role'])) { if (isset($get_params['role'])) {
@ -166,7 +201,6 @@ function ROUTE_ansible_list() {
)); ));
} }
function ROUTE_vaultpass() { function ROUTE_vaultpass() {
global $config_db; global $config_db;
$theFile = $config_db->getProp(CONFIG_KEY,'VaultPassFile'); $theFile = $config_db->getProp(CONFIG_KEY,'VaultPassFile');
@ -189,10 +223,13 @@ function ROUTE_vaultpass() {
function ROUTE_whatsmyhostname() { function ROUTE_whatsmyhostname() {
global $config_db; global $config_db;
$mac = getClientMac(); $mac = getClientMac();
if (!$mac) return false; if (!$mac) {
echo 'no-mac-address';
return false;
}
$base = $config_db->getProp(CONFIG_KEY,'NamingBase'); $base = $config_db->getProp(CONFIG_KEY,'NamingBase');
$role = $config_db->getProp(CONFIG_KEY,'DefaultRole'); $role = $config_db->getProp(CONFIG_KEY,'DefaultRole');
echo newHostname($mac, $base, $role); newHostname($mac, $base, $role);
} }
function ROUTE_mac2hostname() { function ROUTE_mac2hostname() {
@ -216,42 +253,5 @@ function ROUTE_mac2hostname() {
$base = $config_db->getProp(CONFIG_KEY,'NamingBase'); $base = $config_db->getProp(CONFIG_KEY,'NamingBase');
} }
echo newHostname($mac, $base, $role); newHostname($mac, $base, $role);
} }
function newHostname($mac, $base, $role) {
global $config_db;
global $hosts_db;
if(!preg_match('/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/', $mac)) {
echo "invalid mac address";
return;
}
if ($client=getClient($mac)) {
echo $client['name'];
return;
}
$domainName = (string)$config_db->getKeyValue('DomainName');
$digits = $config_db->getProp(CONFIG_KEY,'NamingDigits');
$formatstring = '%s-%0'.$digits.'d';
$nr=0;
$hostname = '';
do {
$nr++;
$hostname = sprintf($formatstring, $base, $nr);
# TODO: we'll use the hostname in future, maybe
# if (trim($domainName) != '') {
# $hostname .= '.' . $domainName;
# }
} while ($hosts_db->getKey($hostname));
$hosts_db->setKey($hostname, 'remote', array(
'MacAddress' => $mac,
'Role' => $role,
'Description' => ''
));
echo $hostname;
}