mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-03 23:09:57 -05:00
Refactor clustering setup code
This commit is contained in:
parent
721c100bb0
commit
ad20323b52
2 changed files with 30 additions and 44 deletions
|
@ -19,8 +19,6 @@ import (
|
|||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/challenge/tlsalpn01"
|
||||
|
@ -103,31 +101,14 @@ func NewConfig(inst *caddy.Instance) (*Config, error) {
|
|||
certCache, ok := inst.Storage[CertCacheInstStorageKey].(*certmagic.Cache)
|
||||
inst.StorageMu.RUnlock()
|
||||
if !ok || certCache == nil {
|
||||
// set up the clustering plugin, if there is one (and there should always
|
||||
// be one since this tls plugin requires it) -- this should be done exactly
|
||||
// once, but we can't do it during init while plugins are still registering,
|
||||
// so do it as soon as we run a setup)
|
||||
if atomic.CompareAndSwapInt32(&clusterPluginSetup, 0, 1) {
|
||||
clusterPluginName := os.Getenv("CADDY_CLUSTERING")
|
||||
if clusterPluginName == "" {
|
||||
clusterPluginName = "file" // name of default storage plugin
|
||||
}
|
||||
clusterFn, ok := clusterProviders[clusterPluginName]
|
||||
if ok {
|
||||
storage, err := clusterFn()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("constructing cluster plugin %s: %v", clusterPluginName, err)
|
||||
}
|
||||
certmagic.Default.Storage = storage
|
||||
} else {
|
||||
return nil, fmt.Errorf("unrecognized cluster plugin (was it included in the Caddy build?): %s", clusterPluginName)
|
||||
}
|
||||
if err := makeClusteringPlugin(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
certCache = certmagic.NewCache(certmagic.CacheOptions{
|
||||
GetConfigForCert: func(cert certmagic.Certificate) (certmagic.Config, error) {
|
||||
inst.StorageMu.Lock()
|
||||
inst.StorageMu.RLock()
|
||||
cfgMap, ok := inst.Storage[configMapKey].(map[string]*Config)
|
||||
inst.StorageMu.Unlock()
|
||||
inst.StorageMu.RUnlock()
|
||||
if ok {
|
||||
for hostname, cfg := range cfgMap {
|
||||
if cfg.Manager != nil && hostname == cert.Names[0] {
|
||||
|
@ -135,8 +116,6 @@ func NewConfig(inst *caddy.Instance) (*Config, error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
// returning Default not strictly necessary, since Default is used as template
|
||||
// anyway; but this makes it clear that that's what we fall back to
|
||||
return certmagic.Default, nil
|
||||
},
|
||||
})
|
||||
|
|
|
@ -50,25 +50,8 @@ func init() {
|
|||
// are specified by the user in the config file. All the automatic HTTPS
|
||||
// stuff comes later outside of this function.
|
||||
func setupTLS(c *caddy.Controller) error {
|
||||
// set up the clustering plugin, if there is one (and there should always
|
||||
// be one since this tls plugin requires it) -- this should be done exactly
|
||||
// once, but we can't do it during init while plugins are still registering,
|
||||
// so do it as soon as we run a setup)
|
||||
if atomic.CompareAndSwapInt32(&clusterPluginSetup, 0, 1) {
|
||||
clusterPluginName := os.Getenv("CADDY_CLUSTERING")
|
||||
if clusterPluginName == "" {
|
||||
clusterPluginName = "file" // name of default storage plugin
|
||||
}
|
||||
clusterFn, ok := clusterProviders[clusterPluginName]
|
||||
if ok {
|
||||
storage, err := clusterFn()
|
||||
if err != nil {
|
||||
return fmt.Errorf("constructing cluster plugin %s: %v", clusterPluginName, err)
|
||||
}
|
||||
certmagic.Default.Storage = storage
|
||||
} else {
|
||||
return fmt.Errorf("unrecognized cluster plugin (was it included in the Caddy build?): %s", clusterPluginName)
|
||||
}
|
||||
if err := makeClusteringPlugin(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configGetter, ok := configGetters[c.ServerType()]
|
||||
|
@ -464,6 +447,30 @@ func loadCertsInDir(cfg *Config, c *caddy.Controller, dir string) error {
|
|||
})
|
||||
}
|
||||
|
||||
func makeClusteringPlugin() error {
|
||||
// set up the clustering plugin, if there is one (and there should always
|
||||
// be one since this tls plugin requires it) -- this should be done exactly
|
||||
// once, but we can't do it during init while plugins are still registering,
|
||||
// so do it as soon as we run a setup)
|
||||
if atomic.CompareAndSwapInt32(&clusterPluginSetup, 0, 1) {
|
||||
clusterPluginName := os.Getenv("CADDY_CLUSTERING")
|
||||
if clusterPluginName == "" {
|
||||
clusterPluginName = "file" // name of default storage plugin
|
||||
}
|
||||
clusterFn, ok := clusterProviders[clusterPluginName]
|
||||
if ok {
|
||||
storage, err := clusterFn()
|
||||
if err != nil {
|
||||
return fmt.Errorf("constructing cluster plugin %s: %v", clusterPluginName, err)
|
||||
}
|
||||
certmagic.Default.Storage = storage
|
||||
} else {
|
||||
return fmt.Errorf("unrecognized cluster plugin (was it included in the Caddy build?): %s", clusterPluginName)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func constructDefaultClusterPlugin() (certmagic.Storage, error) {
|
||||
return &certmagic.FileStorage{Path: caddy.AssetsPath()}, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue