From ac4fb1c8170c951af08c23d097a529ebb33ec4ab Mon Sep 17 00:00:00 2001 From: Baptiste Foy Date: Fri, 3 Jan 2025 14:42:17 +0100 Subject: [PATCH] feat(fleet): Add install info on installer script install --- pkg/fleet/installer/packages/datadog_agent.go | 3 +- pkg/fleet/installer/setup/common/setup.go | 5 ++ pkg/util/installinfo/install_info_nix.go | 54 +------------------ pkg/util/installinfo/install_info_nix_test.go | 6 +-- pkg/util/installinfo/install_info_windows.go | 14 +++++ 5 files changed, 26 insertions(+), 56 deletions(-) create mode 100644 pkg/util/installinfo/install_info_windows.go diff --git a/pkg/fleet/installer/packages/datadog_agent.go b/pkg/fleet/installer/packages/datadog_agent.go index 7a07b99389ee0b..31a9f35218a1d1 100644 --- a/pkg/fleet/installer/packages/datadog_agent.go +++ b/pkg/fleet/installer/packages/datadog_agent.go @@ -19,6 +19,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/fleet/telemetry" "github.com/DataDog/datadog-agent/pkg/util/installinfo" "github.com/DataDog/datadog-agent/pkg/util/log" + "github.com/DataDog/datadog-agent/pkg/version" ) const ( @@ -144,7 +145,7 @@ func SetupAgent(ctx context.Context, _ []string) (err error) { // write installinfo before start, or the agent could write it // TODO: add installer version properly - if err = installinfo.WriteInstallInfo("installer_package", "manual_update"); err != nil { + if err = installinfo.WriteInstallInfo("installer", fmt.Sprintf("installer-%s", version.AgentVersion), "manual_update"); err != nil { return fmt.Errorf("failed to write install info: %v", err) } diff --git a/pkg/fleet/installer/setup/common/setup.go b/pkg/fleet/installer/setup/common/setup.go index b6e512d7dc5d67..e9d2f485430726 100644 --- a/pkg/fleet/installer/setup/common/setup.go +++ b/pkg/fleet/installer/setup/common/setup.go @@ -19,6 +19,7 @@ import ( "github.com/DataDog/datadog-agent/pkg/fleet/installer/env" "github.com/DataDog/datadog-agent/pkg/fleet/installer/oci" "github.com/DataDog/datadog-agent/pkg/fleet/telemetry" + "github.com/DataDog/datadog-agent/pkg/util/installinfo" "github.com/DataDog/datadog-agent/pkg/version" ) @@ -114,6 +115,10 @@ func (s *Setup) Run() (err error) { if err != nil { return fmt.Errorf("failed to install installer: %w", err) } + err = installinfo.WriteInstallInfo("installer", fmt.Sprintf("installer-%s", version.AgentVersion), fmt.Sprintf("install-%s.sh", s.flavor)) + if err != nil { + return fmt.Errorf("failed to write install info: %w", err) + } for _, p := range packages { url := oci.PackageURL(s.Env, p.name, p.version) err = s.installPackage(p.name, url) diff --git a/pkg/util/installinfo/install_info_nix.go b/pkg/util/installinfo/install_info_nix.go index 58124e8ab1a79c..aa1ba9321c25cd 100644 --- a/pkg/util/installinfo/install_info_nix.go +++ b/pkg/util/installinfo/install_info_nix.go @@ -13,11 +13,9 @@ package installinfo import ( - "context" "encoding/json" "fmt" "os" - "os/exec" "path/filepath" "strconv" "strings" @@ -28,8 +26,6 @@ import ( "gopkg.in/yaml.v2" ) -const execTimeout = 30 * time.Second - var ( configDir = "/etc/datadog-agent" installInfoFile = filepath.Join(configDir, "install_info") @@ -37,14 +33,12 @@ var ( ) // WriteInstallInfo write install info and signature files -func WriteInstallInfo(installerVersion, installType string) error { +func WriteInstallInfo(tool, toolVersion, installType string) error { // avoid rewriting the files if they already exist if _, err := os.Stat(installInfoFile); err == nil { - log.Info("Install info file already exists, skipping") return nil } - tool, version := getToolVersion() - if err := writeInstallInfo(tool, version, installerVersion); err != nil { + if err := writeInstallInfo(tool, toolVersion, installType); err != nil { return fmt.Errorf("failed to write install info file: %v", err) } if err := writeInstallSignature(installType); err != nil { @@ -63,50 +57,6 @@ func RmInstallInfo() { } } -func getToolVersion() (string, string) { - tool := "unknown" - version := "unknown" - if _, err := exec.LookPath("dpkg-query"); err == nil { - tool = "dpkg" - toolVersion, err := getDpkgVersion() - if err == nil { - version = toolVersion - } - return tool, version - } - if _, err := exec.LookPath("rpm"); err == nil { - tool = "rpm" - toolVersion, err := getRPMVersion() - if err == nil { - version = fmt.Sprintf("rpm-%s", toolVersion) - } - } - return tool, version -} - -func getRPMVersion() (string, error) { - cancelctx, cancelfunc := context.WithTimeout(context.Background(), execTimeout) - defer cancelfunc() - output, err := exec.CommandContext(cancelctx, "rpm", "-q", "-f", "/bin/rpm", "--queryformat", "%%{VERSION}").Output() - return string(output), err -} - -func getDpkgVersion() (string, error) { - cancelctx, cancelfunc := context.WithTimeout(context.Background(), execTimeout) - defer cancelfunc() - cmd := exec.CommandContext(cancelctx, "dpkg-query", "--showformat=${Version}", "--show", "dpkg") - output, err := cmd.Output() - if err != nil { - log.Warnf("Failed to get dpkg version: %s", err) - return "", err - } - splitVersion := strings.Split(strings.TrimSpace(string(output)), ".") - if len(splitVersion) < 3 { - return "", fmt.Errorf("failed to parse dpkg version: %s", string(output)) - } - return strings.Join(splitVersion[:3], "."), nil -} - func writeInstallInfo(tool, version, installerVersion string) error { info := installInfoMethod{ Method: InstallInfo{ diff --git a/pkg/util/installinfo/install_info_nix_test.go b/pkg/util/installinfo/install_info_nix_test.go index 34acacd32d715b..57e648c0bcce47 100644 --- a/pkg/util/installinfo/install_info_nix_test.go +++ b/pkg/util/installinfo/install_info_nix_test.go @@ -74,11 +74,11 @@ func TestDoubleWrite(t *testing.T) { s, _ := getFromPath(installInfoFile) assert.Nil(t, s) - assert.NoError(t, WriteInstallInfo("v1", "")) + assert.NoError(t, WriteInstallInfo("dpkg", "v1", "")) v1, err := getFromPath(installInfoFile) assert.NoError(t, err) - assert.NoError(t, WriteInstallInfo("v2", "")) + assert.NoError(t, WriteInstallInfo("dpkg", "v2", "")) v2, err := getFromPath(installInfoFile) assert.NoError(t, err) @@ -89,7 +89,7 @@ func TestRmInstallInfo(t *testing.T) { tmpDir := t.TempDir() installInfoFile = filepath.Join(tmpDir, "install_info") installSigFile = filepath.Join(tmpDir, "install.json") - assert.NoError(t, WriteInstallInfo("v1", "")) + assert.NoError(t, WriteInstallInfo("tool", "v1", "")) assert.True(t, fileExists(installInfoFile)) assert.True(t, fileExists(installSigFile)) diff --git a/pkg/util/installinfo/install_info_windows.go b/pkg/util/installinfo/install_info_windows.go new file mode 100644 index 00000000000000..e1f052f5bed990 --- /dev/null +++ b/pkg/util/installinfo/install_info_windows.go @@ -0,0 +1,14 @@ +// Unless explicitly stated otherwise all files in this repository are licensed +// under the Apache License Version 2.0. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright 2016-present Datadog, Inc. + +//go:build windows + +package installinfo + +// WriteInstallInfo write install info and signature files +func WriteInstallInfo(_, _, _ string) error { + // Placeholder for Windows, this is done in tools/windows/DatadogAgentInstaller + return nil +}