@@ -17,6 +17,7 @@ import (
1717 "github.com/kardianos/service"
1818 "github.com/otiai10/copy"
1919 "github.com/schollz/progressbar/v3"
20+ "gopkg.in/yaml.v3"
2021
2122 "github.com/elastic/elastic-agent-libs/logp"
2223 "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
@@ -25,6 +26,7 @@ import (
2526 "github.com/elastic/elastic-agent/internal/pkg/cli"
2627 v1 "github.com/elastic/elastic-agent/pkg/api/v1"
2728 "github.com/elastic/elastic-agent/pkg/utils"
29+ manifestutils "github.com/elastic/elastic-agent/pkg/utils/manifest"
2830)
2931
3032const (
@@ -61,17 +63,20 @@ func Install(cfgFile, topPath string, unprivileged bool, log *logp.Logger, pt *p
6163 }
6264 }
6365
64- err = setupInstallPath (topPath , ownership )
65- if err != nil {
66- return utils.FileOwner {}, fmt .Errorf ("error setting up install path: %w" , err )
67- }
68-
6966 manifest , err := readPackageManifest (dir )
7067 if err != nil {
7168 return utils.FileOwner {}, fmt .Errorf ("reading package manifest: %w" , err )
7269 }
7370
7471 pathMappings := manifest .Package .PathMappings
72+ pathMapper := manifestutils .NewPathMapper (pathMappings )
73+
74+ targetVersionedHome := filepath .FromSlash (pathMapper .Map (manifest .Package .VersionedHome ))
75+
76+ err = setupInstallPath (topPath , ownership , targetVersionedHome , manifest .Package .Version )
77+ if err != nil {
78+ return utils.FileOwner {}, fmt .Errorf ("error setting up install path: %w" , err )
79+ }
7580
7681 pt .Describe ("Copying install files" )
7782 copyConcurrency := calculateCopyConcurrency (streams )
@@ -184,7 +189,7 @@ func Install(cfgFile, topPath string, unprivileged bool, log *logp.Logger, pt *p
184189}
185190
186191// setup the basic topPath, and the .installed file
187- func setupInstallPath (topPath string , ownership utils.FileOwner ) error {
192+ func setupInstallPath (topPath string , ownership utils.FileOwner , versionedHome string , version string ) error {
188193 // ensure parent directory exists
189194 err := os .MkdirAll (filepath .Dir (topPath ), 0755 )
190195 if err != nil {
@@ -198,7 +203,7 @@ func setupInstallPath(topPath string, ownership utils.FileOwner) error {
198203 }
199204
200205 // create the install marker
201- if err := CreateInstallMarker (topPath , ownership ); err != nil {
206+ if err := CreateInstallMarker (topPath , ownership , versionedHome , version ); err != nil {
202207 return fmt .Errorf ("failed to create install marker: %w" , err )
203208 }
204209 return nil
@@ -516,16 +521,32 @@ func hasAllSSDs(block ghw.BlockInfo) bool {
516521
517522// CreateInstallMarker creates a `.installed` file at the given install path,
518523// and then calls fixInstallMarkerPermissions to set the ownership provided by `ownership`
519- func CreateInstallMarker (topPath string , ownership utils.FileOwner ) error {
524+ func CreateInstallMarker (topPath string , ownership utils.FileOwner , home string , version string ) error {
520525 markerFilePath := filepath .Join (topPath , paths .MarkerFileName )
521- handle , err := os . Create (markerFilePath )
526+ err := createInstallMarkerFile (markerFilePath , version , home )
522527 if err != nil {
523- return err
528+ return fmt . Errorf ( "creating install marker: %w" , err )
524529 }
525- _ = handle .Close ()
526530 return fixInstallMarkerPermissions (markerFilePath , ownership )
527531}
528532
533+ func createInstallMarkerFile (markerFilePath string , version string , home string ) error {
534+ handle , err := os .Create (markerFilePath )
535+ if err != nil {
536+ return fmt .Errorf ("creating destination file %q : %w" , markerFilePath , err )
537+ }
538+ defer func () {
539+ _ = handle .Close ()
540+ }()
541+ installDescriptor := v1 .NewInstallDescriptor ()
542+ installDescriptor .AgentInstalls = []v1.AgentInstallDesc {{Version : version , VersionedHome : home }}
543+ err = yaml .NewEncoder (handle ).Encode (installDescriptor )
544+ if err != nil {
545+ return fmt .Errorf ("writing install descriptor: %w" , err )
546+ }
547+ return nil
548+ }
549+
529550func UnprivilegedUser (username , password string ) (string , string ) {
530551 if username != "" {
531552 return username , password
0 commit comments