fix env and systemd overrides

This commit is contained in:
2026-01-14 11:31:33 +00:00
parent 00680499ee
commit 803706341e
2 changed files with 45 additions and 59 deletions

View File

@@ -1,6 +1,6 @@
# pb # pb
A simple, rsync-based PocketBase deployment tool. A simple and _fast_ PocketBase deployment tool.
## Commands ## Commands

102
main.go
View File

@@ -1069,6 +1069,10 @@ func performSetup(ctx *deploymentContext) error {
return fmt.Errorf("systemd setup failed: %w", err) 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 return nil
} }
@@ -1102,11 +1106,15 @@ func runDeploy() error {
} }
} }
dirs := []string{"pb_public", "pb_migrations", "pb_hooks"} paths := []string{"pb_public", "pb_migrations", "pb_hooks", "pb.toml"}
if err := syncLocalDirectories(ctx.serverIP, ctx.serviceDir, dirs); err != nil { if err := syncLocalPaths(ctx.serverIP, ctx.serviceDir, paths); err != nil {
return fmt.Errorf("failed to sync local directories: %w", err) 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 { if err := restartPocketBaseService(ctx); err != nil {
return err return err
} }
@@ -1693,51 +1701,16 @@ if [ ! -x "$binary" ]; then
chmod +x "$binary" chmod +x "$binary"
rm -f "$tmp" rm -f "$tmp"
fi fi
env_file="%s" env_file="%s"
data_dir="%[5]s" data_dir="%[5]s"
if [ -n "$data_dir" ]; then if [ -n "$data_dir" ]; then
mkdir -p "$data_dir" mkdir -p "$data_dir"
fi fi
if [ ! -f "$env_file" ]; then env_dir=$(dirname "$env_file")
cat <<'EOF' > "$env_file" mkdir -p "$env_dir"
PORT=%[6]d if [ ! -f "$env_file" ]; then
POCKETBASE_DATA_DIR=%[5]s touch "$env_file"
EOF fi
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
`, serviceDir, serviceDir, assetURL, envFile, volume, port) `, serviceDir, serviceDir, assetURL, envFile, volume, port)
} }
@@ -1746,7 +1719,7 @@ func systemdScript(serviceDir, envFile, serviceName string) string {
cat <<'EOF' > /etc/systemd/system/pb@.service cat <<'EOF' > /etc/systemd/system/pb@.service
[Unit] [Unit]
Description = PocketBase instance %%i Description = PocketBase instance %%i
After = network.target After = network.target
[Service] [Service]
Type = simple Type = simple
@@ -1759,7 +1732,7 @@ StandardOutput = append:%s/%%i.log
StandardError = append:%s/%%i.log StandardError = append:%s/%%i.log
WorkingDirectory = %s WorkingDirectory = %s
EnvironmentFile = %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] [Install]
WantedBy = multi-user.target WantedBy = multi-user.target
@@ -1767,7 +1740,21 @@ EOF
systemctl daemon-reload systemctl daemon-reload
systemctl --no-block enable --now pb@%s systemctl --no-block enable --now pb@%s
systemctl --no-block restart 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 { func systemdRestartScript(serviceName string) string {
@@ -1841,24 +1828,23 @@ func runSSHCollect(server, script string) (string, error) {
return out.String(), nil return out.String(), nil
} }
func syncLocalDirectories(server, remoteBase string, dirs []string) error { func syncLocalPaths(server, remoteBase string, paths []string) error {
var toSync []string var toSync []string
for _, dir := range dirs { for _, path := range paths {
localPath := filepath.Join(".", dir) localPath := filepath.Join(".", path)
info, err := os.Stat(localPath) info, err := os.Stat(localPath)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { 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 continue
} }
return err return err
} }
if !info.IsDir() { if info.IsDir() {
fmt.Printf("- %s exists but is not a directory, skipping\n", dir) toSync = append(toSync, path+"/")
continue continue
} }
toSync = append(toSync, path)
toSync = append(toSync, dir+"/")
} }
if len(toSync) == 0 { if len(toSync) == 0 {