webcron.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * The MIT License
  4. *
  5. * Copyright 2017 Jeroen De Meerleer <me@jeroened.be>.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. require_once "include/initialize.inc.php";
  26. if(file_exists('/tmp/webcron.lock') && file_get_contents('/tmp/webcron.lock') + get_configvalue('master.crashtimeout') > time() )
  27. {
  28. die('Script is already running');
  29. }
  30. unlink('/tmp/webcron.lock');
  31. file_put_contents('/tmp/webcron.lock', time());
  32. /**
  33. * Reboot finalize
  34. */
  35. if (file_exists("cache/get-services.trigger")) {
  36. if (file_exists("cache/reboot-time.trigger") && file_get_contents("cache/reboot-time.trigger") < time()) {
  37. $rebootjobs = unserialize(file_get_contents("cache/get-services.trigger"));
  38. foreach($rebootjobs as $job) {
  39. $services = array();
  40. $url = "ssh " . $job['host'] . " '" . "sudo systemctl list-units | cat" . "' 2>&1";
  41. exec($url, $services);
  42. $services = implode("\n", $services);
  43. $stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
  44. $stmt->execute(array($job['jobID'], '0', $services, time()));
  45. }
  46. unlink("cache/get-services.trigger");
  47. unlink("cache/reboot-time.trigger");
  48. }
  49. }
  50. $stmt = $db->prepare('SELECT jobID, url, host, delay, nextrun, expected FROM jobs WHERE nextrun <= ?');
  51. $stmt->execute(array(time()));
  52. $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
  53. $client = new \GuzzleHttp\Client();
  54. $rebootjobs = array();
  55. if (file_exists("cache/get-services.trigger")) {
  56. $rebootjobs = unserialize(file_get_contents("cache/get-services.trigger"));
  57. }
  58. foreach ($results as $result) {
  59. if (filter_var($result["url"], FILTER_VALIDATE_URL)) {
  60. $res = $client->request('GET', $result['url'], ['http_errors' => false]);
  61. $statuscode = $res->getStatusCode();
  62. $body = $res->getBody();
  63. } else {
  64. if($result["url"] != "reboot") {
  65. $body = '';
  66. $statuscode = 0;
  67. $url = "ssh " . $result['host'] . " '" . $result['url'] . "' 2>&1";
  68. exec($url, $body, $statuscode);
  69. $body = implode("\n", $body);
  70. } else {
  71. $rebootjobs = array();
  72. if (file_exists('cache/get-services.trigger')) {
  73. $rebootjobs = unserialize(file_get_contents('cache/get-services.trigger'));
  74. }
  75. if (!job_in_array($result['jobID'], $rebootjobs)) {
  76. $rebootjobs[] = $result;
  77. touch("cache/reboot.trigger");
  78. $nosave = true;
  79. }
  80. }
  81. }
  82. if($nosave !== true) {
  83. $stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
  84. $stmt->execute(array($result['jobID'], $statuscode, $body, time()));
  85. }
  86. $nextrun = $result['nextrun'];
  87. do {
  88. $nextrun = $nextrun + $result['delay'];
  89. } while ($nextrun < time());
  90. $nexttime = $db->prepare("UPDATE jobs SET nextrun = ? WHERE jobID = ?");
  91. $nexttime->execute(array($nextrun, $result["jobID"]));
  92. $nosave = false;
  93. }
  94. if ((get_configvalue('dbclean.enabled') == 'true') && (get_configvalue('dbclean.lastrun') + (60 * 60 * 24 * get_configvalue('dbclean.delay')) < time())) clean_database();
  95. unlink('/tmp/webcron.lock');
  96. if(file_exists("cache/reboot.trigger")) {
  97. unlink("cache/reboot.trigger");
  98. $rebootser = serialize($rebootjobs);
  99. file_put_contents("cache/get-services.trigger", $rebootser);
  100. file_put_contents("cache/reboot-time.trigger", time() + (get_configvalue('jobs.reboottime') * 60));
  101. $rebooted_hosts = array();
  102. foreach($rebootjobs as $job) {
  103. $url = "ssh " . $job['host'] . " '" . 'sudo shutdown -r +' . get_configvalue('jobs.reboottime') . ' "A reboot has been scheduled. Please save your work."' . "'";
  104. exec($url);
  105. }
  106. }
  107. require_once 'include/finalize.inc.php';