feat: add SYNC_SYSTEM_NAMES env var to sync system names with hostname (#1917)

This commit is contained in:
Sven van Ginkel
2026-09-24 23:45:59 +02:00
committed by GitHub
parent b5ef015451
commit 4d10ea2e03
2 changed files with 38 additions and 0 deletions

View File

@@ -279,6 +279,10 @@ func (sys *System) createRecords(data *system.CombinedData) (*core.Record, error
if err := createSystemDetailsRecord(txApp, data.Details, sys.Id); err != nil { if err := createSystemDetailsRecord(txApp, data.Details, sys.Id); err != nil {
return err return err
} }
// sync display name with hostname if enabled (details are fetched once per agent connection)
if syncNames, _ := utils.GetEnv("SYNC_SYSTEM_NAMES"); syncNames == "true" && data.Details.Hostname != "" {
systemRecord.Set("name", data.Details.Hostname)
}
} }
if data.Monitors != nil { if data.Monitors != nil {

View File

@@ -0,0 +1,34 @@
//go:build testing
package systems
import (
"testing"
"github.com/henrygd/beszel/internal/entities/system"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateRecordsSyncSystemNames(t *testing.T) {
for _, tc := range []struct {
name string
env string
hostname string
expected string
}{
{"disabled", "", "new-host", "test-system"},
{"enabled", "true", "new-host", "new-host"},
{"enabled with empty hostname", "true", "", "test-system"},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("SYNC_SYSTEM_NAMES", tc.env)
sys, app := newTestSystemWithHub(t)
_, err := sys.createRecords(&system.CombinedData{Details: &system.Details{Hostname: tc.hostname}})
require.NoError(t, err)
record, err := app.FindRecordById("systems", sys.Id)
require.NoError(t, err)
assert.Equal(t, tc.expected, record.GetString("name"))
})
}
}