@@ -39,6 +39,44 @@ func (h *Headscale) initDB() error {
39
39
}
40
40
41
41
_ = 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
+ }
42
80
43
81
// If the Machine table has a column for registered,
44
82
// find all occourences of "false" and drop them. Then
@@ -54,13 +92,13 @@ func (h *Headscale) initDB() error {
54
92
55
93
for _ , machine := range machines {
56
94
log .Info ().
57
- Str ("machine" , machine .Name ).
95
+ Str ("machine" , machine .Hostname ).
58
96
Str ("machine_key" , machine .MachineKey ).
59
97
Msg ("Deleting unregistered machine" )
60
98
if err := h .db .Delete (& Machine {}, machine .ID ).Error ; err != nil {
61
99
log .Error ().
62
100
Err (err ).
63
- Str ("machine" , machine .Name ).
101
+ Str ("machine" , machine .Hostname ).
64
102
Str ("machine_key" , machine .MachineKey ).
65
103
Msg ("Error deleting unregistered machine" )
66
104
}
0 commit comments