@@ -189,7 +189,7 @@ func newEnrollCmd(
189
189
)
190
190
}
191
191
192
- // newEnrollCmdWithStore creates an new enrollment and accept a custom store.
192
+ // newEnrollCmdWithStore creates a new enrollment and accept a custom store.
193
193
func newEnrollCmdWithStore (
194
194
log * logger.Logger ,
195
195
options * enrollCmdOption ,
@@ -204,10 +204,11 @@ func newEnrollCmdWithStore(
204
204
}, nil
205
205
}
206
206
207
- // Execute tries to enroll the agent into Fleet.
207
+ // Execute enrolls the agent into Fleet.
208
208
func (c * enrollCmd ) Execute (ctx context.Context , streams * cli.IOStreams ) error {
209
209
var err error
210
210
defer c .stopAgent () // ensure its stopped no matter what
211
+
211
212
span , ctx := apm .StartSpan (ctx , "enroll" , "app.internal" )
212
213
defer func () {
213
214
apm .CaptureError (ctx , err ).Send ()
@@ -252,7 +253,7 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
252
253
// Ensure that the agent does not use a proxy configuration
253
254
// when connecting to the local fleet server.
254
255
// Note that when running fleet-server the enroll request will be sent to :8220,
255
- // however when the agent is running afterwards requests will be sent to :8221
256
+ // however when the agent is running afterward requests will be sent to :8221
256
257
c .remoteConfig .Transport .Proxy .Disable = true
257
258
}
258
259
@@ -273,7 +274,7 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
273
274
274
275
err = c .enrollWithBackoff (ctx , persistentConfig )
275
276
if err != nil {
276
- return errors . New ( err , "fail to enroll" )
277
+ return fmt . Errorf ( "fail to enroll: %w" , err )
277
278
}
278
279
279
280
if c .options .FixPermissions {
@@ -284,17 +285,23 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
284
285
}
285
286
286
287
defer func () {
287
- fmt .Fprintln (streams .Out , "Successfully enrolled the Elastic Agent." )
288
+ if err != nil {
289
+ fmt .Fprintf (streams .Err , "Something went wrong while enrolling the Elastic Agent: %v\n " , err )
290
+ } else {
291
+ fmt .Fprintln (streams .Out , "Successfully enrolled the Elastic Agent." )
292
+ }
288
293
}()
289
294
290
295
if c .agentProc == nil {
291
- if err := c .daemonReload (ctx ); err != nil {
292
- c .log .Infow ("Elastic Agent might not be running; unable to trigger restart" , "error" , err )
293
- } else {
294
- c .log .Info ("Successfully triggered restart on running Elastic Agent." )
296
+ if err = c .daemonReloadWithBackoff (ctx ); err != nil {
297
+ c .log .Errorf ("Elastic Agent might not be running; unable to trigger restart: %v" , err )
298
+ return fmt .Errorf ("could not reload agent daemon, unable to trigger restart: %w" , err )
295
299
}
300
+
301
+ c .log .Info ("Successfully triggered restart on running Elastic Agent." )
296
302
return nil
297
303
}
304
+
298
305
c .log .Info ("Elastic Agent has been enrolled; start Elastic Agent" )
299
306
return nil
300
307
}
@@ -460,24 +467,35 @@ func (c *enrollCmd) prepareFleetTLS() error {
460
467
461
468
func (c * enrollCmd ) daemonReloadWithBackoff (ctx context.Context ) error {
462
469
err := c .daemonReload (ctx )
470
+ if err != nil &&
471
+ (errors .Is (err , context .DeadlineExceeded ) ||
472
+ errors .Is (err , context .Canceled )) {
473
+ return fmt .Errorf ("could not reload daemon: %w" , err )
474
+ }
463
475
if err == nil {
464
476
return nil
465
477
}
466
478
467
479
signal := make (chan struct {})
480
+ defer close (signal )
468
481
backExp := backoff .NewExpBackoff (signal , 10 * time .Second , 1 * time .Minute )
469
482
470
- for i := 5 ; i >= 0 ; i -- {
483
+ for i := 0 ; i < 5 ; i ++ {
471
484
backExp .Wait ()
472
485
c .log .Info ("Retrying to restart..." )
473
486
err = c .daemonReload (ctx )
487
+ if err != nil &&
488
+ (errors .Is (err , context .DeadlineExceeded ) ||
489
+ errors .Is (err , context .Canceled )) {
490
+ return fmt .Errorf ("could not reload daemon after %d retries: %w" ,
491
+ i + 1 , err )
492
+ }
474
493
if err == nil {
475
- break
494
+ return nil
476
495
}
477
496
}
478
497
479
- close (signal )
480
- return err
498
+ return fmt .Errorf ("could not reload agent's daemon, all retries failed. Last error: %w" , err )
481
499
}
482
500
483
501
func (c * enrollCmd ) daemonReload (ctx context.Context ) (err error ) {
@@ -501,8 +519,20 @@ func (c *enrollCmd) enrollWithBackoff(ctx context.Context, persistentConfig map[
501
519
502
520
c .log .Infof ("Starting enrollment to URL: %s" , c .client .URI ())
503
521
err := c .enroll (ctx , persistentConfig )
522
+ if err == nil {
523
+ return nil
524
+ }
525
+
526
+ const deadline = 10 * time .Minute
527
+ const frequency = 60 * time .Second
528
+
529
+ c .log .Infof ("1st enrollment attempt failed, retrying for %s, every %s enrolling to URL: %s" ,
530
+ deadline ,
531
+ frequency ,
532
+ c .client .URI ())
504
533
signal := make (chan struct {})
505
- backExp := backoff .NewExpBackoff (signal , 60 * time .Second , 10 * time .Minute )
534
+ defer close (signal )
535
+ backExp := backoff .NewExpBackoff (signal , frequency , deadline )
506
536
507
537
for {
508
538
retry := false
@@ -521,7 +551,6 @@ func (c *enrollCmd) enrollWithBackoff(ctx context.Context, persistentConfig map[
521
551
err = c .enroll (ctx , persistentConfig )
522
552
}
523
553
524
- close (signal )
525
554
return err
526
555
}
527
556
@@ -570,8 +599,10 @@ func (c *enrollCmd) enroll(ctx context.Context, persistentConfig map[string]inte
570
599
c .options .FleetServer .ElasticsearchInsecure ,
571
600
)
572
601
if err != nil {
573
- return err
602
+ return fmt .Errorf (
603
+ "failed creating fleet-server bootstrap config: %w" , err )
574
604
}
605
+
575
606
// no longer need bootstrap at this point
576
607
serverConfig .Server .Bootstrap = false
577
608
fleetConfig .Server = serverConfig .Server
@@ -591,11 +622,11 @@ func (c *enrollCmd) enroll(ctx context.Context, persistentConfig map[string]inte
591
622
592
623
reader , err := yamlToReader (configToStore )
593
624
if err != nil {
594
- return err
625
+ return fmt . Errorf ( "yamlToReader failed: %w" , err )
595
626
}
596
627
597
628
if err := safelyStoreAgentInfo (c .configStore , reader ); err != nil {
598
- return err
629
+ return fmt . Errorf ( "failed to store agent config: %w" , err )
599
630
}
600
631
601
632
// clear action store
0 commit comments