Skip to content

Commit a6052ca

Browse files
committed
Re-invoke watcher after takeover
1 parent 3ade6df commit a6052ca

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

internal/pkg/agent/application/upgrade/upgrade.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,26 @@ func (u *Upgrader) rollbackToPreviousVersion(ctx context.Context, topDir string,
420420
return nil, fmt.Errorf("stat() on upgrade marker %q failed: %w", updateMarkerPath, err)
421421
}
422422

423-
watcherExecutable := ""
423+
// read the upgrade marker
424+
updateMarker, err := LoadMarker(paths.DataFrom(topDir))
425+
if err != nil {
426+
return nil, fmt.Errorf("loading marker: %w", err)
427+
}
428+
429+
if updateMarker == nil {
430+
return nil, ErrNilUpdateMarker
431+
}
432+
433+
// extract the agent installs involved in the upgrade and select the most appropriate watcher executable
434+
previous, current, err := extractAgentInstallsFromMarker(updateMarker)
435+
if err != nil {
436+
return nil, fmt.Errorf("extracting current and previous install details: %w", err)
437+
}
438+
watcherExecutable := u.watcherHelper.SelectWatcherExecutable(topDir, previous, current)
439+
424440
err = withTakeOverWatcher(ctx, u.log, topDir, u.watcherHelper, func() error {
425441
// read the upgrade marker
426-
updateMarker, err := LoadMarker(paths.DataFrom(topDir))
442+
updateMarker, err = LoadMarker(paths.DataFrom(topDir))
427443
if err != nil {
428444
return fmt.Errorf("loading marker: %w", err)
429445
}
@@ -452,23 +468,17 @@ func (u *Upgrader) rollbackToPreviousVersion(ctx context.Context, topDir string,
452468
return fmt.Errorf("persisting rollback in update marker: %w", err)
453469
}
454470

455-
// extract the agent installs involved in the upgrade and select the most appropriate watcher executable
456-
previous, current, err := extractAgentInstallsFromMarker(updateMarker)
457-
if err != nil {
458-
return fmt.Errorf("extracting current and previous install details: %w", err)
459-
}
460-
watcherExecutable = u.watcherHelper.SelectWatcherExecutable(topDir, previous, current)
461471
return nil
462472
})
463473

464-
if err != nil {
465-
return nil, err
474+
// Invoke watcher again (now that we released the watcher applocks)
475+
_, invokeWatcherErr := u.watcherHelper.InvokeWatcher(u.log, watcherExecutable)
476+
if invokeWatcherErr != nil {
477+
return nil, goerrors.Join(err, fmt.Errorf("invoking watcher: %w", invokeWatcherErr))
466478
}
467479

468-
// Invoke watcher again (now that we released the watcher applocks)
469-
_, err = u.watcherHelper.InvokeWatcher(u.log, watcherExecutable)
470480
if err != nil {
471-
return nil, fmt.Errorf("invoking watcher: %w", err)
481+
return nil, err
472482
}
473483

474484
return nil, nil

internal/pkg/agent/application/upgrade/upgrade_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,22 @@ func TestManualRollback(t *testing.T) {
11431143
},
11441144
additionalAsserts: nil,
11451145
},
1146+
{
1147+
name: "update marker is malformed - rollback fails",
1148+
setup: func(t *testing.T, topDir string, agent *infomocks.Agent, watcherHelper *MockWatcherHelper) {
1149+
err := os.WriteFile(markerFilePath(paths.DataFrom(topDir)), []byte("this is not a proper YAML file"), 0600)
1150+
require.NoError(t, err, "error setting up update marker")
1151+
locker := filelock.NewAppLocker(topDir, "watcher.lock")
1152+
err = locker.TryLock()
1153+
require.NoError(t, err, "error locking initial watcher AppLocker")
1154+
// there's no takeover watcher so no expectation on that or InvokeWatcher
1155+
},
1156+
artifactSettings: artifact.DefaultConfig(),
1157+
upgradeSettings: configuration.DefaultUpgradeConfig(),
1158+
version: "1.2.3",
1159+
wantErr: assert.Error,
1160+
additionalAsserts: nil,
1161+
},
11461162
{
11471163
name: "update marker ok but rollback available is empty - error",
11481164
setup: func(t *testing.T, topDir string, agent *infomocks.Agent, watcherHelper *MockWatcherHelper) {
@@ -1152,6 +1168,9 @@ func TestManualRollback(t *testing.T) {
11521168
err = locker.TryLock()
11531169
require.NoError(t, err, "error locking initial watcher AppLocker")
11541170
watcherHelper.EXPECT().TakeOverWatcher(t.Context(), mock.Anything, topDir).Return(locker, nil)
1171+
newerWatcherExecutable := filepath.Join(topDir, "data", "elastic-agent-4.5.6-newver", "elastic-agent")
1172+
watcherHelper.EXPECT().SelectWatcherExecutable(topDir, agentInstall123, agentInstall456).Return(newerWatcherExecutable)
1173+
watcherHelper.EXPECT().InvokeWatcher(mock.Anything, newerWatcherExecutable).Return(&exec.Cmd{Path: newerWatcherExecutable, Args: []string{"watch", "for realsies"}}, nil)
11551174
},
11561175
artifactSettings: artifact.DefaultConfig(),
11571176
upgradeSettings: configuration.DefaultUpgradeConfig(),
@@ -1178,6 +1197,9 @@ func TestManualRollback(t *testing.T) {
11781197
err = locker.TryLock()
11791198
require.NoError(t, err, "error locking initial watcher AppLocker")
11801199
watcherHelper.EXPECT().TakeOverWatcher(t.Context(), mock.Anything, topDir).Return(locker, nil)
1200+
newerWatcherExecutable := filepath.Join(topDir, "data", "elastic-agent-4.5.6-newver", "elastic-agent")
1201+
watcherHelper.EXPECT().SelectWatcherExecutable(topDir, agentInstall123, agentInstall456).Return(newerWatcherExecutable)
1202+
watcherHelper.EXPECT().InvokeWatcher(mock.Anything, newerWatcherExecutable).Return(&exec.Cmd{Path: newerWatcherExecutable, Args: []string{"watch", "for realsies"}}, nil)
11811203
},
11821204
artifactSettings: artifact.DefaultConfig(),
11831205
upgradeSettings: configuration.DefaultUpgradeConfig(),
@@ -1204,6 +1226,9 @@ func TestManualRollback(t *testing.T) {
12041226
err = locker.TryLock()
12051227
require.NoError(t, err, "error locking initial watcher AppLocker")
12061228
watcherHelper.EXPECT().TakeOverWatcher(t.Context(), mock.Anything, topDir).Return(locker, nil)
1229+
newerWatcherExecutable := filepath.Join(topDir, "data", "elastic-agent-4.5.6-newver", "elastic-agent")
1230+
watcherHelper.EXPECT().SelectWatcherExecutable(topDir, agentInstall123, agentInstall456).Return(newerWatcherExecutable)
1231+
watcherHelper.EXPECT().InvokeWatcher(mock.Anything, newerWatcherExecutable).Return(&exec.Cmd{Path: newerWatcherExecutable, Args: []string{"watch", "for realsies"}}, nil)
12071232
},
12081233
artifactSettings: artifact.DefaultConfig(),
12091234
upgradeSettings: configuration.DefaultUpgradeConfig(),

0 commit comments

Comments
 (0)