Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat: analytics plugin for number of instances (#483)
Kyle Mendellbd65d273fe84
diff --git a/backend/go.mod b/backend/go.modindex 34d9fde7b..6125d77b6 100644--- a/backend/go.mod+++ b/backend/go.mod@@ -3,6 +3,7 @@ module github.com/ofkm/arcane-backendgo 1.25.0require (+ github.com/cenkalti/backoff/v5 v5.0.2github.com/compose-spec/compose-go/v2 v2.8.2github.com/docker/docker v28.4.0+incompatiblegithub.com/docker/go-connections v0.6.0@@ -21,6 +22,7 @@ require (github.com/samber/slog-gin v1.17.2github.com/shirou/gopsutil/v3 v3.24.5golang.org/x/crypto v0.42.0+ golang.org/x/sync v0.17.0gopkg.in/yaml.v3 v3.0.1gorm.io/driver/postgres v1.6.0gorm.io/gorm v1.30.5@@ -96,7 +98,6 @@ require (go.yaml.in/yaml/v3 v3.0.4 // indirectgolang.org/x/arch v0.18.0 // indirectgolang.org/x/net v0.43.0 // indirect- golang.org/x/sync v0.17.0 // indirectgolang.org/x/sys v0.36.0 // indirectgolang.org/x/text v0.29.0 // indirectgolang.org/x/time v0.12.0 // indirectdiff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.goindex 497d59ddd..7929b6acd 100644--- a/backend/internal/bootstrap/bootstrap.go+++ b/backend/internal/bootstrap/bootstrap.go@@ -138,7 +138,7 @@ func (app *App) Start() {defer app.DB.Close()defer app.CancelApp()- registerJobs(app.AppCtx, app.Scheduler, app.Services)+ registerJobs(app.AppCtx, app.Scheduler, app.Services, app.Config)go func() {slog.InfoContext(app.AppCtx, "Starting scheduler goroutine")diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.goindex 60f789eb5..3321abb15 100644--- a/backend/internal/bootstrap/jobs_bootstrap.go+++ b/backend/internal/bootstrap/jobs_bootstrap.go@@ -5,6 +5,7 @@ import ("fmt""log/slog"+ "github.com/ofkm/arcane-backend/internal/config""github.com/ofkm/arcane-backend/internal/job")@@ -16,7 +17,7 @@ func initializeScheduler() (*job.Scheduler, error) {return scheduler, nil}-func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServices *Services) {+func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServices *Services, appConfig *config.Config) {autoUpdateJob := job.NewAutoUpdateJob(scheduler, appServices.Updater, appServices.Settings)if err := autoUpdateJob.Register(appCtx); err != nil {slog.ErrorContext(appCtx, "Failed to register auto-update job", slog.Any("error", err))@@ -27,6 +28,12 @@ func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServicesslog.ErrorContext(appCtx, "Failed to register image polling job", slog.Any("error", err))}+ // Register analytics heartbeat job+ analyticsJob := job.NewAnalyticsJob(scheduler, appServices.Settings, nil, appConfig)+ if err := analyticsJob.Register(appCtx); err != nil {+ slog.ErrorContext(appCtx, "Failed to register analytics heartbeat job", slog.Any("error", err))+ }+if err := job.RegisterEventCleanupJob(appCtx, scheduler, appServices.Event); err != nil {slog.ErrorContext(appCtx, "Failed to register event cleanup job", slog.Any("error", err))}diff --git a/backend/internal/config/config.go b/backend/internal/config/config.goindex cb662da51..25e6aaca7 100644--- a/backend/internal/config/config.go+++ b/backend/internal/config/config.go@@ -32,6 +32,8 @@ type Config struct {AgentToken stringAgentBootstrapToken stringUpdateCheckDisabled bool++ AnalyticsDisabled bool}func Load() *Config {@@ -57,6 +59,8 @@ func Load() *Config {AgentToken: os.Getenv("AGENT_TOKEN"),AgentBootstrapToken: os.Getenv("AGENT_BOOTSTRAP_TOKEN"),UpdateCheckDisabled: getBoolEnvOrDefault("UPDATE_CHECK_DISABLED", false),++ AnalyticsDisabled: getBoolEnvOrDefault("ANALYTICS_DISABLED", false),}}diff --git a/backend/internal/config/version.go b/backend/internal/config/version.gonew file mode 100644index 000000000..e8e01ab68--- /dev/null+++ b/backend/internal/config/version.go@@ -0,0 +1,6 @@+package config++var (+ Version = "dev"+ Revision = "unknown"+)diff --git a/backend/internal/job/analytics_job.go b/backend/internal/job/analytics_job.gonew file mode 100644index 000000000..42f526802--- /dev/null+++ b/backend/internal/job/analytics_job.go@@ -0,0 +1,193 @@+package job++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "log/slog"+ "net/http"+ "strings"+ "time"++ backoff "github.com/cenkalti/backoff/v5"+ "github.com/go-co-op/gocron/v2"+ "github.com/ofkm/arcane-backend/internal/config"+ "github.com/ofkm/arcane-backend/internal/services"+)++const (+ AnalyticsJobName = "analytics-heartbeat"+ defaultHeartbeatEndpoint = "https://analytics.ofkm.arcane.dev/heartbeat"+ analyticsInterval = 24 * time.Hour+)++type AnalyticsJob struct {+ scheduler *Scheduler+ settingsService *services.SettingsService+ httpClient *http.Client+ heartbeatURL string+ cfg *config.Config+}++func NewAnalyticsJob(+ scheduler *Scheduler,+ settingsService *services.SettingsService,+ httpClient *http.Client,+ cfg *config.Config,+) *AnalyticsJob {+ if httpClient == nil {+ httpClient = &http.Client{Timeout: 30 * time.Second}+ }+ return &AnalyticsJob{+ scheduler: scheduler,+ settingsService: settingsService,+ httpClient: httpClient,+ heartbeatURL: defaultHeartbeatEndpoint,+ cfg: cfg,+ }+}++func RegisterAnalyticsJob(+ ctx context.Context,+ scheduler *Scheduler,+ settingsService *services.SettingsService,+ httpClient *http.Client,+ cfg *config.Config,+) error {+ j := NewAnalyticsJob(scheduler, settingsService, httpClient, cfg)+ return j.Register(ctx)+}++func (j *AnalyticsJob) Register(ctx context.Context) error {+ if j.cfg.AnalyticsDisabled || !j.isProduction() {+ slog.InfoContext(ctx, "analytics disabled or not in production; heartbeat job not registered",+ "analyticsDisabled", j.cfg.AnalyticsDisabled, "env", j.cfg.Environment)+ return nil+ }++ j.scheduler.RemoveJobByName(AnalyticsJobName)++ jobDefinition := gocron.DurationJob(analyticsInterval)+ slog.InfoContext(ctx, "registering analytics heartbeat job",+ "jobName", AnalyticsJobName, "interval", analyticsInterval.String(), "endpoint", j.heartbeatURL)++ return j.scheduler.RegisterJob(+ ctx,+ AnalyticsJobName,+ jobDefinition,+ j.Execute,+ true, // run immediately on startup+ )+}++func UpdateAnalyticsJobSchedule(+ ctx context.Context,+ scheduler *Scheduler,+ settingsService *services.SettingsService,+ httpClient *http.Client,+ cfg *config.Config,+) error {+ j := NewAnalyticsJob(scheduler, settingsService, httpClient, cfg)+ return j.Reschedule(ctx)+}++func (j *AnalyticsJob) Reschedule(ctx context.Context) error {+ if j.cfg.AnalyticsDisabled || !j.isProduction() {+ j.scheduler.RemoveJobByName(AnalyticsJobName)+ slog.InfoContext(ctx, "analytics disabled or not in production; removed heartbeat job if present",+ "analyticsDisabled", j.cfg.AnalyticsDisabled, "env", j.cfg.Environment)+ return nil+ }++ slog.InfoContext(ctx, "analytics settings changed; rescheduling heartbeat job",+ "jobName", AnalyticsJobName, "interval", analyticsInterval.String())++ return j.scheduler.RescheduleDurationJobByName(ctx, AnalyticsJobName, analyticsInterval, j.Execute, true)+}++func (j *AnalyticsJob) Remove(ctx context.Context) {+ j.scheduler.RemoveJobByName(AnalyticsJobName)+ slog.InfoContext(ctx, "analytics heartbeat job removed", "jobName", AnalyticsJobName)+}++func (j *AnalyticsJob) Execute(parentCtx context.Context) error {+ if j.cfg.AnalyticsDisabled || !j.isProduction() {+ slog.InfoContext(parentCtx, "analytics disabled or not in production; skipping heartbeat",+ "analyticsDisabled", j.cfg.AnalyticsDisabled, "env", j.cfg.Environment)+ return nil+ }++ instanceID := j.settingsService.GetStringSetting(parentCtx, "instanceId", "")+ if instanceID == "" {+ if ensuredID, err := j.settingsService.EnsureInstanceID(parentCtx); err == nil {+ instanceID = ensuredID+ } else {+ slog.WarnContext(parentCtx, "failed to ensure instance ID; using 'unknown'", "error", err)+ instanceID = "unknown"+ }+ }++ payload := struct {+ Version string `json:"version"`+ InstanceID string `json:"instance_id"`+ }{+ Version: getVersion(),+ InstanceID: instanceID,+ }++ body, err := json.Marshal(payload)+ if err != nil {+ return fmt.Errorf("failed to marshal analytics heartbeat body: %w", err)+ }++ slog.InfoContext(parentCtx, "sending analytics heartbeat",+ "jobName", AnalyticsJobName)++ _, err = backoff.Retry(+ parentCtx,+ func() (struct{}, error) {+ ctx, cancel := context.WithTimeout(parentCtx, 20*time.Second)+ defer cancel()++ req, err := http.NewRequestWithContext(ctx, http.MethodPost, j.heartbeatURL, bytes.NewReader(body))+ if err != nil {+ return struct{}{}, fmt.Errorf("failed to create request: %w", err)+ }+ req.Header.Set("Content-Type", "application/json")++ resp, err := j.httpClient.Do(req)+ if err != nil {+ return struct{}{}, fmt.Errorf("failed to send request: %w", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode < 200 || resp.StatusCode >= 300 {+ return struct{}{}, fmt.Errorf("request failed with status: %d", resp.StatusCode)+ }+ return struct{}{}, nil+ },+ backoff.WithBackOff(backoff.NewExponentialBackOff()),+ backoff.WithMaxTries(3),+ )++ if err != nil {+ slog.ErrorContext(parentCtx, "analytics heartbeat failed", "error", err)+ return fmt.Errorf("analytics heartbeat request failed: %w", err)+ }++ slog.InfoContext(parentCtx, "analytics heartbeat sent successfully", "jobName", AnalyticsJobName)+ return nil+}++func (j *AnalyticsJob) isProduction() bool {+ env := strings.ToLower(strings.TrimSpace(j.cfg.Environment))+ return env == "production" || env == "prod"+}++func getVersion() string {+ if v := strings.TrimSpace(config.Version); v != "" && v != "dev" {+ return v+ }+ return "unknown"+}diff --git a/backend/internal/job/job.go b/backend/internal/job/job.goindex 6884374cd..78e8be91c 100644--- a/backend/internal/job/job.go+++ b/backend/internal/job/job.go@@ -4,7 +4,7 @@ import ("context""fmt""log/slog"- "time" // added+ "time""github.com/go-co-op/gocron/v2""github.com/google/uuid"diff --git a/backend/internal/models/settings.go b/backend/internal/models/settings.goindex 85cefd92c..994ade8f7 100644--- a/backend/internal/models/settings.go+++ b/backend/internal/models/settings.go@@ -63,6 +63,8 @@ type Settings struct {// OnboardingOnboardingCompleted SettingVariable `key:"onboardingCompleted,public"`OnboardingSteps SettingVariable `key:"onboardingSteps"`++ InstanceID SettingVariable `key:"instanceId,internal"`}func (SettingVariable) TableName() string {diff --git a/backend/internal/services/settings_service.go b/backend/internal/services/settings_service.goindex 135d4d47f..e1d20c911 100644--- a/backend/internal/services/settings_service.go+++ b/backend/internal/services/settings_service.go@@ -12,6 +12,7 @@ import ("github.com/google/uuid""gorm.io/gorm"+ "gorm.io/gorm/clause""github.com/ofkm/arcane-backend/internal/config""github.com/ofkm/arcane-backend/internal/database"@@ -78,6 +79,9 @@ func (s *SettingsService) getDefaultSettings() *models.Settings {// Onboarding settingsOnboardingCompleted: models.SettingVariable{Value: "false"},OnboardingSteps: models.SettingVariable{Value: "[]"},++ // Instance+ InstanceID: models.SettingVariable{Value: ""},}}@@ -289,7 +293,7 @@ func (s *SettingsService) EnsureDefaultSettings(ctx context.Context) error {defaultSettings := s.getDefaultSettings()defaultSettingVars := defaultSettings.ToSettingVariableSlice(true, false)- return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {+ if err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {for _, defaultSetting := range defaultSettingVars {var existing models.SettingVariableerr := tx.Where("key = ?", defaultSetting.Key).First(&existing).Error@@ -301,10 +305,56 @@ func (s *SettingsService) EnsureDefaultSettings(ctx context.Context) error {} else if err != nil {return fmt.Errorf("failed to check for existing setting %s: %w", defaultSetting.Key, err)}- // If setting exists, leave it as is (don't overwrite user values)+ }+ return nil+ }); err != nil {+ return err+ }++ if _, err := s.EnsureInstanceID(ctx); err != nil {+ return err+ }+ return nil+}++func (s *SettingsService) EnsureInstanceID(ctx context.Context) (string, error) {+ const key = "instanceId"++ var newID string+ err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {+ var sv models.SettingVariable++ if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).+ Where("key = ?", key).+ First(&sv).Error; err != nil {+ if errors.Is(err, gorm.ErrRecordNotFound) {+ newID = uuid.New().String()+ if createErr := tx.Create(&models.SettingVariable{Key: key, Value: newID}).Error; createErr != nil {+ return fmt.Errorf("failed to persist %s: %w", key, createErr)+ }+ return nil+ }+ return fmt.Errorf("failed to load %s: %w", key, err)+ }++ if sv.Value != "" {+ newID = sv.Value+ return nil+ }++ newID = uuid.New().String()+ if updErr := tx.Model(&models.SettingVariable{}).+ Where("key = ?", key).+ Update("value", newID).Error; updErr != nil {+ return fmt.Errorf("failed to update %s: %w", key, updErr)}return nil})++ if err != nil {+ return "", err+ }+ return newID, nil}func (s *SettingsService) GetBoolSetting(ctx context.Context, key string, defaultValue bool) bool {diff --git a/docker/Dockerfile b/docker/Dockerfileindex 3286bf4f4..c081659cd 100644--- a/docker/Dockerfile+++ b/docker/Dockerfile@@ -1,5 +1,7 @@# Tags passed to "go build"ARG BUILD_TAGS=""+ARG VERSION="dev"+ARG REVISION="unknown"# Stage 1: Build FrontendFROM node:24 AS frontend-builder@@ -16,6 +18,8 @@ RUN pnpm -C frontend build# Stage 2: Build BackendFROM golang:1.25-alpine AS backend-builderARG BUILD_TAGS+ARG VERSION+ARG REVISIONWORKDIR /buildRUN apk add --no-cache git ca-certificates tzdata curlCOPY ./backend/go.mod ./backend/go.sum ./@@ -27,7 +31,7 @@ RUN CGO_ENABLED=0 \GOOS=linux \go build \-tags "${BUILD_TAGS}" \- -ldflags='-w -s' \+ -ldflags "-w -s -X github.com/ofkm/arcane-backend/internal/config.Version=${VERSION} -X github.com/ofkm/arcane-backend/internal/config.Revision=${REVISION}" \-trimpath \-o /build/arcane \./cmd/main.go@@ -58,8 +62,9 @@ COPY --chmod=755 ../scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.shEXPOSE 3552VOLUME ["/app/data"]-ARG VERSION="1.1.0"-ARG REVISION="4f1c9a9d"+ARG VERSION="dev"+ARG REVISION="unknown"+LABEL org.opencontainers.image.authors="OFKM Technologies"LABEL org.opencontainers.image.url="https://github.com/ofkm/arcane"LABEL org.opencontainers.image.documentation="https://github.com/ofkm/arcane/blob/main/README.md"diff --git a/docker/Dockerfile-agent b/docker/Dockerfile-agentindex 6f8e19768..ce6c1e2b2 100644--- a/docker/Dockerfile-agent+++ b/docker/Dockerfile-agent@@ -1,6 +1,11 @@# Build agent binary+ARG VERSION="dev"+ARG REVISION="unknown"+FROM golang:1.25-alpine AS agent-builderARG BUILD_TAGS="exclude_frontend"+ARG VERSION+ARG REVISIONWORKDIR /buildRUN apk add --no-cache git ca-certificates tzdata curl@@ -15,7 +20,7 @@ RUN CGO_ENABLED=0 \GOOS=linux \go build \-tags "${BUILD_TAGS}" \- -ldflags='-w -s' \+ -ldflags "-w -s -X github.com/ofkm/arcane-backend/internal/config.Version=${VERSION} -X github.com/ofkm/arcane-backend/internal/config.Revision=${REVISION}" \-trimpath \-o /out/arcane-agent \./cmd/main.go@@ -38,13 +43,24 @@ COPY --from=agent-builder /out/docker-compose /usr/local/bin/docker-composeCOPY --chmod=755 ../scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.shENTRYPOINT ["/usr/local/bin/entrypoint.sh"]-ARG VERSION="1.1.0"-ARG REVISION="4f1c9a9d"+ARG VERSION="dev"+ARG REVISION="unknown"-ENV GIN_MODE=release \- PORT=3553 \+ENV ENVIRONMENT=production \AGENT_MODE=true \- ENVIRONMENT=production+ GIN_MODE=release \+ PORT=3553++LABEL org.opencontainers.image.authors="OFKM Technologies"+LABEL org.opencontainers.image.url="https://github.com/ofkm/arcane"+LABEL org.opencontainers.image.documentation="https://github.com/ofkm/arcane/blob/main/README.md"+LABEL org.opencontainers.image.source="https://github.com/ofkm/arcane"+LABEL org.opencontainers.image.version=$VERSION+LABEL org.opencontainers.image.revision=$REVISION+LABEL org.opencontainers.image.licenses="BSD-3-Clause"+LABEL org.opencontainers.image.ref.name="arcane-agent"+LABEL org.opencontainers.image.title="Arcane Agent"+LABEL org.opencontainers.image.description="Arcane Agent"LABEL com.ofkm.arcane.updater="false"LABEL com.ofkm.arcane.server="true"diff --git a/docker/Dockerfile-agent-static b/docker/Dockerfile-agent-staticindex bdeb459f4..03ec5c1c2 100644--- a/docker/Dockerfile-agent-static+++ b/docker/Dockerfile-agent-static@@ -15,6 +15,10 @@ COPY ./backend/.bin/arcane-agent-linux-${TARGETARCH} ./arcane-agentCOPY --chmod=755 ../scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.sh+LABEL org.opencontainers.image.title="Arcane Agent"+LABEL org.opencontainers.image.description="Arcane Agent"+LABEL org.opencontainers.image.source="https://github.com/ofkm/arcane"+LABEL com.ofkm.arcane.updater="false"LABEL com.ofkm.arcane.server="true"diff --git a/docker/Dockerfile-static b/docker/Dockerfile-staticindex 070634d08..b1993cd22 100644--- a/docker/Dockerfile-static+++ b/docker/Dockerfile-static@@ -15,6 +15,10 @@ COPY ./backend/.bin/arcane-linux-${TARGETARCH} ./arcaneCOPY --chmod=755 ../scripts/docker/entrypoint.sh /usr/local/bin/entrypoint.sh+LABEL org.opencontainers.image.title="Arcane"+LABEL org.opencontainers.image.description="Modern Docker Management, Made for Everyone"+LABEL org.opencontainers.image.source="https://github.com/ofkm/arcane"+LABEL com.ofkm.arcane.updater="false"LABEL com.ofkm.arcane.server="true"diff --git a/scripts/development/build-binaries.sh b/scripts/development/build-binaries.shindex e67ca231a..f0eb625c9 100755--- a/scripts/development/build-binaries.sh+++ b/scripts/development/build-binaries.sh@@ -6,7 +6,11 @@ mkdir -p .bin# Read version from repo root .version (trim whitespace), fallback to "dev"VERSION=$(cat ../.version | sed 's/^\s*\|\s*$//g')-LDFLAGS="-w -s -buildid=${VERSION}"+REVISION=$(cat ../.revision 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || echo "unknown")++LDFLAGS="-w -s -buildid=${VERSION} \+ -X github.com/ofkm/arcane-backend/internal/config.Version=${VERSION} \+ -X github.com/ofkm/arcane-backend/internal/config.Revision=${REVISION}"DOCKER_ONLY=falseAGENT_BUILD=falsediff --git a/scripts/development/create-release.sh b/scripts/development/create-release.shindex c085e4fac..3b152645a 100755--- a/scripts/development/create-release.sh+++ b/scripts/development/create-release.sh@@ -127,20 +127,6 @@ LATEST_REVISION=$(git rev-parse --short HEAD)echo "$LATEST_REVISION" > .revisiongit add .revision-# Update default ARG VERSION/REVISION in Dockerfiles (if present)-if [ -f docker/Dockerfile ]; then- echo "Updating docker/Dockerfile ARGs..."- sed -i.bak "s/^ARG VERSION=.*$/ARG VERSION=\"$NEW_VERSION\"/" docker/Dockerfile && rm docker/Dockerfile.bak- sed -i.bak "s/^ARG REVISION=.*$/ARG REVISION=\"$LATEST_REVISION\"/" docker/Dockerfile && rm docker/Dockerfile.bak- git add docker/Dockerfile-fi-if [ -f docker/Dockerfile-agent ]; then- echo "Updating docker/Dockerfile-agent ARGs..."- sed -i.bak "s/^ARG VERSION=.*$/ARG VERSION=\"$NEW_VERSION\"/" docker/Dockerfile-agent && rm docker/Dockerfile-agent.bak- sed -i.bak "s/^ARG REVISION=.*$/ARG REVISION=\"$LATEST_REVISION\"/" docker/Dockerfile-agent && rm docker/Dockerfile-agent.bak- git add docker/Dockerfile-agent-fi-# Generate changelogecho "Generating changelog..."conventional-changelog -p conventionalcommits -i CHANGELOG.md -s