fix env and systemd overrides
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# pb
|
# pb
|
||||||
|
|
||||||
A simple, rsync-based PocketBase deployment tool.
|
A simple and _fast_ PocketBase deployment tool.
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
|
|||||||
86
main.go
86
main.go
@@ -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
|
||||||
}
|
}
|
||||||
@@ -1698,45 +1706,10 @@ data_dir="%[5]s"
|
|||||||
if [ -n "$data_dir" ]; then
|
if [ -n "$data_dir" ]; then
|
||||||
mkdir -p "$data_dir"
|
mkdir -p "$data_dir"
|
||||||
fi
|
fi
|
||||||
|
env_dir=$(dirname "$env_file")
|
||||||
|
mkdir -p "$env_dir"
|
||||||
if [ ! -f "$env_file" ]; then
|
if [ ! -f "$env_file" ]; then
|
||||||
cat <<'EOF' > "$env_file"
|
touch "$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
|
fi
|
||||||
`, serviceDir, serviceDir, assetURL, envFile, volume, port)
|
`, serviceDir, serviceDir, assetURL, envFile, volume, port)
|
||||||
}
|
}
|
||||||
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user