From c580fe1ccbf959f11da7df1e5a011e7a7f6d387b Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 21 Aug 2017 11:11:24 +0200 Subject: [PATCH] Added remote reboot jobs --- include/finalize.inc.php | 8 ++++++++ runnow.php | 15 +++++++++++---- webcron.php | 36 +++++++++++++++++++++++------------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/include/finalize.inc.php b/include/finalize.inc.php index 5533b61..e40a8d5 100644 --- a/include/finalize.inc.php +++ b/include/finalize.inc.php @@ -23,3 +23,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ + +function job_in_array($id, $jobs) { + foreach ($jobs as $job) { + if ($job['jobID'] == $id) return true; + } + + return false; +} \ No newline at end of file diff --git a/runnow.php b/runnow.php index 89e53c8..121f77a 100644 --- a/runnow.php +++ b/runnow.php @@ -53,13 +53,20 @@ if (filter_var($jobnameResult[0]["url"], FILTER_VALIDATE_URL)) { if($jobnameResult[0]["url"] != "reboot") { $body = ''; $statuscode = 0; - exec($jobnameResult[0]["url"] . " 2>&1", $body, $statuscode); + $url = "ssh " . $jobnameResult[0]['host'] . " '" . $jobnameResult[0]['url'] . "'"; + exec($url, $body, $statuscode); $body = implode("\n", $body); $timestamp = time(); } else { - $rebootjobs[] = $jobnameResult[0]['jobID']; - touch("cache/reboot.trigger"); - $nosave = true; + $rebootjobs = array(); + if (file_exists('cache/get-services.trigger')) { + $rebootjobs = unserialize(file_get_contents('cache/get-services.trigger')); + } + if (!job_in_array($jobnameResult[0]['jobID'], $rebootjobs)) { + $rebootjobs[] = $jobnameResult[0]; + touch("cache/reboot.trigger"); + $nosave = true; + } } } if($nosave !== true) { diff --git a/webcron.php b/webcron.php index 70c8082..f69ec3a 100644 --- a/webcron.php +++ b/webcron.php @@ -38,19 +38,19 @@ touch('cache/webcron.lock'); if (file_exists("cache/get-services.trigger")) { if (file_exists("cache/reboot-time.trigger") && file_get_contents("cache/reboot-time.trigger") < time()) { $rebootjobs = unserialize(file_get_contents("cache/get-services.trigger")); - $services = array(); - exec("sudo systemctl list-units | cat", $services); - $services = implode("\n", $services); - + foreach($rebootjobs as $job) { - $stmt = $db->query("SELECT jobID, delay, nextrun FROM jobs WHERE jobID = " . $job); - $result = $stmt->fetchAll(PDO::FETCH_ASSOC)[0]; - + + $services = array(); + $url = "ssh " . $job['host'] . " '" . "sudo systemctl list-units | cat" . "'"; + exec($url, $services); + $services = implode("\n", $services); + $stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)"); - $stmt->execute(array($result['jobID'], '200', $services, time())); + $stmt->execute(array($job['jobID'], '200', $services, time())); - $nextrun = ($result['nextrun'] < time()) ? $result['nextrun'] + $result['delay'] : $result['nextrun']; - if ($nextrun < time() ) { $nextrun = time() + $result['delay']; } + $nextrun = ($job['nextrun'] < time()) ? $job['nextrun'] + $job['delay'] : $job['nextrun']; + if ($nextrun < time() ) { $nextrun = time() + $job['delay']; } $nexttime = $db->prepare("UPDATE jobs SET nextrun = ? WHERE jobID = ?"); $nexttime->execute(array($nextrun, $result["jobID"])); @@ -85,11 +85,16 @@ foreach ($results as $result) { exec($url, $body, $statuscode); $body = implode("\n", $body); } else { - if (!file_exists('cache/get-services.trigger')) { - $rebootjobs[] = $result['jobID']; + $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; touch("cache/reboot.trigger"); $nosave = true; } + } } if($nosave !== true) { @@ -112,6 +117,11 @@ if(file_exists("cache/reboot.trigger")) { $rebootser = serialize($rebootjobs); file_put_contents("cache/get-services.trigger", $rebootser); file_put_contents("cache/reboot-time.trigger", time() + (5 * 60)); - exec('sudo shutdown -r +5 "A reboot has been scheduled. Please save your work."'); + $rebooted_hosts = array(); + foreach($rebootjobs as $job) { + + $url = "ssh " . $job['host'] . " '" . 'sudo shutdown -r +5 "A reboot has been scheduled. Please save your work."' . "'"; + exec($url); + } } require_once 'include/finalize.inc.php';