From 1f3eca1b1bb53b66b11869f6723bfac02f1fd7f1 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 18 Mar 2024 20:30:40 +0300 Subject: [PATCH] setup: allow running when uuid can not be read or saved --- node.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/node.go b/node.go index 6a4f2d0..6dce2d4 100644 --- a/node.go +++ b/node.go @@ -25,6 +25,7 @@ func InitNodeUUID() (*uuid.UUID, error) { } } + slog.Debug("UUID: Reading UUID") f, err := os.Open(UUID_PATH) if err != nil { return nil, err @@ -41,23 +42,33 @@ func InitNodeUUID() (*uuid.UUID, error) { } func createNodeUUID() (*uuid.UUID, error) { - err := os.MkdirAll(CONFIG_PATH, 0755) + uuid, err := uuid.NewRandom() if err != nil { 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 f, err := os.OpenFile(UUID_PATH, mode, 0644) if err != nil { - return nil, err - } - uuid, err := uuid.NewRandom() - if err != nil { - return nil, err + slog.Error(err.Error()) + slog.Warn("Could not open uuid file") + slog.Info("Proccedig without saving node UUID") + return &uuid, nil } n, err := f.Write(uuid[:]) 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") return &uuid, nil