2018-03-01 07:10:04 +00:00
|
|
|
package storeconfig
|
2017-11-09 16:08:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-11-21 09:24:03 +00:00
|
|
|
stdlog "log"
|
2017-11-09 16:08:03 +00:00
|
|
|
|
2018-01-24 16:52:03 +00:00
|
|
|
"github.com/abronan/valkeyrie/store"
|
2017-11-09 16:08:03 +00:00
|
|
|
"github.com/containous/flaeg"
|
|
|
|
"github.com/containous/staert"
|
2018-03-01 07:10:04 +00:00
|
|
|
"github.com/containous/traefik/cmd"
|
2017-11-09 16:08:03 +00:00
|
|
|
)
|
|
|
|
|
2018-03-01 07:10:04 +00:00
|
|
|
// NewCmd builds a new StoreConfig command
|
|
|
|
func NewCmd(traefikConfiguration *cmd.TraefikConfiguration, traefikPointersConfiguration *cmd.TraefikConfiguration) *flaeg.Command {
|
2017-11-09 16:08:03 +00:00
|
|
|
return &flaeg.Command{
|
|
|
|
Name: "storeconfig",
|
2019-03-14 08:30:04 +00:00
|
|
|
Description: `Stores the static traefik configuration into a Key-value stores. Traefik will not start.`,
|
2017-11-09 16:08:03 +00:00
|
|
|
Config: traefikConfiguration,
|
|
|
|
DefaultPointersConfig: traefikPointersConfiguration,
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"parseAllSources": "true",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:10:04 +00:00
|
|
|
// Run store config in KV
|
|
|
|
func Run(kv *staert.KvSource, traefikConfiguration *cmd.TraefikConfiguration) func() error {
|
2017-11-09 16:08:03 +00:00
|
|
|
return func() error {
|
|
|
|
if kv == nil {
|
|
|
|
return fmt.Errorf("error using command storeconfig, no Key-value store defined")
|
|
|
|
}
|
2017-11-21 09:24:03 +00:00
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
fileConfig := traefikConfiguration.Providers.File
|
2017-11-21 09:24:03 +00:00
|
|
|
if fileConfig != nil {
|
2018-11-27 16:42:04 +00:00
|
|
|
traefikConfiguration.Providers.File = nil
|
2017-11-21 09:24:03 +00:00
|
|
|
if len(fileConfig.Filename) == 0 && len(fileConfig.Directory) == 0 {
|
|
|
|
fileConfig.Filename = traefikConfiguration.ConfigFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
jsonConf, err := json.Marshal(traefikConfiguration.Configuration)
|
2017-11-09 16:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-21 09:24:03 +00:00
|
|
|
stdlog.Printf("Storing configuration: %s\n", jsonConf)
|
|
|
|
|
2018-11-27 16:42:04 +00:00
|
|
|
err = kv.StoreConfig(traefikConfiguration.Configuration)
|
2017-11-09 16:08:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-21 09:24:03 +00:00
|
|
|
|
|
|
|
if fileConfig != nil {
|
|
|
|
jsonConf, err = json.Marshal(fileConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdlog.Printf("Storing file configuration: %s\n", jsonConf)
|
2017-12-02 18:25:29 +00:00
|
|
|
config, err := fileConfig.BuildConfiguration()
|
2017-11-21 09:24:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdlog.Print("Writing config to KV")
|
|
|
|
err = kv.StoreConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-02-19 00:04:45 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
// if traefikConfiguration.Configuration.ACME != nil {
|
|
|
|
// account := &acme.Account{}
|
|
|
|
//
|
|
|
|
// accountInitialized, err := keyExists(kv, traefikConfiguration.Configuration.ACME.Storage)
|
|
|
|
// if err != nil && err != store.ErrKeyNotFound {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Check to see if ACME account object is already in kv store
|
|
|
|
// if traefikConfiguration.Configuration.ACME.OverrideCertificates || !accountInitialized {
|
|
|
|
//
|
|
|
|
// // Stores the ACME Account into the KV Store
|
|
|
|
// // Certificates in KV Stores will be overridden
|
|
|
|
// meta := cluster.NewMetadata(account)
|
|
|
|
// err = meta.Marshall()
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// source := staert.KvSource{
|
|
|
|
// Store: kv,
|
|
|
|
// Prefix: traefikConfiguration.Configuration.ACME.Storage,
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// err = source.StoreConfig(meta)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2018-03-05 19:54:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 09:24:03 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
// func keyExists(source *staert.KvSource, key string) (bool, error) {
|
|
|
|
// list, err := source.List(key, nil)
|
|
|
|
// if err != nil {
|
|
|
|
// return false, err
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return len(list) > 0, nil
|
|
|
|
// }
|
2018-09-03 08:12:03 +00:00
|
|
|
|
2018-03-01 07:10:04 +00:00
|
|
|
// CreateKvSource creates KvSource
|
2017-11-09 16:08:03 +00:00
|
|
|
// TLS support is enable for Consul and Etcd backends
|
2018-03-01 07:10:04 +00:00
|
|
|
func CreateKvSource(traefikConfiguration *cmd.TraefikConfiguration) (*staert.KvSource, error) {
|
2017-11-09 16:08:03 +00:00
|
|
|
var kv *staert.KvSource
|
|
|
|
var kvStore store.Store
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch {
|
2018-11-27 16:42:04 +00:00
|
|
|
case traefikConfiguration.Providers.Consul != nil:
|
|
|
|
kvStore, err = traefikConfiguration.Providers.Consul.CreateStore()
|
2017-11-09 16:08:03 +00:00
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: kvStore,
|
2018-11-27 16:42:04 +00:00
|
|
|
Prefix: traefikConfiguration.Providers.Consul.Prefix,
|
2017-11-09 16:08:03 +00:00
|
|
|
}
|
2018-11-27 16:42:04 +00:00
|
|
|
case traefikConfiguration.Providers.Etcd != nil:
|
|
|
|
kvStore, err = traefikConfiguration.Providers.Etcd.CreateStore()
|
2017-11-09 16:08:03 +00:00
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: kvStore,
|
2018-11-27 16:42:04 +00:00
|
|
|
Prefix: traefikConfiguration.Providers.Etcd.Prefix,
|
2017-11-09 16:08:03 +00:00
|
|
|
}
|
2018-11-27 16:42:04 +00:00
|
|
|
case traefikConfiguration.Providers.Zookeeper != nil:
|
|
|
|
kvStore, err = traefikConfiguration.Providers.Zookeeper.CreateStore()
|
2017-11-09 16:08:03 +00:00
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: kvStore,
|
2018-11-27 16:42:04 +00:00
|
|
|
Prefix: traefikConfiguration.Providers.Zookeeper.Prefix,
|
2017-11-09 16:08:03 +00:00
|
|
|
}
|
2018-11-27 16:42:04 +00:00
|
|
|
case traefikConfiguration.Providers.Boltdb != nil:
|
|
|
|
kvStore, err = traefikConfiguration.Providers.Boltdb.CreateStore()
|
2017-11-09 16:08:03 +00:00
|
|
|
kv = &staert.KvSource{
|
|
|
|
Store: kvStore,
|
2018-11-27 16:42:04 +00:00
|
|
|
Prefix: traefikConfiguration.Providers.Boltdb.Prefix,
|
2017-11-09 16:08:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return kv, err
|
|
|
|
}
|