Docker: S3 and SeaweedFS stacks, matching DriverVault
Docker/ gets the same deployment shapes as DriverVault: plain, SeaweedFS in the stack, SeaweedFS split into its roles, and an external S3 endpoint, each as a build-from-source and a registry compose file with an .env example of the same name, plus a README. The API Server now reads PB_S3_* on boot and points PocketBase's file storage at the bucket (only when the settings differ), then asks PocketBase to test it. It never turns S3 off. Plugin state gets an api_data volume, since the image could not write plugins.json. The PocketBase image pinned v0.22.21, which cannot run the v0.23+ migrations or `superuser upsert`; it now pins v0.39.11. Starting from an empty database also exposed two migration bugs: the seed tried to create a passwordless account, and the documents collection saved its self-relation with a blank target. Both are fixed; databases that already ran them are unaffected. Verified on Docker: the SeaweedFS and split stacks come up healthy from an empty database, uploads land in the bucket, and a restart is a no-op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bc41d8b128
commit
5be676fc97
26 files changed
+2557
-72
No files matched your search
@@ -0,0 +1,150 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"pilotvault/apiserver/internal/config"
|
||||
)
|
||||
|
||||
// PocketBase file storage. Document blobs live in PocketBase's file storage, and
|
||||
// by default that is the pb_data volume next to the SQLite database. When the
|
||||
// PB_S3_* variables describe a bucket (the SeaweedFS and S3 compose files set
|
||||
// them), the API Server points PocketBase's "Files storage" at that bucket on
|
||||
// every boot. Clients never notice: a document is always downloaded through
|
||||
// GET /api/documents/{id}/file, which streams from PocketBase whichever backend
|
||||
// it uses.
|
||||
|
||||
// storageSettings is the slice of PocketBase's settings this step owns. The json
|
||||
// names are PocketBase's own (core.S3Config), so the PATCH body is this type
|
||||
// marshalled back out.
|
||||
type storageSettings struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Bucket string `json:"bucket"`
|
||||
Region string `json:"region"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
AccessKey string `json:"accessKey"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
ForcePathStyle bool `json:"forcePathStyle"`
|
||||
}
|
||||
|
||||
// sameExceptSecret compares everything a read of the settings can be trusted
|
||||
// on. PocketBase masks the stored secret, so a rotation of the secret alone is
|
||||
// invisible from here — changing any other PB_S3_* value forces the write, and
|
||||
// so does editing it in the PocketBase admin UI.
|
||||
func (s storageSettings) sameExceptSecret(other storageSettings) bool {
|
||||
s.Secret, other.Secret = "", ""
|
||||
return s == other
|
||||
}
|
||||
|
||||
func wantedStorage(cfg config.Config) storageSettings {
|
||||
return storageSettings{
|
||||
Enabled: true,
|
||||
Bucket: cfg.S3Bucket,
|
||||
Region: cfg.S3Region,
|
||||
Endpoint: cfg.S3Endpoint,
|
||||
AccessKey: cfg.S3AccessKey,
|
||||
Secret: cfg.S3Secret,
|
||||
ForcePathStyle: cfg.S3ForcePathStyle,
|
||||
}
|
||||
}
|
||||
|
||||
// StartFileStorage applies the configured S3 file storage in the background.
|
||||
// It does nothing when no bucket was asked for. A PocketBase that is still
|
||||
// starting is retried for a while; the server itself never waits on this, so a
|
||||
// bad endpoint cannot keep the panel (where the connection is fixed) down.
|
||||
func (s *Server) StartFileStorage() {
|
||||
s.mu.RLock()
|
||||
cfg := s.cfg
|
||||
s.mu.RUnlock()
|
||||
|
||||
if !cfg.StorageConfigured() {
|
||||
if cfg.S3Enabled {
|
||||
log.Println("storage: WARNING: PB_S3_ENABLED is set but the bucket, endpoint or credentials are incomplete — file storage stays on the local volume")
|
||||
}
|
||||
return
|
||||
}
|
||||
if !s.admin.configured() {
|
||||
log.Println("storage: WARNING: PB_S3_* is set but POCKETBASE_ADMIN_EMAIL/PASSWORD are not — cannot point PocketBase at the bucket")
|
||||
return
|
||||
}
|
||||
|
||||
want := wantedStorage(cfg)
|
||||
go func() {
|
||||
const attempts = 10
|
||||
for i := 1; ; i++ {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
err := s.ensureFileStorage(ctx, want)
|
||||
cancel()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if i == attempts {
|
||||
log.Printf("storage: WARNING: could not apply S3 file storage after %d attempts: %v", attempts, err)
|
||||
return
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// ensureFileStorage points PocketBase's record-file storage at the bucket.
|
||||
//
|
||||
// It never turns S3 *off*: files already written to a bucket are reachable only
|
||||
// while PocketBase still points at it, so running the plain compose file again
|
||||
// leaves the setting where it is rather than stranding every existing document.
|
||||
// Moving back to local storage is a deliberate act in PocketBase's admin UI.
|
||||
func (s *Server) ensureFileStorage(ctx context.Context, want storageSettings) error {
|
||||
raw, status, err := s.admin.do(ctx, http.MethodGet, "/api/settings", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status < 200 || status >= 300 {
|
||||
return fmt.Errorf("read settings: status %d: %s", status, raw)
|
||||
}
|
||||
var current struct {
|
||||
S3 storageSettings `json:"s3"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, ¤t); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if current.S3.sameExceptSecret(want) {
|
||||
log.Printf("storage: • file storage already on S3 (%s)", want.Bucket)
|
||||
} else {
|
||||
// Only the s3 block is sent: everything else in the settings — mail,
|
||||
// backups, rate limits — belongs to whoever set it.
|
||||
raw, status, err = s.admin.do(ctx, http.MethodPatch, "/api/settings",
|
||||
map[string]any{"s3": want})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if status < 200 || status >= 300 {
|
||||
return fmt.Errorf("apply settings: status %d: %s", status, raw)
|
||||
}
|
||||
log.Printf("storage: ✓ file storage → S3 (%s at %s)", want.Bucket, want.Endpoint)
|
||||
}
|
||||
|
||||
s.testFileStorage(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
// testFileStorage asks PocketBase to prove it can reach the bucket, and only
|
||||
// says so in the log: uploads will fail if it cannot, but the server still has
|
||||
// to come up.
|
||||
func (s *Server) testFileStorage(ctx context.Context) {
|
||||
raw, status, err := s.admin.do(ctx, http.MethodPost, "/api/settings/test/s3",
|
||||
map[string]any{"filesystem": "storage"})
|
||||
switch {
|
||||
case err != nil:
|
||||
log.Printf("storage: WARNING: S3 storage test failed: %v", err)
|
||||
case status < 200 || status >= 300:
|
||||
log.Printf("storage: WARNING: S3 storage unreachable (status %d): %s", status, raw)
|
||||
default:
|
||||
log.Printf("storage: ✓ S3 storage reachable")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"pilotvault/apiserver/internal/config"
|
||||
)
|
||||
|
||||
func TestStorageSameExceptSecret(t *testing.T) {
|
||||
a := storageSettings{Enabled: true, Bucket: "pilotvault", Region: "us-east-1",
|
||||
Endpoint: "http://seaweedfs:8333", AccessKey: "k", Secret: "s1", ForcePathStyle: true}
|
||||
|
||||
// PocketBase masks the secret on read, so a differing secret alone matches.
|
||||
b := a
|
||||
b.Secret = "******"
|
||||
if !a.sameExceptSecret(b) {
|
||||
t.Fatal("secret-only difference should compare equal")
|
||||
}
|
||||
|
||||
b = a
|
||||
b.Endpoint = "http://seaweedfs-s3:8333"
|
||||
if a.sameExceptSecret(b) {
|
||||
t.Fatal("endpoint change should force a write")
|
||||
}
|
||||
|
||||
if a.sameExceptSecret(storageSettings{}) {
|
||||
t.Fatal("a fresh PocketBase (S3 off) should force a write")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageConfigured(t *testing.T) {
|
||||
full := config.Config{S3Enabled: true, S3Bucket: "b", S3Endpoint: "http://e",
|
||||
S3AccessKey: "k", S3Secret: "s"}
|
||||
if !full.StorageConfigured() {
|
||||
t.Fatal("fully described bucket should be configured")
|
||||
}
|
||||
for name, mut := range map[string]func(*config.Config){
|
||||
"disabled": func(c *config.Config) { c.S3Enabled = false },
|
||||
"no endpoint": func(c *config.Config) { c.S3Endpoint = "" },
|
||||
"no key": func(c *config.Config) { c.S3AccessKey = "" },
|
||||
"no secret": func(c *config.Config) { c.S3Secret = "" },
|
||||
} {
|
||||
c := full
|
||||
mut(&c)
|
||||
if c.StorageConfigured() {
|
||||
t.Errorf("%s: should not be configured", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,33 @@ type Config struct {
|
||||
// 503 and the rest of the server is unaffected.
|
||||
PocketBaseAdminEmail string
|
||||
PocketBaseAdminPassword string
|
||||
|
||||
// PocketBase file storage. With S3Enabled set and the bucket fully
|
||||
// described, the server points PocketBase's "Files storage" at this bucket
|
||||
// on boot instead of the pb_data volume; left unset, uploads stay on disk
|
||||
// exactly as they always have. Only record files (document blobs) move —
|
||||
// PocketBase's own backups are deliberately not touched.
|
||||
//
|
||||
// S3Endpoint has no default: an in-stack SeaweedFS and one outside it are
|
||||
// different addresses, and guessing either would be worse than not trying.
|
||||
S3Enabled bool
|
||||
S3Bucket string
|
||||
S3Region string
|
||||
S3Endpoint string
|
||||
S3AccessKey string
|
||||
S3Secret string
|
||||
// S3ForcePathStyle keeps bucket names in the path rather than the hostname.
|
||||
// True by default because that is what a self-hosted gateway serves —
|
||||
// virtual-host style would need a DNS entry per bucket.
|
||||
S3ForcePathStyle bool
|
||||
}
|
||||
|
||||
// StorageConfigured reports whether S3 file storage has been fully specified.
|
||||
// Anything less counts as "not asked for": a half-filled .env leaves uploads on
|
||||
// the local volume rather than pointing PocketBase at a bucket it cannot reach.
|
||||
func (c Config) StorageConfigured() bool {
|
||||
return c.S3Enabled && c.S3Bucket != "" && c.S3Endpoint != "" &&
|
||||
c.S3AccessKey != "" && c.S3Secret != ""
|
||||
}
|
||||
|
||||
// EnvFile is the .env path (relative to the working directory) that Load reads
|
||||
@@ -39,6 +66,13 @@ func Load() Config {
|
||||
PluginsFile: getenv("PLUGINS_FILE", "plugins.json"),
|
||||
PocketBaseAdminEmail: getenv("POCKETBASE_ADMIN_EMAIL", os.Getenv("PB_ADMIN_EMAIL")),
|
||||
PocketBaseAdminPassword: getenv("POCKETBASE_ADMIN_PASSWORD", os.Getenv("PB_ADMIN_PASSWORD")),
|
||||
S3Enabled: boolEnv("PB_S3_ENABLED", false),
|
||||
S3Bucket: getenv("PB_S3_BUCKET", "pilotvault"),
|
||||
S3Region: getenv("PB_S3_REGION", "us-east-1"),
|
||||
S3Endpoint: strings.TrimRight(getenv("PB_S3_ENDPOINT", ""), "/"),
|
||||
S3AccessKey: getenv("PB_S3_ACCESS_KEY", ""),
|
||||
S3Secret: getenv("PB_S3_SECRET", ""),
|
||||
S3ForcePathStyle: boolEnv("PB_S3_FORCE_PATH_STYLE", true),
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -104,6 +138,18 @@ func getenv(key, def string) string {
|
||||
return def
|
||||
}
|
||||
|
||||
// boolEnv reads a boolean variable, accepting the usual spellings; anything
|
||||
// unrecognised (or unset) falls back to def.
|
||||
func boolEnv(key string, def bool) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(os.Getenv(key))) {
|
||||
case "1", "true", "yes", "on":
|
||||
return true
|
||||
case "0", "false", "no", "off":
|
||||
return false
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func splitCSV(s string) []string {
|
||||
parts := strings.Split(s, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
|
||||
Reference in new issue
Block a user