|
@@ -2,13 +2,11 @@ package opts
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
- "math/big"
|
|
|
"net"
|
|
|
"path"
|
|
|
"regexp"
|
|
|
"strings"
|
|
|
|
|
|
- "github.com/docker/docker/api/types/filters"
|
|
|
units "github.com/docker/go-units"
|
|
|
)
|
|
|
|
|
@@ -236,15 +234,6 @@ func ValidateIPAddress(val string) (string, error) {
|
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
|
|
}
|
|
|
|
|
|
-// ValidateMACAddress validates a MAC address.
|
|
|
-func ValidateMACAddress(val string) (string, error) {
|
|
|
- _, err := net.ParseMAC(strings.TrimSpace(val))
|
|
|
- if err != nil {
|
|
|
- return "", err
|
|
|
- }
|
|
|
- return val, nil
|
|
|
-}
|
|
|
-
|
|
|
// ValidateDNSSearch validates domain for resolvconf search configuration.
|
|
|
// A zero length domain is represented by a dot (.).
|
|
|
func ValidateDNSSearch(val string) (string, error) {
|
|
@@ -274,114 +263,6 @@ func ValidateLabel(val string) (string, error) {
|
|
|
return val, nil
|
|
|
}
|
|
|
|
|
|
-// ValidateSysctl validates a sysctl and returns it.
|
|
|
-func ValidateSysctl(val string) (string, error) {
|
|
|
- validSysctlMap := map[string]bool{
|
|
|
- "kernel.msgmax": true,
|
|
|
- "kernel.msgmnb": true,
|
|
|
- "kernel.msgmni": true,
|
|
|
- "kernel.sem": true,
|
|
|
- "kernel.shmall": true,
|
|
|
- "kernel.shmmax": true,
|
|
|
- "kernel.shmmni": true,
|
|
|
- "kernel.shm_rmid_forced": true,
|
|
|
- }
|
|
|
- validSysctlPrefixes := []string{
|
|
|
- "net.",
|
|
|
- "fs.mqueue.",
|
|
|
- }
|
|
|
- arr := strings.Split(val, "=")
|
|
|
- if len(arr) < 2 {
|
|
|
- return "", fmt.Errorf("sysctl '%s' is not whitelisted", val)
|
|
|
- }
|
|
|
- if validSysctlMap[arr[0]] {
|
|
|
- return val, nil
|
|
|
- }
|
|
|
-
|
|
|
- for _, vp := range validSysctlPrefixes {
|
|
|
- if strings.HasPrefix(arr[0], vp) {
|
|
|
- return val, nil
|
|
|
- }
|
|
|
- }
|
|
|
- return "", fmt.Errorf("sysctl '%s' is not whitelisted", val)
|
|
|
-}
|
|
|
-
|
|
|
-// FilterOpt is a flag type for validating filters
|
|
|
-type FilterOpt struct {
|
|
|
- filter filters.Args
|
|
|
-}
|
|
|
-
|
|
|
-// NewFilterOpt returns a new FilterOpt
|
|
|
-func NewFilterOpt() FilterOpt {
|
|
|
- return FilterOpt{filter: filters.NewArgs()}
|
|
|
-}
|
|
|
-
|
|
|
-func (o *FilterOpt) String() string {
|
|
|
- repr, err := filters.ToParam(o.filter)
|
|
|
- if err != nil {
|
|
|
- return "invalid filters"
|
|
|
- }
|
|
|
- return repr
|
|
|
-}
|
|
|
-
|
|
|
-// Set sets the value of the opt by parsing the command line value
|
|
|
-func (o *FilterOpt) Set(value string) error {
|
|
|
- var err error
|
|
|
- o.filter, err = filters.ParseFlag(value, o.filter)
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-// Type returns the option type
|
|
|
-func (o *FilterOpt) Type() string {
|
|
|
- return "filter"
|
|
|
-}
|
|
|
-
|
|
|
-// Value returns the value of this option
|
|
|
-func (o *FilterOpt) Value() filters.Args {
|
|
|
- return o.filter
|
|
|
-}
|
|
|
-
|
|
|
-// NanoCPUs is a type for fixed point fractional number.
|
|
|
-type NanoCPUs int64
|
|
|
-
|
|
|
-// String returns the string format of the number
|
|
|
-func (c *NanoCPUs) String() string {
|
|
|
- if *c == 0 {
|
|
|
- return ""
|
|
|
- }
|
|
|
- return big.NewRat(c.Value(), 1e9).FloatString(3)
|
|
|
-}
|
|
|
-
|
|
|
-// Set sets the value of the NanoCPU by passing a string
|
|
|
-func (c *NanoCPUs) Set(value string) error {
|
|
|
- cpus, err := ParseCPUs(value)
|
|
|
- *c = NanoCPUs(cpus)
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-// Type returns the type
|
|
|
-func (c *NanoCPUs) Type() string {
|
|
|
- return "decimal"
|
|
|
-}
|
|
|
-
|
|
|
-// Value returns the value in int64
|
|
|
-func (c *NanoCPUs) Value() int64 {
|
|
|
- return int64(*c)
|
|
|
-}
|
|
|
-
|
|
|
-// ParseCPUs takes a string ratio and returns an integer value of nano cpus
|
|
|
-func ParseCPUs(value string) (int64, error) {
|
|
|
- cpu, ok := new(big.Rat).SetString(value)
|
|
|
- if !ok {
|
|
|
- return 0, fmt.Errorf("failed to parse %v as a rational number", value)
|
|
|
- }
|
|
|
- nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
|
|
|
- if !nano.IsInt() {
|
|
|
- return 0, fmt.Errorf("value is too precise")
|
|
|
- }
|
|
|
- return nano.Num().Int64(), nil
|
|
|
-}
|
|
|
-
|
|
|
// ParseLink parses and validates the specified string as a link format (name:alias)
|
|
|
func ParseLink(val string) (string, string, error) {
|
|
|
if val == "" {
|
|
@@ -404,12 +285,6 @@ func ParseLink(val string) (string, string, error) {
|
|
|
return arr[0], arr[1], nil
|
|
|
}
|
|
|
|
|
|
-// ValidateLink validates that the specified string has a valid link format (containerName:alias).
|
|
|
-func ValidateLink(val string) (string, error) {
|
|
|
- _, _, err := ParseLink(val)
|
|
|
- return val, err
|
|
|
-}
|
|
|
-
|
|
|
// MemBytes is a type for human readable memory bytes (like 128M, 2g, etc)
|
|
|
type MemBytes int64
|
|
|
|
|
@@ -450,39 +325,3 @@ func (m *MemBytes) UnmarshalJSON(s []byte) error {
|
|
|
*m = MemBytes(val)
|
|
|
return err
|
|
|
}
|
|
|
-
|
|
|
-// MemSwapBytes is a type for human readable memory bytes (like 128M, 2g, etc).
|
|
|
-// It differs from MemBytes in that -1 is valid and the default.
|
|
|
-type MemSwapBytes int64
|
|
|
-
|
|
|
-// Set sets the value of the MemSwapBytes by passing a string
|
|
|
-func (m *MemSwapBytes) Set(value string) error {
|
|
|
- if value == "-1" {
|
|
|
- *m = MemSwapBytes(-1)
|
|
|
- return nil
|
|
|
- }
|
|
|
- val, err := units.RAMInBytes(value)
|
|
|
- *m = MemSwapBytes(val)
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-// Type returns the type
|
|
|
-func (m *MemSwapBytes) Type() string {
|
|
|
- return "bytes"
|
|
|
-}
|
|
|
-
|
|
|
-// Value returns the value in int64
|
|
|
-func (m *MemSwapBytes) Value() int64 {
|
|
|
- return int64(*m)
|
|
|
-}
|
|
|
-
|
|
|
-func (m *MemSwapBytes) String() string {
|
|
|
- b := MemBytes(*m)
|
|
|
- return b.String()
|
|
|
-}
|
|
|
-
|
|
|
-// UnmarshalJSON is the customized unmarshaler for MemSwapBytes
|
|
|
-func (m *MemSwapBytes) UnmarshalJSON(s []byte) error {
|
|
|
- b := MemBytes(*m)
|
|
|
- return b.UnmarshalJSON(s)
|
|
|
-}
|