From b28ca9238ea33bb77df6e58c6db1411bfdc6917e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 27 Aug 2023 01:13:47 +0200 Subject: [PATCH] Fix selftest when run in docker --- Dockerfile | 10 ++++++--- Makefile | 17 +++++++++------ www/commands/progs_updatecheck.php | 2 +- www/internals/modules/selftest.php | 24 +++++++++++++++------ www/internals/website.php | 34 ++++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index dc98f55..cde8bc7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,10 +11,13 @@ RUN docker-php-ext-install pdo pdo_mysql bcmath COPY .docker /_docker -RUN chmod +Xx /_docker/run.sh && \ +RUN chmod +Xx /_docker/run.sh && \ chmod +Xx /_docker/init.sh -COPY ./www /var/www/html +COPY ./www /var/www/html +COPY ./DOCKER_GIT_INFO /DOCKER_GIT_INFO + +RUN chmod a+r /DOCKER_GIT_INFO # MapVolumes for: /var/www/html/config.php [ro] # MapVolumes for: /var/www/html/dynamic [rw] @@ -23,5 +26,6 @@ COPY ./www /var/www/html EXPOSE 80 -CMD ["/_docker/run.sh"] +ENTRYPOINT ["/_docker/run.sh"] +CMD [] diff --git a/Makefile b/Makefile index 373e92a..01c074a 100644 --- a/Makefile +++ b/Makefile @@ -9,12 +9,15 @@ HASH=$(shell git rev-parse HEAD) run: php -S localhost:8000 -t . -docker: - echo -n "VCSTYPE=" >> DOCKER_GIT_INFO ; echo "git" >> DOCKER_GIT_INFO - echo -n "BRANCH=" >> DOCKER_GIT_INFO ; git rev-parse --abbrev-ref HEAD >> DOCKER_GIT_INFO - echo -n "HASH=" >> DOCKER_GIT_INFO ; git rev-parse HEAD >> DOCKER_GIT_INFO - echo -n "COMMITTIME=" >> DOCKER_GIT_INFO ; git log -1 --format=%cd --date=iso >> DOCKER_GIT_INFO - echo -n "REMOTE=" >> DOCKER_GIT_INFO ; git remote -v | awk '{print $2}' | uniq | tr '\n' ';' >> DOCKER_GIT_INFO +dgi: + [ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO + echo -n "VCSTYPE=" >> DOCKER_GIT_INFO ; { echo "git" ; } >> DOCKER_GIT_INFO + echo -n "BRANCH=" >> DOCKER_GIT_INFO ; { git rev-parse --abbrev-ref HEAD ; } >> DOCKER_GIT_INFO + echo -n "HASH=" >> DOCKER_GIT_INFO ; { git rev-parse HEAD ; } >> DOCKER_GIT_INFO + echo -n "COMMITTIME=" >> DOCKER_GIT_INFO ; { git log -1 --format=%cd --date=iso ; } >> DOCKER_GIT_INFO + echo -n "REMOTE=" >> DOCKER_GIT_INFO ; { git remote -v | awk '{print $$2}' | uniq | tr '\n' ';'; } >> DOCKER_GIT_INFO + +docker: dgi docker build \ -t $(DOCKER_NAME):$(HASH) \ -t $(DOCKER_NAME):$(NAMESPACE)-latest \ @@ -27,6 +30,7 @@ docker: run-docker: docker mkdir -p ".run-data" docker run --rm \ + -it \ --init \ --publish 8080:80 \ --env "SMTP=0" \ @@ -37,6 +41,7 @@ run-docker: docker run-docker-live: docker mkdir -p "$(shell pwd)/.run-data" docker run --rm \ + -it \ --init \ --publish 8080:80 \ --volume "$(shell pwd)/www:/var/www/html/" \ diff --git a/www/commands/progs_updatecheck.php b/www/commands/progs_updatecheck.php index 602549e..2b883b2 100644 --- a/www/commands/progs_updatecheck.php +++ b/www/commands/progs_updatecheck.php @@ -8,7 +8,7 @@ require_once (__DIR__ . '/../internals/website.php'); if (!isset($API_OPTIONS['name'])) { $FRAME_OPTIONS->forceResult(400, "Wrong parameters."); return; } -$name = $API_OPTIONS['name']; +$name = urldecode($API_OPTIONS['name']); $updatedata = $SITE->modules->UpdatesLog()->listUpdateData(); diff --git a/www/internals/modules/selftest.php b/www/internals/modules/selftest.php index 9de38a5..e175bdf 100644 --- a/www/internals/modules/selftest.php +++ b/www/internals/modules/selftest.php @@ -532,25 +532,34 @@ class SelfTest implements IWebsiteModule 'exception' => null, ]; - $r = shell_exec('git status 2>&1'); - $ok = ($r !== null) && (strpos($r, 'Your branch is up to date with') !== false) && (strpos($r, 'nothing to commit, working tree clean') !== false); + $r = Website::inst()->gitStatus(); - if (!$ok) + if (!$r) { return [ 'result' => self::STATUS_ERROR, - 'message' => "{$xname} failed", + 'message' => "{$xname} failed (command error)", 'long' => $r, 'exception' => null, ]; } - else + else if (!$r[2]) + { + return + [ + 'result' => self::STATUS_ERROR, + 'message' => "{$xname} failed (git repo not clean)", + 'long' => $r, + 'exception' => null, + ]; + } + else { return [ 'result' => self::STATUS_OK, - 'message' => "{".$xname."} succeeded", + 'message' => "{".$xname."} succeeded ('$r[0]' | '$r[1]')", 'long' => $r, 'exception' => null, ]; @@ -599,7 +608,8 @@ class SelfTest implements IWebsiteModule $url = $urlobj; if (is_array($urlobj)) $url = $urlobj['url']; - if ($url === 'direct') continue; + if ($url === 'direct') continue; + if ($urlname === 'homebrew-tap') continue; $r = curl_http_request($url); $count++; diff --git a/www/internals/website.php b/www/internals/website.php index 20b3a42..7599717 100644 --- a/www/internals/website.php +++ b/www/internals/website.php @@ -161,6 +161,40 @@ class Website return $this->config['prod']; } + public function gitStatus(): array|false { + if (file_exists('/DOCKER_GIT_INFO')) + { + $dgi = preg_split("/\r\n|\n|\r/", file_get_contents('/DOCKER_GIT_INFO')); + $branch = ''; + $sha = ''; + foreach ($dgi as $line) + { + $split = explode('=', $line, 2); + if (count($split) !== 2) continue; + + if ($split[0] === 'BRANCH') { $branch = $split[1]; continue; } + if ($split[0] === 'HASH') { $sha = $split[1]; continue; } + } + + if ($branch !== '' && $sha !== '') return [$branch, $sha, true]; + return false; + } + else + { + $status = shell_exec('git status 2>&1'); + $branch = shell_exec('git rev-parse --abbrev-ref HEAD'); + $sha = shell_exec('git rev-parse HEAD'); + + if ($status === false || $status === null || $status === '') return false; + if ($branch === false || $branch === null || $branch === '') return false; + if ($sha === false || $sha === null || $sha === '') return false; + + $clean = (str_contains($status, 'Your branch is up to date with')) && (str_contains($status, 'nothing to commit, working tree clean')); + + return [$branch, $sha, $clean]; + } + } + public function isLoggedInByCookie() { if ($this->isLoggedIn !== null) return $this->isLoggedIn;