Skip to content
Open
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
14 changes: 11 additions & 3 deletions pkg/fleet/installer/packages/datadog_agent_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"

"github.com/DataDog/datadog-agent/pkg/fleet/installer/installinfo"
Expand Down Expand Up @@ -481,11 +482,11 @@ func (s *datadogAgentService) StopStable(ctx HookContext) error {
}
switch service.GetServiceManagerType() {
case service.SystemdType:
return systemd.StopUnits(ctx, s.SystemdUnitsStable...)
return systemd.StopUnits(ctx, reverseStringSlice(s.SystemdUnitsStable)...)
case service.UpstartType:
return upstart.StopAll(ctx, s.UpstartServices...)
return upstart.StopAll(ctx, reverseStringSlice(s.UpstartServices)...)
case service.SysvinitType:
return sysvinit.StopAll(ctx, s.SysvinitServices...)
return sysvinit.StopAll(ctx, reverseStringSlice(s.SysvinitServices)...)
default:
return fmt.Errorf("unsupported service manager")
}
Expand Down Expand Up @@ -659,3 +660,10 @@ func writeEmbeddedUnit(dir string, unit string, content []byte) error {
}
return nil
}

func reverseStringSlice(slice []string) []string {
reversed := make([]string, len(slice))
copy(reversed, slice)
slices.Reverse(reversed)
return reversed
}
11 changes: 11 additions & 0 deletions pkg/fleet/installer/packages/service/systemd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os/exec"
"path/filepath"
"syscall"
"time"

"github.com/DataDog/datadog-agent/pkg/fleet/installer/telemetry"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -142,3 +143,13 @@ func IsRunning() (running bool, err error) {
}
return true, nil
}

// JournaldLogs returns the logs for a given unit since a given time
func JournaldLogs(ctx context.Context, unit string, since time.Time) (string, error) {
journalctlCmd := exec.CommandContext(ctx, "journalctl", "_COMM=systemd", "--unit", unit, "-e", "--no-pager", "--since", since.Format(time.RFC3339))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _COMM=systemd filter will restrict logs to only those from the systemd process itself, not the actual service unit logs you're trying to collect. For proper service unit logs, remove this parameter:

journalctlCmd := exec.CommandContext(ctx, "journalctl", "--unit", unit, "-e", "--no-pager", "--since", since.Format(time.RFC3339))

This will return all logs for the specified unit since the given time, which appears to be the intended behavior based on the function name and context.

Suggested change
journalctlCmd := exec.CommandContext(ctx, "journalctl", "_COMM=systemd", "--unit", unit, "-e", "--no-pager", "--since", since.Format(time.RFC3339))
journalctlCmd := exec.CommandContext(ctx, "journalctl", "--unit", unit, "-e", "--no-pager", "--since", since.Format(time.RFC3339))

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

stdout, err := journalctlCmd.Output()
if err != nil {
return "", err
}
return string(stdout), nil
}
27 changes: 12 additions & 15 deletions pkg/fleet/installer/setup/common/services_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,29 @@
package common

import (
"bytes"
"fmt"
"os/exec"
"time"

"github.com/DataDog/datadog-agent/pkg/fleet/installer/packages/service/systemd"
"github.com/DataDog/datadog-agent/pkg/fleet/installer/telemetry"
)

// restartServices restarts the services that need to be restarted after a package upgrade or
// an install script re-run; because the configuration may have changed.
func (s *Setup) restartServices(pkgs []packageWithVersion) error {
t := time.Now()
span, ctx := telemetry.StartSpanFromContext(s.Ctx, "restartServices")
for _, pkg := range pkgs {
switch pkg.name {
case DatadogAgentPackage:
if err := restartService("datadog-agent.service"); err != nil {
return err
err := systemd.RestartUnit(ctx, "datadog-agent.service")
if err != nil {
logs, logsErr := systemd.JournaldLogs(ctx, "datadog-agent.service", t)
span.SetTag("journald_logs", logs)
span.SetTag("journald_logs_err", logsErr)
return fmt.Errorf("failed to restart datadog-agent.service: %w", err)
}
}
}
return nil
}

func restartService(unit string) error {
cmd := exec.Command("systemctl", "restart", unit)
stderr := bytes.Buffer{}
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to restart %s (%s): %s", unit, err.Error(), stderr.String())
}
return nil
}