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

Add a file lock to the data directory on startup to prevent multiple agents. #18483

Merged
merged 3 commits into from
May 14, 2020

Conversation

blakerouse
Copy link
Contributor

What does this PR do?

Adds an agent.lock to the path.data directory.

Why is it important?

Prevents the ability to run multiple agents on the same host.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works

Author's Checklist

  • Multiple agents cannot be started at the same time.

How to test this PR locally

Try to start two elastic-agent at the same time on the same host and see that the second one started errors out with another elastic-agent is already running.

Related issues

@elasticmachine
Copy link
Collaborator

Pinging @elastic/ingest-management (Team:Ingest Management)

@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels May 13, 2020
@@ -42,7 +42,7 @@ func NewDownloader(config *artifact.Config) *Downloader {
func (e *Downloader) Download(_ context.Context, programName, version string) (string, error) {
// create a destination directory root/program
destinationDir := filepath.Join(e.config.TargetDirectory, programName)
if err := os.MkdirAll(destinationDir, os.ModeDir); err != nil {
if err := os.MkdirAll(destinationDir, 0755); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a drive-by fix on Mac os.ModeDir does not create the directory with the proper permissions. 0755 must be used.

@ph ph requested a review from michalpristas May 13, 2020 16:28
@elasticmachine
Copy link
Collaborator

elasticmachine commented May 13, 2020

💚 Build Succeeded

Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

if err := locker.TryLock(); err != nil {
return err
}
defer locker.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

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

can you make sure this is handled even if we are killed. defer statements are skipped if SIGINT or SIGTERM are received and it can prevent us from restarting

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have verified that this does get called in all the cases defined below with signals.

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT)

<-signals

So the defer does get called. I did find a bug in periodic that was preventing app.Start from returning to catch the signals. I have fixed that in my most recent commit.

Copy link
Contributor

@michalpristas michalpristas left a comment

Choose a reason for hiding this comment

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

Tested using various combination of KILL, STOP, TERMINAL STOP ... looks ok

@blakerouse blakerouse merged commit e1a4741 into elastic:master May 14, 2020
@blakerouse blakerouse deleted the agent-locker branch May 14, 2020 15:55
blakerouse added a commit to blakerouse/beats that referenced this pull request May 14, 2020
…agents. (elastic#18483)

* Add a file lock to the data directory on startup to prevent multiple agents.

* Add export comments to AppLocker.

* Fix periodic to not block startup.

(cherry picked from commit e1a4741)
blakerouse added a commit that referenced this pull request May 14, 2020
…agents. (#18483) (#18529)

* Add a file lock to the data directory on startup to prevent multiple agents.

* Add export comments to AppLocker.

* Fix periodic to not block startup.

(cherry picked from commit e1a4741)
v1v added a commit to v1v/beats that referenced this pull request May 15, 2020
…w-oss

* upstream/master: (27 commits)
  Disable host fields for "cloud", panw, cef modules (elastic#18223)
  [docs] Rename monitoring collection from legacy internal collection to legacy collection (elastic#18504)
  Introduce auto detection of format (elastic#18095)
  Add additional fields to address issue elastic#18465 for googlecloud audit log (elastic#18472)
  Fix libbeat import path in seccomp policy template (elastic#18418)
  Address Okta input issue elastic#18530 (elastic#18534)
  [Ingest Manager] Avoid Chown on windows (elastic#18512)
  Fix Cisco ASA/FTD msgs that use a host name as NAT address (elastic#18376)
  [CI] Optimise stash/unstash performance (elastic#18473)
  Libbeat: Remove global loggers from libbeat/metric and libbeat/cloudid (elastic#18500)
  Fix PANW bad mapping of client/source and server/dest packets and bytes (elastic#18525)
  Add a file lock to the data directory on startup to prevent multiple agents. (elastic#18483)
  Followup to 12606 (elastic#18316)
  changed input from syslog to tcp/udp due to unsupported RFC (elastic#18447)
  Improve ECS field mappings in Sysmon module. (elastic#18381)
  [Elastic Agent] Cleaner output of inspect command  (elastic#18405)
  [Elastic Agent] Pick up version from libbeat (elastic#18350)
  Update communitybeats.asciidoc (elastic#18470)
  [Metricbeat] Change visualization interval from 15m to >=15m (elastic#18466)
  docs: Fix typo in kerberos docs (elastic#18503)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Elastic Agent] Prevent a second agent from starting.
3 participants