From d175b9ee340b591e299a807c28ab56c046fb4694 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Thu, 9 Feb 2023 16:12:00 +0100 Subject: [PATCH] Instance recreate check --- pkg/local_instance.go | 45 +++++++++++++++++++++++--------- pkg/local_instance_manager.go | 5 ++++ pkg/sdk.go | 8 +++--- project/classic/aem/script/up.sh | 2 +- project/cloud/aem/script/up.sh | 2 +- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/pkg/local_instance.go b/pkg/local_instance.go index 8a4d6168..73ec7ee4 100644 --- a/pkg/local_instance.go +++ b/pkg/local_instance.go @@ -14,6 +14,7 @@ import ( "github.com/wttech/aemc/pkg/instance" "os" "os/exec" + "path/filepath" "regexp" "sort" "strconv" @@ -149,16 +150,37 @@ var ( ) func (li LocalInstance) Validate() error { + if err := li.checkPassword(); err != nil { + return err + } + if err := li.checkRecreationNeeded(); err != nil { + return err + } + return nil +} + +func (li LocalInstance) checkRecreationNeeded() error { + createLock := li.createLock() + if createLock.IsLocked() { + state, err := createLock.State() + if err != nil { + return err + } + if !state.UpToDate { + return fmt.Errorf("instance '%s' is outdated and need to be recreated; distribution JAR changed from '%s' to '%s'", li.instance.ID(), state.Locked.JarName, state.Current.JarName) + } + } + return nil +} + +func (li LocalInstance) checkPassword() error { if !LocalInstancePasswordRegex.MatchString(li.instance.password) { - return fmt.Errorf("password for instance '%s' need to match regex '%s'", li.instance.ID(), LocalInstancePasswordRegex) + return fmt.Errorf("instance '%s' has a password which does not match regex '%s'", li.instance.ID(), LocalInstancePasswordRegex) } return nil } func (li LocalInstance) Create() error { - if err := li.Validate(); err != nil { - return err - } log.Infof("creating instance '%s'", li.instance.ID()) if err := pathx.DeleteIfExists(li.Dir()); err != nil { return fmt.Errorf("cannot clean up dir for instance '%s': %w", li.instance.ID(), err) @@ -187,12 +209,17 @@ func (li LocalInstance) Create() error { func (li LocalInstance) createLock() osx.Lock[localInstanceCreateLock] { return osx.NewLock(fmt.Sprintf("%s/create.yml", li.LockDir()), func() (localInstanceCreateLock, error) { - return localInstanceCreateLock{Created: time.Now()}, nil + var zero localInstanceCreateLock + jar, err := li.Opts().Jar() + if err != nil { + return zero, err + } + return localInstanceCreateLock{JarName: filepath.Base(jar)}, nil }) } type localInstanceCreateLock struct { - Created time.Time `yaml:"created"` + JarName string `yaml:"jar_name"` } func (li LocalInstance) unpackJarFile() error { @@ -277,9 +304,6 @@ func (li LocalInstance) Start() error { if !li.IsCreated() { return fmt.Errorf("cannot start instance '%s' as it is not created", li.instance.ID()) } - if err := li.Validate(); err != nil { - return err - } if err := li.update(); err != nil { return err } @@ -459,9 +483,6 @@ func (li LocalInstance) Stop() error { if !li.IsCreated() { return fmt.Errorf("cannot stop instance as it is not created") } - if err := li.Validate(); err != nil { - return err - } log.Infof("stopping instance '%s'", li.instance.ID()) cmd, err := li.binScriptCommand(LocalInstanceScriptStop, true) if err != nil { diff --git a/pkg/local_instance_manager.go b/pkg/local_instance_manager.go index dbd80ae3..411f2005 100644 --- a/pkg/local_instance_manager.go +++ b/pkg/local_instance_manager.go @@ -71,6 +71,11 @@ func (o *LocalOpts) Initialize() error { return err } } + for _, instance := range o.manager.Locals() { + if err := instance.Local().Validate(); err != nil { + return err + } + } // preparation phase if err := o.manager.aem.baseOpts.Prepare(); err != nil { return err diff --git a/pkg/sdk.go b/pkg/sdk.go index 0a1f15cc..183ffac0 100644 --- a/pkg/sdk.go +++ b/pkg/sdk.go @@ -47,10 +47,10 @@ func (s SDK) Prepare() error { return err } if check.UpToDate { - log.Debugf("existing SDK '%s' is up-to-date", check.Current.Version) + log.Debugf("existing SDK '%s' is up-to-date", zipFile) return nil } - log.Infof("preparing new SDK '%s'", check.Current.Version) + log.Infof("preparing new SDK '%s'", zipFile) err = s.prepare(zipFile) if err != nil { return err @@ -59,13 +59,13 @@ func (s SDK) Prepare() error { if err != nil { return err } - log.Infof("prepared new SDK '%s'", check.Current.Version) + log.Infof("prepared new SDK '%s'", zipFile) jar, err := s.QuickstartJar() if err != nil { return err } - log.Debugf("found JAR '%s' in unpacked SDK '%s'", jar, check.Current.Version) + log.Debugf("found JAR '%s' in unpacked SDK '%s'", jar, zipFile) return nil } diff --git a/project/classic/aem/script/up.sh b/project/classic/aem/script/up.sh index c97604de..d16b964b 100644 --- a/project/classic/aem/script/up.sh +++ b/project/classic/aem/script/up.sh @@ -1,6 +1,6 @@ #!/usr/bin/env sh -step "creating instances" +step "creating instances (it may take a moment)" aem instance create clc diff --git a/project/cloud/aem/script/up.sh b/project/cloud/aem/script/up.sh index c97604de..d16b964b 100644 --- a/project/cloud/aem/script/up.sh +++ b/project/cloud/aem/script/up.sh @@ -1,6 +1,6 @@ #!/usr/bin/env sh -step "creating instances" +step "creating instances (it may take a moment)" aem instance create clc