Skip to content

Commit

Permalink
Merge pull request #61 from wttech/instance-recreate-check
Browse files Browse the repository at this point in the history
Instance recreate check
  • Loading branch information
krystian-panek-vmltech authored Feb 9, 2023
2 parents bd53856 + d175b9e commit 17e88eb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
45 changes: 33 additions & 12 deletions pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/wttech/aemc/pkg/instance"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/local_instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion project/classic/aem/script/up.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env sh

step "creating instances"
step "creating instances (it may take a moment)"
aem instance create
clc

Expand Down
2 changes: 1 addition & 1 deletion project/cloud/aem/script/up.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env sh

step "creating instances"
step "creating instances (it may take a moment)"
aem instance create
clc

Expand Down

0 comments on commit 17e88eb

Please sign in to comment.