@@ -172,7 +172,7 @@ func newEnrollCmd(
172
172
)
173
173
}
174
174
175
- // newEnrollCmdWithStore creates an new enrollment and accept a custom store.
175
+ // newEnrollCmdWithStore creates a new enrollment and accept a custom store.
176
176
func newEnrollCmdWithStore (
177
177
log * logger.Logger ,
178
178
options * enrollCmdOption ,
@@ -187,10 +187,11 @@ func newEnrollCmdWithStore(
187
187
}, nil
188
188
}
189
189
190
- // Execute tries to enroll the agent into Fleet.
190
+ // Execute enrolls the agent into Fleet.
191
191
func (c * enrollCmd ) Execute (ctx context.Context , streams * cli.IOStreams ) error {
192
192
var err error
193
193
defer c .stopAgent () // ensure its stopped no matter what
194
+
194
195
span , ctx := apm .StartSpan (ctx , "enroll" , "app.internal" )
195
196
defer func () {
196
197
apm .CaptureError (ctx , err ).Send ()
@@ -235,7 +236,7 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
235
236
// Ensure that the agent does not use a proxy configuration
236
237
// when connecting to the local fleet server.
237
238
// Note that when running fleet-server the enroll request will be sent to :8220,
238
- // however when the agent is running afterwards requests will be sent to :8221
239
+ // however when the agent is running afterward requests will be sent to :8221
239
240
c .remoteConfig .Transport .Proxy .Disable = true
240
241
}
241
242
@@ -256,7 +257,7 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
256
257
257
258
err = c .enrollWithBackoff (ctx , persistentConfig )
258
259
if err != nil {
259
- return errors . New ( err , "fail to enroll" )
260
+ return fmt . Errorf ( "fail to enroll: %w" , err )
260
261
}
261
262
262
263
if c .options .FixPermissions {
@@ -267,17 +268,23 @@ func (c *enrollCmd) Execute(ctx context.Context, streams *cli.IOStreams) error {
267
268
}
268
269
269
270
defer func () {
270
- fmt .Fprintln (streams .Out , "Successfully enrolled the Elastic Agent." )
271
+ if err != nil {
272
+ fmt .Fprintf (streams .Err , "Something went wrong while enrolling the Elastic Agent: %v\n " , err )
273
+ } else {
274
+ fmt .Fprintln (streams .Out , "Successfully enrolled the Elastic Agent." )
275
+ }
271
276
}()
272
277
273
278
if c .agentProc == nil {
274
- if err := c .daemonReload (ctx ); err != nil {
275
- c .log .Infow ("Elastic Agent might not be running; unable to trigger restart" , "error" , err )
276
- } else {
277
- c .log .Info ("Successfully triggered restart on running Elastic Agent." )
279
+ if err = c .daemonReloadWithBackoff (ctx ); err != nil {
280
+ c .log .Errorf ("Elastic Agent might not be running; unable to trigger restart: %v" , err )
281
+ return fmt .Errorf ("could not reload agent daemon, unable to trigger restart: %w" , err )
278
282
}
283
+
284
+ c .log .Info ("Successfully triggered restart on running Elastic Agent." )
279
285
return nil
280
286
}
287
+
281
288
c .log .Info ("Elastic Agent has been enrolled; start Elastic Agent" )
282
289
return nil
283
290
}
@@ -443,24 +450,35 @@ func (c *enrollCmd) prepareFleetTLS() error {
443
450
444
451
func (c * enrollCmd ) daemonReloadWithBackoff (ctx context.Context ) error {
445
452
err := c .daemonReload (ctx )
453
+ if err != nil &&
454
+ (errors .Is (err , context .DeadlineExceeded ) ||
455
+ errors .Is (err , context .Canceled )) {
456
+ return fmt .Errorf ("could not reload daemon: %w" , err )
457
+ }
446
458
if err == nil {
447
459
return nil
448
460
}
449
461
450
462
signal := make (chan struct {})
463
+ defer close (signal )
451
464
backExp := backoff .NewExpBackoff (signal , 10 * time .Second , 1 * time .Minute )
452
465
453
- for i := 5 ; i >= 0 ; i -- {
466
+ for i := 0 ; i < 5 ; i ++ {
454
467
backExp .Wait ()
455
468
c .log .Info ("Retrying to restart..." )
456
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 after %d retries: %w" ,
474
+ i + 1 , err )
475
+ }
457
476
if err == nil {
458
- break
477
+ return nil
459
478
}
460
479
}
461
480
462
- close (signal )
463
- return err
481
+ return fmt .Errorf ("could not reload agent's daemon, all retries failed. Last error: %w" , err )
464
482
}
465
483
466
484
func (c * enrollCmd ) daemonReload (ctx context.Context ) error {
@@ -478,8 +496,20 @@ func (c *enrollCmd) enrollWithBackoff(ctx context.Context, persistentConfig map[
478
496
479
497
c .log .Infof ("Starting enrollment to URL: %s" , c .client .URI ())
480
498
err := c .enroll (ctx , persistentConfig )
499
+ if err == nil {
500
+ return nil
501
+ }
502
+
503
+ const deadline = 10 * time .Minute
504
+ const frequency = 60 * time .Second
505
+
506
+ c .log .Infof ("1st enrollment attempt failed, retrying for %s, every %s enrolling to URL: %s" ,
507
+ deadline ,
508
+ frequency ,
509
+ c .client .URI ())
481
510
signal := make (chan struct {})
482
- backExp := backoff .NewExpBackoff (signal , 60 * time .Second , 10 * time .Minute )
511
+ defer close (signal )
512
+ backExp := backoff .NewExpBackoff (signal , frequency , deadline )
483
513
484
514
for {
485
515
retry := false
@@ -498,7 +528,6 @@ func (c *enrollCmd) enrollWithBackoff(ctx context.Context, persistentConfig map[
498
528
err = c .enroll (ctx , persistentConfig )
499
529
}
500
530
501
- close (signal )
502
531
return err
503
532
}
504
533
@@ -547,8 +576,10 @@ func (c *enrollCmd) enroll(ctx context.Context, persistentConfig map[string]inte
547
576
c .options .FleetServer .ElasticsearchInsecure ,
548
577
)
549
578
if err != nil {
550
- return err
579
+ return fmt .Errorf (
580
+ "failed creating fleet-server bootstrap config: %w" , err )
551
581
}
582
+
552
583
// no longer need bootstrap at this point
553
584
serverConfig .Server .Bootstrap = false
554
585
fleetConfig .Server = serverConfig .Server
@@ -568,11 +599,11 @@ func (c *enrollCmd) enroll(ctx context.Context, persistentConfig map[string]inte
568
599
569
600
reader , err := yamlToReader (configToStore )
570
601
if err != nil {
571
- return err
602
+ return fmt . Errorf ( "yamlToReader failed: %w" , err )
572
603
}
573
604
574
605
if err := safelyStoreAgentInfo (c .configStore , reader ); err != nil {
575
- return err
606
+ return fmt . Errorf ( "failed to store agent config: %w" , err )
576
607
}
577
608
578
609
// clear action store
0 commit comments