From 117d7def0e93676f677abfa42e1adb2fd0100e4b Mon Sep 17 00:00:00 2001 From: Lee E Hinman <57081003+leehinman@users.noreply.github.com> Date: Tue, 29 Aug 2023 11:52:49 -0700 Subject: [PATCH] modify runtime variable 'install.in_default' with rpm/deb (#3309) * modify runtime variable 'install.in_default' with rpm/deb modify the `install.in_default` runtime protection variable so that when elastic-agent is installed via deb or rpm package manager it is still considered to be in the default path. This is necessary because endpoint supports being a component when elastic-agent is installed via rpm/deb. * fix typos in comments --- docs/component-specs.md | 3 +- internal/pkg/agent/install/install_unix.go | 52 ---------------- internal/pkg/agent/install/install_windows.go | 6 -- internal/pkg/agent/install/installed.go | 3 +- .../pkg/agent/install/pkgmgr/pkgmgr_unix.go | 61 +++++++++++++++++++ .../agent/install/pkgmgr/pkgmgr_windows.go | 12 ++++ pkg/component/component.go | 3 +- 7 files changed, 79 insertions(+), 61 deletions(-) create mode 100644 internal/pkg/agent/install/pkgmgr/pkgmgr_unix.go create mode 100644 internal/pkg/agent/install/pkgmgr/pkgmgr_windows.go diff --git a/docs/component-specs.md b/docs/component-specs.md index 212e30a0158..5379bda30cd 100644 --- a/docs/component-specs.md +++ b/docs/component-specs.md @@ -91,6 +91,7 @@ The variables that can be accessed by a condition are: - `runtime.family`: OS family, e.g. `"debian"`, `"redhat"`, `"windows"`, `"darwin"` - `runtime.major`, `runtime.minor`: the operating system version. Note that these are strings not integers, so they must be converted in order to use numeric comparison. For example to check if the OS major version is at most 12, use `number(runtime.major) <= 12`. - `user.root`: true if Agent is being run with root / administrator permissions. +- `install.in_default`: true if the Agent is installed in the default location or has been installed via deb or rpm. ### `command` (required for shipper) @@ -190,4 +191,4 @@ operations: #### `service.timeouts.checkin` -The timeout duration for checkins with this component \ No newline at end of file +The timeout duration for checkins with this component diff --git a/internal/pkg/agent/install/install_unix.go b/internal/pkg/agent/install/install_unix.go index cdd7a2c4f52..b6c7a100b4b 100644 --- a/internal/pkg/agent/install/install_unix.go +++ b/internal/pkg/agent/install/install_unix.go @@ -6,60 +6,8 @@ package install -import ( - "os/exec" - "runtime" - "strings" - - "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" -) - // postInstall performs post installation for unix-based systems. func postInstall(topPath string) error { // do nothing return nil } - -func checkPackageInstall() bool { - if runtime.GOOS != "linux" { - return false - } - - binaryName := paths.BinaryName - - // NOTE searching for english words might not be a great idea as far as portability goes. - // list all installed packages then search for paths.BinaryName? - // dpkg is strange as the remove and purge processes leads to the package bing isted after a remove, but not after a purge - - // check debian based systems (or systems that use dpkg) - // If the package has been installed, the status starts with "install" - // If the package has been removed (but not pruged) status starts with "deinstall" - // If purged or never installed, rc is 1 - if _, err := exec.Command("which", "dpkg-query").Output(); err == nil { - out, err := exec.Command("dpkg-query", "-W", "-f", "${Status}", binaryName).Output() - if err != nil { - return false - } - if strings.HasPrefix(string(out), "deinstall") { - return false - } - return true - } - - // check rhel and sles based systems (or systems that use rpm) - // if package has been installed the query will returns the list of associated files. - // otherwise if uninstalled, or has never been installled status ends with "not installed" - if _, err := exec.Command("which", "rpm").Output(); err == nil { - out, err := exec.Command("rpm", "-q", binaryName, "--state").Output() - if err != nil { - return false - } - if strings.HasSuffix(string(out), "not installed") { - return false - } - return true - - } - - return false -} diff --git a/internal/pkg/agent/install/install_windows.go b/internal/pkg/agent/install/install_windows.go index 2a882ad1dbf..be8d2fb44e5 100644 --- a/internal/pkg/agent/install/install_windows.go +++ b/internal/pkg/agent/install/install_windows.go @@ -44,9 +44,3 @@ func postInstall(topPath string) error { return nil } - -// checkPackageInstall is used for unix based systems to see if the Elastic-Agent was installed through a package manager. -// returns false -func checkPackageInstall() bool { - return false -} diff --git a/internal/pkg/agent/install/installed.go b/internal/pkg/agent/install/installed.go index 1608a6e94b9..c483dda7bb8 100644 --- a/internal/pkg/agent/install/installed.go +++ b/internal/pkg/agent/install/installed.go @@ -11,6 +11,7 @@ import ( "github.com/kardianos/service" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" + "github.com/elastic/elastic-agent/internal/pkg/agent/install/pkgmgr" ) // StatusType is the return status types. @@ -31,7 +32,7 @@ const ( func Status(topPath string) (StatusType, string) { expected := filepath.Join(topPath, paths.BinaryName) status, reason := checkService(topPath) - if checkPackageInstall() { + if pkgmgr.InstalledViaExternalPkgMgr() { if status == Installed { return PackageInstall, "service running" } diff --git a/internal/pkg/agent/install/pkgmgr/pkgmgr_unix.go b/internal/pkg/agent/install/pkgmgr/pkgmgr_unix.go new file mode 100644 index 00000000000..a0e8215a82f --- /dev/null +++ b/internal/pkg/agent/install/pkgmgr/pkgmgr_unix.go @@ -0,0 +1,61 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +//go:build !windows + +package pkgmgr + +import ( + "os/exec" + "runtime" + "strings" + + "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" +) + +// InstalledViaExternalPkgMgr returns true if Agent was installed with +// rpm or dep package managers +func InstalledViaExternalPkgMgr() bool { + if runtime.GOOS != "linux" { + return false + } + + binaryName := paths.BinaryName + + // NOTE searching for english words might not be a great idea as far as portability goes. + // list all installed packages then search for paths.BinaryName? + // dpkg is strange as the remove and purge processes leads to the package bing listed after a remove, but not after a purge + + // check debian based systems (or systems that use dpkg) + // If the package has been installed, the status starts with "install" + // If the package has been removed (but not purged) status starts with "deinstall" + // If purged or never installed, rc is 1 + if _, err := exec.Command("which", "dpkg-query").Output(); err == nil { + out, err := exec.Command("dpkg-query", "-W", "-f", "${Status}", binaryName).Output() + if err != nil { + return false + } + if strings.HasPrefix(string(out), "deinstall") { + return false + } + return true + } + + // check rhel and sles based systems (or systems that use rpm) + // if package has been installed the query will returns the list of associated files. + // otherwise if uninstalled, or has never been installed status ends with "not installed" + if _, err := exec.Command("which", "rpm").Output(); err == nil { + out, err := exec.Command("rpm", "-q", binaryName, "--state").Output() + if err != nil { + return false + } + if strings.HasSuffix(string(out), "not installed") { + return false + } + return true + + } + + return false +} diff --git a/internal/pkg/agent/install/pkgmgr/pkgmgr_windows.go b/internal/pkg/agent/install/pkgmgr/pkgmgr_windows.go new file mode 100644 index 00000000000..9b2ba93dd61 --- /dev/null +++ b/internal/pkg/agent/install/pkgmgr/pkgmgr_windows.go @@ -0,0 +1,12 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +//go:build windows + +package pkgmgr + +// InstalledViaExternalPkgMgr returns false under windows +func InstalledViaExternalPkgMgr() bool { + return false +} diff --git a/pkg/component/component.go b/pkg/component/component.go index ce0ca9cc90e..e9cdd7ce86d 100644 --- a/pkg/component/component.go +++ b/pkg/component/component.go @@ -14,6 +14,7 @@ import ( "github.com/elastic/elastic-agent-client/v7/pkg/proto" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" + "github.com/elastic/elastic-agent/internal/pkg/agent/install/pkgmgr" "github.com/elastic/elastic-agent/internal/pkg/agent/transpiler" "github.com/elastic/elastic-agent/internal/pkg/eql" "github.com/elastic/elastic-agent/pkg/features" @@ -844,7 +845,7 @@ func varsForPlatform(platform PlatformDetail) (*transpiler.Vars, error) { } return transpiler.NewVars("", map[string]interface{}{ "install": map[string]interface{}{ - "in_default": paths.ArePathsEqual(paths.Top(), paths.InstallPath(paths.DefaultBasePath)), + "in_default": paths.ArePathsEqual(paths.Top(), paths.InstallPath(paths.DefaultBasePath)) || pkgmgr.InstalledViaExternalPkgMgr(), }, "runtime": map[string]interface{}{ "platform": platform.String(),