Skip to content

Commit

Permalink
Merge pull request #67 from wttech/instance-log-style
Browse files Browse the repository at this point in the history
Instance log style
  • Loading branch information
krystian-panek-vmltech authored Feb 15, 2023
2 parents 7136b2e + a38eee4 commit 1238f26
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 203 deletions.
6 changes: 3 additions & 3 deletions cmd/aem/instance_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (c *CLI) instanceBackupPerformCmd() *cobra.Command {
for _, instance := range instances {
local := instance.Local()
if !local.IsCreated() {
log.Infof("skipping making backup of instance '%s' as it is not created", instance.ID())
log.Infof("%s > skipping making backup as it is not created", instance.ID())
continue
}
file := local.ProposeBackupFileToMake()
Expand Down Expand Up @@ -204,12 +204,12 @@ func (c *CLI) instanceBackupRestoreCmd() *cobra.Command {
for _, instance := range instances {
local := instance.Local()
if local.IsCreated() {
log.Infof("skipping using backup for instance '%s' as it is already created", instance.ID())
log.Infof("%s > skipping using backup as it is already created", instance.ID())
continue
}
file, err := local.ProposeBackupFileToUse()
if err != nil {
log.Warnf("skipping using backup for instance '%s' as cannot find any: %s", instance.ID(), err)
log.Warnf("%s > skipping using backup as cannot find any: %s", instance.ID(), err)
continue
}
running := local.IsRunning()
Expand Down
Binary file modified docs/cli-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions pkg/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func localHosts() []string {
func (i Instance) TimeLocation() *time.Location {
loc, err := i.status.TimeLocation()
if err != nil {
log.Debugf("cannot determine time location of instance '%s': %s", i.id, err)
log.Debugf("%s > cannot determine time location: %s", i.ID(), err)
return time.Now().Location()
}
return loc
Expand All @@ -185,7 +185,7 @@ func (i Instance) AemVersion() string {
// TODO try to retrieve version from filename 'aem/home/instance/[author|publish]/crx-quickstart/app/cq-quickstart-6.5.0-standalone-quickstart.jar'
version, err := i.status.AemVersion()
if err != nil {
log.Debugf("cannot determine AEM version of instance '%s': %s", i.id, err)
log.Debugf("%s > cannot determine AEM version: %s", i.ID(), err)
return instance.AemVersionUnknown
}
return version
Expand All @@ -194,7 +194,7 @@ func (i Instance) AemVersion() string {
func (i Instance) RunModes() []string {
runModes, err := i.status.RunModes()
if err != nil {
log.Debugf("cannot determine run modes of instance '%s': %s", i.id, err)
log.Debugf("%s > cannot determine run modes: %s", i.ID(), err)
return []string{}
}
return runModes
Expand Down
6 changes: 2 additions & 4 deletions pkg/instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,9 @@ func InstanceMsg(instances []Instance, msg string) string {
count := len(instances)
switch count {
case 0:
return fmt.Sprintf("none of instances: %s", msg)
case 1:
return fmt.Sprintf("instance '%s': %s", instances[0].id, msg)
return fmt.Sprintf("[] > %s", msg)
default:
return fmt.Sprintf("instances '%s': %s", InstanceIds(instances), msg)
return fmt.Sprintf("%s > %s", InstanceIds(instances), msg)
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/instance_manager_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func (im *InstanceManager) CheckOne(i Instance, checks []Checker) ([]CheckResult
result := check.Check(i)
results = append(results, result)
if result.abort {
log.Fatalf("instance '%s': %s", i.ID(), result.message)
log.Fatalf("%s > %s", i.ID(), result.message)
}
if result.err != nil {
log.Infof("instance '%s': %s", i.ID(), result.err)
log.Infof("%s > %s", i.ID(), result.err)
} else if len(result.message) > 0 {
log.Infof("instance '%s': %s", i.ID(), result.message)
log.Infof("%s > %s", i.ID(), result.message)
}
if !result.ok && check.Spec().Mandatory {
break
Expand Down
84 changes: 42 additions & 42 deletions pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewLocal(i *Instance) *LocalInstance {

func (li LocalInstance) State() LocalInstanceState {
return LocalInstanceState{
ID: li.instance.id,
ID: li.instance.ID(),
URL: li.instance.http.BaseURL(),
Attributes: li.instance.Attributes(),
AemVersion: li.instance.AemVersion(),
Expand Down Expand Up @@ -167,26 +167,26 @@ func (li LocalInstance) checkRecreationNeeded() error {
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 fmt.Errorf("%s > outdated and need to be recreated as 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("instance '%s' has a password which does not match regex '%s'", li.instance.ID(), LocalInstancePasswordRegex)
return fmt.Errorf("%s > password does not match regex '%s'", li.instance.ID(), LocalInstancePasswordRegex)
}
return nil
}

func (li LocalInstance) Create() error {
log.Infof("creating instance '%s'", li.instance.ID())
log.Infof("%s > creating", 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)
return fmt.Errorf("%s > cannot delete its dir: %w", li.instance.ID(), err)
}
if err := pathx.Ensure(li.Dir()); err != nil {
return fmt.Errorf("cannot create dir for instance '%s': %w", li.instance.ID(), err)
return fmt.Errorf("%s > cannot create its dir: %w", li.instance.ID(), err)
}
if err := li.unpackJarFile(); err != nil {
return err
Expand All @@ -203,7 +203,7 @@ func (li LocalInstance) Create() error {
if err := li.createLock().Lock(); err != nil {
return err
}
log.Infof("created instance '%s'", li.instance.ID())
log.Infof("%s > created", li.instance.ID())
return nil
}

Expand All @@ -223,7 +223,7 @@ type localInstanceCreateLock struct {
}

func (li LocalInstance) unpackJarFile() error {
log.Infof("unpacking files for instance '%s'", li.instance.ID())
log.Infof("%s > unpacking files", li.instance.ID())
jar, err := li.Opts().Jar()
if err != nil {
return err
Expand All @@ -235,7 +235,7 @@ func (li LocalInstance) unpackJarFile() error {
cmd.Dir = li.Dir()
li.instance.manager.aem.CommandOutput(cmd)
if err := cmd.Run(); err != nil {
return fmt.Errorf("cannot unpack files for instance '%s': %w", li.instance.ID(), err)
return fmt.Errorf("%s > cannot unpack files: %w", li.instance.ID(), err)
}
return nil
}
Expand Down Expand Up @@ -302,12 +302,12 @@ func (li LocalInstance) IsInitialized() bool {

func (li LocalInstance) Start() error {
if !li.IsCreated() {
return fmt.Errorf("cannot start instance '%s' as it is not created", li.instance.ID())
return fmt.Errorf("%s > cannot start as it is not created", li.instance.ID())
}
if err := li.update(); err != nil {
return err
}
log.Infof("starting instance '%s'", li.instance.ID())
log.Infof("%s > starting", li.instance.ID())
if err := li.checkPortsOpen(); err != nil {
return err
}
Expand All @@ -316,15 +316,15 @@ func (li LocalInstance) Start() error {
return err
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("cannot execute start script for instance '%s': %w", li.instance.ID(), err)
return fmt.Errorf("%s > cannot execute start script: %w", li.instance.ID(), err)
}
if err := li.awaitAuth(); err != nil {
return err
}
if err := li.startLock().Lock(); err != nil {
return err
}
log.Infof("started instance '%s'", li.instance.ID())
log.Infof("%s > started", li.instance.ID())
return nil
}

Expand Down Expand Up @@ -403,15 +403,15 @@ func (li LocalInstance) recreateSlingPropsFile() error {
propsCombined := append(li.SlingProps, "org.apache.felix.configadmin.plugin.interpolation.secretsdir=${sling.home}/"+LocalInstanceSecretsDir)
propsLoaded, err := properties.LoadString(strings.Join(propsCombined, "\n"))
if err != nil {
return fmt.Errorf("cannot parse Sling properties of instance '%s'", li.instance.ID())
return fmt.Errorf("%s > cannot parse Sling properties file '%s': %w", li.instance.ID(), filePath, err)
}
file, err := os.Create(filePath)
defer file.Close()
_, err = propsLoaded.Write(file, properties.ISO_8859_1)
if err != nil {
return fmt.Errorf("cannot save Sling properties file '%s'", filePath)
return fmt.Errorf("%s > cannot save Sling properties file '%s'", li.instance.ID(), filePath)
}
log.Infof("configured instance Sling properties in file '%s'", filePath)
log.Infof("%s > configured instance Sling properties in file '%s'", li.instance.ID(), filePath)
return nil
}

Expand Down Expand Up @@ -489,9 +489,9 @@ func (li LocalInstance) Stop() error {
return err
}
if err := cmd.Run(); err != nil {
return fmt.Errorf("cannot execute stop script for instance '%s': %w", li.instance.ID(), err)
return fmt.Errorf("%s > cannot execute stop script : %w", li.instance.ID(), err)
}
log.Infof("stopped instance '%s'", li.instance.ID())
log.Infof("%s > stopped", li.instance.ID())
return nil
}

Expand All @@ -506,12 +506,12 @@ func (li LocalInstance) StopAndAwait() error {
}

func (li LocalInstance) Clean() error {
log.Infof("cleaning instance '%s'", li.instance.ID())
log.Infof("%s > cleaning", li.instance.ID())
err := pathx.DeleteIfExists(li.pidFile())
if err != nil {
return err
}
log.Infof("cleaned instance '%s'", li.instance.ID())
log.Infof("%s > cleaned", li.instance.ID())
return nil
}

Expand Down Expand Up @@ -543,7 +543,7 @@ func (li LocalInstance) IsKillable() bool {
}

func (li LocalInstance) Kill() error {
log.Infof("killing instance '%s'", li.instance.ID())
log.Infof("%s > killing", li.instance.ID())
pid, err := li.PID()
if err != nil {
return err
Expand All @@ -558,13 +558,13 @@ func (li LocalInstance) Kill() error {
li.instance.manager.aem.CommandOutput(cmd)

if err := cmd.Run(); err != nil {
return fmt.Errorf("cannot execute kill command for instance '%s' with PID '%d': %w", li.instance.ID(), pid, err)
return fmt.Errorf("%s > cannot execute kill command with PID '%d': %w", li.instance.ID(), pid, err)
}
file := li.pidFile()
if err := pathx.DeleteIfExists(file); err != nil {
return fmt.Errorf("cannot delete PID file '%s' for instance '%s': %w", file, li.instance.ID(), err)
return fmt.Errorf("%s > cannot delete PID file '%s': %w", li.instance.ID(), file, err)
}
log.Infof("killed instance '%s'", li.instance.ID())
log.Infof("%s > killed", li.instance.ID())
return nil
}

Expand All @@ -576,9 +576,9 @@ func (li LocalInstance) Await(stateChecker func() error, timeout time.Duration)
break
}
if time.Now().After(started.Add(timeout)) {
return fmt.Errorf("instance '%s' awaiting reached timeout after %s", li.Instance().ID(), timeout)
return fmt.Errorf("%s > awaiting reached timeout after %s", li.Instance().ID(), timeout)
}
log.Infof("%s | %s", li.instance.ID(), err)
log.Infof("%s > %s", li.instance.ID(), err)
time.Sleep(time.Second * 5)
}
return nil
Expand Down Expand Up @@ -608,7 +608,7 @@ func (li LocalInstance) awaitAuth() error {
return nil
}

log.Infof("awaiting auth on instance '%s'", li.instance.ID())
log.Infof("%s > awaiting auth", li.instance.ID())
err := li.Await(func() error {
address := fmt.Sprintf("%s:%s", li.instance.http.Hostname(), li.instance.http.Port())
reachable, _ := netx.IsReachable(li.instance.http.Hostname(), li.instance.http.Port(), time.Second*3)
Expand All @@ -632,7 +632,7 @@ func (li LocalInstance) awaitAuth() error {
if err != nil {
return err
}
log.Infof("awaited auth on instance '%s'", li.instance.ID())
log.Infof("%s > awaited auth", li.instance.ID())
return nil
}

Expand Down Expand Up @@ -707,12 +707,12 @@ func (li LocalInstance) Delete() error {
if li.IsRunning() {
return fmt.Errorf("cannot delete instance as it is running")
}
log.Infof("deleting instance '%s'", li.instance.ID())
log.Infof("%s > deleting", li.instance.ID())
if err := pathx.Delete(li.Dir()); err != nil {
return fmt.Errorf("cannot delete instance properly: %w", err)

}
log.Infof("deleted instance '%s'", li.instance.ID())
log.Infof("%s > deleted", li.instance.ID())
return nil
}

Expand Down Expand Up @@ -772,7 +772,7 @@ func (li LocalInstance) OutOfDate() bool {
func (li LocalInstance) UpToDate() bool {
check, err := li.startLock().State()
if err != nil {
log.Debugf("cannot check if instance '%s' is up-to-date: %s", li.instance.ID(), err)
log.Debugf("%s > cannot check if is up-to-date: %s", li.instance.ID(), err)
return false
}
return check.UpToDate
Expand Down Expand Up @@ -807,17 +807,17 @@ func (li LocalInstance) ProposeBackupFileToMake() string {

func (li LocalInstance) MakeBackup(file string) error {
if !li.IsCreated() {
return fmt.Errorf("cannot make backup of instance '%s' to file '%s' - instance not created", li.instance.ID(), file)
return fmt.Errorf("%s > cannot make backup to file '%s' as instance not created", li.instance.ID(), file)
}
if li.IsRunning() {
return fmt.Errorf("cannot make a backup of instance '%s' to file '%s' - instance cannot be running", li.instance.ID(), file)
return fmt.Errorf("%s > cannot make a backup to file '%s' as instance cannot be running", li.instance.ID(), file)
}
log.Infof("making backup of instance '%s' to file '%s'", li.instance.ID(), file)
log.Infof("%s > making backup to file '%s'", li.instance.ID(), file)
_, err := filex.ArchiveWithChanged(li.Dir(), file)
if err != nil {
return fmt.Errorf("cannot make a backup of instance '%s' to file '%s': %w", li.instance.ID(), file, err)
return fmt.Errorf("%s > cannot make a backup to file '%s': %w", li.instance.ID(), file, err)
}
log.Infof("made backup of instance '%s' to file '%s'", li.instance.ID(), file)
log.Infof("%s > made backup to file '%s'", li.instance.ID(), file)
return nil
}

Expand All @@ -834,28 +834,28 @@ func (li LocalInstance) ProposeBackupFileToUse() (string, error) {
}
file, err := pathx.GlobSome(pathPattern)
if err != nil {
return "", fmt.Errorf("no backup file found to use for instance '%s': %w", li.instance.ID(), err)
return "", fmt.Errorf("%s > no backup file found to use: %w", li.instance.ID(), err)
}
return file, nil
}

func (li LocalInstance) UseBackup(file string, deleteCreated bool) error {
if li.IsRunning() {
return fmt.Errorf("cannot use backup of instance '%s' from file '%s' - instance cannot be running", li.instance.ID(), file)
return fmt.Errorf("%s > cannot use backup from file '%s' as instance cannot be running", li.instance.ID(), file)
}
if li.IsCreated() {
if !deleteCreated {
return fmt.Errorf("cannot use backup of instance '%s' from file '%s' - instance already created", li.instance.ID(), file)
return fmt.Errorf("%s > cannot use backup from file '%s' as instance is already created", li.instance.ID(), file)
}
if err := li.Delete(); err != nil {
return err
}
}
log.Infof("using backup of instance '%s' from file '%s'", li.instance.ID(), file)
log.Infof("%s > using backup from file '%s'", li.instance.ID(), file)
_, err := filex.UnarchiveWithChanged(file, li.Dir())
if err != nil {
return fmt.Errorf("cannot use a backup of instance '%s' from file '%s': %w", li.instance.ID(), file, err)
return fmt.Errorf("%s > cannot use a backup from file '%s': %w", li.instance.ID(), file, err)
}
log.Infof("used backup of instance '%s' from file '%s'", li.instance.ID(), file)
log.Infof("%s > used backup from file '%s'", li.instance.ID(), file)
return nil
}
Loading

0 comments on commit 1238f26

Please sign in to comment.