diff --git a/internal/deploy/reach.go b/internal/deploy/reach.go index dc67c9f..85776a5 100644 --- a/internal/deploy/reach.go +++ b/internal/deploy/reach.go @@ -18,17 +18,6 @@ import ( "interzone/internal/config" ) -// reachChecks looks at the server from here, as the internet would: whether -// the TCP ports the components need get through (UDP can't be told from -// silence), and whether other servers can federate with it. -func reachChecks(ctx context.Context, c *config.Config) []Finding { - if c.Domain == "" { - return nil - } - out := append(portChecks(ctx, c), federationChecks(ctx, c)...) - return append(out, clientChecks(ctx, c)...) -} - // portChecks connects to every TCP port Ports lists, at the homeserver's // hostname. Refused means the port got through to a server with nothing // listening on it; no answer at all usually means a firewall drops it. diff --git a/internal/deploy/server.go b/internal/deploy/server.go index 424334d..e52e9b2 100644 --- a/internal/deploy/server.go +++ b/internal/deploy/server.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "os/exec" + "slices" "sort" "strconv" "strings" @@ -100,12 +101,46 @@ func probe(ctx context.Context, c *config.Config) (map[string]string, error) { func ServerChecks(c *config.Config) []Finding { ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) defer cancel() + return CheckServer(ctx, c, nil) +} +// CheckServer runs ServerChecks' checks until ctx ends. step, if set, is +// told what's found so far each time a stage finishes, and which stage +// runs next ("" once all have). +func CheckServer(ctx context.Context, c *config.Config, step func(sofar []Finding, next string)) []Finding { var out []Finding add := func(name string, l Level, format string, a ...any) { out = append(out, Finding{Group: "Server", Name: name, Level: l, Detail: fmt.Sprintf(format, a...)}) } + var ips []string + // The checks after the server's own, each its own stage: from here, + // as the internet sees it (UDP ports can't be told from silence). + rest := func() []Finding { + for _, st := range []struct { + name string + run func() []Finding + }{ + {"DNS", func() []Finding { return dnsChecks(ctx, c, ips) }}, + {"ports", func() []Finding { return portChecks(ctx, c) }}, + {"federation", func() []Finding { return federationChecks(ctx, c) }}, + {"apps' view", func() []Finding { return clientChecks(ctx, c) }}, + } { + if step != nil { + step(slices.Clone(out), st.name) + } + if c.Domain != "" || st.name == "DNS" { + out = append(out, st.run()...) + } + } + if step != nil { + step(slices.Clone(out), "") + } + return out + } + if step != nil { + step(nil, "the server") + } facts, err := probe(ctx, c) where := "this machine" if c.Target.Mode == "ssh" { @@ -113,7 +148,7 @@ func ServerChecks(c *config.Config) []Finding { } if err != nil { add("Connect", Fail, "can't run commands on %s: %s", where, err) - return append(append(out, dnsChecks(ctx, c, nil)...), reachChecks(ctx, c)...) + return rest() } add("Connect", Pass, "%s", where) @@ -207,8 +242,8 @@ func ServerChecks(c *config.Config) []Finding { } out = append(out, mirrorChecks(ctx, c, facts)...) - out = append(out, dnsChecks(ctx, c, strings.Fields(facts["ips"]))...) - return append(out, reachChecks(ctx, c)...) + ips = strings.Fields(facts["ips"]) + return rest() } // mirrorChecks makes sure the blackout mirrors carry this server's distro diff --git a/internal/ui/review_scroll_test.go b/internal/ui/review_scroll_test.go index 4995f61..4ef1fb8 100644 --- a/internal/ui/review_scroll_test.go +++ b/internal/ui/review_scroll_test.go @@ -58,3 +58,28 @@ func TestReviewConfirms(t *testing.T) { t.Error("y didn't deploy") } } + +// The checks show up stage by stage, and c stops them, keeping what's +// found so far. +func TestReviewChecksStream(t *testing.T) { + a := demoApp(t, 120, 45) + a.jump(a.index("Review")) + s := a.cur().(*reviewScreen) + if !s.checking { + t.Fatal("Review didn't start its checks") + } + ch := make(chan checksPartMsg, 4) + ch <- checksPartMsg{gen: s.gen, findings: []deploy.Finding{{Group: "Server", Name: "Connect", Level: deploy.Fail, Detail: "no"}}, next: "federation"} + s.update(checksPartMsg{gen: s.gen, next: "DNS", ch: ch}) + if cmd := s.update(waitChecks(ch)()); cmd == nil || len(s.checks) != 1 || !strings.Contains(s.view(92, 40), "checking federation…") { + t.Fatalf("a stage didn't show:\n%s", s.view(92, 40)) + } + s.update(runes("c")) + if s.checking || len(s.checks) != 1 || !strings.Contains(s.view(92, 40), "stopped") { + t.Errorf("c didn't stop the checks:\n%s", s.view(92, 40)) + } + s.update(checksPartMsg{gen: s.gen - 1, next: ""}) // the stopped run, finishing late + if len(s.checks) != 1 { + t.Error("a stopped run's results replaced the ones kept") + } +} diff --git a/internal/ui/screens_deploy.go b/internal/ui/screens_deploy.go index 94ffd89..0437c49 100644 --- a/internal/ui/screens_deploy.go +++ b/internal/ui/screens_deploy.go @@ -24,7 +24,25 @@ import ( type startDeployMsg struct{ opts deploy.Options } -type serverChecksMsg []deploy.Finding +// checksPartMsg is what the checks found so far, and the stage running now +// ("" when they're done). +type checksPartMsg struct { + gen int + findings []deploy.Finding + next string + ch chan checksPartMsg +} + +func waitChecks(ch chan checksPartMsg) tea.Cmd { + return func() tea.Msg { + m, ok := <-ch + if !ok { + return nil + } + m.ch = ch + return m + } +} type reviewScreen struct { cfg *config.Config @@ -35,6 +53,10 @@ type reviewScreen struct { yaml string checks []deploy.Finding checking bool + stage string // the checks' stage running now + since time.Time // when it started + stop context.CancelFunc // c stops the checks + gen int verbose bool // list passed checks too confirm bool // enter was pressed: y deploys spin spinner.Model @@ -63,7 +85,11 @@ func (s *reviewScreen) keys() [][2]string { if s.confirm { return [][2]string{{"y", "deploy now"}, {"esc", "not yet"}} } - k := [][2]string{{"c", "check again"}, {"v", "details"}, {"z", map[bool]string{false: "DNS records", true: "vars.yml"}[s.zone != nil]}, {"↑↓", "scroll"}, {"s", "save files only"}} + again := "check again" + if s.checking { + again = "stop checking" + } + k := [][2]string{{"c", again}, {"v", "details"}, {"z", map[bool]string{false: "DNS records", true: "vars.yml"}[s.zone != nil]}, {"↑↓", "scroll"}, {"s", "save files only"}} if s.comparable() { k = append(k, [2]string{"x", map[bool]string{true: "whole vars.yml", false: "what changes"}[s.showDiff]}) } @@ -176,18 +202,43 @@ func (s *reviewScreen) runChecks() tea.Cmd { return nil } s.checking = true - cfg := *s.cfg - return tea.Batch(s.spin.Tick, func() tea.Msg { return serverChecksMsg(deploy.ServerChecks(&cfg)) }) + s.gen++ + cfg, gen := *s.cfg, s.gen + ctx, stop := context.WithTimeout(context.Background(), 40*time.Second) + s.stop = stop + s.stage, s.since = "the server", time.Now() + ch := make(chan checksPartMsg, 16) // more than the stages: a stopped run never blocks + return tea.Batch(s.spin.Tick, func() tea.Msg { + go func() { + defer close(ch) + deploy.CheckServer(ctx, &cfg, func(sofar []deploy.Finding, next string) { + ch <- checksPartMsg{gen: gen, findings: sofar, next: next} + }) + }() + return waitChecks(ch)() + }) } func (s *reviewScreen) leave(*config.Config) {} func (s *reviewScreen) update(msg tea.Msg) tea.Cmd { switch m := msg.(type) { - case serverChecksMsg: - s.checking = false - s.checks = m - return nil + case checksPartMsg: + if m.gen != s.gen { + return nil + } + if m.next != s.stage { + s.stage, s.since = m.next, time.Now() + } + if m.findings != nil || m.next == "" { + s.checks = m.findings + } + if m.next == "" { + s.checking = false + s.stop() + return nil + } + return waitChecks(m.ch) case zoneMsg: s.zone, s.preview, s.toPrev = m, strings.Join(m, "\n"), true return nil @@ -217,6 +268,12 @@ func (s *reviewScreen) update(msg tea.Msg) tea.Cmd { case "s": return tea.Sequence(nav(1), func() tea.Msg { return startDeployMsg{deploy.Options{Action: deploy.ActionSaveOnly}} }) case "c": + if s.checking { // stop them: what's found so far stays + s.gen++ + s.checking, s.stage = false, "stopped" + s.stop() + return nil + } return s.runChecks() case "v": s.verbose = !s.verbose @@ -274,7 +331,7 @@ func (s *reviewScreen) confirmLines(w int) []string { if len(failed) > 0 { lines = append(lines, sBad.Render(fmt.Sprintf("✗ %d check(s) failed: %s. The deploy may fail too.", len(failed), strings.Join(failed, ", ")))) } - if s.checking { + if s.checking || s.stage == "stopped" { lines = append(lines, sWarn.Render("! The checks haven't finished.")) } lines = append(lines, "", keyLink("y")+sMuted.Render(" deploy now ")+keyLink("esc")+sMuted.Render(" not yet")) @@ -370,12 +427,16 @@ func (s *reviewScreen) view(w, h int) string { // into one line unless details are on; problems are always spelled out. func (s *reviewScreen) checksView(w int) []string { head := rule("Pre-deploy checks", min(w, 72)) + if s.stage == "stopped" { + head = sSection.Render("Pre-deploy checks") + sWarn.Render(" stopped before the end; ") + keyLink("c") + sMuted.Render(" checks again") + } if s.checking { sep := " " if w < 60 { sep = "\n" } - head = sSection.Render("Pre-deploy checks") + sep + s.spin.View() + sMuted.Render(" checking the server and DNS…") + head = sSection.Render("Pre-deploy checks") + sep + s.spin.View() + + sMuted.Render(fmt.Sprintf(" checking %s… %ds ", s.stage, int(time.Since(s.since).Seconds()))) + keyLink("c") + sMuted.Render(" stop") } lines := []string{head, ""} on := s.cfg.HostServices() diff --git a/internal/ui/testdata/layout/review-120x36.txt b/internal/ui/testdata/layout/review-120x36.txt index 79207ff..4dd3dc6 100644 --- a/internal/ui/testdata/layout/review-120x36.txt +++ b/internal/ui/testdata/layout/review-120x36.txt @@ -17,7 +17,7 @@ │ ▾ Servers ││ │ ✓ Ready to deploy. │ │ │ ● example.org ││ ╰──────────────────────────────────────────────────────────────────────╯ │ │ ││ │ -│ ││ Pre-deploy checks ⠋ checking the server and DNS… │ +│ ││ Pre-deploy checks ⠋ checking the server… 0s c stop │ │ ││ │ │ ││ ○ Synapse matrix.example.org │ │ ││ ○ Element Web element.example.org │ @@ -33,4 +33,4 @@ │ ◐ interzone ctrl+y ││ ▼ 42 more · ↑↓ scroll │ │ ◂ fold ctrl+b menu ││ │ ╰────────────────────────╯╰────────────────────────────────────────────────────────────────────────────────────────────╯ - enter deploy · c check again · v details · z DNS records · ↑↓ scroll · s save files only · esc back · ? all keys \ No newline at end of file + enter deploy · c stop checking · v details · z DNS records · ↑↓ scroll · s save files only · esc back · ? all keys \ No newline at end of file diff --git a/internal/ui/testdata/layout/review-46x40.txt b/internal/ui/testdata/layout/review-46x40.txt index 1731412..8001fa2 100644 --- a/internal/ui/testdata/layout/review-46x40.txt +++ b/internal/ui/testdata/layout/review-46x40.txt @@ -16,7 +16,7 @@ ╰──────────────────────────────────────────╯ Pre-deploy checks - ⠋ checking the server and DNS… + ⠋ checking the server… 0s c stop ○ Synapse matrix.example.org diff --git a/internal/ui/testdata/layout/review-48x20.txt b/internal/ui/testdata/layout/review-48x20.txt index 0d87589..5a61e48 100644 --- a/internal/ui/testdata/layout/review-48x20.txt +++ b/internal/ui/testdata/layout/review-48x20.txt @@ -15,6 +15,6 @@ ╰────────────────────────────────────────────╯ Pre-deploy checks - ⠋ checking the server and DNS… + ⠋ checking the server… 0s c stop ▼ 60 more · ↑↓ scroll ‹ back ≡ menu deploy › ⋯ \ No newline at end of file