webcron.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $rebooter = preg_replace("/reboot /", "", $job['url'], 1);
  41. $rebooter = urlencode($rebooter);
  42. $rebooter = str_replace("cmd%3D", "cmd=", $rebooter);
  43. $rebooter = str_replace("services%3D", "services=", $rebooter);
  44. $rebooter = str_replace("%26", "&", $rebooter);
  45. parse_str($rebooter, $rebootcommands);
  46. $cmd = $rebootcommands['services'];
  47. if ($cmd == '') {
  48. $cmd = "sudo systemctl list-units | cat";
  49. }
  50. $url = "ssh " . $job['host'] . " '" . $cmd . "' 2>&1";
  51. exec($url, $services);
  52. $cmd = '';
  53. $services = implode("\n", $services);
  54. $stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
  55. $stmt->execute(array($job['jobID'], '0', $services, time()));
  56. }
  57. unlink("cache/get-services.trigger");
  58. unlink("cache/reboot-time.trigger");
  59. }
  60. }
  61. $stmt = $db->prepare('SELECT * FROM jobs WHERE nextrun <= ? and (nextrun <= lastrun OR lastrun = -1)');
  62. $stmt->execute(array(time()));
  63. $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
  64. $client = new \GuzzleHttp\Client();
  65. $rebootjobs = array();
  66. if (file_exists("cache/get-services.trigger")) {
  67. $rebootjobs = unserialize(file_get_contents("cache/get-services.trigger"));
  68. }
  69. foreach ($results as $result) {
  70. if (filter_var($result["url"], FILTER_VALIDATE_URL)) {
  71. $res = $client->request('GET', $result['url'], ['http_errors' => false]);
  72. $statuscode = $res->getStatusCode();
  73. $body = $res->getBody();
  74. } else {
  75. if(strpos($result["url"],"reboot") !== 0) {
  76. $body = '';
  77. $statuscode = 0;
  78. $url = "ssh " . $result['host'] . " '" . $result['url'] . "' 2>&1";
  79. exec($url, $body, $statuscode);
  80. $body = implode("\n", $body);
  81. } else {
  82. $rebootjobs = array();
  83. if (file_exists('cache/get-services.trigger')) {
  84. $rebootjobs = unserialize(file_get_contents('cache/get-services.trigger'));
  85. }
  86. if (!job_in_array($result['jobID'], $rebootjobs)) {
  87. $rebootjobs[] = $result;
  88. $rebootser = serialize($rebootjobs);
  89. file_put_contents("cache/get-services.trigger", $rebootser);
  90. touch("cache/reboot.trigger");
  91. $nosave = true;
  92. }
  93. }
  94. }
  95. if($nosave !== true) {
  96. $stmt = $db->prepare("INSERT INTO runs(job, statuscode, result, timestamp) VALUES(?, ?, ?, ?)");
  97. $stmt->execute(array($result['jobID'], $statuscode, $body, time()));
  98. }
  99. $nextrun = $result['nextrun'];
  100. do {
  101. $nextrun = $nextrun + $result['delay'];
  102. } while ($nextrun < time());
  103. $nexttime = $db->prepare("UPDATE jobs SET nextrun = ? WHERE jobID = ?");
  104. $nexttime->execute(array($nextrun, $result["jobID"]));
  105. $nosave = false;
  106. }
  107. if ((get_configvalue('dbclean.enabled') == 'true') && (get_configvalue('dbclean.lastrun') + (60 * 60 * 24 * get_configvalue('dbclean.delay')) < time())) clean_database();
  108. unlink('/tmp/webcron.lock');
  109. if(file_exists("cache/reboot.trigger")) {
  110. unlink("cache/reboot.trigger");
  111. $count=0;
  112. foreach($rebootjobs as $job) {
  113. if (!(isset($job['done']) && $job['done'] == true)) {
  114. $rebooter = preg_replace("/reboot /", "", $job['url'], 1);
  115. $rebooter = urlencode($rebooter);
  116. $rebooter = str_replace("cmd%3D", "cmd=", $rebooter);
  117. $rebooter = str_replace("services%3D", "services=", $rebooter);
  118. $rebooter = str_replace("%26", "&", $rebooter);
  119. parse_str($rebooter, $rebootcommands);
  120. $cmd = $rebootcommands['cmd'];
  121. if ($cmd == '') {
  122. $cmd = 'sudo shutdown -r +{m}+ "A reboot has been scheduled. Please save your work."';
  123. }
  124. $cmd = str_replace("{m}+", intdiv(get_configvalue('jobs.rebootwait'), 60), $cmd);
  125. $cmd = str_replace("{s}+", get_configvalue('jobs.rebootwait'), $cmd);
  126. $url = "ssh " . $job['host'] . " '" . $cmd . " &'";
  127. exec($url);
  128. $cmd = '';
  129. $rebootjobs[$count]['done'] = true;
  130. }
  131. $count++;
  132. }
  133. $rebootser = serialize($rebootjobs);
  134. file_put_contents("cache/get-services.trigger", $rebootser);
  135. file_put_contents("cache/reboot-time.trigger", time() + (get_configvalue('jobs.reboottime') + get_configvalue('jobs.rebootwait')));
  136. }
  137. require_once 'include/finalize.inc.php';