#!/usr/bin/env bash # Run a command against this working tree on Linux, inside the same job image # the luhpe-linux-x64 Actions runner uses, without pushing anything. # # The tree is streamed over ssh into a throwaway container started by the # runner's own Docker daemon on luhpe, so it runs under the runner's cpu and # memory limits. What is sent: every tracked file as it is on disk, plus every # UNTRACKED file git does not ignore (a stray .env or key file included - # check `git status` first). A tracked file deleted from the working tree, or # any other local tar error, fails the run before anything is judged; a # partial tree is never shipped. The Rust toolchain and the cargo target # directory are kept in a Docker volume between runs, so only the first run # is a cold build. # # One run at a time: the runs share that volume (toolchain install, target # dirs, a --user run's chown), so the container has a fixed name and a second # run while one is in flight is refused by Docker ("name ... already in use"). # A run whose ssh dropped keeps its container until its command ends. # # Usage: tools/linux_check.sh [--user] [--dry-run] [--] # tools/linux_check.sh 'cargo clippy --all-targets -- -D warnings' # tools/linux_check.sh --user cargo test -p lucisv_core # tools/linux_check.sh grep -rn "two words" crates # # One argument is a shell command line, run as written (pipes, ;, quotes). # Several arguments are one command and its arguments, each kept whole. # By default the command runs as root, which is how the runner's jobs run and # so what CI will see. --user runs it as an unprivileged user instead: file # permissions are enforced there, which root ignores. --dry-run builds the # archive locally (to check the local half), prints its size and the remote # command, and contacts nothing. # # Needs: ssh access to the host `luhpe` (LUCISV_LINUX_HOST overrides) and the # gitea-runner container on it (docs: tools/RELEASING.md, "CI releases"). # Exit status is the command's, or non-zero if the archive or the # connection failed. set -eu -o pipefail host=${LUCISV_LINUX_HOST:-luhpe} image=docker.gitea.com/runner-images:ubuntu-latest name=lucisv-linux-check as_user=0 dry_run=0 while [ $# -gt 0 ]; do case $1 in --user) as_user=1; shift ;; --dry-run) dry_run=1; shift ;; --) shift; break ;; -h|--help) sed -n '2,37p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) break ;; esac done [ $# -ge 1 ] || { echo "usage: $0 [--user] [--dry-run] [--] " >&2; exit 2; } if [ $# -eq 1 ]; then cmd=$1 else cmd=$(printf '%q ' "$@") fi repo_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) cd "$repo_root" # Inside the container: unpack stdin to /w, make sure the toolchain is in the # cache volume, then run the command as root or as uid 1001. inner=' set -eu export RUSTUP_HOME=/cache/rustup CARGO_HOME=/cache/cargo mkdir -p /w && tar xzf - -C /w if [ ! -x "$CARGO_HOME/bin/cargo" ]; then curl --proto =https --tlsv1.2 -sSf https://sh.rustup.rs \ | sh -s -- -y --profile minimal -c clippy,rustfmt --no-modify-path >/dev/null 2>&1 fi export PATH="$CARGO_HOME/bin:$PATH" if [ "$AS_USER" = 1 ]; then export CARGO_TARGET_DIR=/cache/target-user HOME=/tmp/home mkdir -p "$CARGO_TARGET_DIR" "$HOME" chown -R 1001:1001 /w /cache "$HOME" cd /w && exec setpriv --reuid 1001 --regid 1001 --clear-groups bash -c "$CMD" fi export CARGO_TARGET_DIR=/cache/target-root # An earlier --user run leaves the cache owned by uid 1001; root does not mind. cd /w && exec bash -c "$CMD" ' remote="docker exec -i gitea-runner docker run --rm -i --name $name \ -v lucisv-linux-check:/cache \ -e AS_USER=$as_user -e CMD=$(printf '%q' "$cmd") \ $image bash -c $(printf '%q' "$inner")" # The tree as a gzipped tar on stdout. COPYFILE_DISABLE and --no-xattrs keep # macOS tar from adding ._* entries and extended-attribute headers that GNU # tar on the other side warns about. Errors are not hidden: with pipefail a # file tar cannot read fails the whole run. archive() { git ls-files -z --cached --others --exclude-standard \ | COPYFILE_DISABLE=1 tar czf - --no-xattrs --null -T - } if [ "$dry_run" = 1 ]; then bytes=$(archive | wc -c) echo "archive: $((bytes)) bytes; would run on $host:" echo "$remote" exit 0 fi status=0 archive | ssh -o BatchMode=yes "$host" "$remote" || status=$? if [ "$status" = 125 ]; then echo "linux_check: docker refused to start the container; if it says the name" \ "\"$name\" is in use, another run is in flight - wait for it." >&2 fi exit "$status"