-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Alter agent install to detect installation from package #30289
Alter agent install to detect installation from package #30289
Conversation
Alter the elastic-agent install command to not run the installation or uninstall steps (on failure) when the agent has been installed via the package manager of a Linux os. Instead the enroll command will be executed.
This pull request does not have a backport label. Could you fix it @michel-laterman? 🙏
NOTE: |
{:name => "opensuse153", :box => "bento/opensuse-leap-15.3", :platform => "opensuse"}, | ||
{:name => "sles12", :box => "elastic/sles-12-x86_64", :platform => "sles"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
opensuse/sles12 added to help test suse
err = install.StartService() | ||
cfgFile := paths.ConfigFile() | ||
if status != install.PackageInstall { | ||
err = install.Install(cfgFile) // can't run install for package installed agent, but must stop the running service and re-enroll, then start the service? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we need to do the commented steps, currently the enroll command will be ran with the --from-install
flag, but I don't know if that's correct
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure either, what is the behavior in the OS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not need to stop the running service, it should just enroll and then trigger a restart through the Elastic Agent control protocol. It should not need to be restarted through the service manager of the host.
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would like feedback for this suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is expected because remove does not remove modified configuration files, those will remain. Only remove with purge completely removes all data on the system related to a package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly yes, this is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll keep the comments here as the extra documentation can help if anyone else needs to work on this
Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane) |
/test |
// 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 := os.Stat("/etc/dpkg"); err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you thing about doing a which dpkg-query
instead? Only thing would be is which
always present on a system? I believe so.
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is expected because remove does not remove modified configuration files, those will remain. Only remove with purge completely removes all data on the system related to a package.
// If the package has been removed (but not pruged) status starts with "deinstall" | ||
// If purged or never installed, rc is 1 | ||
if _, err := os.Stat("/etc/dpkg"); err == nil { | ||
out, err := exec.Command("dpkg-query", "-W", "-f", "${Status}", paths.BinaryName).Output() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to use the package name explicitly instead of the binary name. Even if they are the same at the moment use a specific constant for that would be better, that way if it ever changes.
Could you also use -s
which will only show the status?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The output of -s
includes more information than what we need:
vagrant@ubuntu-focal:~$ dpkg-query -s elastic-agent
Package: elastic-agent
Status: install ok installed
Priority: extra
Section: default
Installed-Size: 427208
Maintainer: <@ace0c0ea54fb>
Architecture: amd64
Version: 8.2.0
Conffiles:
/etc/elastic-agent/.elastic-agent.active.commit 75bbbe44ef52356bb5e85a458166b1b6
/etc/elastic-agent/elastic-agent.reference.yml 23aa69e9e641a43d7daaa0975edf0ec6
/etc/elastic-agent/elastic-agent.yml f169e731b8e4c8a571dca0107954534d
/etc/init.d/elastic-agent 79b301b56cc62629b9d276d6ae24ca2f
Description: Agent manages other beats based on configuration provided.
License: Elastic-License
Vendor: Elastic
Homepage: https://www.elastic.co/beats/elastic-agent
where the -W -f '${Status}'
flags will give just that Status line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@blakerouse, do you know where the package name is defined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is using the name of the beat for the package name, so the way you have it works. I am just suggesting we use a different constant, with the same value. Just to future proof it that maybe one day they could be different.
Its more of a nitpick and if you prefer they way you have it, that is okay as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question, here, I see we are interacting directly with the CLI
, is there any C-Libary of even go package
that we can use to abstract the package manager?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not finding a c or go library that can query dpkg/rpm for installed packages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a bit of digging we can find that the package name is defined by our Mage files as the BeatServiceName
, this defaults to the same as BeatName
for almost all cases (exception being heartbeat).
beats/dev-tools/mage/settings.go
Lines 70 to 71 in 2379370
BeatName = EnvOr("BEAT_NAME", filepath.Base(CWD())) | |
BeatServiceName = EnvOr("BEAT_SERVICE_NAME", BeatName) |
The value is set to the directory name we run mage out of (i.e., x-pack/
elastic-agent
). So I don't think it's a good idea to introduce a new variable for package name within the current codebase as it won't be picked up by our tooling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sound good
// check rhel and sles based systems (or systems that use rpm) | ||
// if package has been installed query retuns with a list of associated files. | ||
// otherwise if uninstalled, or has never been installled status ends with "not installed" | ||
if _, err := os.Stat("/etc/rpm"); err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about which rpm
here as well?
/test |
err = install.StartService() | ||
cfgFile := paths.ConfigFile() | ||
if status != install.PackageInstall { | ||
err = install.Install(cfgFile) // can't run install for package installed agent, but must stop the running service and re-enroll, then start the service? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure either, what is the behavior in the OS?
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly yes, this is expected.
// If the package has been removed (but not pruged) status starts with "deinstall" | ||
// If purged or never installed, rc is 1 | ||
if _, err := os.Stat("/etc/dpkg"); err == nil { | ||
out, err := exec.Command("dpkg-query", "-W", "-f", "${Status}", paths.BinaryName).Output() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question, here, I see we are interacting directly with the CLI
, is there any C-Libary of even go package
that we can use to abstract the package manager?
Co-authored-by: Pier-Hugues Pellerin <phpellerin@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks ready to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks @michel-laterman
What does this PR do?
Alter the elastic-agent install command to not run the installation or
uninstall steps (on failure) when the agent has been installed via the
package manager of a Linux os. Instead the enroll command will be
executed.
Why is it important?
The current documentation/Kibana gives install commands, having these fail can be frustrating.
Checklist
I have made corresponding changes to the documentationCHANGELOG.next.asciidoc
orCHANGELOG-developer.next.asciidoc
.How to test this PR locally
Install the rpm/deb package and run the install command that Kibana provides:
Related issues