setup: allow running when uuid can not be read or saved

This commit is contained in:
HeshamTB 2024-03-18 20:30:40 +03:00
parent e3b080ba7f
commit 1f3eca1b1b

25
node.go
View File

@ -25,6 +25,7 @@ func InitNodeUUID() (*uuid.UUID, error) {
} }
} }
slog.Debug("UUID: Reading UUID")
f, err := os.Open(UUID_PATH) f, err := os.Open(UUID_PATH)
if err != nil { if err != nil {
return nil, err return nil, err
@ -41,23 +42,33 @@ func InitNodeUUID() (*uuid.UUID, error) {
} }
func createNodeUUID() (*uuid.UUID, error) { func createNodeUUID() (*uuid.UUID, error) {
err := os.MkdirAll(CONFIG_PATH, 0755) uuid, err := uuid.NewRandom()
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = os.MkdirAll(CONFIG_PATH, 0755)
if err != nil {
slog.Error(err.Error())
slog.Warn("Could not create /etc/hvpn directory!")
slog.Info("Proccedig without saving node UUID")
return &uuid, nil
}
mode := os.O_RDWR | os.O_CREATE | os.O_EXCL mode := os.O_RDWR | os.O_CREATE | os.O_EXCL
f, err := os.OpenFile(UUID_PATH, mode, 0644) f, err := os.OpenFile(UUID_PATH, mode, 0644)
if err != nil { if err != nil {
return nil, err slog.Error(err.Error())
} slog.Warn("Could not open uuid file")
uuid, err := uuid.NewRandom() slog.Info("Proccedig without saving node UUID")
if err != nil { return &uuid, nil
return nil, err
} }
n, err := f.Write(uuid[:]) n, err := f.Write(uuid[:])
if err != nil { if err != nil {
return nil, err slog.Error(err.Error())
slog.Warn("Could not write to uuid file")
slog.Info("Proccesing without saving node UUID")
return &uuid, nil
} }
slog.Debug("UUID: Wrote " + fmt.Sprint(n) + "bytes") slog.Debug("UUID: Wrote " + fmt.Sprint(n) + "bytes")
return &uuid, nil return &uuid, nil