From 1ef78c0fdf4017851a409318fc50e04fe6633752 Mon Sep 17 00:00:00 2001 From: Anthony Lapenna Date: Sun, 16 Aug 2020 10:54:50 +1200 Subject: [PATCH] refactor(core/db): refactor instance ID init --- api/bolt/init.go | 19 ++++++++++++++++++- api/cmd/portainer/main.go | 26 +++++--------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/api/bolt/init.go b/api/bolt/init.go index 6cb80c83a..b67c39df0 100644 --- a/api/bolt/init.go +++ b/api/bolt/init.go @@ -1,13 +1,30 @@ package bolt import ( + "github.com/gofrs/uuid" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/bolt/errors" ) // Init creates the default data set. func (store *Store) Init() error { - _, err := store.SettingsService.Settings() + instanceID, err := store.VersionService.InstanceID() + if err == errors.ErrObjectNotFound { + uid, err := uuid.NewV4() + if err != nil { + return err + } + + instanceID = uid.String() + err = store.VersionService.StoreInstanceID(instanceID) + if err != nil { + return err + } + } else if err != nil { + return err + } + + _, err = store.SettingsService.Settings() if err == errors.ErrObjectNotFound { defaultSettings := &portainer.Settings{ AuthenticationMethod: portainer.AuthenticationInternal, diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index a7fd83d0c..5d83916be 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -6,10 +6,6 @@ import ( "strings" "time" - "github.com/gofrs/uuid" - - "github.com/portainer/portainer/api/bolt/errors" - "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/bolt" "github.com/portainer/portainer/api/chisel" @@ -344,23 +340,6 @@ func main() { dataStore := initDataStore(*flags.Data, fileService) defer dataStore.Close() - instanceID, err := dataStore.Version().InstanceID() - if err == errors.ErrObjectNotFound { - uid, err := uuid.NewV4() - if err != nil { - log.Fatal(err) - } - - instanceID = uid.String() - - err = dataStore.Version().StoreInstanceID(instanceID) - if err != nil { - log.Fatal(err) - } - } else if err != nil { - log.Fatal(err) - } - jwtService, err := initJWTService(dataStore) if err != nil { log.Fatal(err) @@ -383,6 +362,11 @@ func main() { reverseTunnelService := chisel.NewService(dataStore) + instanceID, err := dataStore.Version().InstanceID() + if err != nil { + log.Fatal(err) + } + dockerClientFactory := initDockerClientFactory(digitalSignatureService, reverseTunnelService) kubernetesClientFactory := initKubernetesClientFactory(digitalSignatureService, reverseTunnelService, instanceID)