Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filemarker creation process, update error message on uninstall #4172

Merged
merged 40 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fdf8b28
move install file marker create, rename methods, change error message
fearful-symmetry Jan 31, 2024
e6b77f1
change error message
fearful-symmetry Jan 31, 2024
1e52607
change error message
fearful-symmetry Jan 31, 2024
400bb0d
remove calls to InstallMarkerExists
fearful-symmetry Jan 31, 2024
43bf5f0
fix windows helper
fearful-symmetry Jan 31, 2024
75c93dc
fix windows, again
fearful-symmetry Jan 31, 2024
d7538fe
Revert "remove calls to InstallMarkerExists"
fearful-symmetry Feb 5, 2024
fdb90cc
change install file locaton, revert removal of filepath.Dir
fearful-symmetry Feb 5, 2024
97fdec6
fix wording
fearful-symmetry Feb 6, 2024
297bb1a
Merge remote-tracking branch 'upstream/main' into uninstall-filemarker
fearful-symmetry Feb 6, 2024
10d9218
move IsUpgradeable, remove duplication
fearful-symmetry Feb 7, 2024
a398e25
add tests
fearful-symmetry Feb 7, 2024
7960e16
add mostly useless test
fearful-symmetry Feb 7, 2024
5506e07
linter
fearful-symmetry Feb 7, 2024
bb3416d
tinkering with CI
fearful-symmetry Feb 7, 2024
13bd8f6
skip supervisor test on windows
fearful-symmetry Feb 7, 2024
ba39105
tinkering with the linter
fearful-symmetry Feb 7, 2024
ab8797f
remove experiment
fearful-symmetry Feb 7, 2024
d0048f9
refactor install to make adding tests easier
fearful-symmetry Feb 9, 2024
995a1f3
linter
fearful-symmetry Feb 9, 2024
140752e
add more tests...
fearful-symmetry Feb 9, 2024
088232d
still fixing tests
fearful-symmetry Feb 9, 2024
ee51e43
sonarQube
fearful-symmetry Feb 9, 2024
49b84f8
fix windows tests, fight sonarqube
fearful-symmetry Feb 10, 2024
6d3edba
tinker with file marker create
fearful-symmetry Feb 10, 2024
3f4a7f0
Merge remote-tracking branch 'upstream/main' into uninstall-filemarker
fearful-symmetry Feb 12, 2024
ef4b866
move install path
fearful-symmetry Feb 12, 2024
2911ad4
fix bad merge
fearful-symmetry Feb 12, 2024
59b9d29
still cleaning up merge
fearful-symmetry Feb 12, 2024
62f0c01
add tests
fearful-symmetry Feb 12, 2024
9c20838
add tests, docs
fearful-symmetry Feb 13, 2024
aa4e4c1
fix tests
fearful-symmetry Feb 13, 2024
791ba0e
don't check mode bits
fearful-symmetry Feb 13, 2024
b848e5b
move command, fix permissions
fearful-symmetry Feb 13, 2024
91ee83d
SonarQube...
fearful-symmetry Feb 14, 2024
4cff2f1
refactor dir setup to make Sonarqube happy
fearful-symmetry Feb 14, 2024
c0e82e9
add changelog
fearful-symmetry Feb 14, 2024
28ad7df
Update changelog/fragments/1707951532-change-install-marker-creation.…
fearful-symmetry Feb 15, 2024
d6d0fac
Merge branch 'main' into uninstall-filemarker
fearful-symmetry Feb 20, 2024
e2d5d89
Merge branch 'main' into uninstall-filemarker
cmacknz Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions internal/pkg/agent/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ func Install(cfgFile, topPath string, unprivileged bool, log *logp.Logger, pt *p
}
}

// ensure parent directory exists
err = os.MkdirAll(filepath.Dir(topPath), 0755)
err = setupInstallPath(topPath, ownership)
if err != nil {
return utils.FileOwner{}, errors.New(
err,
fmt.Sprintf("failed to create installation parent directory (%s)", filepath.Dir(topPath)),
errors.M("directory", filepath.Dir(topPath)))
return utils.FileOwner{}, fmt.Errorf("error setting up install path: %w", err)
}

manifest, err := readPackageManifest(dir)
Expand Down Expand Up @@ -173,11 +169,6 @@ func Install(cfgFile, topPath string, unprivileged bool, log *logp.Logger, pt *p
}
}

// create the install marker
if err := CreateInstallMarker(topPath, ownership); err != nil {
return utils.FileOwner{}, fmt.Errorf("failed to create install marker: %w", err)
}

// post install (per platform)
err = postInstall(topPath)
if err != nil {
Expand Down Expand Up @@ -216,6 +207,27 @@ func Install(cfgFile, topPath string, unprivileged bool, log *logp.Logger, pt *p
return ownership, nil
}

// setup the basic topPath, and the .installed file
func setupInstallPath(topPath string, ownership utils.FileOwner) error {
// ensure parent directory exists
err := os.MkdirAll(filepath.Dir(topPath), 0755)
if err != nil {
return errors.New(err, fmt.Sprintf("failed to create installation parent directory (%s)", filepath.Dir(topPath)), errors.M("directory", filepath.Dir(topPath)))
}

// create Agent/ directory with more locked-down permissions
err = os.MkdirAll(topPath, 0750)
if err != nil {
return errors.New(err, fmt.Sprintf("failed to create top path (%s)", topPath), errors.M("directory", topPath))
}

// create the install marker
if err := CreateInstallMarker(topPath, ownership); err != nil {
return fmt.Errorf("failed to create install marker: %w", err)
}
return nil
}

func readPackageManifest(extractedPackageDir string) (*v1.PackageManifest, error) {
manifestFilePath := filepath.Join(extractedPackageDir, v1.ManifestFileName)
manifestFile, err := os.Open(manifestFilePath)
Expand Down Expand Up @@ -473,10 +485,14 @@ func hasAllSSDs(block ghw.BlockInfo) bool {
return true
}

// CreateInstallMarker creates a `.installed` file at the given install path,
// and then calls fixInstallMarkerPermissions to set the ownership provided by `ownership`
func CreateInstallMarker(topPath string, ownership utils.FileOwner) error {
markerFilePath := filepath.Join(topPath, paths.MarkerFileName)
if _, err := os.Create(markerFilePath); err != nil {
handle, err := os.Create(markerFilePath)
Copy link
Contributor

@michalpristas michalpristas Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 nice catch. we should have a lint rule for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the windows CI tests will fail on this if you leave the file open in a temp directory created by the golang testing library.

if err != nil {
return err
}
_ = handle.Close()
return fixInstallMarkerPermissions(markerFilePath, ownership)
}
9 changes: 9 additions & 0 deletions internal/pkg/agent/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/cli"
v1 "github.com/elastic/elastic-agent/pkg/api/v1"
"github.com/elastic/elastic-agent/pkg/utils"
)

func TestHasAllSSDs(t *testing.T) {
Expand Down Expand Up @@ -185,3 +187,10 @@ func TestCopyFiles(t *testing.T) {
}

}

func TestSetupInstallPath(t *testing.T) {
tmpdir := t.TempDir()
err := setupInstallPath(tmpdir, utils.CurrentFileOwner())
require.NoError(t, err)
require.FileExists(t, filepath.Join(tmpdir, paths.MarkerFileName))
}
Loading