Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): drop shard overrides merged into services (#3607)

Hampus7f00da6cef31
diff --git a/fluxer_rexctl/cmd/upgrade.go b/fluxer_rexctl/cmd/upgrade.go
index eb32e02df..c4aaad0b2 100644
--- a/fluxer_rexctl/cmd/upgrade.go
+++ b/fluxer_rexctl/cmd/upgrade.go
@@ -373,6 +373,18 @@ func upgradeStack(ctx context.Context, app *App, inst *instance.Instance, e *eng
clearUpgrade(inst)
return ui.Wrap(err, "This rexctl has no Fluxer release built in.", "Download rexctl again, then run rexctl upgrade.")
}
+ merged, err := instance.DropMergedShards(inst.Dir)
+ if err != nil {
+ clearUpgrade(inst)
+ return ui.Wrap(err, fmt.Sprintf("Couldn't update %s.", instance.CustomCompose), "Check that you can write to it, then run rexctl upgrade again.")
+ }
+ if len(merged) > 0 {
+ bases := make([]string, len(merged))
+ for i, name := range merged {
+ bases[i] = strings.TrimSuffix(name, "-shard")
+ }
+ app.UI.Info("Removed %s from %s. Fluxer %s runs them inside %s, which already have the same changes.", ui.JoinAnd(merged), instance.CustomCompose, toShort, ui.JoinAnd(bases))
+ }
if retired := instance.RetiredService(inst.Dir); retired != "" {
clearUpgrade(inst)
return ui.Refused(fmt.Sprintf("%s changes %s, which Fluxer %s no longer runs.", instance.CustomCompose, retired, toShort), retiredFix(retired, fromShort))
diff --git a/fluxer_rexctl/internal/instance/import.go b/fluxer_rexctl/internal/instance/import.go
index 248ebb2fe..03f4374eb 100644
--- a/fluxer_rexctl/internal/instance/import.go
+++ b/fluxer_rexctl/internal/instance/import.go
@@ -11,6 +11,7 @@ import (
"maps"
"os"
"path/filepath"
+ "reflect"
"slices"
"strconv"
"strings"
@@ -20,6 +21,7 @@ import (
"go.yaml.in/yaml/v4"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/secrets"
"github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
)
@@ -541,6 +543,95 @@ func RetiredService(dir string) string {
return ""
}
+func DropMergedShards(dir string) ([]string, error) {
+ path := filepath.Join(dir, CustomCompose)
+ data, err := os.ReadFile(path)
+ if errors.Is(err, fs.ErrNotExist) {
+ return nil, nil
+ }
+ if err != nil {
+ return nil, err
+ }
+ out, dropped := withoutMergedShards(data)
+ if len(dropped) == 0 {
+ return nil, nil
+ }
+ return dropped, envfile.WriteFileAtomic(path, out, false)
+}
+
+func withoutMergedShards(data []byte) ([]byte, []string) {
+ var doc yaml.Node
+ var before map[string]any
+ if yaml.Unmarshal(data, &doc) != nil || yaml.Unmarshal(data, &before) != nil || len(doc.Content) == 0 {
+ return nil, nil
+ }
+ root := doc.Content[0]
+ if root.Kind != yaml.MappingNode {
+ return nil, nil
+ }
+ var services *yaml.Node
+ for i := 0; i+1 < len(root.Content); i += 2 {
+ if root.Content[i].Value == "services" {
+ services = root.Content[i+1]
+ }
+ }
+ decoded, _ := before["services"].(map[string]any)
+ if services == nil || services.Kind != yaml.MappingNode || services.Style&yaml.FlowStyle != 0 || decoded == nil {
+ return nil, nil
+ }
+ lines := strings.Split(string(data), "\n")
+ remove := make([]bool, len(lines))
+ var dropped []string
+ for i := 0; i+1 < len(services.Content); i += 2 {
+ key := services.Content[i]
+ base, ok := strings.CutSuffix(key.Value, "-shard")
+ if !ok || !slices.Contains(stack.Services, base) || slices.Contains(stack.Services, key.Value) {
+ continue
+ }
+ shard, have := decoded[key.Value]
+ if want, ok := decoded[base]; !have || !ok || !reflect.DeepEqual(shard, want) {
+ continue
+ }
+ start, end := key.Line-1, key.Line
+ for end < len(lines) && (strings.TrimSpace(lines[end]) == "" || indent(lines[end]) >= key.Column) {
+ end++
+ }
+ for end > start+1 && strings.TrimSpace(lines[end-1]) == "" {
+ end--
+ }
+ for j := start; j < end; j++ {
+ remove[j] = true
+ }
+ dropped = append(dropped, key.Value)
+ }
+ if len(dropped) == 0 {
+ return nil, nil
+ }
+ kept := make([]string, 0, len(lines))
+ for i, l := range lines {
+ if !remove[i] {
+ kept = append(kept, l)
+ }
+ }
+ out := []byte(strings.Join(kept, "\n"))
+ var after map[string]any
+ if yaml.Unmarshal(out, &after) != nil {
+ return nil, nil
+ }
+ for _, name := range dropped {
+ delete(decoded, name)
+ }
+ if !reflect.DeepEqual(before, after) {
+ return nil, nil
+ }
+ slices.Sort(dropped)
+ return out, dropped
+}
+
+func indent(line string) int {
+ return len(line) - len(strings.TrimLeft(line, " "))
+}
+
func readCustom(dir string) []byte {
data, _ := os.ReadFile(filepath.Join(dir, CustomCompose))
return data
diff --git a/fluxer_rexctl/internal/instance/import_test.go b/fluxer_rexctl/internal/instance/import_test.go
new file mode 100644
index 000000000..5b6073796
--- /dev/null
+++ b/fluxer_rexctl/internal/instance/import_test.go
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package instance
+
+import (
+ "os"
+ "path/filepath"
+ "slices"
+ "testing"
+)
+
+func TestDropMergedShards(t *testing.T) {
+ dir := t.TempDir()
+ in := `x-local-env: &local-env
+ FLUXER_S3_ENDPOINT: http://storage:8333
+
+services:
+ # Keep SeaweedFS from starting
+ seaweedfs:
+ profiles: ["seaweedfs-disabled"]
+ api:
+ environment: *local-env
+ depends_on:
+ seaweedfs-init: !reset null
+ gifs:
+ environment: *local-env
+ gifs-shard:
+ environment: *local-env
+
+ # messages
+ messages:
+ environment: *local-env
+ messages-shard:
+ environment:
+ FLUXER_S3_ENDPOINT: http://storage:8333
+ users-shard:
+ environment:
+ OTHER: "1"
+ unfurl:
+ environment: *local-env
+ unfurl-shard: {environment: *local-env}
+`
+ want := `x-local-env: &local-env
+ FLUXER_S3_ENDPOINT: http://storage:8333
+
+services:
+ # Keep SeaweedFS from starting
+ seaweedfs:
+ profiles: ["seaweedfs-disabled"]
+ api:
+ environment: *local-env
+ depends_on:
+ seaweedfs-init: !reset null
+ gifs:
+ environment: *local-env
+
+ # messages
+ messages:
+ environment: *local-env
+ users-shard:
+ environment:
+ OTHER: "1"
+ unfurl:
+ environment: *local-env
+`
+ path := filepath.Join(dir, CustomCompose)
+ if err := os.WriteFile(path, []byte(in), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ dropped, err := DropMergedShards(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !slices.Equal(dropped, []string{"gifs-shard", "messages-shard", "unfurl-shard"}) {
+ t.Fatalf("dropped %v", dropped)
+ }
+ got, _ := os.ReadFile(path)
+ if string(got) != want {
+ t.Fatalf("got\n%s", got)
+ }
+ if got := RetiredService(dir); got != "users-shard" {
+ t.Fatalf("retired %q", got)
+ }
+}
+
+func TestDropMergedShardsKeepsSharedAnchor(t *testing.T) {
+ dir := t.TempDir()
+ in := `services:
+ gifs-shard: &shared
+ environment:
+ A: "1"
+ gifs: *shared
+`
+ path := filepath.Join(dir, CustomCompose)
+ if err := os.WriteFile(path, []byte(in), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ if dropped, err := DropMergedShards(dir); err != nil || dropped != nil {
+ t.Fatalf("dropped %v, %v", dropped, err)
+ }
+ if got, _ := os.ReadFile(path); string(got) != in {
+ t.Fatalf("changed to\n%s", got)
+ }
+}