diff --git a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl index 9b2c5de8893..c2eb9937226 100644 --- a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl +++ b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl @@ -17,6 +17,8 @@ fi echo "create symlink "$symlink" to "$new_agent_dir/elastic-agent"" ln -s "$new_agent_dir/elastic-agent" "$symlink" +$new_agent_dir/elastic-agent apply-flavor + # reload systemctl and then restart service echo "systemd enable/restart elastic-agent" systemctl daemon-reload 2> /dev/null diff --git a/dev-tools/packaging/templates/linux/preinstall.sh.tmpl b/dev-tools/packaging/templates/linux/preinstall.sh.tmpl index b396c76476c..88f13fca03b 100644 --- a/dev-tools/packaging/templates/linux/preinstall.sh.tmpl +++ b/dev-tools/packaging/templates/linux/preinstall.sh.tmpl @@ -5,6 +5,7 @@ set -e commit_hash="{{ commit_short }}" version_dir="{{agent_package_version}}{{snapshot_suffix}}" symlink="/usr/share/elastic-agent/bin/elastic-agent" +flavor_file="/var/lib/elastic-agent/.flavor" new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$version_dir-$commit_hash" old_agent_dir="" @@ -43,4 +44,19 @@ if test -L "$symlink"; then fi else echo "no previous installation found" + + # create dir in case it does not exist + mkdir -p "$new_agent_dir" + + # 2 is upgrade for Fedora, do not upgrade file when upgrading and file exists + if [[ "$1" != "2" ]]; then + if [[ -n "${ELASTIC_AGENT_FLAVOR}" ]]; then + # Do not modify the file if it already exists + echo "${ELASTIC_AGENT_FLAVOR}" > "$flavor_file" + else + # Defaults to basic installation + echo "basic" > "$flavor_file" + fi + fi fi + diff --git a/internal/pkg/agent/cmd/apply_flavor.go b/internal/pkg/agent/cmd/apply_flavor.go new file mode 100644 index 00000000000..f184df21185 --- /dev/null +++ b/internal/pkg/agent/cmd/apply_flavor.go @@ -0,0 +1,59 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package cmd + +import ( + "errors" + "fmt" + "os" + "path/filepath" + + "github.com/spf13/cobra" + + "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" + "github.com/elastic/elastic-agent/internal/pkg/agent/install" + "github.com/elastic/elastic-agent/internal/pkg/cli" + v1 "github.com/elastic/elastic-agent/pkg/api/v1" +) + +func newApplyFlavorCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "apply-flavor", + Short: "Apply Flavor cleans up unnecessary components from agent installation directory", + Run: func(c *cobra.Command, _ []string) { + if err := applyCmd(); err != nil { + fmt.Fprintf(streams.Err, "Error: %v\n%s\n", err, troubleshootMessage()) + logExternal(fmt.Sprintf("%s apply flavor failed: %s", paths.BinaryName, err)) + os.Exit(1) + } + }, + Hidden: true, + } + + return cmd +} + +func applyCmd() error { + topPath := paths.Top() + detectedFlavor, err := install.UsedFlavor(topPath, "") + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil + } + return err + } + if detectedFlavor == "" { + return nil + } + + versionedHome := paths.VersionedHome(topPath) + manifestFilePath := filepath.Join(versionedHome, v1.ManifestFileName) + flavor, err := install.Flavor(detectedFlavor, manifestFilePath, nil) + if err != nil { + return err + } + + return install.ApplyFlavor(versionedHome, flavor) +} diff --git a/internal/pkg/agent/cmd/common.go b/internal/pkg/agent/cmd/common.go index 059c144399d..0bec7dc3540 100644 --- a/internal/pkg/agent/cmd/common.go +++ b/internal/pkg/agent/cmd/common.go @@ -94,6 +94,7 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command { addCommandIfNotNil(cmd, newComponentCommandWithArgs(args, streams)) addCommandIfNotNil(cmd, newLogsCommandWithArgs(args, streams)) addCommandIfNotNil(cmd, newOtelCommandWithArgs(args, streams)) + addCommandIfNotNil(cmd, newApplyFlavorCommandWithArgs(args, streams)) // windows special hidden sub-command (only added on Windows) reexec := newReExecWindowsCommand(args, streams) diff --git a/internal/pkg/agent/install/.flavors b/internal/pkg/agent/install/.flavors new file mode 100644 index 00000000000..42358f5c5a4 --- /dev/null +++ b/internal/pkg/agent/install/.flavors @@ -0,0 +1,13 @@ +basic: + - agentbeat + - endpoint-security + - pf-host-agent +servers: + - agentbeat + - endpoint-security + - pf-host-agent + - cloudbeat + - apm-server + - fleet-server + - pf-elastic-symbolizer + - pf-elastic-collector diff --git a/internal/pkg/agent/install/flavors.go b/internal/pkg/agent/install/flavors.go index 61f84124ce9..b90e2203387 100644 --- a/internal/pkg/agent/install/flavors.go +++ b/internal/pkg/agent/install/flavors.go @@ -48,7 +48,7 @@ func UsedFlavor(topPath, defaultFlavor string) (string, error) { return "", err } - return string(content), nil + return strings.TrimSpace(string(content)), nil } func Flavor(detectedFlavor string, registryPath string, flavorsRegistry map[string][]string) (FlavorDefinition, error) {