| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 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 | |
| 13 | var 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 |
| 17 | whose install should follow upstream without a system rebuild. It is off until |
| 18 | the 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 | |
| 23 | repo is an https://, ssh://, git:// or file:// URL, an absolute path, or |
| 24 | user@host:path, read as ssh://user@host/path (write user@host:~/path for |
| 25 | a path in your home on a plain server). Any other transport (ext::, fd::) |
| 26 | and any character outside [A-Za-z0-9._~/:@-], which nix's flake-ref parser |
| 27 | would re-encode, is refused; git ls-remote and nix |
| 28 | read the repo by the same URL, and the git that self-update runs, directly or |
| 29 | through nix, is limited to those transports (GIT_ALLOW_PROTOCOL). branch |
| 30 | defaults 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 | |
| 58 | Only the configured branch installs, pinned to the commit ls-remote returned, |
| 59 | and only after it builds and passes the smoke test. A profile holding flow |
| 60 | as both klaus and valley-flow is refused, changing nothing, by every run |
| 61 | and by --rollback, --pin and --unpin, before anything else. |
| 62 | |
| 63 | --rollback puts the previous store path back in place of the element |
| 64 | holding 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 |
| 66 | until --unpin: |
| 67 | runs while pinned exit 0 with a note, so a bad tip isn't reinstalled while |
| 68 | it gets fixed. |
| 69 | |
| 70 | A 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 | l, err := location.Host() |
| 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{Locks: l.SelfUpdateLocks(), StateFile: l.SelfUpdateState, 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 | where, err := l.HostConfig() |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | return fmt.Errorf(`self-update is off: add a self_update block to %s, e.g. |
| 105 | "self_update": {"repo": "git@valley-host:~/the-valley", "branch": "main"}`, where) |
| 106 | } |
| 107 | if err := u.Update(cmd.Context(), retry); err != nil { |
| 108 | return fmt.Errorf("self-update: %w", err) |
| 109 | } |
| 110 | return nil |
| 111 | }, |
| 112 | } |
| 113 | |
| 114 | func init() { |
| 115 | selfUpdateCmd.Flags().BoolP("verbose", "v", false, "also say so when flow is up to date") |
| 116 | selfUpdateCmd.Flags().Bool("retry", false, "build the branch's tip even if it failed before") |
| 117 | selfUpdateCmd.Flags().Bool("rollback", false, "reinstall the previous build; again to return to the newer one") |
| 118 | selfUpdateCmd.Flags().Bool("pin", false, "leave the current install alone until --unpin") |
| 119 | selfUpdateCmd.Flags().Bool("unpin", false, "let self-update install the branch's tip again") |
| 120 | selfUpdateCmd.MarkFlagsMutuallyExclusive("retry", "rollback", "pin", "unpin") |
| 121 | rootCmd.AddCommand(selfUpdateCmd) |
| 122 | } |