Skip to content

Commit

Permalink
[Elastic Agent] Fix RPM and DEB packaging for Elastic Agent (#19959) (#…
Browse files Browse the repository at this point in the history
…19992)

* Fix the path.config argument.

* Fix run to be the default subcommand, add in the logging flags to elastic-agent.

* Run mage fmt.

* Fix more packaging issues.

* Fix format.

* Fix packaging.

* Fix go vet.

(cherry picked from commit f053295)
  • Loading branch information
blakerouse authored Jul 16, 2020
1 parent 4084104 commit af116fa
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
8 changes: 4 additions & 4 deletions dev-tools/packaging/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ shared:
/etc/init.d/{{.BeatServiceName}}:
template: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/{{.PackageType}}/init.sh.tmpl'
mode: 0755
/etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
/var/lib/{{.BeatName}}/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
mode: 0644
/etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
/var/lib/{{.BeatName}}/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz:
source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz'
mode: 0644
/etc/{{.BeatName}}/data/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz.sha512:
/var/lib/{{.BeatName}}/downloads/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz.sha512:
source: '{{ elastic_beats_dir }}/x-pack/filebeat/build/distributions/filebeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz.sha512'
mode: 0644
/etc/{{.BeatName}}/data/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz.sha512:
/var/lib/{{.BeatName}}/downloads/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz.sha512:
source: '{{ elastic_beats_dir }}/x-pack/metricbeat/build/distributions/metricbeat-{{ beat_version }}{{if .Snapshot}}-SNAPSHOT{{end}}-{{.GOOS}}-{{.AgentArchName}}.tar.gz.sha512'
mode: 0644

Expand Down
2 changes: 1 addition & 1 deletion dev-tools/packaging/templates/linux/systemd.unit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Group={{ .BeatUser }}
Environment="BEAT_LOG_OPTS="
Environment="BEAT_CONFIG_OPTS=-c /etc/{{.BeatName}}/{{.BeatName}}.yml"
Environment="BEAT_PATH_OPTS=--path.home /usr/share/{{.BeatName}} --path.config /etc/{{.BeatName}} --path.data /var/lib/{{.BeatName}} --path.logs /var/log/{{.BeatName}}"
ExecStart=/usr/share/{{.BeatName}}/bin/{{.BeatName}} -environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS
ExecStart=/usr/share/{{.BeatName}}/bin/{{.BeatName}} --environment systemd $BEAT_LOG_OPTS $BEAT_CONFIG_OPTS $BEAT_PATH_OPTS
Restart=always

[Install]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $workdir = Split-Path $MyInvocation.MyCommand.Path
# Create the new service.
New-Service -name {{.BeatName}} `
-displayName {{.BeatName | title}} `
-binaryPathName "`"$workdir\{{.BeatName}}.exe`" -environment=windows_service -c `"$workdir\{{.BeatName}}.yml`" --path.home `"$workdir`" --path.data `"C:\ProgramData\{{.BeatName}}`" --path.logs `"C:\ProgramData\{{.BeatName}}\logs`" -E logging.files.redirect_stderr=true"
-binaryPathName "`"$workdir\{{.BeatName}}.exe`" --environment=windows_service -c `"$workdir\{{.BeatName}}.yml`" --path.home `"$workdir`" --path.data `"C:\ProgramData\{{.BeatName}}`" --path.logs `"C:\ProgramData\{{.BeatName}}\logs`" -E logging.files.redirect_stderr=true"

# Attempt to set the service to delayed start using sc config.
Try {
Expand Down
2 changes: 1 addition & 1 deletion libbeat/logp/configure/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
flag.BoolVar(&verbose, "v", false, "Log at INFO level")
flag.BoolVar(&toStderr, "e", false, "Log to stderr and disable syslog/file output")
common.StringArrVarFlag(nil, &debugSelectors, "d", "Enable certain debug selectors")
flag.Var((*environmentVar)(&environment), "environment", "set environment the Beat is run in")
flag.Var((*environmentVar)(&environment), "environment", "set environment being ran in")
}

// Logging builds a logp.Config based on the given common.Config and the specified
Expand Down
6 changes: 4 additions & 2 deletions x-pack/elastic-agent/pkg/agent/application/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func InjectAgentConfig(c *config.Config) error {
func agentGlobalConfig() map[string]interface{} {
return map[string]interface{}{
"path": map[string]interface{}{
"data": paths.Data(),
"home": paths.Home(),
"data": paths.Data(),
"config": paths.Config(),
"home": paths.Home(),
"logs": paths.Logs(),
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ioStore interface {

// AgentConfigFile is a name of file used to store agent information
func AgentConfigFile() string {
return filepath.Join(paths.Home(), defaultAgentConfigFile)
return filepath.Join(paths.Config(), defaultAgentConfigFile)
}

// AgentActionStoreFile is the file that will contains the action that can be replayed after restart.
Expand Down
13 changes: 10 additions & 3 deletions x-pack/elastic-agent/pkg/agent/application/paths/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (
)

var (
homePath string
dataPath string
logsPath string
homePath string
configPath string
dataPath string
logsPath string
)

func init() {
exePath := retrieveExecutablePath()

fs := flag.CommandLine
fs.StringVar(&homePath, "path.home", exePath, "Agent root path")
fs.StringVar(&configPath, "path.config", exePath, "Config path is the directory Agent looks for its config file")
fs.StringVar(&dataPath, "path.data", filepath.Join(exePath, "data"), "Data path contains Agent managed binaries")
fs.StringVar(&logsPath, "path.logs", exePath, "Logs path contains Agent log output")
}
Expand All @@ -31,6 +33,11 @@ func Home() string {
return homePath
}

// Config returns a directory where configuration file lives
func Config() string {
return configPath
}

// Data returns the data directory for Agent
func Data() string {
return dataPath
Expand Down
31 changes: 17 additions & 14 deletions x-pack/elastic-agent/pkg/agent/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package cmd

import (
"flag"
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"

// import logp flags
_ "github.com/elastic/beats/v7/libbeat/logp/configure"

"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/basecmd"
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/cli"
Expand All @@ -20,23 +22,17 @@ import (
const defaultConfig = "elastic-agent.yml"

type globalFlags struct {
PathConfig string
PathConfigFile string
FlagStrictPerms bool
PathConfigFile string
}

// Config returns path which identifies configuration file.
func (f *globalFlags) Config() string {
if len(f.PathConfigFile) == 0 || f.PathConfigFile == defaultConfig {
return filepath.Join(paths.Home(), defaultConfig)
return filepath.Join(paths.Config(), defaultConfig)
}
return f.PathConfigFile
}

func (f *globalFlags) StrictPermission() bool {
return f.FlagStrictPerms
}

// NewCommand returns the default command for the agent.
func NewCommand() *cobra.Command {
return NewCommandWithArgs(os.Args, cli.NewIOStreams())
Expand All @@ -50,19 +46,26 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {

flags := &globalFlags{}

// path flags
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.home"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.config"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.data"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("path.logs"))
cmd.PersistentFlags().StringVarP(&flags.PathConfigFile, "c", "c", defaultConfig, `Configuration file, relative to path.config`)

cmd.PersistentFlags().StringVarP(&flags.PathConfigFile, "", "c", defaultConfig, fmt.Sprintf(`Configuration file, relative to path.config (default "%s")`, defaultConfig))
cmd.PersistentFlags().StringVarP(&flags.PathConfig, "path.config", "", "${path.home}", "Configuration path")
cmd.PersistentFlags().BoolVarP(&flags.FlagStrictPerms, "strict.perms", "", true, "Strict permission checking on config files")
// logging flags
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("v"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("e"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("d"))
cmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("environment"))

// Add version.
// subcommands
run := newRunCommandWithArgs(flags, args, streams)
cmd.AddCommand(basecmd.NewDefaultCommandsWithArgs(args, streams)...)
cmd.AddCommand(newRunCommandWithArgs(flags, args, streams))
cmd.AddCommand(run)
cmd.AddCommand(newEnrollCommandWithArgs(flags, args, streams))
cmd.AddCommand(newIntrospectCommandWithArgs(flags, args, streams))
cmd.Run = run.Run

return cmd
}

0 comments on commit af116fa

Please sign in to comment.