tidy up deployment output
This commit is contained in:
16
README.md
16
README.md
@@ -2,15 +2,17 @@
|
|||||||
|
|
||||||
A simple, rsync-based PocketBase deployment tool.
|
A simple, rsync-based PocketBase deployment tool.
|
||||||
|
|
||||||
## init
|
## Commands
|
||||||
|
|
||||||
- start a new PocketBase project (optionally provide a service name via `pb init <name>`)
|
### `init`
|
||||||
|
|
||||||
## dev
|
Start a new PocketBase project (optionally provide a service name via `pb init <name>`)
|
||||||
|
|
||||||
- run a local dev server
|
### `dev`
|
||||||
|
|
||||||
## deploy
|
Run the local dev server.
|
||||||
|
|
||||||
|
### `deploy`
|
||||||
|
|
||||||
Deploys pocketbase to a remote server. This will:
|
Deploys pocketbase to a remote server. This will:
|
||||||
|
|
||||||
@@ -20,6 +22,6 @@ Deploys pocketbase to a remote server. This will:
|
|||||||
4. Copy pb_public, pb_migrations and pb_hooks.
|
4. Copy pb_public, pb_migrations and pb_hooks.
|
||||||
5. Start the service.
|
5. Start the service.
|
||||||
|
|
||||||
## logs
|
### logs
|
||||||
|
|
||||||
## secrets
|
### secrets
|
||||||
|
|||||||
99
deploy.go
99
deploy.go
@@ -1,15 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
)
|
)
|
||||||
@@ -303,9 +306,26 @@ systemctl restart pb@%s
|
|||||||
|
|
||||||
func runSSHCommand(server, script string) error {
|
func runSSHCommand(server, script string) error {
|
||||||
cmd := exec.Command("ssh", append(sshArgs(server), "bash", "--noprofile", "--norc", "-c", script)...)
|
cmd := exec.Command("ssh", append(sshArgs(server), "bash", "--noprofile", "--norc", "-c", script)...)
|
||||||
cmd.Stdout = os.Stdout
|
stdoutPipe, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
return cmd.Run()
|
if err := cmd.Start(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- filterEnvStream(stdoutPipe, os.Stdout)
|
||||||
|
}()
|
||||||
|
|
||||||
|
waitErr := cmd.Wait()
|
||||||
|
pipeErr := <-done
|
||||||
|
if waitErr != nil {
|
||||||
|
return waitErr
|
||||||
|
}
|
||||||
|
return pipeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func runSSHOutput(server, script string) (string, error) {
|
func runSSHOutput(server, script string) (string, error) {
|
||||||
@@ -316,7 +336,8 @@ func runSSHOutput(server, script string) (string, error) {
|
|||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(out.String()), nil
|
filtered := stripLeadingEnvLines(out.String())
|
||||||
|
return strings.TrimSpace(filtered), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func syncLocalDirectories(server, remoteBase string, dirs []string) error {
|
func syncLocalDirectories(server, remoteBase string, dirs []string) error {
|
||||||
@@ -388,3 +409,75 @@ func closeSSHControlMaster(server string) {
|
|||||||
args = append(args, "-O", "exit", fmt.Sprintf("root@%s", server))
|
args = append(args, "-O", "exit", fmt.Sprintf("root@%s", server))
|
||||||
_ = exec.Command("ssh", args...).Run()
|
_ = exec.Command("ssh", args...).Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func filterEnvStream(r io.Reader, w io.Writer) error {
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
scanner.Buffer(make([]byte, 0, 64*1024), 2*1024*1024)
|
||||||
|
skipping := true
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if skipping && isEnvLine(line) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if skipping {
|
||||||
|
skipping = false
|
||||||
|
}
|
||||||
|
if _, err := fmt.Fprintln(w, line); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func stripLeadingEnvLines(input string) string {
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(input))
|
||||||
|
scanner.Buffer(make([]byte, 0, 64*1024), 2*1024*1024)
|
||||||
|
skipping := true
|
||||||
|
var builder strings.Builder
|
||||||
|
addedLine := false
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
if skipping && isEnvLine(line) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if skipping {
|
||||||
|
skipping = false
|
||||||
|
}
|
||||||
|
if addedLine {
|
||||||
|
builder.WriteByte('\n')
|
||||||
|
}
|
||||||
|
builder.WriteString(line)
|
||||||
|
addedLine = true
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
return builder.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func isEnvLine(line string) bool {
|
||||||
|
trimmed := strings.TrimSpace(line)
|
||||||
|
if trimmed == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
idx := strings.IndexRune(trimmed, '=')
|
||||||
|
if idx <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
key := trimmed[:idx]
|
||||||
|
for i, r := range key {
|
||||||
|
if i == 0 {
|
||||||
|
if r != '_' && !unicode.IsLetter(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if r != '_' && !unicode.IsLetter(r) && !unicode.IsDigit(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user