traefik/cmd/storeconfig/storeconfig.go

151 lines
4.2 KiB
Go
Raw Normal View History

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
"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-05-17 11:32:05 +00:00
Description: `Stores the static traefik configuration into a Key-value store. Traefik will not start.`,
2017-11-09 16:08:03 +00:00
Config: traefikConfiguration,
DefaultPointersConfig: traefikPointersConfiguration,
2019-03-18 10:30:07 +00:00
HideHelp: true, // TODO storeconfig
2017-11-09 16:08:03 +00:00
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
fileConfig := traefikConfiguration.Providers.File
2017-11-21 09:24:03 +00:00
if fileConfig != nil {
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
}
}
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)
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
// 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
// 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-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
2019-03-18 10:30:07 +00:00
// var kvStore store.Store
2017-11-09 16:08:03 +00:00
var err error
2019-03-18 10:30:07 +00:00
// TODO kv store
// switch {
// case traefikConfiguration.Providers.Consul != nil:
// kvStore, err = traefikConfiguration.Providers.Consul.CreateStore()
// kv = &staert.KvSource{
// Store: kvStore,
// Prefix: traefikConfiguration.Providers.Consul.Prefix,
// }
// case traefikConfiguration.Providers.Etcd != nil:
// kvStore, err = traefikConfiguration.Providers.Etcd.CreateStore()
// kv = &staert.KvSource{
// Store: kvStore,
// Prefix: traefikConfiguration.Providers.Etcd.Prefix,
// }
// case traefikConfiguration.Providers.Zookeeper != nil:
// kvStore, err = traefikConfiguration.Providers.Zookeeper.CreateStore()
// kv = &staert.KvSource{
// Store: kvStore,
// Prefix: traefikConfiguration.Providers.Zookeeper.Prefix,
// }
// case traefikConfiguration.Providers.Boltdb != nil:
// kvStore, err = traefikConfiguration.Providers.Boltdb.CreateStore()
// kv = &staert.KvSource{
// Store: kvStore,
// Prefix: traefikConfiguration.Providers.Boltdb.Prefix,
// }
// }
2017-11-09 16:08:03 +00:00
return kv, err
}