2019-07-01 06:07:58 +08:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-06-15 01:58:28 +08:00
|
|
|
package caddy
|
2019-03-27 05:45:51 +08:00
|
|
|
|
|
|
|
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-06-29 09:28:28 +08:00
|
|
|
"runtime/debug"
|
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-06-29 09:28:28 +08:00
|
|
|
// Config represents a Caddy configuration.
|
|
|
|
type Config struct {
|
|
|
|
Admin *AdminConfig `json:"admin,omitempty"`
|
|
|
|
|
|
|
|
StorageRaw json.RawMessage `json:"storage,omitempty"`
|
|
|
|
storage certmagic.Storage
|
|
|
|
|
|
|
|
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
|
|
|
|
|
|
|
|
// apps stores the decoded Apps values,
|
|
|
|
// keyed by module name.
|
|
|
|
apps map[string]App
|
|
|
|
|
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// App is a thing that Caddy runs.
|
|
|
|
type App interface {
|
|
|
|
Start() error
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// run the new config and start all its apps
|
|
|
|
err := run(newCfg, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// swap old config with the new one
|
|
|
|
oldCfg := currentCfg
|
|
|
|
currentCfg = newCfg
|
2019-05-17 06:05:38 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// Stop, Cleanup each old app
|
|
|
|
unsyncedStop(oldCfg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// run runs newCfg and starts all its apps if
|
|
|
|
// start is true. If any errors happen, cleanup
|
|
|
|
// is performed if any modules were provisioned;
|
|
|
|
// apps that were started already will be stopped,
|
|
|
|
// so this function should not leak resources if
|
|
|
|
// an error is returned.
|
|
|
|
func run(newCfg *Config, start bool) error {
|
|
|
|
if newCfg == nil {
|
|
|
|
return 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
|
|
|
|
// was an error; if no error, it will get
|
|
|
|
// cleaned up on next config cycle
|
|
|
|
ctx, cancel := NewContext(Context{Context: context.Background(), cfg: newCfg})
|
|
|
|
defer func() {
|
2019-05-17 06:05:38 +08:00
|
|
|
if err != nil {
|
2019-09-30 23:16:01 +08:00
|
|
|
cancel() // clean up now
|
2019-03-27 09:42:52 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
}()
|
|
|
|
newCfg.cancelFunc = cancel // clean up later
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// set up storage and make it CertMagic's default storage, too
|
|
|
|
err = func() error {
|
|
|
|
if newCfg.StorageRaw != nil {
|
|
|
|
val, err := ctx.LoadModuleInline("module", "caddy.storage", newCfg.StorageRaw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading storage module: %v", err)
|
2019-05-17 06:05:38 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
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
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
if newCfg.storage == nil {
|
|
|
|
newCfg.storage = &certmagic.FileStorage{Path: dataDir()}
|
|
|
|
}
|
|
|
|
certmagic.Default.Storage = newCfg.storage
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load, Provision, Validate each app and their submodules
|
|
|
|
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)
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
newCfg.apps[modName] = val.(App)
|
2019-04-08 14:00:14 +08:00
|
|
|
}
|
2019-09-30 23:16:01 +08:00
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-26 03:54:48 +08:00
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
if !start {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-26 03:54:48 +08:00
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// Start
|
|
|
|
return func() error {
|
|
|
|
var started []string
|
|
|
|
for name, a := range newCfg.apps {
|
|
|
|
err := a.Start()
|
|
|
|
if err != nil {
|
|
|
|
// an app failed to start, so we need to stop
|
|
|
|
// all other apps that were already started
|
|
|
|
for _, otherAppName := range started {
|
|
|
|
err2 := newCfg.apps[otherAppName].Stop()
|
|
|
|
if err2 != nil {
|
|
|
|
err = fmt.Errorf("%v; additionally, aborting app %s: %v",
|
|
|
|
err, otherAppName, err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s app module: start: %v", name, err)
|
|
|
|
}
|
|
|
|
started = append(started, name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}()
|
2019-07-13 00:07:11 +08:00
|
|
|
}
|
2019-04-03 05:31:02 +08:00
|
|
|
|
2019-07-13 00:07:11 +08:00
|
|
|
// Stop stops running the current configuration.
|
|
|
|
// It is the antithesis of Run(). This function
|
|
|
|
// will log any errors that occur during the
|
|
|
|
// stopping of individual apps and continue to
|
|
|
|
// stop the others.
|
|
|
|
func Stop() error {
|
|
|
|
currentCfgMu.Lock()
|
|
|
|
defer currentCfgMu.Unlock()
|
|
|
|
unsyncedStop(currentCfg)
|
|
|
|
currentCfg = nil
|
2019-03-27 05:45:51 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// unsyncedStop stops cfg from running, but if
|
2019-07-13 00:07:11 +08:00
|
|
|
// applicable, you need to acquire locks yourself.
|
2019-09-30 23:16:01 +08:00
|
|
|
// It is a no-op if cfg is nil. If any app
|
2019-07-13 00:07:11 +08:00
|
|
|
// returns an error when stopping, it is logged
|
|
|
|
// and the function continues with the next app.
|
2019-09-30 23:16:01 +08:00
|
|
|
// This function assumes all apps in cfg were
|
|
|
|
// successfully started.
|
|
|
|
func unsyncedStop(cfg *Config) {
|
|
|
|
if cfg == nil {
|
2019-07-13 00:07:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop each app
|
2019-09-30 23:16:01 +08:00
|
|
|
for name, a := range cfg.apps {
|
2019-07-13 00:07:11 +08:00
|
|
|
err := a.Stop()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] stop %s: %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-30 23:16:01 +08:00
|
|
|
// clean up all modules
|
|
|
|
cfg.cancelFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate loads, provisions, and validates
|
|
|
|
// cfg, but does not start running it.
|
|
|
|
func Validate(cfg *Config) error {
|
|
|
|
return run(cfg, false)
|
2019-07-13 00:07:11 +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-06-29 09:28:28 +08:00
|
|
|
// GoModule returns the build info of this Caddy
|
|
|
|
// build from debug.BuildInfo (requires Go modules).
|
|
|
|
// If no version information is available, a non-nil
|
|
|
|
// value will still be returned, but with an
|
|
|
|
// unknown version.
|
2019-08-08 13:59:02 +08:00
|
|
|
func GoModule() *debug.Module {
|
|
|
|
var mod debug.Module
|
|
|
|
return goModule(&mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// goModule holds the actual implementation of GoModule.
|
|
|
|
// Allocating debug.Module in GoModule() and passing a
|
|
|
|
// reference to goModule enables mid-stack inlining.
|
|
|
|
func goModule(mod *debug.Module) *debug.Module {
|
|
|
|
mod.Version = "unknown"
|
2019-06-29 09:28:28 +08:00
|
|
|
bi, ok := debug.ReadBuildInfo()
|
|
|
|
if ok {
|
2019-07-13 00:15:27 +08:00
|
|
|
mod.Path = bi.Main.Path
|
2019-06-29 09:28:28 +08:00
|
|
|
// The recommended way to build Caddy involves
|
|
|
|
// creating a separate main module, which
|
|
|
|
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
|
2019-07-13 00:15:27 +08:00
|
|
|
// once that issue is fixed, we should just be able to use bi.Main... hopefully.
|
|
|
|
for _, dep := range bi.Deps {
|
2019-09-14 02:43:28 +08:00
|
|
|
if dep.Path == "github.com/caddyserver/caddy/v2" {
|
2019-08-18 09:14:55 +08:00
|
|
|
return dep
|
2019-06-29 09:28:28 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-18 09:14:55 +08:00
|
|
|
return &bi.Main
|
2019-06-29 09:28:28 +08:00
|
|
|
}
|
2019-07-13 00:15:27 +08:00
|
|
|
return mod
|
2019-06-29 09:28:28 +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
|
|
|
)
|