gunk-dev/the-valley

self_update.go

sourcec14b0e68e9c3main's published tip history
5.2 KiB
1package cmd
2
3import (
4 "errors"
5 "fmt"
6 "os"
7
8 "the-valley/flow/internal/config"
9 "the-valley/flow/internal/selfupdate"
10 "github.com/spf13/cobra"
11)
12
13var selfUpdateCmd = &cobra.Command{
14 Use: "self-update",
15 Short: "Install the tip of the configured branch into the nix profile",
16 Long: `Brings the installed flow up to the tip of a configured branch, for a host
17whose install should follow upstream without a system rebuild. It is off until
18the host config, ~/.klaus/config.json, has a self_update block (a repo's
19.klaus/config.json can't set it):
20
21 "self_update": {"repo": "git@valley-host:~/the-valley", "branch": "main"}
22
23repo is an https://, ssh://, git:// or file:// URL, an absolute path, or
24user@host:path, read as ssh://user@host/path (write user@host:~/path for
25a path in your home on a plain server). Any other transport (ext::, fd::)
26and any character outside [A-Za-z0-9._~/:@-], which nix's flake-ref parser
27would re-encode, is refused; git ls-remote and nix
28read the repo by the same URL, and the git that self-update runs, directly or
29through nix, is limited to those transports (GIT_ALLOW_PROTOCOL). branch
30defaults to main. Each run:
31 1. takes a lock under $XDG_STATE_HOME/klaus, so runs never overlap: a run
32 that finds another in progress exits 0 and says so;
33 2. resolves the branch's commit with git ls-remote;
34 3. exits 0 if the installed flow was built from that commit (quietly;
35 -v says so);
36 4. reads the names of the packages the flake offers at that commit, and
37 refuses, changing nothing, unless it offers packages.<system>.klaus, as
38 the-valley's does. Point self_update.repo at a repository whose flake
39 offers it and run self-update again: the run builds, smoke-tests and
40 installs it as below;
41 5. exits 0 if that commit already failed to build, test or install (it is
42 retried when the branch moves, or with --retry);
43 6. builds exactly that commit's package, never the flake's default package:
44 nix build 'git+<repo>?ref=refs/heads/<branch>&rev=<sha>#packages.<system>.klaus';
45 7. smoke-tests the build: its klaus --version must report the commit, and
46 its klaus status must exit 0. A failure records the commit as failed,
47 installs nothing, and exits non-zero;
48 8. installs it into the user's default nix profile in place of the element
49 holding flow, klaus or valley-flow, leaving other elements alone. If the
50 build does not go in, the element gets its store path back and the
51 commit is recorded as failed; if that fails too, the profile holds no
52 flow, and the state records no install and no failure, so the next run
53 installs the tip. Any system install stays underneath as the fallback;
54 9. records the current and previous store paths, commits and profile
55 elements, the time and any failure in
56 $XDG_STATE_HOME/klaus/self-update.json.
57
58Only the configured branch installs, pinned to the commit ls-remote returned,
59and only after it builds and passes the smoke test. A profile holding flow
60as both klaus and valley-flow is refused, changing nothing, by every run
61and by --rollback, --pin and --unpin, before anything else.
62
63--rollback puts the previous store path back in place of the element
64holding flow, so it returns as the element it was taken from; a second
65--rollback returns to the newer one. --pin leaves the current install alone
66until --unpin:
67runs while pinned exit 0 with a note, so a bad tip isn't reinstalled while
68it gets fixed.
69
70A running flow dashboard notices the new build on PATH and restarts onto it.`,
71 Args: cobra.NoArgs,
72 RunE: func(cmd *cobra.Command, args []string) error {
73 cmd.SilenceUsage = true
74 retry, _ := cmd.Flags().GetBool("retry")
75 rollback, _ := cmd.Flags().GetBool("rollback")
76 pin, _ := cmd.Flags().GetBool("pin")
77 unpin, _ := cmd.Flags().GetBool("unpin")
78 verbose, _ := cmd.Flags().GetBool("verbose")
79
80 dir, err := selfupdate.DefaultDir()
81 if err != nil {
82 return err
83 }
84 // Host config only: a repo's config must not choose what installs.
85 cfg, err := config.Load("")
86 if err != nil {
87 return err
88 }
89 u := &selfupdate.Updater{Dir: dir, Version: version, Out: os.Stdout, Verbose: verbose, Branch: "main"}
90 if su := cfg.SelfUpdate; su != nil {
91 u.Repo, u.Branch = su.Repo, su.BranchName()
92 }
93 switch {
94 case rollback:
95 return u.Rollback(cmd.Context())
96 case pin || unpin:
97 return u.Pin(cmd.Context(), pin)
98 }
99 if u.Repo == "" {
100 return errors.New(`self-update is off: add a self_update block to ~/.klaus/config.json, e.g.
101 "self_update": {"repo": "git@valley-host:~/the-valley", "branch": "main"}`)
102 }
103 if err := u.Update(cmd.Context(), retry); err != nil {
104 return fmt.Errorf("self-update: %w", err)
105 }
106 return nil
107 },
108}
109
110func init() {
111 selfUpdateCmd.Flags().BoolP("verbose", "v", false, "also say so when flow is up to date")
112 selfUpdateCmd.Flags().Bool("retry", false, "build the branch's tip even if it failed before")
113 selfUpdateCmd.Flags().Bool("rollback", false, "reinstall the previous build; again to return to the newer one")
114 selfUpdateCmd.Flags().Bool("pin", false, "leave the current install alone until --unpin")
115 selfUpdateCmd.Flags().Bool("unpin", false, "let self-update install the branch's tip again")
116 selfUpdateCmd.MarkFlagsMutuallyExclusive("retry", "rollback", "pin", "unpin")
117 rootCmd.AddCommand(selfUpdateCmd)
118}