2015-11-01 15:35:01 +00:00
|
|
|
package provider
|
2015-09-07 13:25:13 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-07 15:39:22 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"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"
|
2016-03-31 16:57:08 +00:00
|
|
|
"github.com/containous/traefik/safe"
|
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
|
|
|
)
|
|
|
|
|
2016-08-16 17:13:18 +00:00
|
|
|
var _ Provider = (*File)(nil)
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// File holds configurations of the File provider.
|
2015-11-02 18:48:34 +00:00
|
|
|
type File struct {
|
2016-06-24 07:58:42 +00:00
|
|
|
BaseProvider `mapstructure:",squash"`
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
// Provide allows the provider to provide configurations to traefik
|
|
|
|
// using the given configuration channel.
|
2016-11-09 18:27:04 +00:00
|
|
|
func (provider *File) Provide(configurationChan chan<- types.ConfigMessage, pool *safe.Pool, constraints types.Constraints) error {
|
2015-09-07 13:25:13 +00:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error creating file watcher", err)
|
2015-10-01 10:04:25 +00:00
|
|
|
return err
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-07 15:39:22 +00:00
|
|
|
file, err := os.Open(provider.Filename)
|
2015-09-07 13:25:13 +00:00
|
|
|
if err != nil {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error opening file", err)
|
2015-10-01 10:04:25 +00:00
|
|
|
return err
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2015-09-07 15:39:22 +00:00
|
|
|
defer file.Close()
|
2015-09-07 13:25:13 +00:00
|
|
|
|
2015-10-03 14:50:53 +00:00
|
|
|
if provider.Watch {
|
|
|
|
// Process events
|
2016-04-13 18:36:23 +00:00
|
|
|
pool.Go(func(stop chan bool) {
|
2015-10-03 14:50:53 +00:00
|
|
|
defer watcher.Close()
|
|
|
|
for {
|
|
|
|
select {
|
2016-04-13 18:36:23 +00:00
|
|
|
case <-stop:
|
|
|
|
return
|
2015-10-03 14:50:53 +00:00
|
|
|
case event := <-watcher.Events:
|
|
|
|
if strings.Contains(event.Name, file.Name()) {
|
|
|
|
log.Debug("File event:", event)
|
2015-11-01 18:29:47 +00:00
|
|
|
configuration := provider.loadFileConfig(file.Name())
|
2015-10-03 14:50:53 +00:00
|
|
|
if configuration != nil {
|
2015-11-13 10:50:32 +00:00
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "file",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
2015-10-03 14:50:53 +00:00
|
|
|
}
|
2015-09-07 21:25:07 +00:00
|
|
|
}
|
2015-10-03 14:50:53 +00:00
|
|
|
case error := <-watcher.Errors:
|
|
|
|
log.Error("Watcher event error", error)
|
2015-09-07 15:39:22 +00:00
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
2016-03-31 16:57:08 +00:00
|
|
|
})
|
2015-09-07 16:10:33 +00:00
|
|
|
err = watcher.Add(filepath.Dir(file.Name()))
|
2015-10-03 14:50:53 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error adding file watcher", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-09-07 15:39:22 +00:00
|
|
|
}
|
2015-09-07 13:25:13 +00:00
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
configuration := provider.loadFileConfig(file.Name())
|
2015-11-13 10:50:32 +00:00
|
|
|
configurationChan <- types.ConfigMessage{
|
|
|
|
ProviderName: "file",
|
|
|
|
Configuration: configuration,
|
|
|
|
}
|
2015-10-01 10:04:25 +00:00
|
|
|
return nil
|
2015-09-07 13:25:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-01 18:29:47 +00:00
|
|
|
func (provider *File) loadFileConfig(filename string) *types.Configuration {
|
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 {
|
2015-09-11 14:37:13 +00:00
|
|
|
log.Error("Error reading file:", err)
|
2015-09-07 13:25:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-07 22:15:14 +00:00
|
|
|
return configuration
|
2015-09-12 13:10:03 +00:00
|
|
|
}
|