Skip to content

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

TypeScript fluxerfluxerapp

fix(rexctl): wait for the boot start before refusing a command (#3723)

Hampus380dce2f9bf6
diff --git a/fluxer_rexctl/cmd/app.go b/fluxer_rexctl/cmd/app.go
index 3e20b3295..2698260db 100644
--- a/fluxer_rexctl/cmd/app.go
+++ b/fluxer_rexctl/cmd/app.go
@@ -14,6 +14,7 @@ import (
"runtime"
"strconv"
"strings"
+ "time"
"github.com/spf13/cobra"
@@ -387,8 +388,13 @@ func (a *App) adminClient(inst *instance.Instance, p *engine.Project) *adminapi.
return c
}
+var bootStarting = host.BootUnitStarting
+
func (a *App) Locked(ctx context.Context, inst *instance.Instance, fn func() error) error {
l, err := inst.Lock(ctx)
+ if errors.Is(err, instance.ErrLocked) && bootStarting(ctx, inst.Project()) {
+ l, err = a.waitForBootStart(ctx, inst)
+ }
if errors.Is(err, instance.ErrLocked) {
return ui.Refused("Another rexctl command is changing this instance.", "Wait for it to finish, then try again.")
}
@@ -398,6 +404,44 @@ func (a *App) Locked(ctx context.Context, inst *instance.Instance, fn func() err
return a.holding(ctx, inst, l, fn)
}
+func (a *App) lockedNow(ctx context.Context, inst *instance.Instance, fn func() error) error {
+ l, err := inst.Lock(ctx)
+ if errors.Is(err, instance.ErrLocked) {
+ return ui.Refused("Another rexctl command is changing this instance.", "Wait for it to finish, then try again.")
+ }
+ if err != nil {
+ return err
+ }
+ return a.holding(ctx, inst, l, fn)
+}
+
+func (a *App) waitForBootStart(ctx context.Context, inst *instance.Instance) (*instance.Lock, error) {
+ var l *instance.Lock
+ err := a.UI.Step(ctx, "Waiting for Fluxer to finish starting", func(ctx context.Context, s *ui.Step) error {
+ deadline := time.Now().Add(bootLockWait)
+ for {
+ got, err := inst.Lock(ctx)
+ if err == nil {
+ l = got
+ s.Done("Fluxer finished starting.")
+ return nil
+ }
+ if !errors.Is(err, instance.ErrLocked) {
+ return err
+ }
+ if time.Now().After(deadline) || !bootStarting(ctx, inst.Project()) {
+ return err
+ }
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-time.After(2 * time.Second):
+ }
+ }
+ })
+ return l, err
+}
+
func (a *App) holding(ctx context.Context, inst *instance.Instance, l *instance.Lock, fn func() error) error {
defer l.Release()
if err := rootlessGuard(inst); err != nil {
diff --git a/fluxer_rexctl/cmd/lifecycle.go b/fluxer_rexctl/cmd/lifecycle.go
index 09cdf5a25..90022543e 100644
--- a/fluxer_rexctl/cmd/lifecycle.go
+++ b/fluxer_rexctl/cmd/lifecycle.go
@@ -683,7 +683,7 @@ func bootLocked(ctx context.Context, app *App, inst *instance.Instance, fn func(
return app.holding(ctx, inst, l, fn)
}
if !errors.Is(err, instance.ErrLocked) || time.Now().After(deadline) {
- return app.Locked(ctx, inst, fn)
+ return app.lockedNow(ctx, inst, fn)
}
select {
case <-ctx.Done():
diff --git a/fluxer_rexctl/internal/host/bootunit.go b/fluxer_rexctl/internal/host/bootunit.go
index d272689c8..0f598edbb 100644
--- a/fluxer_rexctl/internal/host/bootunit.go
+++ b/fluxer_rexctl/internal/host/bootunit.go
@@ -186,6 +186,24 @@ func unitIs(ctx context.Context, rootless bool, name, state string) bool {
return strings.TrimSpace(string(out)) == state
}
+func BootUnitStarting(ctx context.Context, project string) bool {
+ if !BootUnitSupported() {
+ return false
+ }
+ name := project + ".service"
+ for _, user := range []bool{false, true} {
+ args := []string{"is-active", name}
+ if user {
+ args = append([]string{"--user"}, args...)
+ }
+ out, _ := exec.CommandContext(ctx, "systemctl", args...).Output()
+ if strings.TrimSpace(string(out)) == "activating" {
+ return true
+ }
+ }
+ return false
+}
+
func CheckBootUnit(ctx context.Context, u BootUnit) (BootUnitState, error) {
if !BootUnitSupported() {
return BootUnitState{}, nil