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

feat(fleet): Add install info on installer script install #32646

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/fleet/installer/packages/datadog_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -143,8 +144,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)
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/fleet/installer/setup/common/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
54 changes: 2 additions & 52 deletions pkg/util/installinfo/install_info_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
package installinfo

import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
Expand All @@ -28,23 +26,19 @@ import (
"gopkg.in/yaml.v2"
)

const execTimeout = 30 * time.Second

var (
configDir = "/etc/datadog-agent"
installInfoFile = filepath.Join(configDir, "install_info")
installSigFile = filepath.Join(configDir, "install.json")
)

// 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 {
Expand All @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/installinfo/install_info_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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))
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/installinfo/install_info_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading