From ae4b4e8fe187ac9518086f0b4d0cdff166a9e15b Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Fri, 4 Sep 2020 13:13:08 +0200 Subject: [PATCH] mode --- dev-tools/packaging/packages.yml | 8 ++++-- .../templates/linux/elastic-agent.sh.tmpl | 11 ++++++++ .../templates/linux/elastic-agent.unit.tmpl | 19 +++++++++++++ .../pkg/agent/application/paths/paths.go | 28 +++++++++---------- 4 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 dev-tools/packaging/templates/linux/elastic-agent.sh.tmpl create mode 100644 dev-tools/packaging/templates/linux/elastic-agent.unit.tmpl diff --git a/dev-tools/packaging/packages.yml b/dev-tools/packaging/packages.yml index 2dcfba0f8a3..fec57894775 100644 --- a/dev-tools/packaging/packages.yml +++ b/dev-tools/packaging/packages.yml @@ -53,17 +53,17 @@ shared: source: build/golang-crossbuild/god-{{.GOOS}}-{{.Platform.Arch}} mode: 0755 /usr/bin/{{.BeatName}}: - template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/beatname.sh.tmpl' + template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/elastic-agent.sh.tmpl' mode: 0755 /lib/systemd/system/{{.BeatServiceName}}.service: - template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/systemd.unit.tmpl' + template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/elastic-agent.unit.tmpl' mode: 0644 /etc/init.d/{{.BeatServiceName}}: template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/{{.PackageType}}/init.sh.tmpl' mode: 0755 /var/lib/{{.BeatName}}/data/{{.BeatName}}-{{ commit_short }}/{{.BeatName}}{{.BinaryExt}}: source: build/golang-crossbuild/{{.BeatName}}-{{.GOOS}}-{{.Platform.Arch}}{{.BinaryExt}} - mode: 0644 + mode: 0755 /var/lib/{{.BeatName}}/data/{{.BeatName}}-{{ commit_short }}/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz: source: '{{.AgentDropPath}}/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz' mode: 0644 @@ -806,6 +806,7 @@ specs: '{{.BeatName}}{{.BinaryExt}}': source: data/{{.BeatName}}-{{ commit_short }}/{{.BeatName}}{{.BinaryExt}} symlink: true + mode: 0755 - os: darwin types: [dmg] @@ -838,6 +839,7 @@ specs: /usr/share/{{.BeatName}}/bin/{{.BeatName}}{{.BinaryExt}}: source: /var/lib/{{.BeatName}}/data/{{.BeatName}}-{{ commit_short }}/{{.BeatName}}{{.BinaryExt}} symlink: true + mode: 0755 - os: linux types: [docker] diff --git a/dev-tools/packaging/templates/linux/elastic-agent.sh.tmpl b/dev-tools/packaging/templates/linux/elastic-agent.sh.tmpl new file mode 100644 index 00000000000..744abc05702 --- /dev/null +++ b/dev-tools/packaging/templates/linux/elastic-agent.sh.tmpl @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +# Script to run {{.BeatName | title}} in foreground with the same path settings that +# the init script / systemd unit file would do. + +exec /usr/share/{{.BeatName}}/bin/{{.BeatName}} \ + --path.home /var/lib/{{.BeatName}} \ + --path.config /etc/{{.BeatName}} \ + --path.data /var/lib/{{.BeatName}}/data \ + --path.logs /var/log/{{.BeatName}} \ + "$@" diff --git a/dev-tools/packaging/templates/linux/elastic-agent.unit.tmpl b/dev-tools/packaging/templates/linux/elastic-agent.unit.tmpl new file mode 100644 index 00000000000..f7ffb886884 --- /dev/null +++ b/dev-tools/packaging/templates/linux/elastic-agent.unit.tmpl @@ -0,0 +1,19 @@ +[Unit] +Description={{.Description}} +Documentation={{.URL}} +Wants=network-online.target +After=network-online.target + +[Service] +{{ if ne .BeatUser "root" -}} +User={{ .BeatUser }} +Group={{ .BeatUser }} +{{- end }} +Environment="BEAT_LOG_OPTS=" +Environment="BEAT_CONFIG_OPTS=-c /etc/{{.BeatName}}/{{.BeatName}}.yml" +Environment="BEAT_PATH_OPTS=--path.home /var/lib/{{.BeatName}} --path.config /etc/{{.BeatName}} --path.data /var/lib/{{.BeatName}}/data --path.logs /var/log/{{.BeatName}}" +ExecStart=/usr/share/{{.BeatName}}/bin/{{.BeatName}} --environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go index d32be56657d..48544ec7593 100644 --- a/x-pack/elastic-agent/pkg/agent/application/paths/paths.go +++ b/x-pack/elastic-agent/pkg/agent/application/paths/paths.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "runtime" + "sync" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" ) @@ -19,26 +20,18 @@ var ( dataPath string logsPath string serviceName string + + overridesLoader sync.Once ) func init() { initialHome := initialHome() - var homePathVar, configPathVar, dataPathVar, logsPathVar string - fs := flag.CommandLine - fs.StringVar(&homePathVar, "path.home", initialHome, "Agent root path") - fs.StringVar(&configPathVar, "path.config", initialHome, "Config path is the directory Agent looks for its config file") - fs.StringVar(&dataPathVar, "path.data", filepath.Join(initialHome, "data"), "Data path contains Agent managed binaries") - fs.StringVar(&logsPathVar, "path.logs", initialHome, "Logs path contains Agent log output") - - // avoid rewriting initialized values by flagSet later - homePath = homePathVar - configPath = configPathVar - dataPath = dataPathVar - logsPath = logsPathVar - - getOverrides() + fs.StringVar(&homePath, "path.home", initialHome, "Agent root path") + fs.StringVar(&configPath, "path.config", initialHome, "Config path is the directory Agent looks for its config file") + fs.StringVar(&dataPath, "path.data", filepath.Join(initialHome, "data"), "Data path contains Agent managed binaries") + fs.StringVar(&logsPath, "path.logs", initialHome, "Logs path contains Agent log output") } // UpdatePaths update paths based on changes in paths file. @@ -78,27 +71,34 @@ func getOverrides() { // ServiceName return predefined service name if defined by initial call. func ServiceName() string { + // needs to do this at this place because otherwise it will + // get overwritten by flags behavior. + overridesLoader.Do(getOverrides) return serviceName } // Home returns a directory where binary lives // Executable is not supported on nacl. func Home() string { + overridesLoader.Do(getOverrides) return homePath } // Config returns a directory where configuration file lives func Config() string { + overridesLoader.Do(getOverrides) return configPath } // Data returns the data directory for Agent func Data() string { + overridesLoader.Do(getOverrides) return dataPath } // Logs returns a the log directory for Agent func Logs() string { + overridesLoader.Do(getOverrides) return logsPath }