2014-08-09 22:30:27 +00:00
|
|
|
package daemon
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
import (
|
2015-12-10 23:35:10 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-06-07 07:45:21 +00:00
|
|
|
"errors"
|
2015-12-10 23:35:10 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-06-21 20:42:47 +00:00
|
|
|
"runtime"
|
2015-12-10 23:35:10 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2014-08-10 01:18:32 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2015-12-10 23:35:10 +00:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
2016-03-08 21:03:37 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2015-12-10 23:35:10 +00:00
|
|
|
"github.com/imdario/mergo"
|
2016-06-21 20:42:47 +00:00
|
|
|
"github.com/spf13/pflag"
|
2013-10-05 02:25:15 +00:00
|
|
|
)
|
|
|
|
|
2016-05-06 04:45:55 +00:00
|
|
|
const (
|
|
|
|
// defaultMaxConcurrentDownloads is the default value for
|
|
|
|
// maximum number of downloads that
|
|
|
|
// may take place at a time for each pull.
|
|
|
|
defaultMaxConcurrentDownloads = 3
|
|
|
|
// defaultMaxConcurrentUploads is the default value for
|
|
|
|
// maximum number of uploads that
|
|
|
|
// may take place at a time for each push.
|
|
|
|
defaultMaxConcurrentUploads = 5
|
2016-06-20 19:14:27 +00:00
|
|
|
// stockRuntimeName is the reserved name/alias used to represent the
|
|
|
|
// OCI runtime being shipped with the docker daemon package.
|
|
|
|
stockRuntimeName = "runc"
|
2016-05-06 04:45:55 +00:00
|
|
|
)
|
|
|
|
|
2014-01-30 02:34:43 +00:00
|
|
|
const (
|
2014-02-01 11:38:39 +00:00
|
|
|
defaultNetworkMtu = 1500
|
2014-09-16 17:37:50 +00:00
|
|
|
disableNetworkBridge = "none"
|
2014-01-30 02:34:43 +00:00
|
|
|
)
|
|
|
|
|
2016-05-26 21:07:30 +00:00
|
|
|
const (
|
|
|
|
defaultShutdownTimeout = 15
|
|
|
|
)
|
|
|
|
|
2016-02-02 19:33:41 +00:00
|
|
|
// flatOptions contains configuration keys
|
|
|
|
// that MUST NOT be parsed as deep structures.
|
|
|
|
// Use this to differentiate these options
|
|
|
|
// with others like the ones in CommonTLSOptions.
|
|
|
|
var flatOptions = map[string]bool{
|
|
|
|
"cluster-store-opts": true,
|
|
|
|
"log-opts": true,
|
2016-05-23 21:49:50 +00:00
|
|
|
"runtimes": true,
|
2016-08-10 10:52:06 +00:00
|
|
|
"default-ulimits": true,
|
2016-02-02 19:33:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
// LogConfig represents the default log configuration.
|
|
|
|
// It includes json tags to deserialize configuration from a file
|
2016-03-28 10:57:55 +00:00
|
|
|
// using the same names that the flags in the command line use.
|
2015-12-10 23:35:10 +00:00
|
|
|
type LogConfig struct {
|
|
|
|
Type string `json:"log-driver,omitempty"`
|
|
|
|
Config map[string]string `json:"log-opts,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-03-28 18:55:20 +00:00
|
|
|
// commonBridgeConfig stores all the platform-common bridge driver specific
|
|
|
|
// configuration.
|
|
|
|
type commonBridgeConfig struct {
|
|
|
|
Iface string `json:"bridge,omitempty"`
|
|
|
|
FixedCIDR string `json:"fixed-cidr,omitempty"`
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
// CommonTLSOptions defines TLS configuration for the daemon server.
|
|
|
|
// It includes json tags to deserialize configuration from a file
|
2016-03-28 10:57:55 +00:00
|
|
|
// using the same names that the flags in the command line use.
|
2015-12-10 23:35:10 +00:00
|
|
|
type CommonTLSOptions struct {
|
|
|
|
CAFile string `json:"tlscacert,omitempty"`
|
|
|
|
CertFile string `json:"tlscert,omitempty"`
|
|
|
|
KeyFile string `json:"tlskey,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:57:55 +00:00
|
|
|
// CommonConfig defines the configuration of a docker daemon which is
|
2015-04-24 22:36:11 +00:00
|
|
|
// common across platforms.
|
2015-12-10 23:35:10 +00:00
|
|
|
// It includes json tags to deserialize configuration from a file
|
2016-03-28 10:57:55 +00:00
|
|
|
// using the same names that the flags in the command line use.
|
2015-04-24 22:36:11 +00:00
|
|
|
type CommonConfig struct {
|
2015-12-10 23:35:10 +00:00
|
|
|
AuthorizationPlugins []string `json:"authorization-plugins,omitempty"` // AuthorizationPlugins holds list of authorization plugins
|
|
|
|
AutoRestart bool `json:"-"`
|
|
|
|
Context map[string][]string `json:"-"`
|
|
|
|
DisableBridge bool `json:"-"`
|
|
|
|
DNS []string `json:"dns,omitempty"`
|
|
|
|
DNSOptions []string `json:"dns-opts,omitempty"`
|
|
|
|
DNSSearch []string `json:"dns-search,omitempty"`
|
|
|
|
ExecOptions []string `json:"exec-opts,omitempty"`
|
|
|
|
GraphDriver string `json:"storage-driver,omitempty"`
|
|
|
|
GraphOptions []string `json:"storage-opts,omitempty"`
|
|
|
|
Labels []string `json:"labels,omitempty"`
|
|
|
|
Mtu int `json:"mtu,omitempty"`
|
|
|
|
Pidfile string `json:"pidfile,omitempty"`
|
2015-12-13 10:10:41 +00:00
|
|
|
RawLogs bool `json:"raw-logs,omitempty"`
|
2015-12-10 23:35:10 +00:00
|
|
|
Root string `json:"graph,omitempty"`
|
2016-01-31 02:45:49 +00:00
|
|
|
SocketGroup string `json:"group,omitempty"`
|
2015-12-10 23:35:10 +00:00
|
|
|
TrustKeyPath string `json:"-"`
|
2016-05-16 03:18:48 +00:00
|
|
|
CorsHeaders string `json:"api-cors-header,omitempty"`
|
2016-03-28 14:57:39 +00:00
|
|
|
EnableCors bool `json:"api-enable-cors,omitempty"`
|
2016-07-27 15:30:15 +00:00
|
|
|
|
|
|
|
// LiveRestoreEnabled determines whether we should keep containers
|
|
|
|
// alive upon daemon shutdown/start
|
|
|
|
LiveRestoreEnabled bool `json:"live-restore,omitempty"`
|
2015-09-10 23:12:00 +00:00
|
|
|
|
|
|
|
// ClusterStore is the storage backend used for the cluster information. It is used by both
|
|
|
|
// multihost networking (to store networks and endpoints information) and by the node discovery
|
|
|
|
// mechanism.
|
2015-12-10 23:35:10 +00:00
|
|
|
ClusterStore string `json:"cluster-store,omitempty"`
|
2015-09-10 23:12:00 +00:00
|
|
|
|
2015-09-28 23:22:57 +00:00
|
|
|
// ClusterOpts is used to pass options to the discovery package for tuning libkv settings, such
|
|
|
|
// as TLS configuration settings.
|
2015-12-10 23:35:10 +00:00
|
|
|
ClusterOpts map[string]string `json:"cluster-store-opts,omitempty"`
|
2015-09-28 23:22:57 +00:00
|
|
|
|
2015-09-10 23:12:00 +00:00
|
|
|
// ClusterAdvertise is the network endpoint that the Engine advertises for the purpose of node
|
|
|
|
// discovery. This should be a 'host:port' combination on which that daemon instance is
|
|
|
|
// reachable by other hosts.
|
2015-12-10 23:35:10 +00:00
|
|
|
ClusterAdvertise string `json:"cluster-advertise,omitempty"`
|
|
|
|
|
2016-05-06 04:45:55 +00:00
|
|
|
// MaxConcurrentDownloads is the maximum number of downloads that
|
|
|
|
// may take place at a time for each pull.
|
|
|
|
MaxConcurrentDownloads *int `json:"max-concurrent-downloads,omitempty"`
|
|
|
|
|
|
|
|
// MaxConcurrentUploads is the maximum number of uploads that
|
|
|
|
// may take place at a time for each push.
|
|
|
|
MaxConcurrentUploads *int `json:"max-concurrent-uploads,omitempty"`
|
|
|
|
|
2016-05-26 21:07:30 +00:00
|
|
|
// ShutdownTimeout is the timeout value (in seconds) the daemon will wait for the container
|
|
|
|
// to stop when daemon is being shutdown
|
|
|
|
ShutdownTimeout int `json:"shutdown-timeout,omitempty"`
|
|
|
|
|
2016-01-22 18:14:48 +00:00
|
|
|
Debug bool `json:"debug,omitempty"`
|
|
|
|
Hosts []string `json:"hosts,omitempty"`
|
|
|
|
LogLevel string `json:"log-level,omitempty"`
|
|
|
|
TLS bool `json:"tls,omitempty"`
|
|
|
|
TLSVerify bool `json:"tlsverify,omitempty"`
|
|
|
|
|
|
|
|
// Embedded structs that allow config
|
|
|
|
// deserialization without the full struct.
|
|
|
|
CommonTLSOptions
|
2016-07-01 01:07:35 +00:00
|
|
|
|
|
|
|
// SwarmDefaultAdvertiseAddr is the default host/IP or network interface
|
|
|
|
// to use if a wildcard address is specified in the ListenAddr value
|
|
|
|
// given to the /swarm/init endpoint and no advertise address is
|
|
|
|
// specified.
|
|
|
|
SwarmDefaultAdvertiseAddr string `json:"swarm-default-advertise-addr"`
|
2016-07-20 23:11:28 +00:00
|
|
|
MetricsAddress string `json:"metrics-addr"`
|
2016-07-01 01:07:35 +00:00
|
|
|
|
2016-01-22 18:14:48 +00:00
|
|
|
LogConfig
|
2016-01-25 21:30:33 +00:00
|
|
|
bridgeConfig // bridgeConfig holds bridge network specific configuration.
|
2016-03-08 21:03:37 +00:00
|
|
|
registry.ServiceOptions
|
2015-12-10 23:35:10 +00:00
|
|
|
|
|
|
|
reloadLock sync.Mutex
|
2016-01-19 19:16:07 +00:00
|
|
|
valuesSet map[string]interface{}
|
2016-10-06 14:09:54 +00:00
|
|
|
|
|
|
|
Experimental bool `json:"experimental"` // Experimental indicates whether experimental features should be exposed or not
|
2013-10-05 02:25:15 +00:00
|
|
|
}
|
2013-10-21 16:04:42 +00:00
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// InstallCommonFlags adds flags to the pflag.FlagSet to configure the daemon
|
|
|
|
func (config *Config) InstallCommonFlags(flags *pflag.FlagSet) {
|
2016-05-06 04:45:55 +00:00
|
|
|
var maxConcurrentDownloads, maxConcurrentUploads int
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
config.ServiceOptions.InstallCliFlags(flags)
|
|
|
|
|
|
|
|
flags.Var(opts.NewNamedListOptsRef("storage-opts", &config.GraphOptions, nil), "storage-opt", "Storage driver options")
|
|
|
|
flags.Var(opts.NewNamedListOptsRef("authorization-plugins", &config.AuthorizationPlugins, nil), "authorization-plugin", "Authorization plugins to load")
|
|
|
|
flags.Var(opts.NewNamedListOptsRef("exec-opts", &config.ExecOptions, nil), "exec-opt", "Runtime execution options")
|
|
|
|
flags.StringVarP(&config.Pidfile, "pidfile", "p", defaultPidFile, "Path to use for daemon PID file")
|
|
|
|
flags.StringVarP(&config.Root, "graph", "g", defaultGraph, "Root of the Docker runtime")
|
|
|
|
flags.BoolVarP(&config.AutoRestart, "restart", "r", true, "--restart on the daemon has been deprecated in favor of --restart policies on docker run")
|
2016-06-22 22:36:51 +00:00
|
|
|
flags.MarkDeprecated("restart", "Please use a restart policy on docker run")
|
2016-06-21 20:42:47 +00:00
|
|
|
flags.StringVarP(&config.GraphDriver, "storage-driver", "s", "", "Storage driver to use")
|
|
|
|
flags.IntVar(&config.Mtu, "mtu", 0, "Set the containers network MTU")
|
|
|
|
flags.BoolVar(&config.RawLogs, "raw-logs", false, "Full timestamps without ANSI coloring")
|
2014-08-10 01:18:32 +00:00
|
|
|
// FIXME: why the inconsistency between "hosts" and "sockets"?
|
2016-06-21 20:42:47 +00:00
|
|
|
flags.Var(opts.NewListOptsRef(&config.DNS, opts.ValidateIPAddress), "dns", "DNS server to use")
|
|
|
|
flags.Var(opts.NewNamedListOptsRef("dns-opts", &config.DNSOptions, nil), "dns-opt", "DNS options to use")
|
|
|
|
flags.Var(opts.NewListOptsRef(&config.DNSSearch, opts.ValidateDNSSearch), "dns-search", "DNS search domains to use")
|
|
|
|
flags.Var(opts.NewNamedListOptsRef("labels", &config.Labels, opts.ValidateLabel), "label", "Set key=value labels to the daemon")
|
|
|
|
flags.StringVar(&config.LogConfig.Type, "log-driver", "json-file", "Default driver for container logs")
|
|
|
|
flags.Var(opts.NewNamedMapOpts("log-opts", config.LogConfig.Config, nil), "log-opt", "Default log driver options for containers")
|
|
|
|
flags.StringVar(&config.ClusterAdvertise, "cluster-advertise", "", "Address or interface name to advertise")
|
|
|
|
flags.StringVar(&config.ClusterStore, "cluster-store", "", "URL of the distributed storage backend")
|
|
|
|
flags.Var(opts.NewNamedMapOpts("cluster-store-opts", config.ClusterOpts, nil), "cluster-store-opt", "Set cluster store options")
|
2016-11-15 19:45:20 +00:00
|
|
|
flags.StringVar(&config.CorsHeaders, "api-cors-header", "", "Set CORS headers in the Engine API")
|
2016-06-21 20:42:47 +00:00
|
|
|
flags.IntVar(&maxConcurrentDownloads, "max-concurrent-downloads", defaultMaxConcurrentDownloads, "Set the max concurrent downloads for each pull")
|
|
|
|
flags.IntVar(&maxConcurrentUploads, "max-concurrent-uploads", defaultMaxConcurrentUploads, "Set the max concurrent uploads for each push")
|
2016-05-26 21:07:30 +00:00
|
|
|
flags.IntVar(&config.ShutdownTimeout, "shutdown-timeout", defaultShutdownTimeout, "Set the default shutdown timeout")
|
2016-05-06 04:45:55 +00:00
|
|
|
|
2016-06-23 15:25:51 +00:00
|
|
|
flags.StringVar(&config.SwarmDefaultAdvertiseAddr, "swarm-default-advertise-addr", "", "Set default address or interface for swarm advertised address")
|
2016-10-06 14:09:54 +00:00
|
|
|
flags.BoolVar(&config.Experimental, "experimental", false, "Enable experimental features")
|
2016-07-01 01:07:35 +00:00
|
|
|
|
2016-07-20 23:11:28 +00:00
|
|
|
flags.StringVar(&config.MetricsAddress, "metrics-addr", "", "Set default address and port to serve the metrics api on")
|
|
|
|
|
2016-05-06 04:45:55 +00:00
|
|
|
config.MaxConcurrentDownloads = &maxConcurrentDownloads
|
|
|
|
config.MaxConcurrentUploads = &maxConcurrentUploads
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-19 19:16:07 +00:00
|
|
|
// IsValueSet returns true if a configuration value
|
|
|
|
// was explicitly set in the configuration file.
|
|
|
|
func (config *Config) IsValueSet(name string) bool {
|
|
|
|
if config.valuesSet == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := config.valuesSet[name]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-06-21 20:42:47 +00:00
|
|
|
// NewConfig returns a new fully initialized Config struct
|
|
|
|
func NewConfig() *Config {
|
|
|
|
config := Config{}
|
|
|
|
config.LogConfig.Config = make(map[string]string)
|
|
|
|
config.ClusterOpts = make(map[string]string)
|
|
|
|
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
config.V2Only = true
|
|
|
|
}
|
|
|
|
return &config
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
func parseClusterAdvertiseSettings(clusterStore, clusterAdvertise string) (string, error) {
|
2016-06-07 07:45:21 +00:00
|
|
|
if runtime.GOOS == "solaris" && (clusterAdvertise != "" || clusterStore != "") {
|
|
|
|
return "", errors.New("Cluster Advertise Settings not supported on Solaris")
|
|
|
|
}
|
2015-12-10 23:35:10 +00:00
|
|
|
if clusterAdvertise == "" {
|
|
|
|
return "", errDiscoveryDisabled
|
|
|
|
}
|
|
|
|
if clusterStore == "" {
|
|
|
|
return "", fmt.Errorf("invalid cluster configuration. --cluster-advertise must be accompanied by --cluster-store configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
advertise, err := discovery.ParseAdvertise(clusterAdvertise)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("discovery advertise parsing failed (%v)", err)
|
|
|
|
}
|
|
|
|
return advertise, nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 12:08:05 +00:00
|
|
|
// GetConflictFreeLabels validate Labels for conflict
|
|
|
|
// In swarm the duplicates for labels are removed
|
|
|
|
// so we only take same values here, no conflict values
|
|
|
|
// If the key-value is the same we will only take the last label
|
|
|
|
func GetConflictFreeLabels(labels []string) ([]string, error) {
|
|
|
|
labelMap := map[string]string{}
|
|
|
|
for _, label := range labels {
|
|
|
|
stringSlice := strings.SplitN(label, "=", 2)
|
|
|
|
if len(stringSlice) > 1 {
|
|
|
|
// If there is a conflict we will return an error
|
|
|
|
if v, ok := labelMap[stringSlice[0]]; ok && v != stringSlice[1] {
|
|
|
|
return nil, fmt.Errorf("conflict labels for %s=%s and %s=%s", stringSlice[0], stringSlice[1], stringSlice[0], v)
|
|
|
|
}
|
|
|
|
labelMap[stringSlice[0]] = stringSlice[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newLabels := []string{}
|
|
|
|
for k, v := range labelMap {
|
|
|
|
newLabels = append(newLabels, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
return newLabels, nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
// ReloadConfiguration reads the configuration in the host and reloads the daemon and server.
|
2016-06-21 20:42:47 +00:00
|
|
|
func ReloadConfiguration(configFile string, flags *pflag.FlagSet, reload func(*Config)) error {
|
2015-12-10 23:35:10 +00:00
|
|
|
logrus.Infof("Got signal to reload configuration, reloading from: %s", configFile)
|
|
|
|
newConfig, err := getConflictFreeConfiguration(configFile, flags)
|
|
|
|
if err != nil {
|
2016-02-18 21:55:03 +00:00
|
|
|
return err
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
2016-03-11 08:50:49 +00:00
|
|
|
|
2016-05-23 21:49:50 +00:00
|
|
|
if err := ValidateConfiguration(newConfig); err != nil {
|
2016-03-11 08:50:49 +00:00
|
|
|
return fmt.Errorf("file configuration validation failed (%v)", err)
|
|
|
|
}
|
|
|
|
|
2016-07-12 12:08:05 +00:00
|
|
|
// Labels of the docker engine used to allow multiple values associated with the same key.
|
|
|
|
// This is deprecated in 1.13, and, be removed after 3 release cycles.
|
|
|
|
// The following will check the conflict of labels, and report a warning for deprecation.
|
|
|
|
//
|
|
|
|
// TODO: After 3 release cycles (1.16) an error will be returned, and labels will be
|
|
|
|
// sanitized to consolidate duplicate key-value pairs (config.Labels = newLabels):
|
|
|
|
//
|
|
|
|
// newLabels, err := GetConflictFreeLabels(newConfig.Labels)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// newConfig.Labels = newLabels
|
|
|
|
//
|
|
|
|
if _, err := GetConflictFreeLabels(newConfig.Labels); err != nil {
|
|
|
|
logrus.Warnf("Engine labels with duplicate keys and conflicting values have been deprecated: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:55:03 +00:00
|
|
|
reload(newConfig)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// boolValue is an interface that boolean value flags implement
|
|
|
|
// to tell the command line how to make -name equivalent to -name=true.
|
|
|
|
type boolValue interface {
|
|
|
|
IsBoolFlag() bool
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MergeDaemonConfigurations reads a configuration file,
|
|
|
|
// loads the file configuration in an isolated structure,
|
|
|
|
// and merges the configuration provided from flags on top
|
|
|
|
// if there are no conflicts.
|
2016-06-21 20:42:47 +00:00
|
|
|
func MergeDaemonConfigurations(flagsConfig *Config, flags *pflag.FlagSet, configFile string) (*Config, error) {
|
2015-12-10 23:35:10 +00:00
|
|
|
fileConfig, err := getConflictFreeConfiguration(configFile, flags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:49:50 +00:00
|
|
|
if err := ValidateConfiguration(fileConfig); err != nil {
|
2016-03-11 08:50:49 +00:00
|
|
|
return nil, fmt.Errorf("file configuration validation failed (%v)", err)
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
// merge flags configuration on top of the file configuration
|
|
|
|
if err := mergo.Merge(fileConfig, flagsConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:49:50 +00:00
|
|
|
// We need to validate again once both fileConfig and flagsConfig
|
|
|
|
// have been merged
|
|
|
|
if err := ValidateConfiguration(fileConfig); err != nil {
|
|
|
|
return nil, fmt.Errorf("file configuration validation failed (%v)", err)
|
|
|
|
}
|
|
|
|
|
2015-12-10 23:35:10 +00:00
|
|
|
return fileConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getConflictFreeConfiguration loads the configuration from a JSON file.
|
|
|
|
// It compares that configuration with the one provided by the flags,
|
|
|
|
// and returns an error if there are conflicts.
|
2016-06-21 20:42:47 +00:00
|
|
|
func getConflictFreeConfiguration(configFile string, flags *pflag.FlagSet) (*Config, error) {
|
2015-12-10 23:35:10 +00:00
|
|
|
b, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:16:07 +00:00
|
|
|
var config Config
|
2015-12-10 23:35:10 +00:00
|
|
|
var reader io.Reader
|
|
|
|
if flags != nil {
|
|
|
|
var jsonConfig map[string]interface{}
|
|
|
|
reader = bytes.NewReader(b)
|
|
|
|
if err := json.NewDecoder(reader).Decode(&jsonConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:16:07 +00:00
|
|
|
configSet := configValuesSet(jsonConfig)
|
|
|
|
|
|
|
|
if err := findConfigurationConflicts(configSet, flags); err != nil {
|
2015-12-10 23:35:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-19 19:16:07 +00:00
|
|
|
|
2016-02-18 21:55:03 +00:00
|
|
|
// Override flag values to make sure the values set in the config file with nullable values, like `false`,
|
2016-07-03 17:58:11 +00:00
|
|
|
// are not overridden by default truthy values from the flags that were not explicitly set.
|
2016-02-18 21:55:03 +00:00
|
|
|
// See https://github.com/docker/docker/issues/20289 for an example.
|
|
|
|
//
|
|
|
|
// TODO: Rewrite configuration logic to avoid same issue with other nullable values, like numbers.
|
|
|
|
namedOptions := make(map[string]interface{})
|
|
|
|
for key, value := range configSet {
|
2016-06-22 22:36:51 +00:00
|
|
|
f := flags.Lookup(key)
|
2016-02-18 21:55:03 +00:00
|
|
|
if f == nil { // ignore named flags that don't match
|
|
|
|
namedOptions[key] = value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := f.Value.(boolValue); ok {
|
|
|
|
f.Value.Set(fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(namedOptions) > 0 {
|
|
|
|
// set also default for mergeVal flags that are boolValue at the same time.
|
2016-06-21 20:42:47 +00:00
|
|
|
flags.VisitAll(func(f *pflag.Flag) {
|
2016-02-18 21:55:03 +00:00
|
|
|
if opt, named := f.Value.(opts.NamedOption); named {
|
|
|
|
v, set := namedOptions[opt.Name()]
|
|
|
|
_, boolean := f.Value.(boolValue)
|
|
|
|
if set && boolean {
|
|
|
|
f.Value.Set(fmt.Sprintf("%v", v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:16:07 +00:00
|
|
|
config.valuesSet = configSet
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader = bytes.NewReader(b)
|
|
|
|
err = json.NewDecoder(reader).Decode(&config)
|
|
|
|
return &config, err
|
|
|
|
}
|
|
|
|
|
2016-01-19 19:16:07 +00:00
|
|
|
// configValuesSet returns the configuration values explicitly set in the file.
|
|
|
|
func configValuesSet(config map[string]interface{}) map[string]interface{} {
|
2015-12-10 23:35:10 +00:00
|
|
|
flatten := make(map[string]interface{})
|
|
|
|
for k, v := range config {
|
2016-02-02 19:33:41 +00:00
|
|
|
if m, isMap := v.(map[string]interface{}); isMap && !flatOptions[k] {
|
2015-12-10 23:35:10 +00:00
|
|
|
for km, vm := range m {
|
|
|
|
flatten[km] = vm
|
|
|
|
}
|
2016-02-02 19:33:41 +00:00
|
|
|
continue
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
2016-02-02 19:33:41 +00:00
|
|
|
|
|
|
|
flatten[k] = v
|
2015-12-10 23:35:10 +00:00
|
|
|
}
|
2016-01-19 19:16:07 +00:00
|
|
|
return flatten
|
|
|
|
}
|
|
|
|
|
|
|
|
// findConfigurationConflicts iterates over the provided flags searching for
|
2016-01-20 22:16:49 +00:00
|
|
|
// duplicated configurations and unknown keys. It returns an error with all the conflicts if
|
2016-01-19 19:16:07 +00:00
|
|
|
// it finds any.
|
2016-06-21 20:42:47 +00:00
|
|
|
func findConfigurationConflicts(config map[string]interface{}, flags *pflag.FlagSet) error {
|
2016-01-20 22:16:49 +00:00
|
|
|
// 1. Search keys from the file that we don't recognize as flags.
|
|
|
|
unknownKeys := make(map[string]interface{})
|
|
|
|
for key, value := range config {
|
2016-06-22 22:36:51 +00:00
|
|
|
if flag := flags.Lookup(key); flag == nil {
|
2016-01-20 22:16:49 +00:00
|
|
|
unknownKeys[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:14:48 +00:00
|
|
|
// 2. Discard values that implement NamedOption.
|
|
|
|
// Their configuration name differs from their flag name, like `labels` and `label`.
|
2016-02-18 21:55:03 +00:00
|
|
|
if len(unknownKeys) > 0 {
|
2016-06-21 20:42:47 +00:00
|
|
|
unknownNamedConflicts := func(f *pflag.Flag) {
|
2016-02-18 21:55:03 +00:00
|
|
|
if namedOption, ok := f.Value.(opts.NamedOption); ok {
|
|
|
|
if _, valid := unknownKeys[namedOption.Name()]; valid {
|
|
|
|
delete(unknownKeys, namedOption.Name())
|
|
|
|
}
|
2016-01-20 22:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-18 21:55:03 +00:00
|
|
|
flags.VisitAll(unknownNamedConflicts)
|
2016-01-20 22:16:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(unknownKeys) > 0 {
|
|
|
|
var unknown []string
|
|
|
|
for key := range unknownKeys {
|
|
|
|
unknown = append(unknown, key)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("the following directives don't match any configuration option: %s", strings.Join(unknown, ", "))
|
|
|
|
}
|
2015-12-10 23:35:10 +00:00
|
|
|
|
2016-01-20 22:16:49 +00:00
|
|
|
var conflicts []string
|
2015-12-10 23:35:10 +00:00
|
|
|
printConflict := func(name string, flagValue, fileValue interface{}) string {
|
|
|
|
return fmt.Sprintf("%s: (from flag: %v, from file: %v)", name, flagValue, fileValue)
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:16:49 +00:00
|
|
|
// 3. Search keys that are present as a flag and as a file option.
|
2016-06-21 20:42:47 +00:00
|
|
|
duplicatedConflicts := func(f *pflag.Flag) {
|
2015-12-10 23:35:10 +00:00
|
|
|
// search option name in the json configuration payload if the value is a named option
|
|
|
|
if namedOption, ok := f.Value.(opts.NamedOption); ok {
|
2016-01-19 19:16:07 +00:00
|
|
|
if optsValue, ok := config[namedOption.Name()]; ok {
|
2015-12-10 23:35:10 +00:00
|
|
|
conflicts = append(conflicts, printConflict(namedOption.Name(), f.Value.String(), optsValue))
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-21 20:42:47 +00:00
|
|
|
// search flag name in the json configuration payload
|
|
|
|
for _, name := range []string{f.Name, f.Shorthand} {
|
2016-01-19 19:16:07 +00:00
|
|
|
if value, ok := config[name]; ok {
|
2015-12-10 23:35:10 +00:00
|
|
|
conflicts = append(conflicts, printConflict(name, f.Value.String(), value))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 22:16:49 +00:00
|
|
|
flags.Visit(duplicatedConflicts)
|
2015-12-10 23:35:10 +00:00
|
|
|
|
|
|
|
if len(conflicts) > 0 {
|
|
|
|
return fmt.Errorf("the following directives are specified both as a flag and in the configuration file: %s", strings.Join(conflicts, ", "))
|
|
|
|
}
|
|
|
|
return nil
|
2014-08-10 01:18:32 +00:00
|
|
|
}
|
2016-03-11 08:50:49 +00:00
|
|
|
|
2016-05-23 21:49:50 +00:00
|
|
|
// ValidateConfiguration validates some specific configs.
|
2016-05-06 04:45:55 +00:00
|
|
|
// such as config.DNS, config.Labels, config.DNSSearch,
|
|
|
|
// as well as config.MaxConcurrentDownloads, config.MaxConcurrentUploads.
|
2016-05-23 21:49:50 +00:00
|
|
|
func ValidateConfiguration(config *Config) error {
|
2016-03-11 08:50:49 +00:00
|
|
|
// validate DNS
|
|
|
|
for _, dns := range config.DNS {
|
|
|
|
if _, err := opts.ValidateIPAddress(dns); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate DNSSearch
|
|
|
|
for _, dnsSearch := range config.DNSSearch {
|
|
|
|
if _, err := opts.ValidateDNSSearch(dnsSearch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate Labels
|
|
|
|
for _, label := range config.Labels {
|
|
|
|
if _, err := opts.ValidateLabel(label); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-06 04:45:55 +00:00
|
|
|
// validate MaxConcurrentDownloads
|
|
|
|
if config.IsValueSet("max-concurrent-downloads") && config.MaxConcurrentDownloads != nil && *config.MaxConcurrentDownloads < 0 {
|
|
|
|
return fmt.Errorf("invalid max concurrent downloads: %d", *config.MaxConcurrentDownloads)
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate MaxConcurrentUploads
|
|
|
|
if config.IsValueSet("max-concurrent-uploads") && config.MaxConcurrentUploads != nil && *config.MaxConcurrentUploads < 0 {
|
|
|
|
return fmt.Errorf("invalid max concurrent uploads: %d", *config.MaxConcurrentUploads)
|
|
|
|
}
|
2016-05-23 21:49:50 +00:00
|
|
|
|
|
|
|
// validate that "default" runtime is not reset
|
|
|
|
if runtimes := config.GetAllRuntimes(); len(runtimes) > 0 {
|
2016-06-20 19:14:27 +00:00
|
|
|
if _, ok := runtimes[stockRuntimeName]; ok {
|
|
|
|
return fmt.Errorf("runtime name '%s' is reserved", stockRuntimeName)
|
2016-05-23 21:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:14:27 +00:00
|
|
|
if defaultRuntime := config.GetDefaultRuntimeName(); defaultRuntime != "" && defaultRuntime != stockRuntimeName {
|
2016-05-23 21:49:50 +00:00
|
|
|
runtimes := config.GetAllRuntimes()
|
|
|
|
if _, ok := runtimes[defaultRuntime]; !ok {
|
|
|
|
return fmt.Errorf("specified default runtime '%s' does not exist", defaultRuntime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:50:49 +00:00
|
|
|
return nil
|
|
|
|
}
|