Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

feat: add `no_self_update` build tag to disable hub and agent self-update (#2724)

henrygd161f009b4bad
diff --git a/internal/cmd/agent/agent.go b/internal/cmd/agent/agent.go
index 95a2a1563..2ba82b670 100644
--- a/internal/cmd/agent/agent.go
+++ b/internal/cmd/agent/agent.go
@@ -13,6 +13,7 @@ import (
"github.com/henrygd/beszel/agent"
"github.com/henrygd/beszel/agent/health"
"github.com/henrygd/beszel/agent/utils"
+ "github.com/henrygd/beszel/internal/ghupdate"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh"
)
@@ -59,6 +60,9 @@ func (opts *cmdOptions) parse() bool {
pflag.StringVarP(&opts.hubURL, "url", "u", "", "URL of the Beszel hub")
pflag.StringVarP(&opts.token, "token", "t", "", "Token to use for authentication")
chinaMirrors := pflag.BoolP("china-mirrors", "c", false, "Use mirror for update (gh.beszel.dev) instead of GitHub")
+ if ghupdate.SelfUpdateDisabled {
+ _ = pflag.CommandLine.MarkHidden("china-mirrors")
+ }
version := pflag.BoolP("version", "v", false, "Show version information")
help := pflag.BoolP("help", "h", false, "Show this help message")
@@ -86,7 +90,9 @@ func (opts *cmdOptions) parse() bool {
builder.WriteString("\nCommands:\n")
builder.WriteString(" fingerprint View or reset the agent fingerprint\n")
builder.WriteString(" health Check if the agent is running\n")
- builder.WriteString(" update Update to the latest version\n")
+ if !ghupdate.SelfUpdateDisabled {
+ builder.WriteString(" update Update to the latest version\n")
+ }
builder.WriteString("\nFlags:\n")
fmt.Print(builder.String())
pflag.PrintDefaults()
diff --git a/internal/cmd/hub/hub.go b/internal/cmd/hub/hub.go
index 1fbfcbd81..678632b26 100644
--- a/internal/cmd/hub/hub.go
+++ b/internal/cmd/hub/hub.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/henrygd/beszel"
+ "github.com/henrygd/beszel/internal/ghupdate"
"github.com/henrygd/beszel/internal/hub"
_ "github.com/henrygd/beszel/internal/migrations"
@@ -47,9 +48,10 @@ func getBaseApp() *pocketbase.PocketBase {
baseApp.RootCmd.Short = ""
// add update command
updateCmd := &cobra.Command{
- Use: "update",
- Short: "Update " + beszel.AppName + " to the latest version",
- Run: hub.Update,
+ Use: "update",
+ Short: "Update " + beszel.AppName + " to the latest version",
+ Run: hub.Update,
+ Hidden: ghupdate.SelfUpdateDisabled,
}
updateCmd.Flags().Bool("china-mirrors", false, "Use mirror (gh.beszel.dev) instead of GitHub")
baseApp.RootCmd.AddCommand(updateCmd)
diff --git a/internal/ghupdate/ghupdate.go b/internal/ghupdate/ghupdate.go
index 881e6c6f3..5e9c37796 100644
--- a/internal/ghupdate/ghupdate.go
+++ b/internal/ghupdate/ghupdate.go
@@ -6,6 +6,7 @@ package ghupdate
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"io"
"log/slog"
@@ -81,7 +82,15 @@ type updater struct {
currentVersion string
}
+// ErrSelfUpdateDisabled is returned by Update when the binary was built with
+// the no_self_update build tag.
+var ErrSelfUpdateDisabled = errors.New("self-update is disabled in this build; use your package manager to update")
+
func Update(config Config) (updated bool, err error) {
+ if SelfUpdateDisabled {
+ return false, ErrSelfUpdateDisabled
+ }
+
p := &updater{
currentVersion: beszel.Version,
config: config,
diff --git a/internal/ghupdate/selfupdate.go b/internal/ghupdate/selfupdate.go
new file mode 100644
index 000000000..b8ec74950
--- /dev/null
+++ b/internal/ghupdate/selfupdate.go
@@ -0,0 +1,7 @@
+//go:build !no_self_update
+
+package ghupdate
+
+// SelfUpdateDisabled reports whether self-update was disabled at build time
+// with the no_self_update build tag.
+const SelfUpdateDisabled = false
diff --git a/internal/ghupdate/selfupdate_disabled.go b/internal/ghupdate/selfupdate_disabled.go
new file mode 100644
index 000000000..cafc20f22
--- /dev/null
+++ b/internal/ghupdate/selfupdate_disabled.go
@@ -0,0 +1,8 @@
+//go:build no_self_update
+
+package ghupdate
+
+// SelfUpdateDisabled reports whether self-update was disabled at build time
+// with the no_self_update build tag. Package managers that handle upgrades
+// themselves (e.g. Homebrew) should build with this tag.
+const SelfUpdateDisabled = true
diff --git a/internal/ghupdate/selfupdate_disabled_test.go b/internal/ghupdate/selfupdate_disabled_test.go
new file mode 100644
index 000000000..018de0cc3
--- /dev/null
+++ b/internal/ghupdate/selfupdate_disabled_test.go
@@ -0,0 +1,18 @@
+//go:build no_self_update
+
+package ghupdate
+
+import (
+ "errors"
+ "testing"
+)
+
+func TestUpdateDisabled(t *testing.T) {
+ updated, err := Update(Config{ArchiveExecutable: "beszel", DataDir: t.TempDir()})
+ if updated {
+ t.Fatal("expected no update when self-update is disabled")
+ }
+ if !errors.Is(err, ErrSelfUpdateDisabled) {
+ t.Fatalf("expected ErrSelfUpdateDisabled, got %v", err)
+ }
+}