Skip to content

Commit 83cbfb3

Browse files
Upgrade protected
1 parent 0a9752c commit 83cbfb3

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

pkg/local_instance.go

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ package pkg
22

33
import (
44
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"regexp"
9+
"sort"
10+
"strconv"
11+
"strings"
12+
"time"
13+
514
"github.com/magiconair/properties"
615
"github.com/wttech/aemc/pkg/common"
716
"github.com/wttech/aemc/pkg/common/cryptox"
@@ -12,14 +21,6 @@ import (
1221
"github.com/wttech/aemc/pkg/common/stringsx"
1322
"github.com/wttech/aemc/pkg/common/timex"
1423
"github.com/wttech/aemc/pkg/instance"
15-
"os"
16-
"os/exec"
17-
"path/filepath"
18-
"regexp"
19-
"sort"
20-
"strconv"
21-
"strings"
22-
"time"
2324

2425
"github.com/samber/lo"
2526
log "github.com/sirupsen/logrus"
@@ -253,8 +254,22 @@ func (li LocalInstance) Create() error {
253254
return nil
254255
}
255256

257+
// Upgrade performs in-place upgrade of the AEM instance.
258+
// See: https://experienceleague.adobe.com/en/docs/experience-manager-65/content/implementing/deploying/upgrading/in-place-upgrade
256259
func (li LocalInstance) Upgrade() error {
260+
if !li.IsCreated() {
261+
return fmt.Errorf("%s > cannot upgrade as it is not created", li.instance.IDColor())
262+
}
263+
if li.IsRunning() {
264+
return fmt.Errorf("%s > cannot upgrade as it is running", li.instance.IDColor())
265+
}
257266
log.Infof("%s > upgrading", li.instance.IDColor())
267+
268+
// Reset init lock to force re-initialization of sling.properties and other configs on next start.
269+
// This is needed because unpack overwrites sling.properties with default values from the new JAR
270+
if err := li.initLock().Unlock(); err != nil {
271+
return err
272+
}
258273
if err := li.unpackJarFile(); err != nil {
259274
return err
260275
}
@@ -264,6 +279,7 @@ func (li LocalInstance) Upgrade() error {
264279
if err := li.adapt(); err != nil {
265280
return err
266281
}
282+
267283
log.Infof("%s > upgraded", li.instance.IDColor())
268284
return nil
269285
}
@@ -321,31 +337,42 @@ func (li LocalInstance) unpackJarFile() error {
321337
if !pathx.Exists(startScript) {
322338
return fmt.Errorf("%s > unpacking files went wrong as e.g start script does not exist '%s'", li.instance.IDColor(), startScript)
323339
}
340+
if err := li.cleanupOutdatedAppJars(); err != nil {
341+
return err
342+
}
343+
return nil
344+
}
324345

325-
// Only check crx-quickstart/app for JARs
346+
// cleanupOutdatedAppJars removes old JAR files from crx-quickstart/app after upgrade.
347+
// When upgrading AEM, the new quickstart JAR unpacks a new app JAR but leaves the old one.
348+
// This causes conflicts, so we keep only the newest JAR file.
349+
func (li LocalInstance) cleanupOutdatedAppJars() error {
326350
appDir := filepath.Join(li.QuickstartDir(), "app")
327351
jarFiles, err := filepath.Glob(filepath.Join(appDir, "*.jar"))
328352
if err != nil {
329353
return fmt.Errorf("%s > error searching for JAR files in app dir: %w", li.instance.IDColor(), err)
330354
}
331-
if len(jarFiles) > 1 {
332-
type jarInfo struct {
333-
path string
334-
modTime time.Time
335-
}
336-
var jars []jarInfo
337-
for _, path := range jarFiles {
338-
info, err := os.Stat(path)
339-
if err == nil {
340-
jars = append(jars, jarInfo{path, info.ModTime()})
341-
}
355+
if len(jarFiles) <= 1 {
356+
return nil
357+
}
358+
type jarInfo struct {
359+
path string
360+
modTime time.Time
361+
}
362+
var jars []jarInfo
363+
for _, path := range jarFiles {
364+
info, err := os.Stat(path)
365+
if err == nil {
366+
jars = append(jars, jarInfo{path, info.ModTime()})
342367
}
343-
sort.Slice(jars, func(i, j int) bool {
344-
return jars[i].modTime.After(jars[j].modTime)
345-
})
346-
for _, ji := range jars[1:] {
347-
log.Infof("%s > removing outdated JAR from app dir: %s", li.instance.IDColor(), ji.path)
348-
_ = os.Remove(ji.path)
368+
}
369+
sort.Slice(jars, func(i, j int) bool {
370+
return jars[i].modTime.After(jars[j].modTime)
371+
})
372+
for _, ji := range jars[1:] {
373+
log.Infof("%s > removing outdated JAR from app dir: %s", li.instance.IDColor(), ji.path)
374+
if err := os.Remove(ji.path); err != nil {
375+
return fmt.Errorf("%s > cannot remove outdated JAR '%s': %w", li.instance.IDColor(), ji.path, err)
349376
}
350377
}
351378
return nil

0 commit comments

Comments
 (0)