provisionContext creates a new context from the given configuration and provisions storage and apps. If `newCfg` is nil a new empty configuration will be created. If `replaceAdminServer` is true any currently active admin server will be replaced with a new admin server based on the provided configur
(newCfg *Config, replaceAdminServer bool)
| 482 | // If `replaceAdminServer` is true any currently active admin server will be replaced |
| 483 | // with a new admin server based on the provided configuration. |
| 484 | func provisionContext(newCfg *Config, replaceAdminServer bool) (Context, error) { |
| 485 | // because we will need to roll back any state |
| 486 | // modifications if this function errors, we |
| 487 | // keep a single error value and scope all |
| 488 | // sub-operations to their own functions to |
| 489 | // ensure this error value does not get |
| 490 | // overridden or missed when it should have |
| 491 | // been set by a short assignment |
| 492 | var err error |
| 493 | |
| 494 | if newCfg == nil { |
| 495 | newCfg = new(Config) |
| 496 | } |
| 497 | |
| 498 | // create a context within which to load |
| 499 | // modules - essentially our new config's |
| 500 | // execution environment; be sure that |
| 501 | // cleanup occurs when we return if there |
| 502 | // was an error; if no error, it will get |
| 503 | // cleaned up on next config cycle |
| 504 | ctx, cancelCause := NewContextWithCause(Context{Context: context.Background(), cfg: newCfg}) |
| 505 | defer func() { |
| 506 | if err != nil { |
| 507 | globalMetrics.configSuccess.Set(0) |
| 508 | // if there were any errors during startup, |
| 509 | // we should cancel the new context we created |
| 510 | // since the associated config won't be used; |
| 511 | // this will cause all modules that were newly |
| 512 | // provisioned to clean themselves up |
| 513 | cancelCause(fmt.Errorf("configuration error: %w", err)) |
| 514 | |
| 515 | // also undo any other state changes we made |
| 516 | if currentCtx.cfg != nil { |
| 517 | certmagic.Default.Storage = currentCtx.cfg.storage |
| 518 | } |
| 519 | } |
| 520 | }() |
| 521 | newCfg.cancelFunc = cancelCause // clean up later |
| 522 | |
| 523 | // set up logging before anything bad happens |
| 524 | if newCfg.Logging == nil { |
| 525 | newCfg.Logging = new(Logging) |
| 526 | } |
| 527 | err = newCfg.Logging.openLogs(ctx) |
| 528 | if err != nil { |
| 529 | return ctx, err |
| 530 | } |
| 531 | |
| 532 | // create the new filesystem map |
| 533 | newCfg.fileSystems = &filesystems.FileSystemMap{} |
| 534 | |
| 535 | // prepare the new config for use |
| 536 | newCfg.apps = make(map[string]App) |
| 537 | newCfg.failedApps = make(map[string]error) |
| 538 | |
| 539 | // set up global storage and make it CertMagic's default storage, too |
| 540 | err = func() error { |
| 541 | if newCfg.StorageRaw != nil { |
no test coverage detected