Skip to content

Commit

Permalink
modify runtime variable 'install.in_default' with rpm/deb (#3309)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
leehinman authored Aug 29, 2023
1 parent 9727255 commit 117d7de
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 61 deletions.
3 changes: 2 additions & 1 deletion docs/component-specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -190,4 +191,4 @@ operations:

#### `service.timeouts.checkin`

The timeout duration for checkins with this component
The timeout duration for checkins with this component
52 changes: 0 additions & 52 deletions internal/pkg/agent/install/install_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 0 additions & 6 deletions internal/pkg/agent/install/install_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion internal/pkg/agent/install/installed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
}
Expand Down
61 changes: 61 additions & 0 deletions internal/pkg/agent/install/pkgmgr/pkgmgr_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions internal/pkg/agent/install/pkgmgr/pkgmgr_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 117d7de

Please sign in to comment.