2017-04-14 15:57:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The MIT License
|
|
|
|
*
|
|
|
|
* Copyright 2017 Jeroen De Meerleer <me@jeroened.be>.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once "include/initialize.inc.php";
|
|
|
|
|
2018-09-21 15:59:55 +02:00
|
|
|
if(file_exists('/tmp/webcron.lock') && file_get_contents('/tmp/webcron.lock') + get_configvalue('master.crashtimeout') > time() )
|
2017-04-15 10:13:22 +02:00
|
|
|
{
|
|
|
|
die('Script is already running');
|
|
|
|
}
|
2018-09-21 15:13:33 +02:00
|
|
|
unlink('/tmp/webcron.lock');
|
|
|
|
file_put_contents('/tmp/webcron.lock', time());
|
2017-04-15 10:13:22 +02:00
|
|
|
|
2017-05-09 20:30:41 +02:00
|
|
|
/**
|
|
|
|
* Reboot finalize
|
|
|
|
*/
|
|
|
|
if (file_exists("cache/get-services.trigger")) {
|
2017-05-11 10:46:55 +02:00
|
|
|
if (file_exists("cache/reboot-time.trigger") && file_get_contents("cache/reboot-time.trigger") < time()) {
|
|
|
|
$rebootjobs = unserialize(file_get_contents("cache/get-services.trigger"));
|
2017-08-21 11:11:24 +02:00
|
|
|
|
2017-05-11 10:46:55 +02:00
|
|
|
foreach($rebootjobs as $job) {
|
2018-09-05 15:04:24 +02:00
|
|
|
$services = array();
|
|
|
|
$url = "ssh " . $job['host'] . " '" . "sudo systemctl list-units | cat" . "' 2>&1";
|
|
|
|
exec($url, $services);
|
|
|
|
$services = implode("\n", $services);
|
2017-05-11 10:46:55 +02:00
|
|
|
|
2018-09-05 15:04:24 +02:00
|
|
|
$stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
|
2018-09-07 13:55:46 +02:00
|
|
|
$stmt->execute(array($job['jobID'], '0', $services, time()));
|
2017-05-11 10:46:55 +02:00
|
|
|
}
|
|
|
|
unlink("cache/get-services.trigger");
|
|
|
|
unlink("cache/reboot-time.trigger");
|
2017-05-09 20:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-14 15:57:47 +02:00
|
|
|
|
2018-09-21 13:35:47 +02:00
|
|
|
$stmt = $db->prepare('SELECT jobID, url, host, delay, nextrun, expected FROM jobs WHERE nextrun <= ?');
|
2018-09-21 15:21:58 +02:00
|
|
|
$stmt->execute(array(time()));
|
2017-05-09 20:30:41 +02:00
|
|
|
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
2017-04-14 15:57:47 +02:00
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
|
2017-05-09 20:30:41 +02:00
|
|
|
$rebootjobs = array();
|
2017-05-10 10:41:34 +02:00
|
|
|
if (file_exists("cache/get-services.trigger")) {
|
|
|
|
$rebootjobs = unserialize(file_get_contents("cache/get-services.trigger"));
|
|
|
|
}
|
|
|
|
|
2017-04-14 15:57:47 +02:00
|
|
|
foreach ($results as $result) {
|
2017-05-09 20:30:41 +02:00
|
|
|
|
2017-08-23 15:51:12 +02:00
|
|
|
if (filter_var($result["url"], FILTER_VALIDATE_URL)) {
|
2019-01-13 19:59:40 +01:00
|
|
|
$res = $client->request('GET', $result['url'], ['http_errors' => false]);
|
2017-04-14 15:57:47 +02:00
|
|
|
|
2017-05-09 20:30:41 +02:00
|
|
|
$statuscode = $res->getStatusCode();
|
|
|
|
$body = $res->getBody();
|
2017-05-10 10:08:35 +02:00
|
|
|
} else {
|
2017-05-09 20:30:41 +02:00
|
|
|
|
|
|
|
if($result["url"] != "reboot") {
|
|
|
|
$body = '';
|
2017-05-10 11:34:42 +02:00
|
|
|
$statuscode = 0;
|
2017-09-02 17:11:04 +02:00
|
|
|
$url = "ssh " . $result['host'] . " '" . $result['url'] . "' 2>&1";
|
2017-08-18 18:58:04 +02:00
|
|
|
exec($url, $body, $statuscode);
|
2017-05-10 11:34:42 +02:00
|
|
|
$body = implode("\n", $body);
|
2017-05-09 20:30:41 +02:00
|
|
|
} else {
|
2017-08-21 11:11:24 +02:00
|
|
|
$rebootjobs = array();
|
|
|
|
if (file_exists('cache/get-services.trigger')) {
|
|
|
|
$rebootjobs = unserialize(file_get_contents('cache/get-services.trigger'));
|
|
|
|
}
|
|
|
|
if (!job_in_array($result['jobID'], $rebootjobs)) {
|
|
|
|
$rebootjobs[] = $result;
|
2017-05-11 10:46:55 +02:00
|
|
|
touch("cache/reboot.trigger");
|
|
|
|
$nosave = true;
|
|
|
|
}
|
2017-08-21 11:11:24 +02:00
|
|
|
|
2017-05-09 20:30:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:06:57 +02:00
|
|
|
if($nosave !== true) {
|
2017-05-09 20:30:41 +02:00
|
|
|
$stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
|
|
|
|
$stmt->execute(array($result['jobID'], $statuscode, $body, time()));
|
|
|
|
}
|
2018-08-31 16:30:49 +02:00
|
|
|
|
|
|
|
$nextrun = $result['nextrun'];
|
|
|
|
do {
|
|
|
|
$nextrun = $nextrun + $result['delay'];
|
2018-08-31 18:20:19 +02:00
|
|
|
} while ($nextrun < time());
|
2018-05-08 20:45:40 +02:00
|
|
|
|
|
|
|
$nexttime = $db->prepare("UPDATE jobs SET nextrun = ? WHERE jobID = ?");
|
|
|
|
$nexttime->execute(array($nextrun, $result["jobID"]));
|
2017-05-09 20:30:41 +02:00
|
|
|
$nosave = false;
|
2017-04-14 15:57:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 16:31:56 +02:00
|
|
|
if ((get_configvalue('dbclean.enabled') == 'true') && (get_configvalue('dbclean.lastrun') + (60 * 60 * 24 * get_configvalue('dbclean.delay')) < time())) clean_database();
|
2018-09-13 12:49:28 +02:00
|
|
|
|
2018-07-27 10:44:58 +02:00
|
|
|
unlink('/tmp/webcron.lock');
|
2017-04-15 10:13:22 +02:00
|
|
|
|
2017-05-09 20:30:41 +02:00
|
|
|
if(file_exists("cache/reboot.trigger")) {
|
|
|
|
unlink("cache/reboot.trigger");
|
|
|
|
$rebootser = serialize($rebootjobs);
|
|
|
|
file_put_contents("cache/get-services.trigger", $rebootser);
|
2018-09-13 12:44:07 +02:00
|
|
|
file_put_contents("cache/reboot-time.trigger", time() + (get_configvalue('jobs.reboottime') * 60));
|
2017-08-21 11:11:24 +02:00
|
|
|
$rebooted_hosts = array();
|
|
|
|
foreach($rebootjobs as $job) {
|
|
|
|
|
2018-09-13 12:44:07 +02:00
|
|
|
$url = "ssh " . $job['host'] . " '" . 'sudo shutdown -r +' . get_configvalue('jobs.reboottime') . ' "A reboot has been scheduled. Please save your work."' . "'";
|
2017-08-21 11:11:24 +02:00
|
|
|
exec($url);
|
|
|
|
}
|
2017-05-09 20:30:41 +02:00
|
|
|
}
|
2017-04-14 15:57:47 +02:00
|
|
|
require_once 'include/finalize.inc.php';
|