2019-03-27 05:45:51 +08:00
|
|
|
package caddy2
|
|
|
|
|
|
|
|
import (
|
2019-05-17 06:05:38 +08:00
|
|
|
"context"
|
2019-03-27 05:45:51 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-03-27 09:42:52 +08:00
|
|
|
"log"
|
2019-03-27 05:45:51 +08:00
|
|
|
"strings"
|
2019-03-27 09:42:52 +08:00
|
|
|
"sync"
|
2019-03-27 05:45:51 +08:00
|
|
|
"time"
|
2019-04-26 03:54:48 +08:00
|
|
|
|
|
|
|
"github.com/mholt/certmagic"
|
2019-03-27 05:45:51 +08:00
|
|
|
)
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// Run runs Caddy with the given config.
|
2019-05-17 06:05:38 +08:00
|
|
|
func Run(newCfg *Config) error {
|
|
|
|
currentCfgMu.Lock()
|
|
|
|
defer currentCfgMu.Unlock()
|
|
|
|
|
|
|
|
if newCfg != nil {
|
|
|
|
// because we will need to roll back any state
|
|
|
|
// modifications if this function errors, we
|
|
|
|
// keep a single error value and scope all
|
|
|
|
// sub-operations to their own functions to
|
|
|
|
// ensure this error value does not get
|
|
|
|
// overridden or missed when it should have
|
|
|
|
// been set by a short assignment
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// prepare the new config for use
|
|
|
|
newCfg.apps = make(map[string]App)
|
|
|
|
|
|
|
|
// create a context within which to load
|
|
|
|
// modules - essentially our new config's
|
|
|
|
// execution environment; be sure that
|
|
|
|
// cleanup occurs when we return if there
|
2019-05-21 00:59:20 +08:00
|
|
|
// was an error; if no error, it will get
|
2019-05-17 06:05:38 +08:00
|
|
|
// cleaned up on next config cycle
|
|
|
|
ctx, cancel := NewContext(Context{Context: context.Background(), cfg: newCfg})
|
|
|
|
defer func() {
|
2019-04-26 03:54:48 +08:00
|
|
|
if err != nil {
|
2019-05-17 06:05:38 +08:00
|
|
|
cancel() // clean up now
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
2019-05-17 06:05:38 +08:00
|
|
|
}()
|
|
|
|
newCfg.cancelFunc = cancel // clean up later
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
// set up storage and make it CertMagic's default storage, too
|
|
|
|
err = func() error {
|
|
|
|
if newCfg.StorageRaw != nil {
|
|
|
|
val, err := ctx.LoadModuleInline("system", "caddy.storage", newCfg.StorageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading storage module: %v", err)
|
|
|
|
}
|
|
|
|
stor, err := val.(StorageConverter).CertMagicStorage()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating storage value: %v", err)
|
|
|
|
}
|
|
|
|
newCfg.storage = stor
|
|
|
|
newCfg.StorageRaw = nil // allow GC to deallocate - TODO: Does this help?
|
|
|
|
}
|
|
|
|
if newCfg.storage == nil {
|
|
|
|
newCfg.storage = &certmagic.FileStorage{Path: dataDir()}
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-05-17 06:05:38 +08:00
|
|
|
certmagic.Default.Storage = newCfg.storage
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
// Load, Provision, Validate
|
|
|
|
err = func() error {
|
|
|
|
for modName, rawMsg := range newCfg.AppsRaw {
|
|
|
|
val, err := ctx.LoadModule(modName, rawMsg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading app module '%s': %v", modName, err)
|
|
|
|
}
|
|
|
|
newCfg.apps[modName] = val.(App)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
2019-04-08 14:00:14 +08:00
|
|
|
if err != nil {
|
2019-05-17 06:05:38 +08:00
|
|
|
return err
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
// Start
|
|
|
|
err = func() error {
|
|
|
|
var started []string
|
|
|
|
for name, a := range newCfg.apps {
|
|
|
|
err := a.Start()
|
|
|
|
if err != nil {
|
|
|
|
for _, otherAppName := range started {
|
|
|
|
err2 := newCfg.apps[otherAppName].Stop()
|
|
|
|
if err2 != nil {
|
|
|
|
err = fmt.Errorf("%v; additionally, aborting app %s: %v",
|
|
|
|
err, otherAppName, err2)
|
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
2019-05-17 06:05:38 +08:00
|
|
|
return fmt.Errorf("%s app module: start: %v", name, err)
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
2019-05-17 06:05:38 +08:00
|
|
|
started = append(started, name)
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
2019-05-17 06:05:38 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
// swap old config with the new one
|
|
|
|
oldCfg := currentCfg
|
|
|
|
currentCfg = newCfg
|
|
|
|
|
|
|
|
// Stop, Cleanup
|
2019-04-26 03:54:48 +08:00
|
|
|
if oldCfg != nil {
|
|
|
|
for name, a := range oldCfg.apps {
|
|
|
|
err := a.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] stop %s: %v", name, err)
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
// clean up old modules
|
|
|
|
oldCfg.cancelFunc()
|
2019-04-03 05:31:02 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// App is a thing that Caddy runs.
|
|
|
|
type App interface {
|
2019-05-17 06:05:38 +08:00
|
|
|
Start() error
|
2019-04-26 03:54:48 +08:00
|
|
|
Stop() error
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config represents a Caddy configuration.
|
|
|
|
type Config struct {
|
2019-05-23 02:32:36 +08:00
|
|
|
StorageRaw json.RawMessage `json:"storage,omitempty"`
|
2019-04-26 03:54:48 +08:00
|
|
|
storage certmagic.Storage
|
|
|
|
|
2019-05-23 02:32:36 +08:00
|
|
|
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
|
2019-04-08 14:00:14 +08:00
|
|
|
|
2019-04-26 03:54:48 +08:00
|
|
|
// apps stores the decoded Apps values,
|
2019-04-08 14:00:14 +08:00
|
|
|
// keyed by module name.
|
2019-04-26 03:54:48 +08:00
|
|
|
apps map[string]App
|
2019-04-08 14:00:14 +08:00
|
|
|
|
2019-05-17 06:05:38 +08:00
|
|
|
cancelFunc context.CancelFunc
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:51 +08:00
|
|
|
// Duration is a JSON-string-unmarshable duration type.
|
|
|
|
type Duration time.Duration
|
|
|
|
|
|
|
|
// UnmarshalJSON satisfies json.Unmarshaler.
|
2019-04-01 10:41:29 +08:00
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
2019-03-27 05:45:51 +08:00
|
|
|
dd, err := time.ParseDuration(strings.Trim(string(b), `"`))
|
2019-04-01 10:41:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-30 13:09:51 +08:00
|
|
|
*d = Duration(dd)
|
2019-04-01 10:41:29 +08:00
|
|
|
return nil
|
2019-03-27 05:45:51 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:42:55 +08:00
|
|
|
// CtxKey is a value type for use with context.WithValue.
|
|
|
|
type CtxKey string
|
2019-04-08 14:00:14 +08:00
|
|
|
|
|
|
|
// currentCfg is the currently-loaded configuration.
|
|
|
|
var (
|
|
|
|
currentCfg *Config
|
2019-04-26 03:54:48 +08:00
|
|
|
currentCfgMu sync.RWMutex
|
2019-04-08 14:00:14 +08:00
|
|
|
)
|