|
@@ -11,61 +11,85 @@ import (
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
"github.com/docker/docker/pkg/parsers"
|
|
"github.com/docker/docker/pkg/parsers"
|
|
"github.com/docker/docker/pkg/ulimit"
|
|
"github.com/docker/docker/pkg/ulimit"
|
|
|
|
+ "github.com/docker/docker/volume"
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
- alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
|
|
|
|
- domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
|
|
|
|
- DefaultHTTPHost = "127.0.0.1" // Default HTTP Host used if only port is provided to -H flag e.g. docker -d -H tcp://:8080
|
|
|
|
|
|
+ alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
|
|
|
|
+ domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
|
|
|
|
+ // DefaultHTTPHost Default HTTP Host used if only port is provided to -H flag e.g. docker -d -H tcp://:8080
|
|
|
|
+ DefaultHTTPHost = "127.0.0.1"
|
|
|
|
+ // DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. docker -d -H tcp://
|
|
// TODO Windows. DefaultHTTPPort is only used on Windows if a -H parameter
|
|
// TODO Windows. DefaultHTTPPort is only used on Windows if a -H parameter
|
|
// is not supplied. A better longer term solution would be to use a named
|
|
// is not supplied. A better longer term solution would be to use a named
|
|
// pipe as the default on the Windows daemon.
|
|
// pipe as the default on the Windows daemon.
|
|
- DefaultHTTPPort = 2375 // Default HTTP Port
|
|
|
|
- DefaultUnixSocket = "/var/run/docker.sock" // Docker daemon by default always listens on the default unix socket
|
|
|
|
|
|
+ DefaultHTTPPort = 2375 // Default HTTP Port
|
|
|
|
+ // DefaultUnixSocket Path for the unix socket.
|
|
|
|
+ // Docker daemon by default always listens on the default unix socket
|
|
|
|
+ DefaultUnixSocket = "/var/run/docker.sock"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+// ListVar Defines a flag with the specified names and usage, and put the value
|
|
|
|
+// list into ListOpts that will hold the values.
|
|
func ListVar(values *[]string, names []string, usage string) {
|
|
func ListVar(values *[]string, names []string, usage string) {
|
|
flag.Var(newListOptsRef(values, nil), names, usage)
|
|
flag.Var(newListOptsRef(values, nil), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// MapVar Defines a flag with the specified names and usage, and put the value
|
|
|
|
+// map into MapOpt that will hold the values (key,value).
|
|
func MapVar(values map[string]string, names []string, usage string) {
|
|
func MapVar(values map[string]string, names []string, usage string) {
|
|
flag.Var(newMapOpt(values, nil), names, usage)
|
|
flag.Var(newMapOpt(values, nil), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// LogOptsVar Defines a flag with the specified names and usage for --log-opts,
|
|
|
|
+// and put the value map into MapOpt that will hold the values (key,value).
|
|
func LogOptsVar(values map[string]string, names []string, usage string) {
|
|
func LogOptsVar(values map[string]string, names []string, usage string) {
|
|
flag.Var(newMapOpt(values, nil), names, usage)
|
|
flag.Var(newMapOpt(values, nil), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// HostListVar Defines a flag with the specified names and usage and put the
|
|
|
|
+// value into a ListOpts that will hold the values, validating the Host format.
|
|
func HostListVar(values *[]string, names []string, usage string) {
|
|
func HostListVar(values *[]string, names []string, usage string) {
|
|
flag.Var(newListOptsRef(values, ValidateHost), names, usage)
|
|
flag.Var(newListOptsRef(values, ValidateHost), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// IPListVar Defines a flag with the specified names and usage and put the
|
|
|
|
+// value into a ListOpts that will hold the values, validating the IP format.
|
|
func IPListVar(values *[]string, names []string, usage string) {
|
|
func IPListVar(values *[]string, names []string, usage string) {
|
|
flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
|
|
flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
-func DnsSearchListVar(values *[]string, names []string, usage string) {
|
|
|
|
- flag.Var(newListOptsRef(values, ValidateDnsSearch), names, usage)
|
|
|
|
|
|
+// DNSSearchListVar Defines a flag with the specified names and usage and put the
|
|
|
|
+// value into a ListOpts that will hold the values, validating the DNS search format.
|
|
|
|
+func DNSSearchListVar(values *[]string, names []string, usage string) {
|
|
|
|
+ flag.Var(newListOptsRef(values, ValidateDNSSearch), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// IPVar Defines a flag with the specified names and usage for IP and will use
|
|
|
|
+// the specified defaultValue if the specified value is not valid.
|
|
func IPVar(value *net.IP, names []string, defaultValue, usage string) {
|
|
func IPVar(value *net.IP, names []string, defaultValue, usage string) {
|
|
flag.Var(NewIpOpt(value, defaultValue), names, usage)
|
|
flag.Var(NewIpOpt(value, defaultValue), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// LabelListVar Defines a flag with the specified names and usage and put the
|
|
|
|
+// value into a ListOpts that will hold the values, validating the label format.
|
|
func LabelListVar(values *[]string, names []string, usage string) {
|
|
func LabelListVar(values *[]string, names []string, usage string) {
|
|
flag.Var(newListOptsRef(values, ValidateLabel), names, usage)
|
|
flag.Var(newListOptsRef(values, ValidateLabel), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// UlimitMapVar Defines a flag with the specified names and usage for --ulimit,
|
|
|
|
+// and put the value map into a UlimitOpt that will hold the values.
|
|
func UlimitMapVar(values map[string]*ulimit.Ulimit, names []string, usage string) {
|
|
func UlimitMapVar(values map[string]*ulimit.Ulimit, names []string, usage string) {
|
|
flag.Var(NewUlimitOpt(values), names, usage)
|
|
flag.Var(NewUlimitOpt(values), names, usage)
|
|
}
|
|
}
|
|
|
|
|
|
-// ListOpts type
|
|
|
|
|
|
+// ListOpts type that hold a list of values and a validation function.
|
|
type ListOpts struct {
|
|
type ListOpts struct {
|
|
values *[]string
|
|
values *[]string
|
|
validator ValidatorFctType
|
|
validator ValidatorFctType
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// NewListOpts Create a new ListOpts with the specified validator.
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
|
var values []string
|
|
var values []string
|
|
return *newListOptsRef(&values, validator)
|
|
return *newListOptsRef(&values, validator)
|
|
@@ -138,12 +162,14 @@ func (opts *ListOpts) Len() int {
|
|
return len((*opts.values))
|
|
return len((*opts.values))
|
|
}
|
|
}
|
|
|
|
|
|
-//MapOpts type
|
|
|
|
|
|
+//MapOpts type that holds a map of values and a validation function.
|
|
type MapOpts struct {
|
|
type MapOpts struct {
|
|
values map[string]string
|
|
values map[string]string
|
|
validator ValidatorFctType
|
|
validator ValidatorFctType
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Set validates if needed the input value and add it to the
|
|
|
|
+// internal map, by splitting on '='.
|
|
func (opts *MapOpts) Set(value string) error {
|
|
func (opts *MapOpts) Set(value string) error {
|
|
if opts.validator != nil {
|
|
if opts.validator != nil {
|
|
v, err := opts.validator(value)
|
|
v, err := opts.validator(value)
|
|
@@ -172,10 +198,13 @@ func newMapOpt(values map[string]string, validator ValidatorFctType) *MapOpts {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// Validators
|
|
|
|
|
|
+// ValidatorFctType validator that return a validate string and/or an error
|
|
type ValidatorFctType func(val string) (string, error)
|
|
type ValidatorFctType func(val string) (string, error)
|
|
|
|
+
|
|
|
|
+// ValidatorFctListType validator that return a validate list of string and/or an error
|
|
type ValidatorFctListType func(val string) ([]string, error)
|
|
type ValidatorFctListType func(val string) ([]string, error)
|
|
|
|
|
|
|
|
+// ValidateAttach Validates that the specified string is a valid attach option.
|
|
func ValidateAttach(val string) (string, error) {
|
|
func ValidateAttach(val string) (string, error) {
|
|
s := strings.ToLower(val)
|
|
s := strings.ToLower(val)
|
|
for _, str := range []string{"stdin", "stdout", "stderr"} {
|
|
for _, str := range []string{"stdin", "stdout", "stderr"} {
|
|
@@ -183,9 +212,10 @@ func ValidateAttach(val string) (string, error) {
|
|
return s, nil
|
|
return s, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR.")
|
|
|
|
|
|
+ return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateLink Validates that the specified string has a valid link format (containerName:alias).
|
|
func ValidateLink(val string) (string, error) {
|
|
func ValidateLink(val string) (string, error) {
|
|
if _, _, err := parsers.ParseLink(val); err != nil {
|
|
if _, _, err := parsers.ParseLink(val); err != nil {
|
|
return val, err
|
|
return val, err
|
|
@@ -193,22 +223,53 @@ func ValidateLink(val string) (string, error) {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
|
|
|
|
-// ValidatePath will make sure 'val' is in the form:
|
|
|
|
-// [host-dir:]container-path[:rw|ro] - but doesn't validate the mode part
|
|
|
|
|
|
+// ValidateDevice Validate a path for devices
|
|
|
|
+// It will make sure 'val' is in the form:
|
|
|
|
+// [host-dir:]container-path[:mode]
|
|
|
|
+func ValidateDevice(val string) (string, error) {
|
|
|
|
+ return validatePath(val, false)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// ValidatePath Validate a path for volumes
|
|
|
|
+// It will make sure 'val' is in the form:
|
|
|
|
+// [host-dir:]container-path[:rw|ro]
|
|
|
|
+// It will also validate the mount mode.
|
|
func ValidatePath(val string) (string, error) {
|
|
func ValidatePath(val string) (string, error) {
|
|
|
|
+ return validatePath(val, true)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func validatePath(val string, validateMountMode bool) (string, error) {
|
|
var containerPath string
|
|
var containerPath string
|
|
|
|
+ var mode string
|
|
|
|
|
|
if strings.Count(val, ":") > 2 {
|
|
if strings.Count(val, ":") > 2 {
|
|
return val, fmt.Errorf("bad format for volumes: %s", val)
|
|
return val, fmt.Errorf("bad format for volumes: %s", val)
|
|
}
|
|
}
|
|
|
|
|
|
- splited := strings.SplitN(val, ":", 2)
|
|
|
|
- if len(splited) == 1 {
|
|
|
|
|
|
+ splited := strings.SplitN(val, ":", 3)
|
|
|
|
+ if splited[0] == "" {
|
|
|
|
+ return val, fmt.Errorf("bad format for volumes: %s", val)
|
|
|
|
+ }
|
|
|
|
+ switch len(splited) {
|
|
|
|
+ case 1:
|
|
containerPath = splited[0]
|
|
containerPath = splited[0]
|
|
- val = path.Clean(splited[0])
|
|
|
|
- } else {
|
|
|
|
|
|
+ val = path.Clean(containerPath)
|
|
|
|
+ case 2:
|
|
|
|
+ if isValid, _ := volume.ValidateMountMode(splited[1]); validateMountMode && isValid {
|
|
|
|
+ containerPath = splited[0]
|
|
|
|
+ mode = splited[1]
|
|
|
|
+ val = fmt.Sprintf("%s:%s", path.Clean(containerPath), mode)
|
|
|
|
+ } else {
|
|
|
|
+ containerPath = splited[1]
|
|
|
|
+ val = fmt.Sprintf("%s:%s", splited[0], path.Clean(containerPath))
|
|
|
|
+ }
|
|
|
|
+ case 3:
|
|
containerPath = splited[1]
|
|
containerPath = splited[1]
|
|
- val = fmt.Sprintf("%s:%s", splited[0], path.Clean(splited[1]))
|
|
|
|
|
|
+ mode = splited[2]
|
|
|
|
+ if isValid, _ := volume.ValidateMountMode(splited[2]); validateMountMode && !isValid {
|
|
|
|
+ return val, fmt.Errorf("bad mount mode specified : %s", mode)
|
|
|
|
+ }
|
|
|
|
+ val = fmt.Sprintf("%s:%s:%s", splited[0], containerPath, mode)
|
|
}
|
|
}
|
|
|
|
|
|
if !path.IsAbs(containerPath) {
|
|
if !path.IsAbs(containerPath) {
|
|
@@ -217,17 +278,24 @@ func ValidatePath(val string) (string, error) {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateEnv Validate an environment variable and returns it
|
|
|
|
+// It will use EnvironmentVariableRegexp to ensure the name of the environment variable is valid.
|
|
|
|
+// If no value is specified, it returns the current value using os.Getenv.
|
|
func ValidateEnv(val string) (string, error) {
|
|
func ValidateEnv(val string) (string, error) {
|
|
arr := strings.Split(val, "=")
|
|
arr := strings.Split(val, "=")
|
|
if len(arr) > 1 {
|
|
if len(arr) > 1 {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
|
|
+ if !EnvironmentVariableRegexp.MatchString(arr[0]) {
|
|
|
|
+ return val, ErrBadEnvVariable{fmt.Sprintf("variable '%s' is not a valid environment variable", val)}
|
|
|
|
+ }
|
|
if !doesEnvExist(val) {
|
|
if !doesEnvExist(val) {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
|
|
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateIPAddress Validates an Ip address
|
|
func ValidateIPAddress(val string) (string, error) {
|
|
func ValidateIPAddress(val string) (string, error) {
|
|
var ip = net.ParseIP(strings.TrimSpace(val))
|
|
var ip = net.ParseIP(strings.TrimSpace(val))
|
|
if ip != nil {
|
|
if ip != nil {
|
|
@@ -236,6 +304,7 @@ func ValidateIPAddress(val string) (string, error) {
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateMACAddress Validates a MAC address
|
|
func ValidateMACAddress(val string) (string, error) {
|
|
func ValidateMACAddress(val string) (string, error) {
|
|
_, err := net.ParseMAC(strings.TrimSpace(val))
|
|
_, err := net.ParseMAC(strings.TrimSpace(val))
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -244,9 +313,9 @@ func ValidateMACAddress(val string) (string, error) {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
|
|
|
|
-// Validates domain for resolvconf search configuration.
|
|
|
|
|
|
+// ValidateDNSSearch Validates domain for resolvconf search configuration.
|
|
// A zero length domain is represented by .
|
|
// A zero length domain is represented by .
|
|
-func ValidateDnsSearch(val string) (string, error) {
|
|
|
|
|
|
+func ValidateDNSSearch(val string) (string, error) {
|
|
if val = strings.Trim(val, " "); val == "." {
|
|
if val = strings.Trim(val, " "); val == "." {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
@@ -264,6 +333,8 @@ func validateDomain(val string) (string, error) {
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateExtraHost Validate that the given string is a valid extrahost and returns it
|
|
|
|
+// ExtraHost are in the form of name:ip where the ip has to be a valid ip (ipv4 or ipv6)
|
|
func ValidateExtraHost(val string) (string, error) {
|
|
func ValidateExtraHost(val string) (string, error) {
|
|
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
|
// allow for IPv6 addresses in extra hosts by only splitting on first ":"
|
|
arr := strings.SplitN(val, ":", 2)
|
|
arr := strings.SplitN(val, ":", 2)
|
|
@@ -276,13 +347,16 @@ func ValidateExtraHost(val string) (string, error) {
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateLabel Validate that the given string is a valid label, and returns it
|
|
|
|
+// Labels are in the form on key=value
|
|
func ValidateLabel(val string) (string, error) {
|
|
func ValidateLabel(val string) (string, error) {
|
|
- if strings.Count(val, "=") != 1 {
|
|
|
|
|
|
+ if strings.Count(val, "=") < 1 {
|
|
return "", fmt.Errorf("bad attribute format: %s", val)
|
|
return "", fmt.Errorf("bad attribute format: %s", val)
|
|
}
|
|
}
|
|
return val, nil
|
|
return val, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// ValidateHost Validate that the given string is a valid host and returns it
|
|
func ValidateHost(val string) (string, error) {
|
|
func ValidateHost(val string) (string, error) {
|
|
host, err := parsers.ParseHost(DefaultHTTPHost, DefaultUnixSocket, val)
|
|
host, err := parsers.ParseHost(DefaultHTTPHost, DefaultUnixSocket, val)
|
|
if err != nil {
|
|
if err != nil {
|