From 8afda7cd8c11704973d87d112dc6f66a3714b225 Mon Sep 17 00:00:00 2001 From: Steve Cliff Date: Sat, 9 May 2026 15:20:13 +0100 Subject: [PATCH] fix(version): use internal/version as single source for build constants The Dockerfile only set `-X main.version=...`, so docker-built binaries left `internal/version.Version` at its default "dev". The update logic (host_update.go:61, hosts.go:94, fleet_update.go:101 et al.) compares against `internal/version.Version`, so a v1.0.0 host always looked out-of-date to a v1.0.0 server, the chip never cleared, and pressing "update" re-downloaded the same bundled binary on a loop. Collapse the two version sources: drop the `var version/commit/date` locals in cmd/{server,agent}/main.go, route everything through internal/version (now also carrying Date), and have both the Dockerfile and the Makefile set the same single set of -X flags. Verified end-to-end: make build and docker build both emit binaries whose --version reflects the build VERSION. --- Makefile | 6 ++++-- cmd/agent/main.go | 17 ++++++----------- cmd/server/main.go | 13 ++++--------- deploy/Dockerfile.server | 6 +++++- internal/version/version.go | 4 ++++ 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index b258757..73719b2 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,10 @@ VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || ec COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo none) DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ) VERSION_PKG := gitea.dcglab.co.uk/steve/restic-manager/internal/version -LDFLAGS := -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE) \ - -X $(VERSION_PKG).Version=$(VERSION) -X $(VERSION_PKG).Commit=$(COMMIT) +LDFLAGS := -s -w \ + -X $(VERSION_PKG).Version=$(VERSION) \ + -X $(VERSION_PKG).Commit=$(COMMIT) \ + -X $(VERSION_PKG).Date=$(DATE) GOFLAGS := -trimpath DOCKER_IMAGE ?= gitea.dcglab.co.uk/steve/restic-manager DOCKER_TAG ?= dev diff --git a/cmd/agent/main.go b/cmd/agent/main.go index e04dd2d..7a2cc8c 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -22,12 +22,7 @@ import ( "gitea.dcglab.co.uk/steve/restic-manager/internal/agent/wsclient" "gitea.dcglab.co.uk/steve/restic-manager/internal/api" "gitea.dcglab.co.uk/steve/restic-manager/internal/restic" -) - -var ( - version = "dev" - commit = "none" - date = "unknown" + "gitea.dcglab.co.uk/steve/restic-manager/internal/version" ) func main() { @@ -66,7 +61,7 @@ func run() error { flag.Parse() if *showVersion { - fmt.Printf("restic-manager-agent %s (commit %s, built %s)\n", version, commit, date) + fmt.Printf("restic-manager-agent %s (commit %s, built %s)\n", version.Version, version.Commit, version.Date) return nil } @@ -82,14 +77,14 @@ func run() error { if *enrollServer == "" { return errors.New("enrollment: -enroll-server is required with -enroll-token") } - return doEnroll(*enrollServer, *enrollToken, cfg, version) + return doEnroll(*enrollServer, *enrollToken, cfg, version.Version) } // Announce-and-approve: -enroll-server set, no token, agent not // yet enrolled. Run the announce flow inline; on success the cfg // has the bearer + host_id and we drop into the normal run loop. if !cfg.Enrolled() && *enrollServer != "" { - if err := doAnnounce(*enrollServer, cfg, version); err != nil { + if err := doAnnounce(*enrollServer, cfg, version.Version); err != nil { return fmt.Errorf("announce: %w", err) } } @@ -106,7 +101,7 @@ func run() error { return fmt.Errorf("sysinfo: %w", err) } slog.Info("agent starting", - "version", version, + "version", version.Version, "host_id", cfg.HostID, "server", cfg.ServerURL, "restic_version", snap.ResticVersion, @@ -136,7 +131,7 @@ func run() error { CertPinSHA256: cfg.CertPinSHA256, HelloPayload: api.HelloPayload{ ProtocolVersion: snap.ProtocolVersion, - AgentVersion: version, + AgentVersion: version.Version, ResticVersion: snap.ResticVersion, Hostname: snap.Hostname, OS: snap.OS, diff --git a/cmd/server/main.go b/cmd/server/main.go index d598571..1cd5151 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -26,12 +26,7 @@ import ( "gitea.dcglab.co.uk/steve/restic-manager/internal/server/ui" "gitea.dcglab.co.uk/steve/restic-manager/internal/server/ws" "gitea.dcglab.co.uk/steve/restic-manager/internal/store" -) - -var ( - version = "dev" - commit = "none" - date = "unknown" + "gitea.dcglab.co.uk/steve/restic-manager/internal/version" ) func main() { @@ -47,7 +42,7 @@ func run() error { flag.Parse() if *showVersion { - fmt.Printf("restic-manager-server %s (commit %s, built %s)\n", version, commit, date) + fmt.Printf("restic-manager-server %s (commit %s, built %s)\n", version.Version, version.Commit, version.Date) return nil } @@ -123,7 +118,7 @@ func run() error { NotificationHub: notifHub, UpdateWatcher: updateWatcher, UI: renderer, - Version: version, + Version: version.Version, OIDC: oidcClient, Metrics: metricsRegistry, } @@ -177,7 +172,7 @@ func run() error { errCh := make(chan error, 1) go func() { - slog.Info("server listening", "addr", cfg.Listen, "version", version) + slog.Info("server listening", "addr", cfg.Listen, "version", version.Version) errCh <- srv.Start() }() diff --git a/deploy/Dockerfile.server b/deploy/Dockerfile.server index 1580706..bfd28ad 100644 --- a/deploy/Dockerfile.server +++ b/deploy/Dockerfile.server @@ -26,7 +26,11 @@ ARG DATE=unknown ARG TARGETOS ARG TARGETARCH -ENV LDFLAGS="-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}" +ENV VERSION_PKG="gitea.dcglab.co.uk/steve/restic-manager/internal/version" +ENV LDFLAGS="-s -w \ + -X ${VERSION_PKG}.Version=${VERSION} \ + -X ${VERSION_PKG}.Commit=${COMMIT} \ + -X ${VERSION_PKG}.Date=${DATE}" # Server: built for the image's runtime arch. RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ diff --git a/internal/version/version.go b/internal/version/version.go index 3a5fb70..db5a7a1 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -13,4 +13,8 @@ var ( // Commit is the short git SHA. Informational only; surfaced via // /api/version but not used for any comparison. Commit = "" + + // Date is the RFC3339 build timestamp. Informational only; printed + // by `--version` but not used for any comparison. + Date = "unknown" ) -- 2.52.0