Skip to content

Commit

Permalink
[Installer] use bootstraped installer to continue bootstrap (#24687)
Browse files Browse the repository at this point in the history
Co-authored-by: raphaelgavache <[email protected]>
Co-authored-by: arbll <[email protected]>
  • Loading branch information
3 people authored Apr 16, 2024
1 parent 082fcbc commit e68edd1
Show file tree
Hide file tree
Showing 18 changed files with 208 additions and 98 deletions.
41 changes: 25 additions & 16 deletions cmd/installer/subcommands/bootstrap/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,39 @@ func bootstrapFxWrapper(ctx context.Context, params *cliParams, installScriptPar
)
}

func bootstrap(ctx context.Context, params *cliParams, installScriptParams *installScriptParams, config config.Component, log log.Component, _ telemetry.Component) error {
url := packageURL(config.GetString("site"), params.pkg, params.version)
if params.url != "" {
url = params.url
}
ctxCarrier := tracer.TextMapCarrier{
tracer.DefaultTraceIDHeader: fmt.Sprint(installScriptParams.Telemetry.TraceID),
tracer.DefaultParentIDHeader: fmt.Sprint(installScriptParams.Telemetry.ParentID),
tracer.DefaultPriorityHeader: fmt.Sprint(installScriptParams.Telemetry.Priority),
func bootstrap(ctx context.Context, params *cliParams, installScriptParams *installScriptParams, config config.Component, log log.Component, _ telemetry.Component) (err error) {
var spanOptions []tracer.StartSpanOption
if installScriptParams.Telemetry.TraceID != 0 && installScriptParams.Telemetry.ParentID != 0 {
ctxCarrier := tracer.TextMapCarrier{
tracer.DefaultTraceIDHeader: fmt.Sprint(installScriptParams.Telemetry.TraceID),
tracer.DefaultParentIDHeader: fmt.Sprint(installScriptParams.Telemetry.ParentID),
tracer.DefaultPriorityHeader: fmt.Sprint(installScriptParams.Telemetry.Priority),
}
spanCtx, err := tracer.Extract(ctxCarrier)
if err != nil {
log.Errorf("failed to extract span context from install script params: %v", err)
}
spanOptions = append(spanOptions, tracer.ChildOf(spanCtx))
}
spanCtx, err := tracer.Extract(ctxCarrier)
if err != nil {
log.Errorf("failed to extract span context from install script params: %v", err)
}
span, ctx := tracer.StartSpanFromContext(ctx, "cmd/bootstrap", tracer.ChildOf(spanCtx))
defer span.Finish()

span, ctx := tracer.StartSpanFromContext(ctx, "cmd/bootstrap", spanOptions...)
defer func() { span.Finish(tracer.WithError(err)) }()
span.SetTag(ext.ManualKeep, true)
span.SetTag("params.url", params.url)
span.SetTag("params.pkg", params.pkg)
span.SetTag("params.version", params.version)
span.SetTag("script_params.telemetry.trace_id", installScriptParams.Telemetry.TraceID)
span.SetTag("script_params.telemetry.span_id", installScriptParams.Telemetry.ParentID)
span.SetTag("script_params.features.apm_instrumentation", installScriptParams.Features.APMInstrumentation)

if params.pkg == "" && params.url == "" {
return installer.Bootstrap(ctx, config)
}

url := packageURL(config.GetString("site"), params.pkg, params.version)
if params.url != "" {
url = params.url
}
span.SetTag("params.url", params.url)
return installer.BootstrapURL(ctx, url, config)
}

Expand Down
5 changes: 1 addition & 4 deletions cmd/installer/subcommands/purge/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/DataDog/datadog-agent/cmd/installer/command"
"github.com/DataDog/datadog-agent/comp/core"
"github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/updater/telemetry"
"github.com/DataDog/datadog-agent/comp/updater/telemetry/telemetryimpl"
"github.com/DataDog/datadog-agent/pkg/installer"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
)
Expand All @@ -38,11 +36,10 @@ func purgeFxWrapper() error {
LogParams: logimpl.ForOneShot("INSTALLER", "info", true),
}),
core.Bundle(),
telemetryimpl.Module(),
)
}

func purge(_ telemetry.Component) error {
func purge() error {
installer.Purge()
return nil
}
4 changes: 3 additions & 1 deletion omnibus/package-scripts/installer-deb/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
readonly PACKAGES_DIR=/opt/datadog-packages
readonly INSTALL_DIR=/opt/datadog-installer
readonly BOOTSTRAP_INSTALLER=${INSTALL_DIR}/bin/installer/installer
readonly STABLE_INSTALLER=${PACKAGES_DIR}/datadog-installer/stable/bin/installer/installer
readonly HELPER=${INSTALL_DIR}/bin/installer/helper
readonly LOG_DIR=/var/log/datadog
readonly PACKAGES_LOCK_DIR=/var/run/datadog-packages
Expand Down Expand Up @@ -82,6 +83,7 @@ fi
chmod 750 ${HELPER}
setcap cap_setuid+ep ${HELPER}

$BOOTSTRAP_INSTALLER bootstrap --url "oci://public.ecr.aws/datadog/installer-package:latest"
$BOOTSTRAP_INSTALLER bootstrap --url "oci://public.ecr.aws/datadog/installer-package:7.54.0-devel.git.366.99b427c.pipeline.32143438-1"
$STABLE_INSTALLER bootstrap

exit 0
8 changes: 0 additions & 8 deletions omnibus/package-scripts/installer-deb/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ PACKAGES_LOCK_DIR=/var/run/datadog-packages

set -e

remove_packages()
{
echo "Removing packages installed by datadog-installer"
$INSTALL_DIR/bin/installer/installer purge
}

case "$1" in
purge)
remove_packages
echo "Deleting dd-agent user"
deluser dd-agent --quiet
deluser dd-installer --quiet
Expand All @@ -31,7 +24,6 @@ case "$1" in
rm -rf $PACKAGES_LOCK_DIR
;;
remove)
remove_packages
;;
*)
;;
Expand Down
5 changes: 1 addition & 4 deletions omnibus/package-scripts/installer-deb/prerm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

readonly INSTALL_DIR=/opt/datadog-installer

SYSTEMCTL_SKIP_SYSV=true systemctl stop datadog-installer || true
SYSTEMCTL_SKIP_SYSV=true systemctl disable datadog-installer || true

set -e

case "$1" in
remove)
remove | purge)
$INSTALL_DIR/bin/installer/installer purge
;;
*)
Expand Down
5 changes: 4 additions & 1 deletion omnibus/package-scripts/installer-rpm/posttrans
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

readonly PACKAGES_DIR=/opt/datadog-packages
readonly INSTALL_DIR=/opt/datadog-installer
readonly BOOTSTRAP_INSTALLER=${INSTALL_DIR}/bin/installer/installer
readonly STABLE_INSTALLER=${PACKAGES_DIR}/datadog-installer/stable/bin/installer/installer
readonly HELPER=${INSTALL_DIR}/bin/installer/helper
readonly LOG_DIR=/var/log/datadog
readonly PACKAGES_LOCK_DIR=/var/run/datadog-packages
Expand Down Expand Up @@ -65,7 +67,8 @@ fi
chmod 750 ${HELPER}
setcap cap_setuid+ep ${HELPER}

$BOOTSTRAP_INSTALLER bootstrap --url "oci://public.ecr.aws/datadog/installer-package:latest"
$BOOTSTRAP_INSTALLER bootstrap --url "oci://public.ecr.aws/datadog/installer-package:7.54.0-devel.git.366.99b427c.pipeline.32143438-1"
$STABLE_INSTALLER bootstrap

exit 0

5 changes: 3 additions & 2 deletions pkg/installer/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@ func (m *packageManager) installStable(ctx context.Context, pkg string, version
if err != nil {
return fmt.Errorf("could not create repository: %w", err)
}
return m.setupUnits(ctx, pkg)
}

func (m *packageManager) setupUnits(ctx context.Context, pkg string) error {
m.installLock.Lock()
defer m.installLock.Unlock()
switch pkg {
case packageDatadogAgent:
return service.SetupAgentUnits(ctx)
case packageAPMInjector:
return service.SetupAPMInjector(ctx)
case packageDatadogInstaller:
return service.SetupInstallerUnit(ctx)
default:
return nil
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ func BootstrapURL(ctx context.Context, url string, config config.Reader) error {
return i.BootstrapURL(ctx, url)
}

// Bootstrap is the generic installer bootstrap.
func Bootstrap(ctx context.Context, config config.Reader) error {
rc := newNoopRemoteConfig()
i, err := newInstaller(rc, defaultRepositoriesPath, defaultLocksPath, config)
if err != nil {
return fmt.Errorf("could not create installer: %w", err)
}
err = i.Start(ctx)
if err != nil {
return fmt.Errorf("could not start installer: %w", err)
}
defer func() {
err := i.Stop(ctx)
if err != nil {
log.Errorf("could not stop installer: %v", err)
}
}()
return i.Bootstrap(ctx)
}

// Purge removes files installed by the installer
func Purge() {
purge(defaultLocksPath, defaultRepositoriesPath)
Expand All @@ -111,6 +131,7 @@ func purge(locksPath, repositoryPath string) {
span, ctx := tracer.StartSpanFromContext(context.Background(), "purge")
defer span.Finish(tracer.WithError(err))
service.RemoveAgentUnits(ctx)
service.RemoveInstallerUnits(ctx)
if err = service.RemoveAPMInjector(ctx); err != nil {
log.Warnf("installer: could not remove APM injector: %v", err)
}
Expand Down Expand Up @@ -237,6 +258,22 @@ func (i *installerImpl) BootstrapVersion(ctx context.Context, pkg string, versio
return i.bootstrapPackage(ctx, stablePackage.URL, stablePackage.Name, stablePackage.Version)
}

// Bootstrap is the generic bootstrap of the installer
func (i *installerImpl) Bootstrap(ctx context.Context) (err error) {
span, ctx := tracer.StartSpanFromContext(ctx, "bootstrap")
defer func() { span.Finish(tracer.WithError(err)) }()
i.m.Lock()
defer i.m.Unlock()
i.refreshState(ctx)
defer i.refreshState(ctx)

if err = i.setupInstallerUnits(ctx); err != nil {
return err
}

return nil
}

// BootstrapURL installs the stable version of the package.
func (i *installerImpl) BootstrapURL(ctx context.Context, url string) (err error) {
span, ctx := tracer.StartSpanFromContext(ctx, "bootstrap_url")
Expand Down Expand Up @@ -344,6 +381,26 @@ func (i *installerImpl) StopExperiment(ctx context.Context, pkg string) (err err
return nil
}

func (i *installerImpl) setupInstallerUnits(ctx context.Context) (err error) {
systemdRunning, err := service.IsSystemdRunning()
if err != nil {
return fmt.Errorf("error checking if systemd is running: %w", err)
}
if !systemdRunning {
log.Infof("Installer: Systemd is not running, skipping unit setup")
return nil
}
err = service.SetupInstallerUnits(ctx)
if err != nil {
return fmt.Errorf("failed to setup datadog-installer systemd units: %w", err)
}
if !i.remoteUpdates {
service.RemoveInstallerUnits(ctx)
return
}
return service.StartInstallerStable(ctx)
}

func (i *installerImpl) handleCatalogUpdate(c catalog) error {
i.m.Lock()
defer i.m.Unlock()
Expand Down
4 changes: 2 additions & 2 deletions pkg/installer/service/apm_inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (a *apmInjectorInstaller) setLDPreloadConfig(ctx context.Context) error {
return err
}

return executeCommand(ctx, string(replaceLDPreloadCommand))
return executeHelperCommand(ctx, string(replaceLDPreloadCommand))
}

// setLDPreloadConfigContent sets the content of the LD preload configuration
Expand Down Expand Up @@ -195,7 +195,7 @@ func (a *apmInjectorInstaller) deleteLDPreloadConfig(ctx context.Context) error
return err
}

return executeCommand(ctx, string(replaceLDPreloadCommand))
return executeHelperCommand(ctx, string(replaceLDPreloadCommand))
}

// deleteLDPreloadConfigContent deletes the content of the LD preload configuration
Expand Down
12 changes: 6 additions & 6 deletions pkg/installer/service/cmd_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ const execTimeout = 30 * time.Second

// ChownDDAgent changes the owner of the given path to the dd-agent user.
func ChownDDAgent(ctx context.Context, path string) error {
return executeCommand(ctx, `{"command":"chown dd-agent","path":"`+path+`"}`)
return executeHelperCommand(ctx, `{"command":"chown dd-agent","path":"`+path+`"}`)
}

// RemoveAll removes all files under a given path under /opt/datadog-packages regardless of their owner.
func RemoveAll(ctx context.Context, path string) error {
return executeCommand(ctx, `{"command":"rm","path":"`+path+`"}`)
return executeHelperCommand(ctx, `{"command":"rm","path":"`+path+`"}`)
}

func createAgentSymlink(ctx context.Context) error {
return executeCommand(ctx, `{"command":"agent-symlink"}`)
return executeHelperCommand(ctx, `{"command":"agent-symlink"}`)
}

func rmAgentSymlink(ctx context.Context) error {
return executeCommand(ctx, `{"command":"rm-agent-symlink"}`)
return executeHelperCommand(ctx, `{"command":"rm-agent-symlink"}`)
}

// SetCapHelper sets cap setuid on the newly installed helper
func SetCapHelper(ctx context.Context, path string) error {
return executeCommand(ctx, `{"command":"setcap cap_setuid+ep", "path":"`+path+`"}`)
return executeHelperCommand(ctx, `{"command":"setcap cap_setuid+ep", "path":"`+path+`"}`)
}

func executeCommand(ctx context.Context, command string) error {
func executeHelperCommand(ctx context.Context, command string) error {
var err error
var stderr io.ReadCloser
span, _ := tracer.StartSpanFromContext(ctx, "execute_command")
Expand Down
2 changes: 1 addition & 1 deletion pkg/installer/service/datadog_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ func setInstallerAgentGroup(ctx context.Context) error {
if strings.Contains(string(out), "dd-agent") {
return nil
}
return executeCommand(ctx, string(addInstallerToAgentGroup))
return executeHelperCommand(ctx, string(addInstallerToAgentGroup))
}
19 changes: 10 additions & 9 deletions pkg/installer/service/datadog_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const (

var installerUnits = []string{installerUnit, installerUnitExp}

// SetupInstallerUnit installs and starts the installer systemd units
func SetupInstallerUnit(ctx context.Context) (err error) {
// SetupInstallerUnits installs and starts the installer systemd units
func SetupInstallerUnits(ctx context.Context) (err error) {
defer func() {
if err != nil {
log.Errorf("Failed to setup installer units: %s, reverting", err)
Expand All @@ -41,15 +41,16 @@ func SetupInstallerUnit(ctx context.Context) (err error) {
if err = enableUnit(ctx, installerUnit); err != nil {
return err
}

if err = startUnit(ctx, installerUnit); err != nil {
return err
}
return nil
}

// RemoveInstallerUnit removes the installer systemd units
func RemoveInstallerUnit(ctx context.Context) {
// StartInstallerStable starts the stable systemd units for the installer
func StartInstallerStable(ctx context.Context) (err error) {
return startUnit(ctx, installerUnit)
}

// RemoveInstallerUnits removes the installer systemd units
func RemoveInstallerUnits(ctx context.Context) {
var err error
for _, unit := range installerUnits {
if err = disableUnit(ctx, unit); err != nil {
Expand All @@ -66,7 +67,7 @@ func StartInstallerExperiment(ctx context.Context) error {
return startUnit(ctx, installerUnitExp)
}

// StopInstallerExperiment installs the stable systemd units for the installer
// StopInstallerExperiment starts the stable systemd units for the installer
func StopInstallerExperiment(ctx context.Context) error {
return startUnit(ctx, installerUnit)
}
13 changes: 9 additions & 4 deletions pkg/installer/service/datadog_installer_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ package service

import "context"

// SetupInstallerUnit noop
func SetupInstallerUnit(_ context.Context) (err error) {
// SetupInstallerUnits noop
func SetupInstallerUnits(_ context.Context) (err error) {
return nil
}

// RemoveInstallerUnit noop
func RemoveInstallerUnit(_ context.Context) {
// RemoveInstallerUnits noop
func RemoveInstallerUnits(_ context.Context) {
}

// StartInstallerExperiment noop
Expand All @@ -27,3 +27,8 @@ func StartInstallerExperiment(_ context.Context) error {
func StopInstallerExperiment(_ context.Context) error {
return nil
}

// StartInstallerStable noop
func StartInstallerStable(_ context.Context) error {
return nil
}
Loading

0 comments on commit e68edd1

Please sign in to comment.