From e05083ebeb3575489c6a9797d1c0c0598a75d4e8 Mon Sep 17 00:00:00 2001 From: jamesread Date: Tue, 5 Apr 2022 14:24:40 +0100 Subject: [PATCH] bugfix: installation ID now follows configdir --- cmd/OliveTin/main.go | 13 ++++++++---- internal/updatecheck/updateCheck.go | 31 ++++++++++------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/cmd/OliveTin/main.go b/cmd/OliveTin/main.go index 638bbfc..d456bb5 100644 --- a/cmd/OliveTin/main.go +++ b/cmd/OliveTin/main.go @@ -14,6 +14,7 @@ import ( config "github.com/jamesread/OliveTin/internal/config" "github.com/spf13/viper" "os" + "path" ) var ( @@ -25,7 +26,7 @@ var ( func init() { log.SetFormatter(&log.TextFormatter{ - ForceQuote: true, + ForceQuote: true, DisableTimestamp: true, }) @@ -37,7 +38,7 @@ func init() { log.SetLevel(log.DebugLevel) // Default to debug, to catch cfg issues - var configDir string; + var configDir string flag.StringVar(&configDir, "configdir", ".", "Config directory path") flag.Parse() @@ -82,11 +83,15 @@ func reloadConfig() { } func main() { - log.Info("OliveTin started") + configDir := path.Dir(viper.ConfigFileUsed()) + + log.WithFields(log.Fields{ + "configDir": configDir, + }).Infof("OliveTin started") log.Debugf("Config: %+v", cfg) - go updatecheck.StartUpdateChecker(version, commit, cfg) + go updatecheck.StartUpdateChecker(version, commit, cfg, configDir) go grpcapi.Start(cfg) diff --git a/internal/updatecheck/updateCheck.go b/internal/updatecheck/updateCheck.go index 9875da1..812b0e6 100644 --- a/internal/updatecheck/updateCheck.go +++ b/internal/updatecheck/updateCheck.go @@ -30,24 +30,7 @@ var AvailableVersion = "none" // CurrentVersion is set by the main cmd (which is in tern set as a compile constant) var CurrentVersion = "?" -func getInstanceIDFilename() string { - directory := "./" - - if _, err := os.Stat("/etc/OliveTin"); !os.IsNotExist(err) { - directory = "/etc/OliveTin/" - } - - if _, err := os.Stat("/config"); !os.IsNotExist(err) { - directory = "/config/" - } - - return directory + "installation-id.txt" - -} - -func installationID() string { - filename := getInstanceIDFilename() - +func installationID(filename string) string { content := "unset" contentBytes, err := ioutil.ReadFile(filename) @@ -64,10 +47,18 @@ func installationID() string { fileHandle.Close() } else { content = string(contentBytes) + + _, err := uuid.Parse(content) + + if err != nil { + log.Errorf("Invalid installation ID, %v", err) + content = "invalid-installation-id" + } } log.WithFields(log.Fields{ "content": content, + "from": filename, }).Infof("Installation ID") return content @@ -83,7 +74,7 @@ func isInContainer() bool { // StartUpdateChecker will start a job that runs periodically, checking // for updates. -func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config) { +func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config, configDir string) { CurrentVersion = currentVersion if !cfg.CheckForUpdates { @@ -96,7 +87,7 @@ func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config CurrentCommit: currentCommit, OS: runtime.GOOS, Arch: runtime.GOARCH, - InstallationID: installationID(), + InstallationID: installationID(configDir + "/installation-id.txt"), InContainer: isInContainer(), }