@@ -71,6 +71,14 @@ func init() {
71
71
}
72
72
}
73
73
74
+ // TODO substitute all the references to watcher with calls to the interface
75
+ type WatcherHelper interface {
76
+ InvokeWatcher (log * logger.Logger , agentExecutable string ) (* exec.Cmd , error )
77
+ SelectWatcherExecutable (topDir string , agentInstalls ... agentInstall ) string
78
+ WaitForWatcher (ctx context.Context , log * logger.Logger , markerFilePath string , waitTime time.Duration ) error
79
+ TakeOverWatcher (ctx context.Context , topDir string ) (* filelock.AppLocker , error )
80
+ }
81
+
74
82
// Upgrader performs an upgrade
75
83
type Upgrader struct {
76
84
log * logger.Logger
@@ -204,7 +212,7 @@ func checkUpgrade(log *logger.Logger, currentVersion, newVersion agentVersion, m
204
212
func (u * Upgrader ) Upgrade (ctx context.Context , version string , rollback bool , sourceURI string , action * fleetapi.ActionUpgrade , det * details.Details , skipVerifyOverride bool , skipDefaultPgp bool , pgpBytes ... string ) (_ reexec.ShutdownCallbackFn , err error ) {
205
213
206
214
if rollback {
207
- return u .forceRollbackToPreviousVersion (ctx , version , action , det )
215
+ return u .forceRollbackToPreviousVersion (ctx , paths . Top (), version , action , det )
208
216
}
209
217
210
218
u .log .Infow ("Upgrading agent" , "version" , version , "source_uri" , sourceURI )
@@ -388,19 +396,19 @@ func (u *Upgrader) Upgrade(ctx context.Context, version string, rollback bool, s
388
396
return cb , nil
389
397
}
390
398
391
- func (u * Upgrader ) forceRollbackToPreviousVersion (ctx context.Context , version string , action * fleetapi.ActionUpgrade , d * details.Details ) (reexec.ShutdownCallbackFn , error ) {
399
+ func (u * Upgrader ) forceRollbackToPreviousVersion (ctx context.Context , topDir string , version string , action * fleetapi.ActionUpgrade , d * details.Details ) (reexec.ShutdownCallbackFn , error ) {
392
400
// Formal checks for verifying we can rollback properly:
393
401
// 1. d.Metadata.RollbacksAvailable should contain the desired version with a valid TTL (it may need to be written by main agent process before starting watcher)
394
402
// 2. there has been at least the first restart with the new agent (i.e. we are not still downloading/extracting/rotating)
395
403
// 3. upgrade marker exists
396
404
// these should be revalidated after taking over watcher
397
- err := u .PersistManualRollback (ctx )
405
+ err := u .PersistManualRollback (ctx , topDir )
398
406
if err != nil {
399
407
return nil , err
400
408
}
401
409
402
410
// Invoke watcher again
403
- _ , err = InvokeWatcher (u .log , paths .BinaryPath (paths .VersionedHome (paths . Top () ), agentName ))
411
+ _ , err = InvokeWatcher (u .log , paths .BinaryPath (paths .VersionedHome (topDir ), agentName ))
404
412
if err != nil {
405
413
return nil , fmt .Errorf ("invoking watcher: %w" , err )
406
414
}
@@ -409,8 +417,8 @@ func (u *Upgrader) forceRollbackToPreviousVersion(ctx context.Context, version s
409
417
410
418
}
411
419
412
- func (u * Upgrader ) PersistManualRollback (ctx context.Context ) error {
413
- watcherApplock , err := u .takeOverWatcher (ctx )
420
+ func (u * Upgrader ) PersistManualRollback (ctx context.Context , topDir string ) error {
421
+ watcherApplock , err := u .takeOverWatcher (ctx , topDir )
414
422
if err != nil {
415
423
return fmt .Errorf ("taking over watcher processes: %w" , err )
416
424
}
@@ -422,7 +430,7 @@ func (u *Upgrader) PersistManualRollback(ctx context.Context) error {
422
430
}(watcherApplock )
423
431
424
432
// read the upgrade marker
425
- updateMarker , err := LoadMarker (paths .Data ( ))
433
+ updateMarker , err := LoadMarker (paths .DataFrom ( topDir ))
426
434
if err != nil {
427
435
return fmt .Errorf ("loading marker: %w" , err )
428
436
}
@@ -435,7 +443,7 @@ func (u *Upgrader) PersistManualRollback(ctx context.Context) error {
435
443
return nil
436
444
}
437
445
438
- func (u * Upgrader ) takeOverWatcher (ctx context.Context ) (* filelock.AppLocker , error ) {
446
+ func (u * Upgrader ) takeOverWatcher (ctx context.Context , topDir string ) (* filelock.AppLocker , error ) {
439
447
440
448
takeoverCtx , takeoverCancel := context .WithTimeout (ctx , 30 * time .Second )
441
449
defer takeoverCancel ()
@@ -479,7 +487,7 @@ func (u *Upgrader) takeOverWatcher(ctx context.Context) (*filelock.AppLocker, er
479
487
case <- takeoverCtx .Done ():
480
488
return nil , fmt .Errorf ("timed out taking over watcher applocker" )
481
489
case <- takeOverTicker .C :
482
- locker := filelock .NewAppLocker (paths . Top () , "watcher.lock" )
490
+ locker := filelock .NewAppLocker (topDir , "watcher.lock" )
483
491
err := locker .TryLock ()
484
492
if err != nil {
485
493
u .log .Errorf ("error locking watcher applocker: %s" , err )
0 commit comments