Skip to content

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

TypeScript fluxerfluxerapp

feat(rexctl): build deploy specs and resolve published ports

Hampus6b7c568ea83f
diff --git a/fluxer_rexctl/internal/deploy/admin.go b/fluxer_rexctl/internal/deploy/admin.go
new file mode 100644
index 000000000..16b5a4599
--- /dev/null
+++ b/fluxer_rexctl/internal/deploy/admin.go
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package deploy
+
+import (
+ "context"
+ "maps"
+ "slices"
+ "strings"
+ "time"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+)
+
+func SendAdminImport(ctx context.Context, inst *instance.Instance, api *adminapi.Client) error {
+ ai := inst.State.AdminImport
+ if ai == nil || ai.SentAt != "" {
+ return nil
+ }
+ if len(ai.Values) > 0 {
+ schema, err := adminapi.ConfigSchema()
+ if err != nil {
+ return err
+ }
+ current, err := api.RawConfig(ctx)
+ if err != nil {
+ return err
+ }
+ patch := map[string]any{}
+ for _, name := range slices.Sorted(maps.Keys(ai.Values)) {
+ k, err := adminapi.FindConfigKey(schema, name)
+ if err != nil || isSet(current, k) {
+ continue
+ }
+ value, err := adminapi.ParseConfigValue(k, ai.Values[name])
+ if err != nil {
+ continue
+ }
+ adminapi.MergePatch(patch, adminapi.ConfigPatch(k.Key, value))
+ }
+ if len(patch) > 0 {
+ if err := api.PatchConfig(ctx, patch); err != nil {
+ return err
+ }
+ }
+ }
+ ai.SentAt = time.Now().UTC().Format(time.RFC3339)
+ return save(inst)
+}
+
+func isSet(cfg map[string]any, k adminapi.ConfigKey) bool {
+ path := k.Key
+ if k.Type == "secret" {
+ path = k.SecretSet
+ }
+ var cur any = cfg
+ for _, p := range strings.Split(path, ".") {
+ obj, ok := cur.(map[string]any)
+ if !ok {
+ return false
+ }
+ if cur, ok = obj[p]; !ok {
+ return false
+ }
+ }
+ switch v := cur.(type) {
+ case nil:
+ return false
+ case bool:
+ return k.Type != "secret" || v
+ case string:
+ return v != ""
+ case []any:
+ return len(v) > 0
+ case map[string]any:
+ return len(v) > 0
+ }
+ return true
+}
diff --git a/fluxer_rexctl/internal/deploy/api.go b/fluxer_rexctl/internal/deploy/api.go
new file mode 100644
index 000000000..a29d915d9
--- /dev/null
+++ b/fluxer_rexctl/internal/deploy/api.go
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package deploy
+
+import (
+ "context"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+)
+
+type ApplyOptions struct {
+ Pull bool
+ Wait bool
+ Confirm func(Preview) bool
+ OnEvent func(engine.Event)
+ Failed func(*engine.Project, error)
+}
+
+type Preview struct {
+ Keys []string
+ Promised []string
+ Restarts []string
+ Removes []string
+ Networks []string
+ Unchanged bool
+}
+
+type Change struct {
+ Keys []string
+ Restarted []string
+ Removed []string
+ Applied bool
+ Reverted bool
+ Unchanged bool
+}
diff --git a/fluxer_rexctl/internal/deploy/ports.go b/fluxer_rexctl/internal/deploy/ports.go
new file mode 100644
index 000000000..0cdb82a63
--- /dev/null
+++ b/fluxer_rexctl/internal/deploy/ports.go
@@ -0,0 +1,112 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package deploy
+
+import (
+ "fmt"
+ "maps"
+ "slices"
+ "strconv"
+ "strings"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+type portVar struct {
+ name string
+ def int
+ protos []string
+}
+
+var portVars = []portVar{
+ {"FLUXER_HTTP_PORT", 80, []string{"tcp"}},
+ {"FLUXER_HTTPS_PORT", 443, []string{"tcp", "udp"}},
+ {"FLUXER_LIVEKIT_TCP_PORT", 7881, []string{"tcp"}},
+ {"FLUXER_LIVEKIT_UDP_PORT", 7882, []string{"udp"}},
+}
+
+func portOf(env map[string]string, v portVar) int {
+ if n, err := strconv.Atoi(strings.TrimSpace(env[v.name])); err == nil && n > 0 && n <= 65535 {
+ return n
+ }
+ return v.def
+}
+
+func portDiff(oldEnv, newEnv map[string]string) (added, removed []host.Port) {
+ for _, v := range portVars {
+ was, will := portOf(oldEnv, v), portOf(newEnv, v)
+ if was == will {
+ continue
+ }
+ for _, proto := range v.protos {
+ added = append(added, host.Port{Number: will, Proto: proto})
+ removed = append(removed, host.Port{Number: was, Proto: proto})
+ }
+ }
+ return added, removed
+}
+
+func PortChanges(inst *instance.Instance, changes map[string]*string) (opened, closed []host.Port, err error) {
+ rs, err := ReleaseSet(inst)
+ if err != nil {
+ return nil, nil, err
+ }
+ next := maps.Clone(inst.State.Settings)
+ for name, v := range changes {
+ if v == nil {
+ delete(next, name)
+ } else {
+ next[name] = *v
+ }
+ }
+ oldEnv, err := Environment(rs, inst.State, inst.State.Settings)
+ if err != nil {
+ return nil, nil, err
+ }
+ newEnv, err := Environment(rs, inst.State, next)
+ if err != nil {
+ return nil, nil, err
+ }
+ opened, closed = portDiff(oldEnv, newEnv)
+ return opened, closed, nil
+}
+
+func checkPortsFree(ports []host.Port) error {
+ seen := map[host.Port]bool{}
+ for _, p := range ports {
+ if seen[p] {
+ continue
+ }
+ seen[p] = true
+ busy, owner, err := host.PortInUse(p.Proto, p.Number)
+ if err != nil || !busy {
+ continue
+ }
+ msg := fmt.Sprintf("Port %d is already in use on this machine.", p.Number)
+ if owner != "" {
+ msg = fmt.Sprintf("Port %d is already in use by %s.", p.Number, owner)
+ }
+ return ui.Refused(msg, "Pick another port, or stop what uses it, then try again.")
+ }
+ return nil
+}
+
+func PortList(ports []host.Port) string {
+ var nums []int
+ for _, p := range ports {
+ if !slices.Contains(nums, p.Number) {
+ nums = append(nums, p.Number)
+ }
+ }
+ slices.Sort(nums)
+ parts := make([]string, len(nums))
+ for i, n := range nums {
+ parts[i] = strconv.Itoa(n)
+ }
+ if len(parts) < 2 {
+ return strings.Join(parts, "")
+ }
+ return strings.Join(parts[:len(parts)-1], ", ") + " and " + parts[len(parts)-1]
+}
diff --git a/fluxer_rexctl/internal/deploy/resolve.go b/fluxer_rexctl/internal/deploy/resolve.go
new file mode 100644
index 000000000..1c55d0f2d
--- /dev/null
+++ b/fluxer_rexctl/internal/deploy/resolve.go
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package deploy
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"
+)
+
+func Interrupted(inst *instance.Instance) string {
+ p := inst.State.Pending
+ if p == nil {
+ return ""
+ }
+ if len(p.Keys) == 1 {
+ return fmt.Sprintf("The change to %s didn't finish. rexctl put the old value back.", p.Keys[0])
+ }
+ if len(p.Keys) == 0 {
+ return "The last settings change didn't finish. rexctl put the old values back."
+ }
+ return fmt.Sprintf("The change to %s and %s didn't finish. rexctl put the old values back.",
+ strings.Join(p.Keys[:len(p.Keys)-1], ", "), p.Keys[len(p.Keys)-1])
+}
+
+func resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) error {
+ p := inst.State.Pending
+ if p == nil {
+ return nil
+ }
+ rs, err := ReleaseSet(inst)
+ if err != nil {
+ return err
+ }
+ ipv6 := parseBool(p.Settings[settings.IPv6]) != parseBool(inst.State.Settings[settings.IPv6])
+ if _, err := render(inst, rs, inst.State.Settings); err != nil {
+ return err
+ }
+ if err := save(inst); err != nil {
+ return err
+ }
+ if ipv6 {
+ if err := e.Named(inst.Project(), inst.Dir).Down(context.WithoutCancel(ctx), false); err != nil {
+ return err
+ }
+ }
+ if !inst.State.Stopped {
+ s, err := specFor(inst, rs, inst.State.Settings)
+ if err != nil {
+ return err
+ }
+ proj, err := e.Load(ctx, s)
+ if err != nil {
+ return err
+ }
+ if err := up(ctx, proj, true, nil); err != nil {
+ return err
+ }
+ }
+ inst.State.Pending = nil
+ return save(inst)
+}
diff --git a/fluxer_rexctl/internal/deploy/spec.go b/fluxer_rexctl/internal/deploy/spec.go
new file mode 100644
index 000000000..79b9d0cd2
--- /dev/null
+++ b/fluxer_rexctl/internal/deploy/spec.go
@@ -0,0 +1,182 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package deploy
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "errors"
+ "fmt"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+const upgradeFix = "Run rexctl upgrade to put this instance on the release this rexctl knows."
+
+func ReleaseSet(inst *instance.Instance) (*stack.Set, error) {
+ set, err := inst.Set()
+ if errors.Is(err, instance.ErrNoReleaseSet) {
+ return nil, &ui.ExitError{Code: ui.ExitRefused, Err: err,
+ Msg: fmt.Sprintf("rexctl can't find the files of Fluxer %s on this machine.", inst.State.Release),
+ Fix: upgradeFix}
+ }
+ if err != nil {
+ return nil, ui.Wrap(err, fmt.Sprintf("rexctl can't read the files of Fluxer %s.", inst.State.Release), upgradeFix)
+ }
+ return set, nil
+}
+
+func CustomFile(inst *instance.Instance) ([]byte, error) {
+ data, err := inst.Custom()
+ if err != nil {
+ return nil, ui.Wrap(err, fmt.Sprintf("rexctl can't read %s.", instance.CustomCompose), "Check that you can read it, then try again.")
+ }
+ return data, nil
+}
+
+func adminImportValues(s *instance.State) map[string]string {
+ if s.AdminImport == nil {
+ return nil
+ }
+ return s.AdminImport.Values
+}
+
+func Environment(set *stack.Set, s *instance.State, chosen map[string]string) (map[string]string, error) {
+ env, err := settings.Effective(set, chosen, s.Secrets, adminImportValues(s))
+ if err != nil {
+ var se *settings.Error
+ if errors.As(err, &se) {
+ return nil, &ui.ExitError{Code: ui.ExitRefused, Msg: se.Problem, Fix: se.Fix, Err: err}
+ }
+ return nil, err
+ }
+ return env, nil
+}
+
+func composeFiles(inst *instance.Instance, chosen map[string]string) []string {
+ files := stack.ComposeFiles(inst.Dir, stack.OverlaysFor(chosen))
+ if _, err := os.Stat(inst.Path(instance.CustomCompose)); err == nil {
+ files = append(files, inst.Path(instance.CustomCompose))
+ }
+ return files
+}
+
+func specFor(inst *instance.Instance, set *stack.Set, chosen map[string]string) (engine.Spec, error) {
+ env, err := Environment(set, inst.State, chosen)
+ if err != nil {
+ return engine.Spec{}, err
+ }
+ return engine.Spec{Name: inst.Project(), Dir: inst.Dir, Files: composeFiles(inst, chosen), Env: env}, nil
+}
+
+func spec(inst *instance.Instance) (engine.Spec, error) {
+ set, err := ReleaseSet(inst)
+ if err != nil {
+ return engine.Spec{}, err
+ }
+ return specFor(inst, set, inst.State.Settings)
+}
+
+func load(ctx context.Context, inst *instance.Instance, e *engine.Engine) (*engine.Project, error) {
+ s, err := spec(inst)
+ if err != nil {
+ return nil, err
+ }
+ return e.Load(ctx, s)
+}
+
+func render(inst *instance.Instance, set *stack.Set, chosen map[string]string) (*stack.Rendered, error) {
+ r, err := stack.Render(set, stack.OverlaysFor(chosen), inst.State.Images.Refs, inst.Project(), inst.Dir)
+ if err != nil {
+ return nil, ui.Wrap(err, "Couldn't write Fluxer's files.", "Check that you can write to "+inst.Dir+", then try again.")
+ }
+ inst.State.Rendered = r.Hashes
+ return r, nil
+}
+
+func save(inst *instance.Instance) error {
+ if err := inst.Save(); err != nil {
+ return ui.Wrap(err, "Couldn't save your settings.", "Check that you can write to "+inst.Dir+", then try again.")
+ }
+ return nil
+}
+
+func mountedHashes(inst *instance.Instance) (map[string]string, error) {
+ out := map[string]string{}
+ for _, dir := range stack.MountedDirs {
+ h := sha256.New()
+ root := inst.Path(dir)
+ err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ if errors.Is(err, fs.ErrNotExist) && path == root {
+ return fs.SkipAll
+ }
+ return err
+ }
+ if d.IsDir() {
+ return nil
+ }
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return err
+ }
+ rel, _ := filepath.Rel(root, path)
+ fmt.Fprintf(h, "%s\x00%d\x00", filepath.ToSlash(rel), len(data))
+ h.Write(data)
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+ out[dir] = "sha256:" + hex.EncodeToString(h.Sum(nil))
+ }
+ return out, nil
+}
+
+func checkDrift(inst *instance.Instance, withEnv bool) error {
+ r, err := instance.Drift(inst)
+ if err != nil {
+ return ui.Wrap(err, "rexctl can't check Fluxer's files.", "Check that you can read "+inst.Dir+", then try again.")
+ }
+ if !withEnv {
+ r.Env = nil
+ }
+ var de *instance.DriftError
+ if err := r.Err(); errors.As(err, &de) {
+ fix := de.Fix
+ if len(de.Lines) > 0 {
+ fix += "\n" + strings.Join(de.Lines, "\n")
+ }
+ return &ui.ExitError{Code: ui.ExitRefused, Msg: de.Msg, Fix: fix, Err: err}
+ } else if err != nil {
+ return err
+ }
+ return nil
+}
+
+func refuseEjected(inst *instance.Instance) error {
+ var ee *instance.EjectedError
+ if err := inst.Managed(); errors.As(err, &ee) {
+ return &ui.ExitError{Code: ui.ExitRefused, Msg: ee.Error(), Fix: ee.Fix(), Err: err}
+ }
+ return nil
+}
+
+func refuseUpgrade(inst *instance.Instance) error {
+ if inst.State.Upgrade == nil {
+ return nil
+ }
+ return ui.Refused("An upgrade of this instance hasn't finished.", "Run rexctl upgrade to finish it, then try again.")
+}
+
+func now() time.Time { return time.Now().UTC().Truncate(time.Second) }