/dev/null 2>&1', $out, $ret); return $ret === 0; } function read_tail($file, $maxBytes = 200000) { if (!file_exists($file)) { return ''; } $size = filesize($file); if ($size === false || $size <= $maxBytes) { return file_get_contents($file); } $fp = fopen($file, 'rb'); if (!$fp) { return ''; } fseek($fp, -$maxBytes, SEEK_END); $data = fread($fp, $maxBytes); fclose($fp); return $data === false ? '' : $data; } if (isset($_GET['ajax'])) { header('Content-Type: application/json; charset=utf-8'); $action = isset($_GET['action']) ? $_GET['action'] : ''; if ($action === 'start') { $running = false; if (file_exists($pid_file)) { $pid_now = trim(file_get_contents($pid_file)); $running = is_process_running($pid_now); } if (!$running) { @unlink($code_file); @unlink($log_file); $script = 'sudo git -C ' . escapeshellarg($src_path) . ' pull > ' . escapeshellarg($log_file) . ' 2>&1; echo $? > ' . escapeshellarg($code_file); $cmd = 'nohup sh -c ' . escapeshellarg($script) . ' >/dev/null 2>&1 & echo $!'; $out = []; $ret = 1; exec($cmd, $out, $ret); $pid = isset($out[0]) ? trim($out[0]) : ''; if ($pid !== '') { file_put_contents($pid_file, $pid); } } echo json_encode(['ok' => true]); exit; } if ($action === 'status') { $pid = file_exists($pid_file) ? trim(file_get_contents($pid_file)) : ''; $running = is_process_running($pid); $done = file_exists($code_file); $exit_code = null; if ($done) { $exit_code = intval(trim(file_get_contents($code_file))); } $log = read_tail($log_file); echo json_encode([ 'ok' => true, 'running' => $running, 'done' => $done, 'exit_code' => $exit_code, 'log' => $log ]); exit; } echo json_encode(['ok' => false, 'msg' => 'unknown action']); exit; } $show_title = "Git Pull - " . $OJ_NAME; require("template/".$OJ_TEMPLATE."/header.php"); ?>
echo "www-data ALL=(ALL) NOPASSWD: /usr/bin/git" | sudo tee /etc/sudoers.d/www-data-gitsudo chmod 440 /etc/sudoers.d/www-data-git