gunk-dev/the-valley

self_update.go

source4cbb5ec9c5dbmain's published tip history
5.5 KiB
1package cmd
2
3import (
4 "fmt"
5 "os"
6
7 "the-valley/flow/internal/config"
8 "the-valley/flow/internal/location"
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 (~/.valley/flow/config.json where it is, else
19~/.klaus/config.json) has a self_update block (a repo's config 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 its state file: whichever of
56 $XDG_STATE_HOME/klaus/self-update.json and
57 ~/.valley/flow/self-update.json exists, else the one the host's
58 naming_generation names (the second under bridge).
59
60Only the configured branch installs, pinned to the commit ls-remote returned,
61and only after it builds and passes the smoke test. A profile holding flow
62as both klaus and valley-flow is refused, changing nothing, by every run
63and by --rollback, --pin and --unpin, before anything else.
64
65--rollback puts the previous store path back in place of the element
66holding flow, so it returns as the element it was taken from; a second
67--rollback returns to the newer one. --pin leaves the current install alone
68until --unpin:
69runs while pinned exit 0 with a note, so a bad tip isn't reinstalled while
70it gets fixed.
71
72A running flow dashboard notices the new build on PATH and restarts onto it.`,
73 Args: cobra.NoArgs,
74 RunE: func(cmd *cobra.Command, args []string) error {
75 cmd.SilenceUsage = true
76 retry, _ := cmd.Flags().GetBool("retry")
77 rollback, _ := cmd.Flags().GetBool("rollback")
78 pin, _ := cmd.Flags().GetBool("pin")
79 unpin, _ := cmd.Flags().GetBool("unpin")
80 verbose, _ := cmd.Flags().GetBool("verbose")
81
82 l, err := location.Host()
83 if err != nil {
84 return err
85 }
86 // Host config only: a repo's config must not choose what installs.
87 cfg, err := config.Load("")
88 if err != nil {
89 return err
90 }
91 u := &selfupdate.Updater{Locks: l.SelfUpdateLocks(), StateFile: l.SelfUpdateState, Version: version, Out: os.Stdout, Verbose: verbose, Branch: "main"}
92 if su := cfg.SelfUpdate; su != nil {
93 u.Repo, u.Branch = su.Repo, su.BranchName()
94 }
95 switch {
96 case rollback:
97 return u.Rollback(cmd.Context())
98 case pin || unpin:
99 return u.Pin(cmd.Context(), pin)
100 }
101 if u.Repo == "" {
102 where, err := l.HostConfig()
103 if err != nil {
104 return err
105 }
106 return fmt.Errorf(`self-update is off: add a self_update block to %s, e.g.
107 "self_update": {"repo": "git@valley-host:~/the-valley", "branch": "main"}`, where)
108 }
109 if err := u.Update(cmd.Context(), retry); err != nil {
110 return fmt.Errorf("self-update: %w", err)
111 }
112 return nil
113 },
114}
115
116func init() {
117 selfUpdateCmd.Flags().BoolP("verbose", "v", false, "also say so when flow is up to date")
118 selfUpdateCmd.Flags().Bool("retry", false, "build the branch's tip even if it failed before")
119 selfUpdateCmd.Flags().Bool("rollback", false, "reinstall the previous build; again to return to the newer one")
120 selfUpdateCmd.Flags().Bool("pin", false, "leave the current install alone until --unpin")
121 selfUpdateCmd.Flags().Bool("unpin", false, "let self-update install the branch's tip again")
122 selfUpdateCmd.MarkFlagsMutuallyExclusive("retry", "rollback", "pin", "unpin")
123 rootCmd.AddCommand(selfUpdateCmd)
124}