Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ linters:

# Detailed settings for specific linters
settings:
goconst:
# Excluding test diagnostics alone still lets fixture strings inflate
# duplicate counts in production files. Skip tests during analysis too.
ignore-tests: true
revive:
rules:
- name: use-any
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Key Differences:
curl -fsSL https://raw.githubusercontent.com/sentiolabs/arc/main/scripts/install.sh | bash
```

The bootstrap script verifies the release checksum. If Arc is already installed,
use `arc self update`. The script still accepts `--force` and `--tag=vX.Y.Z`
for reinstalls and updates from older Arc binaries.

### Linux Packages

Download `.deb`, `.rpm`, or `.pkg.tar.zst` (Arch) from the [latest release](https://github.com/sentiolabs/arc/releases/latest).
Expand Down Expand Up @@ -503,7 +507,19 @@ release tag is preserved for downloads. Older Arc binaries may need a one-time
`arc self update --force` when switching from undotted to dotted RC tags.

`arc self update` asks for confirmation before installing. Pass `-y` or `--yes`
to skip the prompt in scripts.
to skip the prompt in scripts. Updates use `selfupdate-go` v0.2.1 to download
and verify the release archive, then atomically replace the current executable.
No shell installer, curl, or wget is needed for self-updates. A running local
server is stopped only after verification and restarted using the installed
binary; a stopped server stays stopped. Major/minor updates retain the
pre-update database backup.

Package-managed binaries (including Homebrew, system packages, and Nix) must be
updated with their package manager. Native updates refuse managed paths and
unwritable installation directories instead of moving Arc to another location.
On macOS, the native installer also performs ad-hoc signing. If the server fails
to restart after a successful update, the command reports that the binary was
updated and instructs you to retry `arc server start`.

Nightlies are tagged automatically as `v<next patch>-nightly.<date>`. The next
patch, not the released version, is what makes a nightly sort above the current
Expand Down
25 changes: 12 additions & 13 deletions cmd/arc/self.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package main provides the self-management commands for the arc CLI,
// delegating update and channel logic to github.com/sentiolabs/go-selfupdate.
// delegating update and channel logic to github.com/sentiolabs/selfupdate-go.
package main

import (
Expand All @@ -11,31 +11,30 @@ import (
"github.com/sentiolabs/arc/internal/project"
"github.com/sentiolabs/arc/internal/storage/sqlite"
"github.com/sentiolabs/arc/internal/version"
"github.com/sentiolabs/go-selfupdate"
"github.com/sentiolabs/go-selfupdate/cobracmd"
"github.com/sentiolabs/selfupdate-go"
"github.com/sentiolabs/selfupdate-go/cobracmd"
"golang.org/x/mod/semver"
)

// cliName is the binary name, which is also arc's GitHub repository name.
const cliName = "arc"

// installScriptURL is arc's install script, run with --force --tag=<tag>.
const installScriptURL = "https://raw.githubusercontent.com/sentiolabs/arc/main/scripts/install.sh"

// selfCmd is registered by main.go. It is the go-selfupdate command tree with
// selfCmd is registered by main.go. It is the selfupdate-go command tree with
// arc's wiring: GitHub releases of sentiolabs/arc, the channel from
// ~/.arc/config.toml, and a database backup before a major or minor bump.
var selfCmd = cobracmd.New(newSelfUpdater(), cobracmd.WithCheckShorthand("c"))

// newSelfUpdater builds arc's updater.
func newSelfUpdater() *selfupdate.Updater {
lifecycle := newUpdateLifecycle()
return &selfupdate.Updater{
Name: cliName,
Version: version.Short(),
Source: &selfupdate.GitHubSource{Owner: "sentiolabs", Repo: cliName},
Store: arcChannelStore(),
Installer: &selfupdate.ScriptInstaller{ScriptURL: installScriptURL},
PreInstall: preInstallBackup(backupArcDatabase),
Name: cliName,
Version: version.Short(),
Source: &selfupdate.GitHubSource{Owner: "sentiolabs", Repo: cliName},
Store: arcChannelStore(),
Installer: lifecycle,
PreInstall: lifecycle.preInstall,
PostInstall: lifecycle.postInstall,
}
}

Expand Down
166 changes: 166 additions & 0 deletions cmd/arc/self_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package main

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"time"

"github.com/sentiolabs/selfupdate-go"
)

const updateRecoveryTimeout = 30 * time.Second

// updateLifecycle keeps daemon downtime inside the verified installation phase.
// The callbacks also let tests exercise failure recovery without a real daemon.
type updateLifecycle struct {
installer selfupdate.Installer
resolve func() error
running func() bool
stop func(context.Context) error
start func(context.Context) error
backup func(context.Context, string, string) error
restart bool
}

// newUpdateLifecycle wires the archive installer to the local daemon.
func newUpdateLifecycle() *updateLifecycle {
archive := &selfupdate.ArchiveInstaller{Name: cliName}
lifecycle := &updateLifecycle{
installer: archive,
running: func() bool {
_, running := isServerRunning()
return running
},
backup: preInstallBackup(backupArcDatabase),
}
lifecycle.resolve = func() error {
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("locate executable: %w", err)
}
archive.TargetPath, err = filepath.EvalSymlinks(exe)
if err != nil {
return fmt.Errorf("resolve executable: %w", err)
}
// Capture the selected config after Cobra has parsed global flags.
args := []string{}
if configPath != "" {
args = append(args, "--config", configPath)
}
run := func(ctx context.Context, action string) error {
//nolint:gosec // Execute only our own canonical binary with fixed server actions.
cmd := exec.CommandContext(ctx, archive.TargetPath, append(args, "server", action)...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
return cmd.Run()
}
lifecycle.stop = func(ctx context.Context) error { return run(ctx, "stop") }
lifecycle.start = func(ctx context.Context) error { return run(ctx, "start") }
return nil
}
return lifecycle
}

// Sweep preserves upstream cleanup even when no newer release is available.
func (l *updateLifecycle) Sweep() error {
if sweeper, ok := l.installer.(selfupdate.Sweeper); ok {
return sweeper.Sweep()
}
return nil
}

// Prepare resolves the executable before it can become an unlinked inode.
// No daemon operation runs until download and verification have succeeded.
func (l *updateLifecycle) Prepare(ctx context.Context, rel selfupdate.Release) (selfupdate.Staged, error) {
l.restart = false
if l.resolve != nil {
if err := l.resolve(); err != nil {
return nil, err
}
}
staged, err := l.installer.Prepare(ctx, rel)
if err != nil {
return nil, err
}
return &lifecycleStaged{Staged: staged, lifecycle: l}, nil
}

// preInstall preserves backup policy and verifies that stopping actually worked.
func (l *updateLifecycle) preInstall(ctx context.Context, current, latest string) error {
if err := ctx.Err(); err != nil {
return err
}
if err := l.backup(ctx, current, latest); err != nil {
return err
}
if !l.running() {
return nil
}
// Let the stop helper finish waiting for shutdown even if the update is
// cancelled. Killing it after SIGTERM can leave recovery observing a live
// daemon that is about to exit, causing it to skip the restart.
l.restart = true
stopCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), updateRecoveryTimeout)
defer cancel()
if err := l.stop(stopCtx); err != nil {
return fmt.Errorf("stop server: %w", err)
}
if l.running() {
return errors.New("server is still running after stop; update aborted")
}
return ctx.Err()
}

func (l *updateLifecycle) postInstall(_ context.Context, _, _ string) error {
// Once replaced, restore service even if the update context was cancelled.
return l.recoverServer()
}

// restartServer makes a single restoration attempt, avoiding duplicate starts.
func (l *updateLifecycle) restartServer(ctx context.Context) error {
if !l.restart {
return nil
}
l.restart = false
if l.running() {
return nil
}
if err := l.start(ctx); err != nil {
return fmt.Errorf("restart server (run 'arc server start' to retry): %w", err)
}
return nil
}

// recoverServer bounds service restoration independently of Ctrl-C.
func (l *updateLifecycle) recoverServer() error {
ctx, cancel := context.WithTimeout(context.Background(), updateRecoveryTimeout)
defer cancel()
return l.restartServer(ctx)
}

// lifecycleStaged restores a stopped daemon even when PreInstall aborts.
// Upstream defers Close immediately after successful preparation.
type lifecycleStaged struct {
selfupdate.Staged
lifecycle *updateLifecycle
}

// Commit returns both replacement and recovery errors when both fail.
func (s *lifecycleStaged) Commit(ctx context.Context) error {
if err := s.Staged.Commit(ctx); err != nil {
return errors.Join(err, s.lifecycle.recoverServer())
}
return nil
}

func (s *lifecycleStaged) Close() error {
// Upstream ignores Close errors, so recovery failure must also be visible.
err := s.lifecycle.recoverServer()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Warning: update recovery failed: %v\n", err)
}
return errors.Join(err, s.Staged.Close())
}
Loading
Loading