Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/scripts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test scripts
on:
workflow_dispatch:
push:
branches:
- main
paths: ['scripts/**', '.github/workflows/scripts.yml']
pull_request:
branches:
- main
paths: ['scripts/**', '.github/workflows/scripts.yml']

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
scripts:
name: Scripts
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Clone this repo
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- name: ShellCheck
run: shellcheck scripts/*.sh

- name: Run the update script tests
run: bash scripts/test-update-stack.sh
12 changes: 10 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ScaleTail is a collection of Docker Compose stacks. Each directory in `services/
- [CONTRIBUTING.md](CONTRIBUTING.md): how to add, update, verify, and submit a service. Follow it.
- [templates/service-template/](templates/service-template/): the starting point for every service. Its comments explain each setting.
- [documentation/standard-setup.md](documentation/standard-setup.md): the shared setup. A service README lists only what differs, under "Deviations from the standard setup".
- [.github/workflows/](.github/workflows/): the Compose validation and Markdown lint that CI runs.
- [scripts/](scripts/): `update-stack.sh` updates a stack that a user copied out of the clone, and `test-update-stack.sh` tests it. They target bash and the GNU tools of Linux distributions.
- [.github/workflows/](.github/workflows/): the Compose validation, Markdown lint, and script tests that CI runs.

## Before you edit

Expand Down Expand Up @@ -50,6 +51,13 @@ git diff --check origin/main

Without `uv`, download that version from the [rumdl releases](https://github.com/rvben/rumdl/releases) and run `rumdl check --config .markdownlint.yml .`.

When you change `scripts/`, run the same checks as CI from the repository root:

```sh
shellcheck scripts/*.sh
bash scripts/test-update-stack.sh
```

Lint the whole tree, because renaming a service breaks a link in the root `README.md`. `git diff --check origin/main` also covers staged and committed changes, but not untracked files, so stage new files first. In a fork, compare with the `main` branch of this repository instead, for example `upstream/main`.

When you have an auth key, start the copied stack and test its main function through the Tailnet. Run each health check command in its container with `docker exec`.
Expand All @@ -58,5 +66,5 @@ Report the checks you ran. State any checks that credentials, hardware, permissi

## Pull requests

- Title the pull request `Service: what changes`, such as `Umami: new service`. For several services, list them or use `All services:`. For other areas, use `CI:`, `Docs:`, or `Template:`.
- Title the pull request `Service: what changes`, such as `Umami: new service`. For several services, list them or use `All services:`. For other areas, use `CI:`, `Docs:`, `Scripts:`, or `Template:`.
- Fill in the [pull request template](.github/PULL_REQUEST_TEMPLATE.md). Maintainers apply the labels.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,6 @@ Then confirm:

Title the pull request `Service: what changes`, such as `Umami: new service`.
For several services, list them or use `All services:`. For other areas, use
`CI:`, `Docs:`, or `Template:`. Fill in the pull request template, and report
the checks you ran and any checks you could not run. Maintainers apply the
labels.
`CI:`, `Docs:`, `Scripts:`, or `Template:`. Fill in the pull request template,
and report the checks you ran and any checks you could not run. Maintainers
apply the labels.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,32 @@ cp -R ScaleTail/services/<service> ~/stacks/<service>
cd ~/stacks/<service>
```

Your `.env`, the Tailscale state, and the application data now live outside the clone, so `git pull` in the clone never conflicts with them. A copy no longer follows the repository, so to pick up changes, update the clone and compare the files:
Your `.env`, the Tailscale state, and the application data now live outside the clone, so `git pull` in the clone never conflicts with them. A copy no longer follows the repository. To pick up changes, run the update script. It downloads the latest version of the service from GitHub, so the clone does not need to be up to date, and it runs on Linux with `bash`, `curl` or `wget`, `tar`, and `git`:

```sh
ScaleTail/scripts/update-stack.sh ~/stacks/<service>
ScaleTail/scripts/update-stack.sh --apply ~/stacks/<service>
```

Without a clone, download the script once and run it with `bash`:

```sh
curl -fsSLo ~/stacks/update-stack.sh https://raw.githubusercontent.com/tailscale-dev/ScaleTail/main/scripts/update-stack.sh
bash ~/stacks/update-stack.sh ~/stacks/<service>
```

The first command only reports what would change, and exits with status 1 when updates are available. The second command writes the changes to `compose.yaml` and `.env`. It copies each file that changes to `.scaletail/backup-<time>/` first, and saves the upstream versions of both files in `.scaletail/`.

- The next run merges your files with the changes made upstream since that saved version, so your own edits stay. Keep the `.scaletail` folder.
- When your edit and an upstream change touch the same lines, the file gets conflict markers (`<<<<<<<` to `>>>>>>>`). Resolve them by hand before you start the stack. The script refuses to run while they remain.
- The first run has no saved version to compare with. It adds the variables that your `.env` lacks and replaces `compose.yaml` with the upstream version. It says whether settings differ or only comments, and keeps your old file in the backup. It prints the `diff` command that compares the two, so copy your own edits back once.
- It never prints the contents of `compose.yaml` or `.env`, only the names of variables.
- It stops when `SERVICE` changes upstream, because that renames the Tailnet device, the containers, and the data folders. Read the "Upgrading" section of the service README, which the script prints, and run it again with `--force`.
- It never starts, stops, or restarts containers. When it is done, run `docker compose pull` and `docker compose up -d` from your copy.

Run `ScaleTail/scripts/update-stack.sh --help` for all options.

To compare the files yourself, update the clone and diff them:

```sh
git -C ScaleTail pull
Expand Down
195 changes: 195 additions & 0 deletions scripts/test-update-stack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#!/usr/bin/env bash
# The fixtures contain Compose variables such as ${TZ} on purpose.
# shellcheck disable=SC2016
# Self-test for update-stack.sh. It serves a fake ScaleTail archive from a
# temporary folder, so it needs no network. Run it with:
# bash scripts/test-update-stack.sh
set -uo pipefail

SCRIPT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)/update-stack.sh
ROOT=$(mktemp -d)
trap 'rm -rf "$ROOT"' EXIT
export SCALETAIL_URL=file://$ROOT/repo.tar.gz
SECRET=SENTINEL-secret-4711
FAILS=0

fail() {
FAILS=$((FAILS + 1))
printf ' FAILED: %s\n' "$*"
}
check() { "$@" || fail "$*"; }
has() { grep -qF -- "$2" "$1"; }

# Run the script in the folder $1. Sets OUT and RC.
run() {
local dir=$1
shift
RC=0
OUT=$(cd "$dir" && bash "$SCRIPT" "$@" 2>&1) || RC=$?
if grep -qF "$SECRET" <<<"$OUT"; then fail "the output contains a secret value"; fi
}

# Publish version $1 of services/demo as the upstream archive.
publish() {
local d=$ROOT/repo/ScaleTail-main/services/demo
rm -rf "$ROOT/repo"
mkdir -p "$d"
cat >"$d/compose.yaml" <<YAML
services:
tailscale:
image: tailscale/tailscale:latest
container_name: tailscale-\${SERVICE}
application:
image: \${IMAGE_URL}
container_name: app-\${SERVICE}
environment:
- TZ=\${TZ}$([ "$1" -lt 2 ] || printf '\n - LOG_LEVEL=${LOG_LEVEL}')
restart: always
YAML
{
printf 'SERVICE=%s\nIMAGE_URL=demo/demo:latest\n\n# Tailscale auth key.\nTS_AUTHKEY=\n\nTZ=Europe/Amsterdam\n' \
"$([ "$1" -lt 3 ] && echo demo || echo demo2)"
[ "$1" -lt 2 ] || printf '\n# Log level of the application.\nLOG_LEVEL=info\n'
} >"$d/.env"
printf '# Demo\n\n## Upgrading\n\nVersion %s notes.\n\n## Troubleshooting\n\nNone.\n' "$1" >"$d/README.md"
tar -czf "$ROOT/repo.tar.gz" -C "$ROOT/repo" ScaleTail-main
}

# Copy the published version to a new stack folder. Sets STACK.
copy() {
STACK=$ROOT/stacks/$1
rm -rf "$STACK"
mkdir -p "$ROOT/stacks"
cp -R "$ROOT/repo/ScaleTail-main/services/demo" "$STACK"
sed -i "s/^TS_AUTHKEY=.*/TS_AUTHKEY=$SECRET/" "$STACK/.env"
}

state() { find "$1" -type f -exec md5sum {} + | sort; }

case_() { printf '%s\n' "$*"; }

case_ "first run with current files: check writes nothing, --apply saves the upstream files"
publish 1
copy demo
before=$(state "$STACK")
run "$STACK"
check [ "$RC" -eq 0 ]
check [ "$before" = "$(state "$STACK")" ]
run "$STACK" --apply
check [ "$RC" -eq 0 ]
check cmp -s "$STACK/.scaletail/compose.yaml" "$STACK/compose.yaml"
check has "$STACK/.env" "$SECRET"
check [ -z "$(find "$STACK/.scaletail" -name 'backup-*')" ]

case_ "first run with an old copy: .env gets the new variable, compose.yaml is replaced"
publish 1
copy demo
publish 2
echo '# my note' >>"$STACK/compose.yaml"
run "$STACK"
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "added: LOG_LEVEL"
check has <(echo "$OUT") "Version 2 notes."
run "$STACK" --apply
check [ "$RC" -eq 0 ]
check has "$STACK/.env" "$SECRET"
check has "$STACK/.env" "# Log level of the application."
check cmp -s "$STACK/compose.yaml" "$ROOT/repo/ScaleTail-main/services/demo/compose.yaml"
check has "$STACK"/.scaletail/backup-*/compose.yaml "# my note"

case_ "with a saved copy: your edits and the upstream change are merged"
publish 1
copy demo
run "$STACK" --apply
sed -i 's/tailscale:latest/tailscale:stable/' "$STACK/compose.yaml"
publish 2
before=$(state "$STACK")
run "$STACK"
check [ "$RC" -eq 1 ]
check [ "$before" = "$(state "$STACK")" ]
run "$STACK" --apply
check [ "$RC" -eq 0 ]
check has "$STACK/compose.yaml" "tailscale:stable"
check has "$STACK/compose.yaml" "LOG_LEVEL"
check has "$STACK/.env" "$SECRET"
check has "$STACK/.env" "LOG_LEVEL=info"
run "$STACK"
check [ "$RC" -eq 0 ]
check has <(echo "$OUT") "up to date"

case_ "a conflict writes markers, keeps a backup, and blocks the next run"
publish 1
copy demo
run "$STACK" --apply
sed -i 's/- TZ=${TZ}/- TZ=UTC/' "$STACK/compose.yaml"
publish 2
run "$STACK" --apply
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "conflict"
check has "$STACK/compose.yaml" "<<<<<<<"
check has "$STACK"/.scaletail/backup-*/compose.yaml "TZ=UTC"
run "$STACK"
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "conflict markers"
sed -i '/^<<<<<<<\|^=======\|^>>>>>>>/d; /- TZ=\${TZ}/d' "$STACK/compose.yaml"
run "$STACK"
check [ "$RC" -eq 0 ]

case_ "a changed SERVICE needs --force"
publish 2
copy demo
run "$STACK" --apply
publish 3
run "$STACK" --apply
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "SERVICE changes upstream"
check has "$STACK/.env" "SERVICE=demo"
run "$STACK" --apply --force
check [ "$RC" -eq 0 ]
check has "$STACK/.env" "SERVICE=demo2"

case_ "the service name comes from the folder, or from --service"
publish 1
copy other
run "$STACK"
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "does not exist upstream"
run "$STACK" --service demo
check [ "$RC" -eq 0 ]

case_ "a first update names what differs in compose.yaml, and prints none of it"
publish 1
copy demo
# Old stacks set the auth key in compose.yaml.
sed -i "s|container_name: tailscale-\${SERVICE}|&\\n environment:\\n - TS_AUTHKEY=$SECRET|" "$STACK/compose.yaml"
publish 2
run "$STACK"
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "settings differ"
if grep -qE '^ *(---|\+\+\+|@@) ' <<<"$OUT"; then fail "the output contains a diff"; fi
run "$STACK" --apply
check [ "$RC" -eq 0 ]
check has <(echo "$OUT") "compare: diff .scaletail/backup-"
check has "$STACK"/.scaletail/backup-*/compose.yaml "$SECRET"

case_ "a first update with only comment changes says so"
publish 1
copy demo
sed -i 's/^services:$/services: # my note/' "$STACK/compose.yaml"
echo '# my other note' >>"$STACK/compose.yaml"
run "$STACK"
check [ "$RC" -eq 1 ]
check has <(echo "$OUT") "only comments or spacing differ"
if grep -qF -- 'my ' <<<"$OUT"; then fail "a comment was printed"; fi
run "$STACK" --apply
if grep -qF -- 'compare:' <<<"$OUT"; then fail "a comment-only update asked for a compare"; fi

case_ "a folder without compose.yaml is an error"
run "$ROOT"
check [ "$RC" -eq 2 ]

if [ "$FAILS" -gt 0 ]; then
printf '%s checks failed\n' "$FAILS"
exit 1
fi
echo "All checks passed"
Loading
Loading