@@ -16,7 +16,7 @@ import (
1616 "strings"
1717 "time"
1818
19- "github.com/otiai10/copy"
19+ filecopy "github.com/otiai10/copy"
2020 "go.elastic.co/apm/v2"
2121
2222 "github.com/elastic/elastic-agent/internal/pkg/agent/application/filelock"
@@ -34,6 +34,7 @@ import (
3434 "github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker"
3535 fleetclient "github.com/elastic/elastic-agent/internal/pkg/fleetapi/client"
3636 "github.com/elastic/elastic-agent/internal/pkg/release"
37+ v1 "github.com/elastic/elastic-agent/pkg/api/v1"
3738 "github.com/elastic/elastic-agent/pkg/control/v2/client"
3839 "github.com/elastic/elastic-agent/pkg/control/v2/cproto"
3940 "github.com/elastic/elastic-agent/pkg/core/logger"
@@ -358,6 +359,9 @@ func (u *Upgrader) Upgrade(ctx context.Context, version string, rollback bool, s
358359 return nil , err
359360 }
360361
362+ //FIXME make it nicer
363+ err = addInstallDesc (version , unpackRes .VersionedHome , unpackRes .Hash , detectedFlavor )
364+
361365 newHash := unpackRes .Hash
362366 if newHash == "" {
363367 return nil , errors .New ("unknown hash" )
@@ -457,6 +461,65 @@ func (u *Upgrader) Upgrade(ctx context.Context, version string, rollback bool, s
457461 return cb , nil
458462}
459463
464+ func addInstallDesc (version string , home string , hash string , flavor string ) error {
465+ installMarkerFilePath := filepath .Join (paths .Top (), paths .MarkerFileName )
466+ installDescriptor , err := readInstallMarker (installMarkerFilePath )
467+ if err != nil {
468+ return err
469+ }
470+
471+ if installDescriptor == nil {
472+ return fmt .Errorf ("no install descriptor found at %q" )
473+ }
474+
475+ existingInstalls := installDescriptor .AgentInstalls
476+ installDescriptor .AgentInstalls = make ([]v1.AgentInstallDesc , len (existingInstalls )+ 1 )
477+ newInstall := v1.AgentInstallDesc {
478+ Version : version ,
479+ Hash : hash ,
480+ VersionedHome : home ,
481+ Flavor : flavor ,
482+ }
483+ installDescriptor .AgentInstalls [0 ] = newInstall
484+ copied := copy (installDescriptor .AgentInstalls [1 :], existingInstalls )
485+ if copied != len (existingInstalls ) {
486+ return fmt .Errorf ("error adding new install %v to existing installs %v" , newInstall , existingInstalls )
487+ }
488+
489+ err = writeInstallMarker (installMarkerFilePath , installDescriptor )
490+ if err != nil {
491+ return fmt .Errorf ("writing updated install marker: %w" , err )
492+ }
493+
494+ return nil
495+ }
496+
497+ func writeInstallMarker (markerFilePath string , descriptor * v1.InstallDescriptor ) error {
498+ installMarkerFile , err := os .Create (markerFilePath )
499+ if err != nil {
500+ return fmt .Errorf ("opening install marker file: %w" , err )
501+ }
502+ defer func (installMarkerFile * os.File ) {
503+ _ = installMarkerFile .Close ()
504+ }(installMarkerFile )
505+ return v1 .WriteInstallDescriptor (installMarkerFile , descriptor )
506+ }
507+
508+ func readInstallMarker (markerFilePath string ) (* v1.InstallDescriptor , error ) {
509+ installMarkerFile , err := os .Open (markerFilePath )
510+ if err != nil {
511+ return nil , fmt .Errorf ("opening install marker file: %w" , err )
512+ }
513+ defer func (installMarkerFile * os.File ) {
514+ _ = installMarkerFile .Close ()
515+ }(installMarkerFile )
516+ installDescriptor , err := v1 .ParseInstallDescriptor (installMarkerFile )
517+ if err != nil {
518+ return nil , fmt .Errorf ("parsing install marker file: %w" , err )
519+ }
520+ return installDescriptor , nil
521+ }
522+
460523func (u * Upgrader ) rollbackToPreviousVersion (ctx context.Context , topDir string , now time.Time , version string , action * fleetapi.ActionUpgrade ) (reexec.ShutdownCallbackFn , error ) {
461524 if version == "" {
462525 return nil , ErrEmptyRollbackVersion
@@ -814,9 +877,9 @@ func copyDir(l *logger.Logger, from, to string, ignoreErrs bool, fileDirCopy fil
814877 copyConcurrency = runtime .NumCPU () * 4
815878 }
816879
817- return fileDirCopy (from , to , copy .Options {
818- OnSymlink : func (_ string ) copy .SymlinkAction {
819- return copy .Shallow
880+ return fileDirCopy (from , to , filecopy .Options {
881+ OnSymlink : func (_ string ) filecopy .SymlinkAction {
882+ return filecopy .Shallow
820883 },
821884 Sync : true ,
822885 OnError : onErr ,
0 commit comments