Skip to content

Commit

Permalink
(fleet) Remove locking mechanism and add pre-remove hooks to the inst…
Browse files Browse the repository at this point in the history
…aller (#34350)
  • Loading branch information
bmermet authored Feb 24, 2025
1 parent 2a17f31 commit 47fe5de
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 254 deletions.
10 changes: 5 additions & 5 deletions pkg/fleet/installer/exec/installer_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,33 +216,33 @@ func (i *InstallerExec) Setup(ctx context.Context) (err error) {

// AvailableDiskSpace returns the available disk space.
func (i *InstallerExec) AvailableDiskSpace() (uint64, error) {
repositories := repository.NewRepositories(paths.PackagesPath, paths.LocksPath)
repositories := repository.NewRepositories(paths.PackagesPath, nil)
return repositories.AvailableDiskSpace()
}

// State returns the state of a package.
func (i *InstallerExec) State(pkg string) (repository.State, error) {
repositories := repository.NewRepositories(paths.PackagesPath, paths.LocksPath)
repositories := repository.NewRepositories(paths.PackagesPath, nil)
return repositories.Get(pkg).GetState()
}

// States returns the states of all packages.
func (i *InstallerExec) States() (map[string]repository.State, error) {
repositories := repository.NewRepositories(paths.PackagesPath, paths.LocksPath)
repositories := repository.NewRepositories(paths.PackagesPath, nil)
states, err := repositories.GetStates()
log.Debugf("repositories states: %v", states)
return states, err
}

// ConfigState returns the state of a package's configuration.
func (i *InstallerExec) ConfigState(pkg string) (repository.State, error) {
repositories := repository.NewRepositories(paths.ConfigsPath, paths.LocksPath)
repositories := repository.NewRepositories(paths.ConfigsPath, nil)
return repositories.Get(pkg).GetState()
}

// ConfigStates returns the states of all packages' configurations.
func (i *InstallerExec) ConfigStates() (map[string]repository.State, error) {
repositories := repository.NewRepositories(paths.ConfigsPath, paths.LocksPath)
repositories := repository.NewRepositories(paths.ConfigsPath, nil)
states, err := repositories.GetStates()
log.Debugf("config repositories states: %v", states)
return states, err
Expand Down
3 changes: 0 additions & 3 deletions pkg/fleet/installer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
cloud.google.com/go/compute/metadata v0.6.0
github.com/DataDog/datadog-agent/pkg/util/log v0.0.0-00010101000000-000000000000
github.com/DataDog/datadog-agent/pkg/version v0.62.3
github.com/DataDog/gopsutil v1.2.2
github.com/Microsoft/go-winio v0.6.2
github.com/google/go-containerregistry v0.20.3
github.com/google/uuid v1.6.0
Expand All @@ -24,7 +23,6 @@ require (

require (
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.62.3 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.16.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
Expand All @@ -41,7 +39,6 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.9.0 // indirect
Expand Down
11 changes: 0 additions & 11 deletions pkg/fleet/installer/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/fleet/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ func NewInstaller(env *env.Env) (Installer, error) {
env: env,
db: db,
downloader: oci.NewDownloader(env, env.HTTPClient()),
packages: repository.NewRepositories(paths.PackagesPath, paths.LocksPath),
configs: repository.NewRepositories(paths.ConfigsPath, paths.LocksPath),
packages: repository.NewRepositories(paths.PackagesPath, packages.PreRemoveHooks),
configs: repository.NewRepositories(paths.ConfigsPath, nil),

userConfigsDir: paths.DefaultUserConfigsDir,
packagesDir: paths.PackagesPath,
Expand Down
23 changes: 12 additions & 11 deletions pkg/fleet/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/fixtures"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/oci"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/packages"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/repository"
)

Expand All @@ -37,9 +38,9 @@ type testPackageManager struct {
installerImpl
}

func newTestPackageManager(t *testing.T, s *fixtures.Server, rootPath string, locksPath string) *testPackageManager {
packages := repository.NewRepositories(rootPath, locksPath)
configs := repository.NewRepositories(t.TempDir(), t.TempDir())
func newTestPackageManager(t *testing.T, s *fixtures.Server, rootPath string) *testPackageManager {
packages := repository.NewRepositories(rootPath, packages.PreRemoveHooks)
configs := repository.NewRepositories(t.TempDir(), nil)
db, err := db.New(filepath.Join(rootPath, "packages.db"))
assert.NoError(t, err)
return &testPackageManager{
Expand All @@ -62,7 +63,7 @@ func (i *testPackageManager) ConfigFS(f fixtures.Fixture) fs.FS {
func TestInstallStable(t *testing.T) {
doTestInstallers(t, func(instFactory installFnFactory, t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := instFactory(installer)(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand All @@ -80,7 +81,7 @@ func TestInstallStable(t *testing.T) {
func TestInstallExperiment(t *testing.T) {
doTestInstallers(t, func(instFactory installFnFactory, t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := instFactory(installer)(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand All @@ -101,7 +102,7 @@ func TestInstallExperiment(t *testing.T) {
func TestInstallPromoteExperiment(t *testing.T) {
doTestInstallers(t, func(instFactory installFnFactory, t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := instFactory(installer)(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand All @@ -123,7 +124,7 @@ func TestInstallPromoteExperiment(t *testing.T) {
func TestUninstallExperiment(t *testing.T) {
doTestInstallers(t, func(instFactory installFnFactory, t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := instFactory(installer)(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand All @@ -145,7 +146,7 @@ func TestUninstallExperiment(t *testing.T) {

func TestInstallSkippedWhenAlreadyInstalled(t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := installer.Install(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand All @@ -164,7 +165,7 @@ func TestInstallSkippedWhenAlreadyInstalled(t *testing.T) {

func TestForceInstallWhenAlreadyInstalled(t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := installer.Install(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand All @@ -184,7 +185,7 @@ func TestForceInstallWhenAlreadyInstalled(t *testing.T) {
func TestReinstallAfterDBClean(t *testing.T) {
doTestInstallers(t, func(instFactory installFnFactory, t *testing.T) {
s := fixtures.NewServer(t)
installer := newTestPackageManager(t, s, t.TempDir(), t.TempDir())
installer := newTestPackageManager(t, s, t.TempDir())
defer installer.db.Close()

err := instFactory(installer)(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
Expand Down Expand Up @@ -248,7 +249,7 @@ func TestPurge(t *testing.T) {
doTestInstallers(t, func(instFactory installFnFactory, t *testing.T) {
s := fixtures.NewServer(t)
rootPath := t.TempDir()
installer := newTestPackageManager(t, s, rootPath, t.TempDir())
installer := newTestPackageManager(t, s, rootPath)

err := instFactory(installer)(testCtx, s.PackageURL(fixtures.FixtureSimpleV1), nil)
assert.NoError(t, err)
Expand Down
11 changes: 1 addition & 10 deletions pkg/fleet/installer/packages/datadog_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,7 @@ func SetupInstaller(ctx context.Context) (err error) {
// Run directory can already be created by the RC client
err = os.Chmod("/opt/datadog-packages/run", 0755)
if err != nil {
return fmt.Errorf("error changing permissions of /opt/datadog-packages/run/locks: %w", err)
}
err = os.MkdirAll("/opt/datadog-packages/run/locks", 0777)
if err != nil {
return fmt.Errorf("error creating /opt/datadog-packages/run/locks: %w", err)
}
// Locks directory can already be created by a package install
err = os.Chmod("/opt/datadog-packages/run/locks", 0777)
if err != nil {
return fmt.Errorf("error changing permissions of /opt/datadog-packages/run/locks: %w", err)
return fmt.Errorf("error changing permissions of /opt/datadog-packages/run: %w", err)
}
err = os.Chown("/etc/datadog-agent", ddAgentUID, ddAgentGID)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/fleet/installer/packages/pre_remove_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.

package packages

import (
"github.com/DataDog/datadog-agent/pkg/fleet/installer/repository"
)

// PreRemoveHooks contains the mapping between packages and their pre-remove hooks
var PreRemoveHooks = map[string]repository.PreRemoveHook{}
2 changes: 0 additions & 2 deletions pkg/fleet/installer/paths/installer_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const (
PackagesPath = "/opt/datadog-packages"
// ConfigsPath is the path to the Fleet-managed configuration directory.
ConfigsPath = "/etc/datadog-agent/managed"
// LocksPath is the path to the packages locks directory.
LocksPath = "/opt/datadog-packages/run/locks"
// RootTmpDir is the temporary path where the bootstrapper will be extracted to.
RootTmpDir = "/opt/datadog-packages/tmp"
// DefaultUserConfigsDir is the default Agent configuration directory.
Expand Down
3 changes: 0 additions & 3 deletions pkg/fleet/installer/paths/installer_paths_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var (
PackagesPath string
// ConfigsPath is the path to the Fleet-managed configuration directory
ConfigsPath string
// LocksPath is the path to the locks directory.
LocksPath string
// RootTmpDir is the temporary path where the bootstrapper will be extracted to.
RootTmpDir string
// DefaultUserConfigsDir is the default Agent configuration directory
Expand All @@ -50,7 +48,6 @@ func init() {
datadogInstallerData, _ = getProgramDataDirForProduct("Datadog Installer")
PackagesPath = filepath.Join(datadogInstallerData, "packages")
ConfigsPath = filepath.Join(datadogInstallerData, "configs")
LocksPath = filepath.Join(datadogInstallerData, "locks")
RootTmpDir = filepath.Join(datadogInstallerData, "tmp")
datadogInstallerPath := "C:\\Program Files\\Datadog\\Datadog Installer"
StableInstallerPath = filepath.Join(datadogInstallerPath, "datadog-installer.exe")
Expand Down
18 changes: 9 additions & 9 deletions pkg/fleet/installer/repository/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ const (

// Repositories manages multiple repositories.
type Repositories struct {
rootPath string
locksPath string
rootPath string
preRemoveHooks map[string]PreRemoveHook
}

// NewRepositories returns a new Repositories.
func NewRepositories(rootPath, locksPath string) *Repositories {
func NewRepositories(rootPath string, preRemoveHooks map[string]PreRemoveHook) *Repositories {
return &Repositories{
rootPath: rootPath,
locksPath: locksPath,
rootPath: rootPath,
preRemoveHooks: preRemoveHooks,
}
}

func (r *Repositories) newRepository(pkg string) *Repository {
return &Repository{
rootPath: filepath.Join(r.rootPath, pkg),
locksPath: filepath.Join(r.locksPath, pkg),
rootPath: filepath.Join(r.rootPath, pkg),
preRemoveHooks: r.preRemoveHooks,
}
}

Expand Down Expand Up @@ -87,8 +87,8 @@ func (r *Repositories) Create(pkg string, version string, stableSourcePath strin
// Delete deletes the repository for the given package name.
func (r *Repositories) Delete(_ context.Context, pkg string) error {
repository := r.newRepository(pkg)
// TODO: locked packages will still be deleted
err := os.RemoveAll(repository.rootPath)

err := repository.Delete()
if err != nil {
return fmt.Errorf("could not delete repository for package %s: %w", pkg, err)
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/fleet/installer/repository/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (

func newTestRepositories(t *testing.T) *Repositories {
rootPath := t.TempDir()
locksRootPath := t.TempDir()
repositories := NewRepositories(rootPath, locksRootPath)
repositories := NewRepositories(rootPath, nil)
return repositories
}

Expand Down Expand Up @@ -53,7 +52,7 @@ func TestRepositoriesReopen(t *testing.T) {
err = repositories.Create("repo2", "v1", t.TempDir())
assert.NoError(t, err)

repositories = NewRepositories(repositories.rootPath, repositories.locksPath)
repositories = NewRepositories(repositories.rootPath, nil)

state, err := repositories.GetStates()
assert.NoError(t, err)
Expand All @@ -64,14 +63,13 @@ func TestRepositoriesReopen(t *testing.T) {

func TestLoadRepositories(t *testing.T) {
rootDir := t.TempDir()
runDir := t.TempDir()

os.Mkdir(path.Join(rootDir, "datadog-agent"), 0755)
os.Mkdir(path.Join(rootDir, tempDirPrefix+"2394812349"), 0755)
os.Mkdir(path.Join(rootDir, "run"), 0755)
os.Mkdir(path.Join(rootDir, "tmp"), 0755)

repositories, err := NewRepositories(rootDir, runDir).loadRepositories()
repositories, err := NewRepositories(rootDir, nil).loadRepositories()
assert.NoError(t, err)
assert.Len(t, repositories, 1)
assert.Contains(t, repositories, "datadog-agent")
Expand Down
Loading

0 comments on commit 47fe5de

Please sign in to comment.