webcron/webcron.php

140 lines
5.4 KiB
PHP
Raw Normal View History

2017-04-14 15:57:47 +02:00
<?php
require_once "include/initialize.inc.php";
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');
}
2020-10-05 22:08:52 +02:00
if(file_exists('/tmp/webcron.lock')) unlink('/tmp/webcron.lock');
2018-09-21 15:13:33 +02:00
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
*/
2020-10-05 22:08:52 +02:00
if (file_exists(__DIR__ . "/cache/get-services.trigger")) {
if (file_exists(__DIR__ . "/cache/reboot-time.trigger") && file_get_contents(__DIR__ . "/cache/reboot-time.trigger") < time()) {
$rebootjobs = json_decode(file_get_contents(__DIR__ . "/cache/get-services.trigger"), true);
2017-08-21 11:11:24 +02:00
foreach($rebootjobs as $job) {
2018-09-05 15:04:24 +02:00
$services = array();
$rebooter = preg_replace("/reboot /", "", $job['url'], 1);
$rebooter = urlencode($rebooter);
$rebooter = str_replace("cmd%3D", "cmd=", $rebooter);
$rebooter = str_replace("services%3D", "services=", $rebooter);
$rebooter = str_replace("%26", "&", $rebooter);
parse_str($rebooter, $rebootcommands);
2019-04-25 16:08:46 +02:00
$cmd = $rebootcommands['services'];
if ($cmd == '') {
$cmd = "sudo systemctl list-units | cat";
}
$url = "ssh " . $job['host'] . " '" . $cmd . "' 2>&1";
2018-09-05 15:04:24 +02:00
exec($url, $services);
2019-04-25 16:08:46 +02:00
$cmd = '';
2018-09-05 15:04:24 +02:00
$services = implode("\n", $services);
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()));
}
2020-10-05 22:08:52 +02:00
unlink(__DIR__ . "/cache/get-services.trigger");
unlink(__DIR__ . "/cache/reboot-time.trigger");
2017-05-09 20:30:41 +02:00
}
}
2017-04-14 15:57:47 +02:00
2019-05-24 18:41:28 +02:00
$stmt = $db->prepare('SELECT * FROM jobs WHERE nextrun <= ? and (nextrun <= lastrun OR lastrun = -1)');
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();
2020-10-05 22:08:52 +02:00
if (file_exists(__DIR__ . "/cache/get-services.trigger")) {
$rebootjobs = json_decode(file_get_contents(__DIR__ . "/cache/get-services.trigger"), true);
}
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 {
2020-10-05 22:08:52 +02:00
if(strpos($result["url"],"reboot") !== 0) {
$nosave = false;
2017-05-09 20:30:41 +02:00
$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 {
2020-10-05 22:08:52 +02:00
$rebootjobs = array();
if (file_exists(__DIR__ . '/cache/get-services.trigger')) {
$rebootjobs = json_decode(file_get_contents(__DIR__ . '/cache/get-services.trigger'), true);
2017-08-21 11:11:24 +02:00
}
if (!job_in_array($result['jobID'], $rebootjobs)) {
2020-10-05 22:08:52 +02:00
echo "no hope";
2017-08-21 11:11:24 +02:00
$rebootjobs[] = $result;
2020-10-05 22:08:52 +02:00
$rebootser = json_encode($rebootjobs);
file_put_contents(__DIR__ . "/cache/get-services.trigger", $rebootser);
touch(__DIR__ . "/cache/reboot.trigger");
$nosave = true;
}
2017-08-21 11:11:24 +02:00
2017-05-09 20:30:41 +02:00
}
}
2020-10-05 22:08:52 +02:00
if(!$nosave) {
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
2020-10-05 22:08:52 +02:00
if(file_exists(__DIR__ . "/cache/reboot.trigger")) {
unlink(__DIR__ . "/cache/reboot.trigger");
$count=0;
2017-08-21 11:11:24 +02:00
foreach($rebootjobs as $job) {
2020-10-05 22:08:52 +02:00
print_r($job);
if (!(isset($job['done']) && $job['done'] == true)) {
$rebooter = preg_replace("/reboot /", "", $job['url'], 1);
$rebooter = urlencode($rebooter);
$rebooter = str_replace("cmd%3D", "cmd=", $rebooter);
$rebooter = str_replace("services%3D", "services=", $rebooter);
$rebooter = str_replace("%26", "&", $rebooter);
parse_str($rebooter, $rebootcommands);
$cmd = $rebootcommands['cmd'];
2019-04-26 17:01:12 +02:00
if ($cmd == '') {
2019-04-27 13:37:16 +02:00
$cmd = 'sudo shutdown -r +{m}+ "A reboot has been scheduled. Please save your work."';
}
2019-04-27 13:37:16 +02:00
$cmd = str_replace("{m}+", intdiv(get_configvalue('jobs.rebootwait'), 60), $cmd);
$cmd = str_replace("{s}+", get_configvalue('jobs.rebootwait'), $cmd);
$url = "ssh " . $job['host'] . " '" . $cmd . " &'";
2020-10-05 22:08:52 +02:00
echo $url;
exec($url);
$cmd = '';
$rebootjobs[$count]['done'] = true;
}
$count++;
2017-08-21 11:11:24 +02:00
}
2020-10-05 22:08:52 +02:00
$rebootser = json_encode($rebootjobs);
file_put_contents(__DIR__ . "/cache/get-services.trigger", $rebootser);
file_put_contents(__DIR__ . "/cache/reboot-time.trigger", time() + (get_configvalue('jobs.reboottime') + get_configvalue('jobs.rebootwait')));
2017-05-09 20:30:41 +02:00
}
2017-04-14 15:57:47 +02:00
require_once 'include/finalize.inc.php';