Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Remove rsync from hosted tenant runtime rollout

rcourtmanb965cf5146e4
diff --git a/docs/release-control/v6/internal/subsystems/deployment-installability.md b/docs/release-control/v6/internal/subsystems/deployment-installability.md
index 05f87a35e2..e8b403ff10 100644
--- a/docs/release-control/v6/internal/subsystems/deployment-installability.md
+++ b/docs/release-control/v6/internal/subsystems/deployment-installability.md
@@ -249,7 +249,9 @@ That same deployment boundary now owns hosted tenant canary rollouts too.
containers through the canonical Docker manager, snapshot tenant data before
swap, and reconcile the control-plane registry to the live container that
actually serves traffic instead of relying on ad hoc host-local scripts that
-swap containers behind the control plane's back.
+swap containers behind the control plane's back. That snapshot-and-restore path
+must be self-contained inside the shipped control-plane command rather than
+depending on undeclared host binaries such as `rsync`.
across pretest, Playwright, and posttest. `scripts/hot-dev.sh` must honor that
lock by suppressing source-triggered rebuilds and manual `pulse` binary restart
churn while the owning proof process is still alive. Stale verify locks must
diff --git a/internal/cloudcp/tenant_runtime_rollout.go b/internal/cloudcp/tenant_runtime_rollout.go
index 6deb5feebc..7c24786c48 100644
--- a/internal/cloudcp/tenant_runtime_rollout.go
+++ b/internal/cloudcp/tenant_runtime_rollout.go
@@ -2,11 +2,14 @@ package cloudcp
import (
"context"
+ "errors"
"fmt"
+ "io"
+ "io/fs"
"os"
- "os/exec"
"path/filepath"
"strings"
+ "syscall"
"time"
cpDocker "github.com/rcourtman/pulse-go-rewrite/internal/cloudcp/docker"
@@ -120,7 +123,7 @@ func RolloutTenantRuntime(ctx context.Context, cfg *CPConfig, opts TenantRuntime
registry: reg,
docker: dockerMgr,
tenantsDir: cfg.TenantsDir(),
- synchronizer: rsyncTenantRuntimeSynchronizer{},
+ synchronizer: filesystemTenantRuntimeSynchronizer{},
now: func() time.Time { return time.Now().UTC() },
sleep: time.Sleep,
healthTimeout: defaultTenantRuntimeRolloutHealthTimeout,
@@ -396,49 +399,180 @@ func sanitizeTenantRuntimeRolloutRunID(raw string, now time.Time) string {
return sanitized
}
-type rsyncTenantRuntimeSynchronizer struct{}
+type filesystemTenantRuntimeSynchronizer struct{}
-func (rsyncTenantRuntimeSynchronizer) Snapshot(ctx context.Context, srcDir, snapshotDir string) error {
- return runTenantRuntimeRsync(ctx, srcDir, snapshotDir)
+func (filesystemTenantRuntimeSynchronizer) Snapshot(ctx context.Context, srcDir, snapshotDir string) error {
+ return syncTenantRuntimeTree(ctx, srcDir, snapshotDir)
}
-func (rsyncTenantRuntimeSynchronizer) Restore(ctx context.Context, snapshotDir, dstDir string) error {
- return runTenantRuntimeRsync(ctx, snapshotDir, dstDir)
+func (filesystemTenantRuntimeSynchronizer) Restore(ctx context.Context, snapshotDir, dstDir string) error {
+ return syncTenantRuntimeTree(ctx, snapshotDir, dstDir)
}
-func runTenantRuntimeRsync(ctx context.Context, srcDir, dstDir string) error {
+func syncTenantRuntimeTree(ctx context.Context, srcDir, dstDir string) error {
srcDir = strings.TrimSpace(srcDir)
dstDir = strings.TrimSpace(dstDir)
if srcDir == "" || dstDir == "" {
return fmt.Errorf("source and destination directories are required")
}
+ srcInfo, err := os.Lstat(srcDir)
+ if err != nil {
+ return fmt.Errorf("stat source dir %s: %w", srcDir, err)
+ }
+ if !srcInfo.IsDir() {
+ return fmt.Errorf("source path %s is not a directory", srcDir)
+ }
if err := os.MkdirAll(dstDir, 0o755); err != nil {
return fmt.Errorf("create destination dir %s: %w", dstDir, err)
}
- cmd := exec.CommandContext(
- ctx,
- "rsync",
- "-a",
- "--delete",
- "--numeric-ids",
- withTrailingSeparator(srcDir),
- withTrailingSeparator(dstDir),
- )
- output, err := cmd.CombinedOutput()
+ if err := clearDirectoryContents(dstDir); err != nil {
+ return fmt.Errorf("clear destination dir %s: %w", dstDir, err)
+ }
+
+ type pendingDirectory struct {
+ path string
+ info fs.FileInfo
+ }
+ pendingDirs := make([]pendingDirectory, 0, 8)
+
+ err = filepath.WalkDir(srcDir, func(path string, d fs.DirEntry, walkErr error) error {
+ if walkErr != nil {
+ return walkErr
+ }
+ if err := ctx.Err(); err != nil {
+ return err
+ }
+
+ info, err := os.Lstat(path)
+ if err != nil {
+ return err
+ }
+ relPath, err := filepath.Rel(srcDir, path)
+ if err != nil {
+ return fmt.Errorf("rel path for %s: %w", path, err)
+ }
+ targetPath := dstDir
+ if relPath != "." {
+ targetPath = filepath.Join(dstDir, relPath)
+ }
+
+ switch mode := info.Mode(); {
+ case info.IsDir():
+ if err := os.MkdirAll(targetPath, 0o755); err != nil {
+ return fmt.Errorf("create directory %s: %w", targetPath, err)
+ }
+ pendingDirs = append(pendingDirs, pendingDirectory{path: targetPath, info: info})
+ return nil
+ case mode&os.ModeSymlink != 0:
+ linkTarget, err := os.Readlink(path)
+ if err != nil {
+ return fmt.Errorf("read symlink %s: %w", path, err)
+ }
+ if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
+ return fmt.Errorf("create symlink parent %s: %w", filepath.Dir(targetPath), err)
+ }
+ if err := os.Symlink(linkTarget, targetPath); err != nil {
+ return fmt.Errorf("create symlink %s: %w", targetPath, err)
+ }
+ if err := preserveOwnership(targetPath, info, true); err != nil {
+ return fmt.Errorf("preserve symlink ownership %s: %w", targetPath, err)
+ }
+ return nil
+ default:
+ if err := copyTenantRuntimeFile(path, targetPath, info); err != nil {
+ return err
+ }
+ return nil
+ }
+ })
if err != nil {
- trimmed := strings.TrimSpace(string(output))
- if trimmed == "" {
- return fmt.Errorf("rsync %s -> %s: %w", srcDir, dstDir, err)
+ return fmt.Errorf("sync %s -> %s: %w", srcDir, dstDir, err)
+ }
+
+ for i := len(pendingDirs) - 1; i >= 0; i-- {
+ dir := pendingDirs[i]
+ if err := preserveDirectoryMetadata(dir.path, dir.info); err != nil {
+ return fmt.Errorf("preserve directory metadata %s: %w", dir.path, err)
}
- return fmt.Errorf("rsync %s -> %s: %w: %s", srcDir, dstDir, err, trimmed)
}
return nil
}
-func withTrailingSeparator(path string) string {
- cleaned := filepath.Clean(path)
- if strings.HasSuffix(cleaned, string(os.PathSeparator)) {
- return cleaned
+func clearDirectoryContents(dir string) error {
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ return err
+ }
+ for _, entry := range entries {
+ if err := os.RemoveAll(filepath.Join(dir, entry.Name())); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func copyTenantRuntimeFile(srcPath, dstPath string, info fs.FileInfo) error {
+ if err := os.MkdirAll(filepath.Dir(dstPath), 0o755); err != nil {
+ return fmt.Errorf("create file parent %s: %w", filepath.Dir(dstPath), err)
+ }
+
+ srcFile, err := os.Open(srcPath)
+ if err != nil {
+ return fmt.Errorf("open source file %s: %w", srcPath, err)
+ }
+ defer srcFile.Close()
+
+ dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode().Perm())
+ if err != nil {
+ return fmt.Errorf("open destination file %s: %w", dstPath, err)
+ }
+ defer dstFile.Close()
+
+ if _, err := io.Copy(dstFile, srcFile); err != nil {
+ return fmt.Errorf("copy file %s -> %s: %w", srcPath, dstPath, err)
+ }
+ if err := dstFile.Chmod(info.Mode().Perm()); err != nil {
+ return fmt.Errorf("chmod destination file %s: %w", dstPath, err)
+ }
+ if err := preserveOwnership(dstPath, info, false); err != nil {
+ return fmt.Errorf("preserve file ownership %s: %w", dstPath, err)
+ }
+ if err := os.Chtimes(dstPath, info.ModTime(), info.ModTime()); err != nil {
+ return fmt.Errorf("preserve file times %s: %w", dstPath, err)
+ }
+ return nil
+}
+
+func preserveDirectoryMetadata(path string, info fs.FileInfo) error {
+ if err := preserveOwnership(path, info, false); err != nil {
+ return err
+ }
+ if err := os.Chmod(path, info.Mode().Perm()); err != nil {
+ return err
+ }
+ if err := os.Chtimes(path, info.ModTime(), info.ModTime()); err != nil {
+ return err
+ }
+ return nil
+}
+
+func preserveOwnership(path string, info fs.FileInfo, symlink bool) error {
+ stat, ok := info.Sys().(*syscall.Stat_t)
+ if !ok || stat == nil {
+ return nil
+ }
+
+ var err error
+ if symlink {
+ err = os.Lchown(path, int(stat.Uid), int(stat.Gid))
+ } else {
+ err = os.Chown(path, int(stat.Uid), int(stat.Gid))
+ }
+ if err == nil {
+ return nil
+ }
+ if errors.Is(err, syscall.EPERM) || errors.Is(err, syscall.ENOTSUP) || errors.Is(err, syscall.ENOSYS) {
+ return nil
}
- return cleaned + string(os.PathSeparator)
+ return err
}
diff --git a/internal/cloudcp/tenant_runtime_rollout_test.go b/internal/cloudcp/tenant_runtime_rollout_test.go
index 9d508bfb5e..aebca658db 100644
--- a/internal/cloudcp/tenant_runtime_rollout_test.go
+++ b/internal/cloudcp/tenant_runtime_rollout_test.go
@@ -3,6 +3,8 @@ package cloudcp
import (
"context"
"fmt"
+ "os"
+ "path/filepath"
"testing"
"time"
@@ -167,6 +169,75 @@ func TestTenantRuntimeRollout_RollsBackOnHealthFailure(t *testing.T) {
}
}
+func TestFilesystemTenantRuntimeSynchronizer_SnapshotAndRestore(t *testing.T) {
+ t.Parallel()
+
+ ctx := context.Background()
+ srcDir := t.TempDir()
+ snapshotDir := t.TempDir()
+ restoreDir := t.TempDir()
+
+ mustWrite := func(path, contents string, mode os.FileMode) {
+ t.Helper()
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ t.Fatalf("mkdir parent %s: %v", path, err)
+ }
+ if err := os.WriteFile(path, []byte(contents), mode); err != nil {
+ t.Fatalf("write file %s: %v", path, err)
+ }
+ }
+
+ mustWrite(filepath.Join(srcDir, "billing.json"), "{\"valid\":true}\n", 0o600)
+ mustWrite(filepath.Join(srcDir, "secrets", "handoff.key"), "handoff-key\n", 0o600)
+ if err := os.Symlink(filepath.Join("secrets", "handoff.key"), filepath.Join(srcDir, ".cloud_handoff_key")); err != nil {
+ t.Fatalf("symlink .cloud_handoff_key: %v", err)
+ }
+
+ mustWrite(filepath.Join(snapshotDir, "stale.txt"), "remove me\n", 0o644)
+
+ syncer := filesystemTenantRuntimeSynchronizer{}
+ if err := syncer.Snapshot(ctx, srcDir, snapshotDir); err != nil {
+ t.Fatalf("Snapshot() error = %v", err)
+ }
+
+ if _, err := os.Stat(filepath.Join(snapshotDir, "stale.txt")); !os.IsNotExist(err) {
+ t.Fatalf("snapshot stale.txt stat err = %v, want not-exist", err)
+ }
+ billingPath := filepath.Join(snapshotDir, "billing.json")
+ billingBytes, err := os.ReadFile(billingPath)
+ if err != nil {
+ t.Fatalf("read snapshot billing.json: %v", err)
+ }
+ if got := string(billingBytes); got != "{\"valid\":true}\n" {
+ t.Fatalf("snapshot billing.json = %q, want %q", got, "{\"valid\":true}\n")
+ }
+ if info, err := os.Stat(billingPath); err != nil {
+ t.Fatalf("stat snapshot billing.json: %v", err)
+ } else if got := info.Mode().Perm(); got != 0o600 {
+ t.Fatalf("snapshot billing.json mode = %o, want 600", got)
+ }
+ if target, err := os.Readlink(filepath.Join(snapshotDir, ".cloud_handoff_key")); err != nil {
+ t.Fatalf("readlink snapshot .cloud_handoff_key: %v", err)
+ } else if target != filepath.Join("secrets", "handoff.key") {
+ t.Fatalf("snapshot symlink target = %q, want %q", target, filepath.Join("secrets", "handoff.key"))
+ }
+
+ mustWrite(filepath.Join(restoreDir, "obsolete.txt"), "old\n", 0o644)
+ if err := syncer.Restore(ctx, snapshotDir, restoreDir); err != nil {
+ t.Fatalf("Restore() error = %v", err)
+ }
+ if _, err := os.Stat(filepath.Join(restoreDir, "obsolete.txt")); !os.IsNotExist(err) {
+ t.Fatalf("restore obsolete.txt stat err = %v, want not-exist", err)
+ }
+ restoredBytes, err := os.ReadFile(filepath.Join(restoreDir, "secrets", "handoff.key"))
+ if err != nil {
+ t.Fatalf("read restored handoff.key: %v", err)
+ }
+ if got := string(restoredBytes); got != "handoff-key\n" {
+ t.Fatalf("restored handoff.key = %q, want %q", got, "handoff-key\n")
+ }
+}
+
func newTestTenantRuntimeRolloutService(
reg tenantRuntimeRolloutRegistry,
docker tenantRuntimeRolloutDocker,