Skip to content

Commit 93c55ba

Browse files
committed
add install descriptor during initial install
1 parent 6fb7c7e commit 93c55ba

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

internal/pkg/agent/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ func ensureInstallMarkerPresent() error {
723723
if err != nil {
724724
return fmt.Errorf("failed to get current file owner: %w", err)
725725
}
726-
if err := install.CreateInstallMarker(paths.Top(), ownership); err != nil {
726+
if err := install.CreateInstallMarker(paths.Top(), ownership, paths.Home(), version.GetAgentPackageVersion()); err != nil {
727727
return fmt.Errorf("unable to create installation marker file during upgrade: %w", err)
728728
}
729729

internal/pkg/agent/install/install.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3032
const (
@@ -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+
529550
func UnprivilegedUser(username, password string) (string, string) {
530551
if username != "" {
531552
return username, password

internal/pkg/agent/install/install_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,19 @@ func TestSetupInstallPath(t *testing.T) {
224224
tmpdir := t.TempDir()
225225
ownership, err := utils.CurrentFileOwner()
226226
require.NoError(t, err)
227-
err = setupInstallPath(tmpdir, ownership)
227+
err = setupInstallPath(tmpdir, ownership, "data/elastic-agent-1.2.3-SNAPSHOT", "1.2.3-SNAPSHOT")
228228
require.NoError(t, err)
229-
require.FileExists(t, filepath.Join(tmpdir, paths.MarkerFileName))
229+
markerFilePath := filepath.Join(tmpdir, paths.MarkerFileName)
230+
require.FileExists(t, markerFilePath)
231+
232+
const expectedInstallDescriptor = `
233+
version: co.elastic.agent/v1
234+
kind: InstallDescriptor
235+
agentInstalls:
236+
- version: 1.2.3-SNAPSHOT
237+
versioned-home: data/elastic-agent-1.2.3-SNAPSHOT
238+
`
239+
actualInstallDescriptorBytes, err := os.ReadFile(markerFilePath)
240+
require.NoError(t, err, "error reading actual install descriptor")
241+
assert.YAMLEq(t, expectedInstallDescriptor, string(actualInstallDescriptorBytes), "expected and actual install descriptor do not match")
230242
}

0 commit comments

Comments
 (0)