From 803706341ef9a349d2f8a82d7b30f23d96909501 Mon Sep 17 00:00:00 2001 From: Nick Goodall Date: Wed, 14 Jan 2026 11:31:33 +0000 Subject: [PATCH] fix env and systemd overrides --- README.md | 2 +- main.go | 102 +++++++++++++++++++++++------------------------------- 2 files changed, 45 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index ccd2fe9..306d263 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # pb -A simple, rsync-based PocketBase deployment tool. +A simple and _fast_ PocketBase deployment tool. ## Commands diff --git a/main.go b/main.go index bc3e5d9..fb531c3 100644 --- a/main.go +++ b/main.go @@ -1069,6 +1069,10 @@ func performSetup(ctx *deploymentContext) error { return fmt.Errorf("systemd setup failed: %w", err) } + if err := runSSHCommand(ctx.serverIP, systemdOverrideScript(ctx.serviceName, ctx.port, ctx.volume)); err != nil { + return fmt.Errorf("systemd override failed: %w", err) + } + return nil } @@ -1102,11 +1106,15 @@ func runDeploy() error { } } - dirs := []string{"pb_public", "pb_migrations", "pb_hooks"} - if err := syncLocalDirectories(ctx.serverIP, ctx.serviceDir, dirs); err != nil { + paths := []string{"pb_public", "pb_migrations", "pb_hooks", "pb.toml"} + if err := syncLocalPaths(ctx.serverIP, ctx.serviceDir, paths); err != nil { return fmt.Errorf("failed to sync local directories: %w", err) } + if err := runSSHCommand(ctx.serverIP, systemdOverrideScript(ctx.serviceName, ctx.port, ctx.volume)); err != nil { + return fmt.Errorf("systemd override failed: %w", err) + } + if err := restartPocketBaseService(ctx); err != nil { return err } @@ -1693,51 +1701,16 @@ if [ ! -x "$binary" ]; then chmod +x "$binary" rm -f "$tmp" fi -env_file="%s" -data_dir="%[5]s" -if [ -n "$data_dir" ]; then - mkdir -p "$data_dir" -fi -if [ ! -f "$env_file" ]; then - cat <<'EOF' > "$env_file" -PORT=%[6]d -POCKETBASE_DATA_DIR=%[5]s -EOF -else - current_port=$(grep '^PORT=' "$env_file" | head -n 1 | cut -d= -f2 || true) - current_data_dir=$(grep '^POCKETBASE_DATA_DIR=' "$env_file" | head -n 1 | cut -d= -f2 || true) - if [ "$current_port" != "%[6]d" ] || [ "$current_data_dir" != "%[5]s" ]; then - tmp=$(mktemp) - awk -v port="%[6]d" -v datadir="%[5]s" ' -/^PORT=/ { - if (port_set == 0) { - printf "PORT=%s\n", port - port_set = 1 - } - next -} -/^POCKETBASE_DATA_DIR=/ { - if (data_set == 0) { - printf "POCKETBASE_DATA_DIR=%s\n", datadir - data_set = 1 - } - next -} -{ - print -} -END { - if (port_set == 0) { - printf "PORT=%s\n", port - } - if (data_set == 0) { - printf "POCKETBASE_DATA_DIR=%s\n", datadir - } -} -' "$env_file" > "$tmp" - mv "$tmp" "$env_file" - fi -fi + env_file="%s" + data_dir="%[5]s" + if [ -n "$data_dir" ]; then + mkdir -p "$data_dir" + fi + env_dir=$(dirname "$env_file") + mkdir -p "$env_dir" + if [ ! -f "$env_file" ]; then + touch "$env_file" + fi `, serviceDir, serviceDir, assetURL, envFile, volume, port) } @@ -1746,7 +1719,7 @@ func systemdScript(serviceDir, envFile, serviceName string) string { cat <<'EOF' > /etc/systemd/system/pb@.service [Unit] Description = PocketBase instance %%i -After = network.target +After = network.target [Service] Type = simple @@ -1759,7 +1732,7 @@ StandardOutput = append:%s/%%i.log StandardError = append:%s/%%i.log WorkingDirectory = %s EnvironmentFile = %s -ExecStart = %s/pocketbase serve --dir=$POCKETBASE_DATA_DIR --http="127.0.0.1:${PORT}" +ExecStart = %s/pocketbase serve --http="127.0.0.1:${PORT}" --dir=${DATA_DIR} --hooksDir=%s/pb_hooks --migrationsDir=%s/pb_migrations --publicDir=%s/pb_public [Install] WantedBy = multi-user.target @@ -1767,7 +1740,21 @@ EOF systemctl daemon-reload systemctl --no-block enable --now pb@%s systemctl --no-block restart pb@%s -`, serviceDir, serviceDir, serviceDir, envFile, serviceDir, serviceName, serviceName) +`, serviceDir, serviceDir, serviceDir, envFile, serviceDir, serviceDir, serviceDir, serviceDir, serviceName, serviceName) +} + +func systemdOverrideScript(serviceName string, port int, volume string) string { + return fmt.Sprintf(`set -euo pipefail +dir="/etc/systemd/system/pb@%s.service.d" +mkdir -p "$dir" +cat <<'EOF' > "$dir/override.conf" +[Service] +Environment=PORT=%d +Environment=DATA_DIR=%s +EOF +systemctl daemon-reload +systemctl --no-block restart pb@%s +`, serviceName, port, volume, serviceName) } func systemdRestartScript(serviceName string) string { @@ -1841,24 +1828,23 @@ func runSSHCollect(server, script string) (string, error) { return out.String(), nil } -func syncLocalDirectories(server, remoteBase string, dirs []string) error { +func syncLocalPaths(server, remoteBase string, paths []string) error { var toSync []string - for _, dir := range dirs { - localPath := filepath.Join(".", dir) + for _, path := range paths { + localPath := filepath.Join(".", path) info, err := os.Stat(localPath) if err != nil { if errors.Is(err, os.ErrNotExist) { - fmt.Printf("- %s does not exist locally, skipping\n", dir) + fmt.Printf("- %s does not exist locally, skipping\n", path) continue } return err } - if !info.IsDir() { - fmt.Printf("- %s exists but is not a directory, skipping\n", dir) + if info.IsDir() { + toSync = append(toSync, path+"/") continue } - - toSync = append(toSync, dir+"/") + toSync = append(toSync, path) } if len(toSync) == 0 {