Skip to content

Commit

Permalink
feat(fleet): Add install info on installer script install
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteFoy committed Jan 3, 2025
1 parent 97e21b6 commit 919645a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 93 deletions.
9 changes: 0 additions & 9 deletions pkg/fleet/installer/packages/datadog_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strings"

"github.com/DataDog/datadog-agent/pkg/fleet/telemetry"
"github.com/DataDog/datadog-agent/pkg/util/installinfo"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

Expand Down Expand Up @@ -142,12 +141,6 @@ func SetupAgent(ctx context.Context, _ []string) (err error) {
return fmt.Errorf("failed to create symlink: %v", err)
}

// 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 {
return fmt.Errorf("failed to write install info: %v", err)
}

_, err = os.Stat("/etc/datadog-agent/datadog.yaml")
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to check if /etc/datadog-agent/datadog.yaml exists: %v", err)
Expand Down Expand Up @@ -204,8 +197,6 @@ func RemoveAgent(ctx context.Context) error {
log.Warnf("Failed to remove agent symlink: %s", err)
spanErr = err
}
installinfo.RmInstallInfo()
// TODO: Return error to caller?
return nil
}

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
65 changes: 2 additions & 63 deletions pkg/util/installinfo/install_info_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,31 @@
package installinfo

import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/google/uuid"
"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 @@ -53,60 +46,6 @@ func WriteInstallInfo(installerVersion, installType string) error {
return nil
}

// RmInstallInfo removes the install info and signature files
func RmInstallInfo() {
if err := os.Remove(installInfoFile); err != nil && !os.IsNotExist(err) {
log.Warnf("Failed to remove install info file: %s", err)
}
if err := os.Remove(installSigFile); err != nil && !os.IsNotExist(err) {
log.Warnf("Failed to remove install signature file: %s", err)
}
}

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
23 changes: 2 additions & 21 deletions pkg/util/installinfo/install_info_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,13 @@ 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)

assert.Equal(t, v1, v2)
}

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.True(t, fileExists(installInfoFile))
assert.True(t, fileExists(installSigFile))

RmInstallInfo()
assert.False(t, fileExists(installInfoFile))
assert.False(t, fileExists(installSigFile))
}

func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
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(tool, toolVersion, installType string) error {
// Placeholder for Windows, this is done in tools/windows/DatadogAgentInstaller
return nil
}

0 comments on commit 919645a

Please sign in to comment.