Skip to content

Commit 06c928b

Browse files
committed
Migrate name and nickname fields
1 parent 62808cb commit 06c928b

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

db.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,44 @@ func (h *Headscale) initDB() error {
3939
}
4040

4141
_ = db.Migrator().RenameColumn(&Machine{}, "ip_address", "ip_addresses")
42+
_ = db.Migrator().RenameColumn(&Machine{}, "name", "hostname")
43+
44+
// GivenName is used as the primary source of DNS names, make sure
45+
// the field is populated and normalized if it was not when the
46+
// machine was registered.
47+
_ = db.Migrator().RenameColumn(&Machine{}, "nickname", "given_name")
48+
if db.Migrator().HasColumn(&Machine{}, "given_name") {
49+
machines := Machines{}
50+
if err := h.db.Find(&machines).Error; err != nil {
51+
log.Error().Err(err).Msg("Error accessing db")
52+
}
53+
54+
for _, machine := range machines {
55+
if machine.GivenName == "" {
56+
normalizedHostname, err := NormalizeToFQDNRules(
57+
machine.Hostname,
58+
h.cfg.OIDC.StripEmaildomain,
59+
)
60+
if err != nil {
61+
log.Error().
62+
Caller().
63+
Str("hostname", machine.Hostname).
64+
Err(err).
65+
Msg("Failed to normalize machine hostname in DB migration")
66+
}
67+
68+
err = h.RenameMachine(&machine, normalizedHostname)
69+
if err != nil {
70+
log.Error().
71+
Caller().
72+
Str("hostname", machine.Hostname).
73+
Err(err).
74+
Msg("Failed to save normalized machine name in DB migration")
75+
}
76+
77+
}
78+
}
79+
}
4280

4381
// If the Machine table has a column for registered,
4482
// find all occourences of "false" and drop them. Then
@@ -54,13 +92,13 @@ func (h *Headscale) initDB() error {
5492

5593
for _, machine := range machines {
5694
log.Info().
57-
Str("machine", machine.Name).
95+
Str("machine", machine.Hostname).
5896
Str("machine_key", machine.MachineKey).
5997
Msg("Deleting unregistered machine")
6098
if err := h.db.Delete(&Machine{}, machine.ID).Error; err != nil {
6199
log.Error().
62100
Err(err).
63-
Str("machine", machine.Name).
101+
Str("machine", machine.Hostname).
64102
Str("machine_key", machine.MachineKey).
65103
Msg("Error deleting unregistered machine")
66104
}

0 commit comments

Comments
 (0)