2017-04-17 10:50:02 +00:00
|
|
|
package file
|
2015-09-07 13:25:13 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-26 13:32:03 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-11-09 11:16:03 +00:00
|
|
|
"os"
|
2017-05-26 13:32:03 +00:00
|
|
|
"path"
|
2017-11-09 11:16:03 +00:00
|
|
|
"path/filepath"
|
2015-09-07 15:39:22 +00:00
|
|
|
"strings"
|
2015-09-24 15:16:13 +00:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2016-08-18 12:20:11 +00:00
|
|
|
"github.com/containous/traefik/log"
|
2017-04-17 10:50:02 +00:00
|
|
|
"github.com/containous/traefik/provider"
|
2016-03-31 16:57:08 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
2017-11-09 11:16:03 +00:00
|
|
|
"github.com/containous/traefik/tls"
|
2016-02-24 15:43:39 +00:00
|
|
|
"github.com/containous/traefik/types"
|
2015-09-24 15:16:13 +00:00
|
|
|
"gopkg.in/fsnotify.v1"
|
2015-09-07 13:25:13 +00:00
|
|
|
)
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
var _ provider.Provider = (*Provider)(nil)
|
2016-08-16 17:13:18 +00:00
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provider holds configurations of the provider.
|
|
|
|
type Provider struct {
|
2017-10-02 08:32:02 +00:00
|
|
|
provider.BaseProvider `mapstructure:",squash" export:"true"`
|
|
|
|
Directory string `description:"Load configuration from one or more .toml files in a directory" export:"true"`
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 10:50:02 +00:00
|
|
|
// Provide allows the file provider to provide configurations to traefik
|
2015-11-01 18:29:47 +00:00
|
|
|
// using the given configuration channel.
|
2017-04-17 10:50:02 +00:00
|
|
|
func (p *Provider) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
2017-05-26 13:32:03 +00:00
|
|
|
configuration, err := p.loadConfig()
|
|
|
|
|
2015-09-07 13:25:13 +00:00
|
|
|
if err != nil {
|
2015-10-01 10:04:25 +00:00
|
|
|
return err
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
if p.Watch {
|
|
|
|
var watchItem string
|
|
|
|
|
|
|
|
if p.Directory != "" {
|
|
|
|
watchItem = p.Directory
|
|
|
|
} else {
|
2017-11-09 11:16:03 +00:00
|
|
|
watchItem = filepath.Dir(p.Filename)
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.addWatcher(pool, watchItem, configurationChan, p.watcherCallback); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendConfigToChannel(configurationChan, configuration)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) addWatcher(pool *safe.Pool, directory string, configurationChan chan<- types.ConfigMessage, callback func(chan<- types.ConfigMessage, fsnotify.Event)) error {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
2015-09-07 13:25:13 +00:00
|
|
|
if err != nil {
|
2017-05-26 13:32:03 +00:00
|
|
|
return fmt.Errorf("error creating file watcher: %s", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
// Process events
|
|
|
|
pool.Go(func(stop chan bool) {
|
|
|
|
defer watcher.Close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
case evt := <-watcher.Events:
|
2017-11-09 11:16:03 +00:00
|
|
|
if p.Directory == "" {
|
|
|
|
_, evtFileName := filepath.Split(evt.Name)
|
|
|
|
_, confFileName := filepath.Split(p.Filename)
|
|
|
|
if evtFileName == confFileName {
|
|
|
|
callback(configurationChan, evt)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
callback(configurationChan, evt)
|
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
case err := <-watcher.Errors:
|
|
|
|
log.Errorf("Watcher event error: %s", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2015-10-03 14:50:53 +00:00
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
})
|
|
|
|
err = watcher.Add(directory)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error adding file watcher: %s", err)
|
2015-09-07 15:39:22 +00:00
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendConfigToChannel(configurationChan chan<- types.ConfigMessage, configuration *types.Configuration) {
|
2015-11-13 10:50:32 +00:00
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "file",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
func loadFileConfig(filename string) (*types.Configuration, error) {
|
2015-11-01 15:35:01 +00:00
|
|
|
configuration := new(types.Configuration)
|
2015-09-07 22:15:14 +00:00
|
|
|
if _, err := toml.DecodeFile(filename, configuration); err != nil {
|
2017-05-26 13:32:03 +00:00
|
|
|
return nil, fmt.Errorf("error reading configuration file: %s", err)
|
|
|
|
}
|
|
|
|
return configuration, nil
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
func loadFileConfigFromDirectory(directory string, configuration *types.Configuration) (*types.Configuration, error) {
|
2017-05-26 13:32:03 +00:00
|
|
|
fileList, err := ioutil.ReadDir(directory)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-11-09 11:16:03 +00:00
|
|
|
return configuration, fmt.Errorf("unable to read directory %s: %v", directory, err)
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
if configuration == nil {
|
|
|
|
configuration = &types.Configuration{
|
|
|
|
Frontends: make(map[string]*types.Frontend),
|
|
|
|
Backends: make(map[string]*types.Backend),
|
|
|
|
TLSConfiguration: make([]*tls.Configuration, 0),
|
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
configTLSMaps := make(map[*tls.Configuration]struct{})
|
|
|
|
for _, item := range fileList {
|
|
|
|
|
|
|
|
if item.IsDir() {
|
|
|
|
configuration, err = loadFileConfigFromDirectory(filepath.Join(directory, item.Name()), configuration)
|
|
|
|
if err != nil {
|
|
|
|
return configuration, fmt.Errorf("unable to load content configuration from subdirectory %s: %v", item, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else if !strings.HasSuffix(item.Name(), ".toml") {
|
2017-05-26 13:32:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var c *types.Configuration
|
2017-11-09 11:16:03 +00:00
|
|
|
c, err = loadFileConfig(path.Join(directory, item.Name()))
|
2017-05-26 13:32:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-11-09 11:16:03 +00:00
|
|
|
return configuration, err
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for backendName, backend := range c.Backends {
|
|
|
|
if _, exists := configuration.Backends[backendName]; exists {
|
|
|
|
log.Warnf("Backend %s already configured, skipping", backendName)
|
|
|
|
} else {
|
|
|
|
configuration.Backends[backendName] = backend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for frontendName, frontend := range c.Frontends {
|
|
|
|
if _, exists := configuration.Frontends[frontendName]; exists {
|
|
|
|
log.Warnf("Frontend %s already configured, skipping", frontendName)
|
|
|
|
} else {
|
|
|
|
configuration.Frontends[frontendName] = frontend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 11:16:03 +00:00
|
|
|
for _, conf := range c.TLSConfiguration {
|
|
|
|
if _, exists := configTLSMaps[conf]; exists {
|
|
|
|
log.Warnf("TLS Configuration %v already configured, skipping", conf)
|
|
|
|
} else {
|
|
|
|
configTLSMaps[conf] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for conf := range configTLSMaps {
|
|
|
|
configuration.TLSConfiguration = append(configuration.TLSConfiguration, conf)
|
|
|
|
}
|
2017-05-26 13:32:03 +00:00
|
|
|
return configuration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) watcherCallback(configurationChan chan<- types.ConfigMessage, event fsnotify.Event) {
|
2017-11-09 11:16:03 +00:00
|
|
|
watchItem := p.Filename
|
|
|
|
if p.Directory != "" {
|
|
|
|
watchItem = p.Directory
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(watchItem); err != nil {
|
|
|
|
log.Debugf("Unable to watch %s : %v", watchItem, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-26 13:32:03 +00:00
|
|
|
configuration, err := p.loadConfig()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error occurred during watcher callback: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendConfigToChannel(configurationChan, configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) loadConfig() (*types.Configuration, error) {
|
|
|
|
if p.Directory != "" {
|
2017-11-09 11:16:03 +00:00
|
|
|
return loadFileConfigFromDirectory(p.Directory, nil)
|
2017-05-26 13:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return loadFileConfig(p.Filename)
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|