Browse code

Merge pull request #18682 from calavera/replace_units_package

Replace pkg/units with docker/go-units.

David Calavera authored on 2015/12/17 03:48:59
Showing 28 changed files
... ...
@@ -28,11 +28,11 @@ import (
28 28
 	"github.com/docker/docker/pkg/progress"
29 29
 	"github.com/docker/docker/pkg/streamformatter"
30 30
 	"github.com/docker/docker/pkg/ulimit"
31
-	"github.com/docker/docker/pkg/units"
32 31
 	"github.com/docker/docker/pkg/urlutil"
33 32
 	"github.com/docker/docker/registry"
34 33
 	tagpkg "github.com/docker/docker/tag"
35 34
 	"github.com/docker/docker/utils"
35
+	"github.com/docker/go-units"
36 36
 )
37 37
 
38 38
 // CmdBuild builds a new image from the source code at a given path.
... ...
@@ -11,7 +11,7 @@ import (
11 11
 	flag "github.com/docker/docker/pkg/mflag"
12 12
 	"github.com/docker/docker/pkg/stringid"
13 13
 	"github.com/docker/docker/pkg/stringutils"
14
-	"github.com/docker/docker/pkg/units"
14
+	"github.com/docker/go-units"
15 15
 )
16 16
 
17 17
 // CmdHistory shows the history of an image.
... ...
@@ -13,7 +13,7 @@ import (
13 13
 	"github.com/docker/docker/opts"
14 14
 	flag "github.com/docker/docker/pkg/mflag"
15 15
 	"github.com/docker/docker/pkg/stringid"
16
-	"github.com/docker/docker/pkg/units"
16
+	"github.com/docker/go-units"
17 17
 )
18 18
 
19 19
 // CmdImages lists the images in a specified repository, or all top-level images if no repository is specified.
... ...
@@ -6,7 +6,7 @@ import (
6 6
 	Cli "github.com/docker/docker/cli"
7 7
 	"github.com/docker/docker/pkg/ioutils"
8 8
 	flag "github.com/docker/docker/pkg/mflag"
9
-	"github.com/docker/docker/pkg/units"
9
+	"github.com/docker/go-units"
10 10
 )
11 11
 
12 12
 // CmdInfo displays system-wide information.
... ...
@@ -9,8 +9,8 @@ import (
9 9
 	"strconv"
10 10
 
11 11
 	"github.com/docker/docker/api/types"
12
-	"github.com/docker/docker/pkg/units"
13 12
 	"github.com/docker/docker/runconfig"
13
+	"github.com/docker/go-units"
14 14
 )
15 15
 
16 16
 var headerRegexp = regexp.MustCompile(`\ADocker/.+\s\((.+)\)\z`)
... ...
@@ -10,7 +10,7 @@ import (
10 10
 	"github.com/docker/docker/api/types"
11 11
 	"github.com/docker/docker/pkg/stringid"
12 12
 	"github.com/docker/docker/pkg/stringutils"
13
-	"github.com/docker/docker/pkg/units"
13
+	"github.com/docker/go-units"
14 14
 )
15 15
 
16 16
 const (
... ...
@@ -13,7 +13,7 @@ import (
13 13
 	"github.com/docker/docker/api/types"
14 14
 	Cli "github.com/docker/docker/cli"
15 15
 	"github.com/docker/docker/pkg/jsonmessage"
16
-	"github.com/docker/docker/pkg/units"
16
+	"github.com/docker/go-units"
17 17
 )
18 18
 
19 19
 type containerStats struct {
... ...
@@ -7,7 +7,7 @@ import (
7 7
 
8 8
 	"github.com/docker/docker/daemon/execdriver"
9 9
 	derr "github.com/docker/docker/errors"
10
-	"github.com/docker/docker/pkg/units"
10
+	"github.com/docker/go-units"
11 11
 )
12 12
 
13 13
 // State holds the current container state, and has methods to get and
... ...
@@ -26,7 +26,7 @@ import (
26 26
 	"github.com/docker/docker/pkg/idtools"
27 27
 	"github.com/docker/docker/pkg/mount"
28 28
 	"github.com/docker/docker/pkg/parsers"
29
-	"github.com/docker/docker/pkg/units"
29
+	"github.com/docker/go-units"
30 30
 
31 31
 	"github.com/opencontainers/runc/libcontainer/label"
32 32
 )
... ...
@@ -15,7 +15,7 @@ import (
15 15
 	"github.com/docker/docker/pkg/devicemapper"
16 16
 	"github.com/docker/docker/pkg/idtools"
17 17
 	"github.com/docker/docker/pkg/mount"
18
-	"github.com/docker/docker/pkg/units"
18
+	"github.com/docker/go-units"
19 19
 )
20 20
 
21 21
 func init() {
... ...
@@ -14,7 +14,7 @@ import (
14 14
 	"github.com/docker/docker/daemon/logger"
15 15
 	"github.com/docker/docker/daemon/logger/loggerutils"
16 16
 	"github.com/docker/docker/pkg/jsonlog"
17
-	"github.com/docker/docker/pkg/units"
17
+	"github.com/docker/go-units"
18 18
 )
19 19
 
20 20
 // Name is the name of the file that the jsonlogger logs to.
... ...
@@ -20,6 +20,7 @@ clone git github.com/mistifyio/go-zfs v2.1.1
20 20
 clone git github.com/tchap/go-patricia v2.1.0
21 21
 clone git github.com/vdemeester/shakers 3c10293ce22b900c27acad7b28656196fcc2f73b
22 22
 clone git golang.org/x/net 47990a1ba55743e6ef1affd3a14e5bac8553615d https://github.com/golang/net.git
23
+clone git github.com/docker/go-units v0.2.0
23 24
 
24 25
 #get libnetwork packages
25 26
 clone git github.com/docker/libnetwork bbd6e6d8ca1e7c9b42f6f53277b0bde72847ff90
... ...
@@ -49,7 +50,7 @@ clone git github.com/miekg/pkcs11 80f102b5cac759de406949c47f0928b99bd64cdf
49 49
 clone git github.com/jfrazelle/go v1.5.1-1
50 50
 clone git github.com/agl/ed25519 d2b94fd789ea21d12fac1a4443dd3a3f79cda72c
51 51
 
52
-clone git github.com/opencontainers/runc v0.0.6 # libcontainer
52
+clone git github.com/opencontainers/runc ba1568de399395774ad84c2ace65937814c542ed # libcontainer
53 53
 clone git github.com/opencontainers/specs 46d949ea81080c5f60dfb72ee91468b1e9fb2998 # specs
54 54
 clone git github.com/seccomp/libseccomp-golang 1b506fc7c24eec5a3693cdcbed40d9c226cfc6a1
55 55
 # libcontainer deps (see src/github.com/opencontainers/runc/Godeps/Godeps.json)
... ...
@@ -11,7 +11,7 @@ import (
11 11
 
12 12
 	"github.com/docker/docker/pkg/blkiodev"
13 13
 	"github.com/docker/docker/pkg/parsers"
14
-	"github.com/docker/docker/pkg/units"
14
+	"github.com/docker/go-units"
15 15
 )
16 16
 
17 17
 var (
... ...
@@ -9,7 +9,7 @@ import (
9 9
 
10 10
 	"github.com/docker/docker/pkg/jsonlog"
11 11
 	"github.com/docker/docker/pkg/term"
12
-	"github.com/docker/docker/pkg/units"
12
+	"github.com/docker/go-units"
13 13
 )
14 14
 
15 15
 // JSONError wraps a concrete Code and Message, `Code` is
... ...
@@ -7,7 +7,7 @@ import (
7 7
 	"strconv"
8 8
 	"strings"
9 9
 
10
-	"github.com/docker/docker/pkg/units"
10
+	"github.com/docker/go-units"
11 11
 )
12 12
 
13 13
 // ReadMemInfo retrieves memory statistics of the host system and returns a
... ...
@@ -6,7 +6,7 @@ import (
6 6
 	"strings"
7 7
 	"testing"
8 8
 
9
-	"github.com/docker/docker/pkg/units"
9
+	"github.com/docker/go-units"
10 10
 )
11 11
 
12 12
 // TestMemInfo tests parseMemInfo with a static meminfo string
13 13
deleted file mode 100644
... ...
@@ -1,33 +0,0 @@
1
-// Package units provides helper function to parse and print size and time units
2
-// in human-readable format.
3
-package units
4
-
5
-import (
6
-	"fmt"
7
-	"time"
8
-)
9
-
10
-// HumanDuration returns a human-readable approximation of a duration
11
-// (eg. "About a minute", "4 hours ago", etc.).
12
-func HumanDuration(d time.Duration) string {
13
-	if seconds := int(d.Seconds()); seconds < 1 {
14
-		return "Less than a second"
15
-	} else if seconds < 60 {
16
-		return fmt.Sprintf("%d seconds", seconds)
17
-	} else if minutes := int(d.Minutes()); minutes == 1 {
18
-		return "About a minute"
19
-	} else if minutes < 60 {
20
-		return fmt.Sprintf("%d minutes", minutes)
21
-	} else if hours := int(d.Hours()); hours == 1 {
22
-		return "About an hour"
23
-	} else if hours < 48 {
24
-		return fmt.Sprintf("%d hours", hours)
25
-	} else if hours < 24*7*2 {
26
-		return fmt.Sprintf("%d days", hours/24)
27
-	} else if hours < 24*30*3 {
28
-		return fmt.Sprintf("%d weeks", hours/24/7)
29
-	} else if hours < 24*365*2 {
30
-		return fmt.Sprintf("%d months", hours/24/30)
31
-	}
32
-	return fmt.Sprintf("%d years", int(d.Hours())/24/365)
33
-}
34 1
deleted file mode 100644
... ...
@@ -1,46 +0,0 @@
1
-package units
2
-
3
-import (
4
-	"testing"
5
-	"time"
6
-)
7
-
8
-func TestHumanDuration(t *testing.T) {
9
-	// Useful duration abstractions
10
-	day := 24 * time.Hour
11
-	week := 7 * day
12
-	month := 30 * day
13
-	year := 365 * day
14
-
15
-	assertEquals(t, "Less than a second", HumanDuration(450*time.Millisecond))
16
-	assertEquals(t, "47 seconds", HumanDuration(47*time.Second))
17
-	assertEquals(t, "About a minute", HumanDuration(1*time.Minute))
18
-	assertEquals(t, "3 minutes", HumanDuration(3*time.Minute))
19
-	assertEquals(t, "35 minutes", HumanDuration(35*time.Minute))
20
-	assertEquals(t, "35 minutes", HumanDuration(35*time.Minute+40*time.Second))
21
-	assertEquals(t, "About an hour", HumanDuration(1*time.Hour))
22
-	assertEquals(t, "About an hour", HumanDuration(1*time.Hour+45*time.Minute))
23
-	assertEquals(t, "3 hours", HumanDuration(3*time.Hour))
24
-	assertEquals(t, "3 hours", HumanDuration(3*time.Hour+59*time.Minute))
25
-	assertEquals(t, "4 hours", HumanDuration(3*time.Hour+60*time.Minute))
26
-	assertEquals(t, "24 hours", HumanDuration(24*time.Hour))
27
-	assertEquals(t, "36 hours", HumanDuration(1*day+12*time.Hour))
28
-	assertEquals(t, "2 days", HumanDuration(2*day))
29
-	assertEquals(t, "7 days", HumanDuration(7*day))
30
-	assertEquals(t, "13 days", HumanDuration(13*day+5*time.Hour))
31
-	assertEquals(t, "2 weeks", HumanDuration(2*week))
32
-	assertEquals(t, "2 weeks", HumanDuration(2*week+4*day))
33
-	assertEquals(t, "3 weeks", HumanDuration(3*week))
34
-	assertEquals(t, "4 weeks", HumanDuration(4*week))
35
-	assertEquals(t, "4 weeks", HumanDuration(4*week+3*day))
36
-	assertEquals(t, "4 weeks", HumanDuration(1*month))
37
-	assertEquals(t, "6 weeks", HumanDuration(1*month+2*week))
38
-	assertEquals(t, "8 weeks", HumanDuration(2*month))
39
-	assertEquals(t, "3 months", HumanDuration(3*month+1*week))
40
-	assertEquals(t, "5 months", HumanDuration(5*month+2*week))
41
-	assertEquals(t, "13 months", HumanDuration(13*month))
42
-	assertEquals(t, "23 months", HumanDuration(23*month))
43
-	assertEquals(t, "24 months", HumanDuration(24*month))
44
-	assertEquals(t, "2 years", HumanDuration(24*month+2*week))
45
-	assertEquals(t, "3 years", HumanDuration(3*year+2*month))
46
-}
47 1
deleted file mode 100644
... ...
@@ -1,95 +0,0 @@
1
-package units
2
-
3
-import (
4
-	"fmt"
5
-	"regexp"
6
-	"strconv"
7
-	"strings"
8
-)
9
-
10
-// See: http://en.wikipedia.org/wiki/Binary_prefix
11
-const (
12
-	// Decimal
13
-
14
-	KB = 1000
15
-	MB = 1000 * KB
16
-	GB = 1000 * MB
17
-	TB = 1000 * GB
18
-	PB = 1000 * TB
19
-
20
-	// Binary
21
-
22
-	KiB = 1024
23
-	MiB = 1024 * KiB
24
-	GiB = 1024 * MiB
25
-	TiB = 1024 * GiB
26
-	PiB = 1024 * TiB
27
-)
28
-
29
-type unitMap map[string]int64
30
-
31
-var (
32
-	decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
33
-	binaryMap  = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
34
-	sizeRegex  = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
35
-)
36
-
37
-var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
38
-var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
39
-
40
-// CustomSize returns a human-readable approximation of a size
41
-// using custom format.
42
-func CustomSize(format string, size float64, base float64, _map []string) string {
43
-	i := 0
44
-	for size >= base {
45
-		size = size / base
46
-		i++
47
-	}
48
-	return fmt.Sprintf(format, size, _map[i])
49
-}
50
-
51
-// HumanSize returns a human-readable approximation of a size
52
-// capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
53
-func HumanSize(size float64) string {
54
-	return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
55
-}
56
-
57
-// BytesSize returns a human-readable size in bytes, kibibytes,
58
-// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
59
-func BytesSize(size float64) string {
60
-	return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
61
-}
62
-
63
-// FromHumanSize returns an integer from a human-readable specification of a
64
-// size using SI standard (eg. "44kB", "17MB").
65
-func FromHumanSize(size string) (int64, error) {
66
-	return parseSize(size, decimalMap)
67
-}
68
-
69
-// RAMInBytes parses a human-readable string representing an amount of RAM
70
-// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
71
-// returns the number of bytes, or -1 if the string is unparseable.
72
-// Units are case-insensitive, and the 'b' suffix is optional.
73
-func RAMInBytes(size string) (int64, error) {
74
-	return parseSize(size, binaryMap)
75
-}
76
-
77
-// Parses the human-readable size string into the amount it represents.
78
-func parseSize(sizeStr string, uMap unitMap) (int64, error) {
79
-	matches := sizeRegex.FindStringSubmatch(sizeStr)
80
-	if len(matches) != 3 {
81
-		return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
82
-	}
83
-
84
-	size, err := strconv.ParseInt(matches[1], 10, 0)
85
-	if err != nil {
86
-		return -1, err
87
-	}
88
-
89
-	unitPrefix := strings.ToLower(matches[2])
90
-	if mul, ok := uMap[unitPrefix]; ok {
91
-		size *= mul
92
-	}
93
-
94
-	return size, nil
95
-}
96 1
deleted file mode 100644
... ...
@@ -1,108 +0,0 @@
1
-package units
2
-
3
-import (
4
-	"reflect"
5
-	"runtime"
6
-	"strings"
7
-	"testing"
8
-)
9
-
10
-func TestBytesSize(t *testing.T) {
11
-	assertEquals(t, "1 KiB", BytesSize(1024))
12
-	assertEquals(t, "1 MiB", BytesSize(1024*1024))
13
-	assertEquals(t, "1 MiB", BytesSize(1048576))
14
-	assertEquals(t, "2 MiB", BytesSize(2*MiB))
15
-	assertEquals(t, "3.42 GiB", BytesSize(3.42*GiB))
16
-	assertEquals(t, "5.372 TiB", BytesSize(5.372*TiB))
17
-	assertEquals(t, "2.22 PiB", BytesSize(2.22*PiB))
18
-}
19
-
20
-func TestHumanSize(t *testing.T) {
21
-	assertEquals(t, "1 kB", HumanSize(1000))
22
-	assertEquals(t, "1.024 kB", HumanSize(1024))
23
-	assertEquals(t, "1 MB", HumanSize(1000000))
24
-	assertEquals(t, "1.049 MB", HumanSize(1048576))
25
-	assertEquals(t, "2 MB", HumanSize(2*MB))
26
-	assertEquals(t, "3.42 GB", HumanSize(float64(3.42*GB)))
27
-	assertEquals(t, "5.372 TB", HumanSize(float64(5.372*TB)))
28
-	assertEquals(t, "2.22 PB", HumanSize(float64(2.22*PB)))
29
-}
30
-
31
-func TestFromHumanSize(t *testing.T) {
32
-	assertSuccessEquals(t, 32, FromHumanSize, "32")
33
-	assertSuccessEquals(t, 32, FromHumanSize, "32b")
34
-	assertSuccessEquals(t, 32, FromHumanSize, "32B")
35
-	assertSuccessEquals(t, 32*KB, FromHumanSize, "32k")
36
-	assertSuccessEquals(t, 32*KB, FromHumanSize, "32K")
37
-	assertSuccessEquals(t, 32*KB, FromHumanSize, "32kb")
38
-	assertSuccessEquals(t, 32*KB, FromHumanSize, "32Kb")
39
-	assertSuccessEquals(t, 32*MB, FromHumanSize, "32Mb")
40
-	assertSuccessEquals(t, 32*GB, FromHumanSize, "32Gb")
41
-	assertSuccessEquals(t, 32*TB, FromHumanSize, "32Tb")
42
-	assertSuccessEquals(t, 32*PB, FromHumanSize, "32Pb")
43
-
44
-	assertError(t, FromHumanSize, "")
45
-	assertError(t, FromHumanSize, "hello")
46
-	assertError(t, FromHumanSize, "-32")
47
-	assertError(t, FromHumanSize, "32.3")
48
-	assertError(t, FromHumanSize, " 32 ")
49
-	assertError(t, FromHumanSize, "32.3Kb")
50
-	assertError(t, FromHumanSize, "32 mb")
51
-	assertError(t, FromHumanSize, "32m b")
52
-	assertError(t, FromHumanSize, "32bm")
53
-}
54
-
55
-func TestRAMInBytes(t *testing.T) {
56
-	assertSuccessEquals(t, 32, RAMInBytes, "32")
57
-	assertSuccessEquals(t, 32, RAMInBytes, "32b")
58
-	assertSuccessEquals(t, 32, RAMInBytes, "32B")
59
-	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32k")
60
-	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K")
61
-	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb")
62
-	assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb")
63
-	assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb")
64
-	assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb")
65
-	assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb")
66
-	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32Pb")
67
-	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32PB")
68
-	assertSuccessEquals(t, 32*PiB, RAMInBytes, "32P")
69
-
70
-	assertError(t, RAMInBytes, "")
71
-	assertError(t, RAMInBytes, "hello")
72
-	assertError(t, RAMInBytes, "-32")
73
-	assertError(t, RAMInBytes, "32.3")
74
-	assertError(t, RAMInBytes, " 32 ")
75
-	assertError(t, RAMInBytes, "32.3Kb")
76
-	assertError(t, RAMInBytes, "32 mb")
77
-	assertError(t, RAMInBytes, "32m b")
78
-	assertError(t, RAMInBytes, "32bm")
79
-}
80
-
81
-func assertEquals(t *testing.T, expected, actual interface{}) {
82
-	if expected != actual {
83
-		t.Errorf("Expected '%v' but got '%v'", expected, actual)
84
-	}
85
-}
86
-
87
-// func that maps to the parse function signatures as testing abstraction
88
-type parseFn func(string) (int64, error)
89
-
90
-// Define 'String()' for pretty-print
91
-func (fn parseFn) String() string {
92
-	fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
93
-	return fnName[strings.LastIndex(fnName, ".")+1:]
94
-}
95
-
96
-func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
97
-	res, err := fn(arg)
98
-	if err != nil || res != expected {
99
-		t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
100
-	}
101
-}
102
-
103
-func assertError(t *testing.T, fn parseFn, arg string) {
104
-	res, err := fn(arg)
105
-	if err == nil && res != -1 {
106
-		t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
107
-	}
108
-}
... ...
@@ -12,8 +12,8 @@ import (
12 12
 	"github.com/docker/docker/pkg/parsers"
13 13
 	"github.com/docker/docker/pkg/signal"
14 14
 	"github.com/docker/docker/pkg/stringutils"
15
-	"github.com/docker/docker/pkg/units"
16 15
 	"github.com/docker/docker/volume"
16
+	"github.com/docker/go-units"
17 17
 )
18 18
 
19 19
 var (
20 20
new file mode 100644
... ...
@@ -0,0 +1,191 @@
0
+
1
+                                 Apache License
2
+                           Version 2.0, January 2004
3
+                        https://www.apache.org/licenses/
4
+
5
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+   1. Definitions.
8
+
9
+      "License" shall mean the terms and conditions for use, reproduction,
10
+      and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+      "Licensor" shall mean the copyright owner or entity authorized by
13
+      the copyright owner that is granting the License.
14
+
15
+      "Legal Entity" shall mean the union of the acting entity and all
16
+      other entities that control, are controlled by, or are under common
17
+      control with that entity. For the purposes of this definition,
18
+      "control" means (i) the power, direct or indirect, to cause the
19
+      direction or management of such entity, whether by contract or
20
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+      outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+      "You" (or "Your") shall mean an individual or Legal Entity
24
+      exercising permissions granted by this License.
25
+
26
+      "Source" form shall mean the preferred form for making modifications,
27
+      including but not limited to software source code, documentation
28
+      source, and configuration files.
29
+
30
+      "Object" form shall mean any form resulting from mechanical
31
+      transformation or translation of a Source form, including but
32
+      not limited to compiled object code, generated documentation,
33
+      and conversions to other media types.
34
+
35
+      "Work" shall mean the work of authorship, whether in Source or
36
+      Object form, made available under the License, as indicated by a
37
+      copyright notice that is included in or attached to the work
38
+      (an example is provided in the Appendix below).
39
+
40
+      "Derivative Works" shall mean any work, whether in Source or Object
41
+      form, that is based on (or derived from) the Work and for which the
42
+      editorial revisions, annotations, elaborations, or other modifications
43
+      represent, as a whole, an original work of authorship. For the purposes
44
+      of this License, Derivative Works shall not include works that remain
45
+      separable from, or merely link (or bind by name) to the interfaces of,
46
+      the Work and Derivative Works thereof.
47
+
48
+      "Contribution" shall mean any work of authorship, including
49
+      the original version of the Work and any modifications or additions
50
+      to that Work or Derivative Works thereof, that is intentionally
51
+      submitted to Licensor for inclusion in the Work by the copyright owner
52
+      or by an individual or Legal Entity authorized to submit on behalf of
53
+      the copyright owner. For the purposes of this definition, "submitted"
54
+      means any form of electronic, verbal, or written communication sent
55
+      to the Licensor or its representatives, including but not limited to
56
+      communication on electronic mailing lists, source code control systems,
57
+      and issue tracking systems that are managed by, or on behalf of, the
58
+      Licensor for the purpose of discussing and improving the Work, but
59
+      excluding communication that is conspicuously marked or otherwise
60
+      designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+      "Contributor" shall mean Licensor and any individual or Legal Entity
63
+      on behalf of whom a Contribution has been received by Licensor and
64
+      subsequently incorporated within the Work.
65
+
66
+   2. Grant of Copyright License. Subject to the terms and conditions of
67
+      this License, each Contributor hereby grants to You a perpetual,
68
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+      copyright license to reproduce, prepare Derivative Works of,
70
+      publicly display, publicly perform, sublicense, and distribute the
71
+      Work and such Derivative Works in Source or Object form.
72
+
73
+   3. Grant of Patent License. Subject to the terms and conditions of
74
+      this License, each Contributor hereby grants to You a perpetual,
75
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+      (except as stated in this section) patent license to make, have made,
77
+      use, offer to sell, sell, import, and otherwise transfer the Work,
78
+      where such license applies only to those patent claims licensable
79
+      by such Contributor that are necessarily infringed by their
80
+      Contribution(s) alone or by combination of their Contribution(s)
81
+      with the Work to which such Contribution(s) was submitted. If You
82
+      institute patent litigation against any entity (including a
83
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+      or a Contribution incorporated within the Work constitutes direct
85
+      or contributory patent infringement, then any patent licenses
86
+      granted to You under this License for that Work shall terminate
87
+      as of the date such litigation is filed.
88
+
89
+   4. Redistribution. You may reproduce and distribute copies of the
90
+      Work or Derivative Works thereof in any medium, with or without
91
+      modifications, and in Source or Object form, provided that You
92
+      meet the following conditions:
93
+
94
+      (a) You must give any other recipients of the Work or
95
+          Derivative Works a copy of this License; and
96
+
97
+      (b) You must cause any modified files to carry prominent notices
98
+          stating that You changed the files; and
99
+
100
+      (c) You must retain, in the Source form of any Derivative Works
101
+          that You distribute, all copyright, patent, trademark, and
102
+          attribution notices from the Source form of the Work,
103
+          excluding those notices that do not pertain to any part of
104
+          the Derivative Works; and
105
+
106
+      (d) If the Work includes a "NOTICE" text file as part of its
107
+          distribution, then any Derivative Works that You distribute must
108
+          include a readable copy of the attribution notices contained
109
+          within such NOTICE file, excluding those notices that do not
110
+          pertain to any part of the Derivative Works, in at least one
111
+          of the following places: within a NOTICE text file distributed
112
+          as part of the Derivative Works; within the Source form or
113
+          documentation, if provided along with the Derivative Works; or,
114
+          within a display generated by the Derivative Works, if and
115
+          wherever such third-party notices normally appear. The contents
116
+          of the NOTICE file are for informational purposes only and
117
+          do not modify the License. You may add Your own attribution
118
+          notices within Derivative Works that You distribute, alongside
119
+          or as an addendum to the NOTICE text from the Work, provided
120
+          that such additional attribution notices cannot be construed
121
+          as modifying the License.
122
+
123
+      You may add Your own copyright statement to Your modifications and
124
+      may provide additional or different license terms and conditions
125
+      for use, reproduction, or distribution of Your modifications, or
126
+      for any such Derivative Works as a whole, provided Your use,
127
+      reproduction, and distribution of the Work otherwise complies with
128
+      the conditions stated in this License.
129
+
130
+   5. Submission of Contributions. Unless You explicitly state otherwise,
131
+      any Contribution intentionally submitted for inclusion in the Work
132
+      by You to the Licensor shall be under the terms and conditions of
133
+      this License, without any additional terms or conditions.
134
+      Notwithstanding the above, nothing herein shall supersede or modify
135
+      the terms of any separate license agreement you may have executed
136
+      with Licensor regarding such Contributions.
137
+
138
+   6. Trademarks. This License does not grant permission to use the trade
139
+      names, trademarks, service marks, or product names of the Licensor,
140
+      except as required for reasonable and customary use in describing the
141
+      origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+   7. Disclaimer of Warranty. Unless required by applicable law or
144
+      agreed to in writing, Licensor provides the Work (and each
145
+      Contributor provides its Contributions) on an "AS IS" BASIS,
146
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+      implied, including, without limitation, any warranties or conditions
148
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+      PARTICULAR PURPOSE. You are solely responsible for determining the
150
+      appropriateness of using or redistributing the Work and assume any
151
+      risks associated with Your exercise of permissions under this License.
152
+
153
+   8. Limitation of Liability. In no event and under no legal theory,
154
+      whether in tort (including negligence), contract, or otherwise,
155
+      unless required by applicable law (such as deliberate and grossly
156
+      negligent acts) or agreed to in writing, shall any Contributor be
157
+      liable to You for damages, including any direct, indirect, special,
158
+      incidental, or consequential damages of any character arising as a
159
+      result of this License or out of the use or inability to use the
160
+      Work (including but not limited to damages for loss of goodwill,
161
+      work stoppage, computer failure or malfunction, or any and all
162
+      other commercial damages or losses), even if such Contributor
163
+      has been advised of the possibility of such damages.
164
+
165
+   9. Accepting Warranty or Additional Liability. While redistributing
166
+      the Work or Derivative Works thereof, You may choose to offer,
167
+      and charge a fee for, acceptance of support, warranty, indemnity,
168
+      or other liability obligations and/or rights consistent with this
169
+      License. However, in accepting such obligations, You may act only
170
+      on Your own behalf and on Your sole responsibility, not on behalf
171
+      of any other Contributor, and only if You agree to indemnify,
172
+      defend, and hold each Contributor harmless for any liability
173
+      incurred by, or claims asserted against, such Contributor by reason
174
+      of your accepting any such warranty or additional liability.
175
+
176
+   END OF TERMS AND CONDITIONS
177
+
178
+   Copyright 2015 Docker, Inc.
179
+
180
+   Licensed under the Apache License, Version 2.0 (the "License");
181
+   you may not use this file except in compliance with the License.
182
+   You may obtain a copy of the License at
183
+
184
+       https://www.apache.org/licenses/LICENSE-2.0
185
+
186
+   Unless required by applicable law or agreed to in writing, software
187
+   distributed under the License is distributed on an "AS IS" BASIS,
188
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+   See the License for the specific language governing permissions and
190
+   limitations under the License.
0 191
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+# Introduction
1
+
2
+go-units is a library to transform human friendly measurements into machine friendly values.
3
+
4
+## Usage
5
+
6
+See the [docs in godoc](https://godoc.org/github.com/docker/go-units) for examples and documentation.
7
+
8
+## License
9
+
10
+go-units is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.
0 11
new file mode 100644
... ...
@@ -0,0 +1,33 @@
0
+// Package units provides helper function to parse and print size and time units
1
+// in human-readable format.
2
+package units
3
+
4
+import (
5
+	"fmt"
6
+	"time"
7
+)
8
+
9
+// HumanDuration returns a human-readable approximation of a duration
10
+// (eg. "About a minute", "4 hours ago", etc.).
11
+func HumanDuration(d time.Duration) string {
12
+	if seconds := int(d.Seconds()); seconds < 1 {
13
+		return "Less than a second"
14
+	} else if seconds < 60 {
15
+		return fmt.Sprintf("%d seconds", seconds)
16
+	} else if minutes := int(d.Minutes()); minutes == 1 {
17
+		return "About a minute"
18
+	} else if minutes < 60 {
19
+		return fmt.Sprintf("%d minutes", minutes)
20
+	} else if hours := int(d.Hours()); hours == 1 {
21
+		return "About an hour"
22
+	} else if hours < 48 {
23
+		return fmt.Sprintf("%d hours", hours)
24
+	} else if hours < 24*7*2 {
25
+		return fmt.Sprintf("%d days", hours/24)
26
+	} else if hours < 24*30*3 {
27
+		return fmt.Sprintf("%d weeks", hours/24/7)
28
+	} else if hours < 24*365*2 {
29
+		return fmt.Sprintf("%d months", hours/24/30)
30
+	}
31
+	return fmt.Sprintf("%d years", int(d.Hours())/24/365)
32
+}
0 33
new file mode 100644
... ...
@@ -0,0 +1,95 @@
0
+package units
1
+
2
+import (
3
+	"fmt"
4
+	"regexp"
5
+	"strconv"
6
+	"strings"
7
+)
8
+
9
+// See: http://en.wikipedia.org/wiki/Binary_prefix
10
+const (
11
+	// Decimal
12
+
13
+	KB = 1000
14
+	MB = 1000 * KB
15
+	GB = 1000 * MB
16
+	TB = 1000 * GB
17
+	PB = 1000 * TB
18
+
19
+	// Binary
20
+
21
+	KiB = 1024
22
+	MiB = 1024 * KiB
23
+	GiB = 1024 * MiB
24
+	TiB = 1024 * GiB
25
+	PiB = 1024 * TiB
26
+)
27
+
28
+type unitMap map[string]int64
29
+
30
+var (
31
+	decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
32
+	binaryMap  = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
33
+	sizeRegex  = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`)
34
+)
35
+
36
+var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
37
+var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
38
+
39
+// CustomSize returns a human-readable approximation of a size
40
+// using custom format.
41
+func CustomSize(format string, size float64, base float64, _map []string) string {
42
+	i := 0
43
+	for size >= base {
44
+		size = size / base
45
+		i++
46
+	}
47
+	return fmt.Sprintf(format, size, _map[i])
48
+}
49
+
50
+// HumanSize returns a human-readable approximation of a size
51
+// capped at 4 valid numbers (eg. "2.746 MB", "796 KB").
52
+func HumanSize(size float64) string {
53
+	return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
54
+}
55
+
56
+// BytesSize returns a human-readable size in bytes, kibibytes,
57
+// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB").
58
+func BytesSize(size float64) string {
59
+	return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
60
+}
61
+
62
+// FromHumanSize returns an integer from a human-readable specification of a
63
+// size using SI standard (eg. "44kB", "17MB").
64
+func FromHumanSize(size string) (int64, error) {
65
+	return parseSize(size, decimalMap)
66
+}
67
+
68
+// RAMInBytes parses a human-readable string representing an amount of RAM
69
+// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
70
+// returns the number of bytes, or -1 if the string is unparseable.
71
+// Units are case-insensitive, and the 'b' suffix is optional.
72
+func RAMInBytes(size string) (int64, error) {
73
+	return parseSize(size, binaryMap)
74
+}
75
+
76
+// Parses the human-readable size string into the amount it represents.
77
+func parseSize(sizeStr string, uMap unitMap) (int64, error) {
78
+	matches := sizeRegex.FindStringSubmatch(sizeStr)
79
+	if len(matches) != 3 {
80
+		return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
81
+	}
82
+
83
+	size, err := strconv.ParseInt(matches[1], 10, 0)
84
+	if err != nil {
85
+		return -1, err
86
+	}
87
+
88
+	unitPrefix := strings.ToLower(matches[2])
89
+	if mul, ok := uMap[unitPrefix]; ok {
90
+		size *= mul
91
+	}
92
+
93
+	return size, nil
94
+}
... ...
@@ -13,7 +13,7 @@ import (
13 13
 	"time"
14 14
 
15 15
 	"github.com/docker/docker/pkg/mount"
16
-	"github.com/docker/docker/pkg/units"
16
+	"github.com/docker/go-units"
17 17
 )
18 18
 
19 19
 const cgroupNamePrefix = "name="
... ...
@@ -78,6 +78,13 @@ func (p Process) Signal(sig os.Signal) error {
78 78
 	return p.ops.signal(sig)
79 79
 }
80 80
 
81
+// IO holds the process's STDIO
82
+type IO struct {
83
+	Stdin  io.WriteCloser
84
+	Stdout io.ReadCloser
85
+	Stderr io.ReadCloser
86
+}
87
+
81 88
 // NewConsole creates new console for process and returns it
82 89
 func (p *Process) NewConsole(rootuid int) (Console, error) {
83 90
 	console, err := NewConsole(rootuid, rootuid)
... ...
@@ -320,3 +320,44 @@ func getPipeFds(pid int) ([]string, error) {
320 320
 	}
321 321
 	return fds, nil
322 322
 }
323
+
324
+// InitializeIO creates pipes for use with the process's STDIO
325
+// and returns the opposite side for each
326
+func (p *Process) InitializeIO(rootuid int) (i *IO, err error) {
327
+	var fds []uintptr
328
+	i = &IO{}
329
+	// cleanup in case of an error
330
+	defer func() {
331
+		if err != nil {
332
+			for _, fd := range fds {
333
+				syscall.Close(int(fd))
334
+			}
335
+		}
336
+	}()
337
+	// STDIN
338
+	r, w, err := os.Pipe()
339
+	if err != nil {
340
+		return nil, err
341
+	}
342
+	fds = append(fds, r.Fd(), w.Fd())
343
+	p.Stdin, i.Stdin = r, w
344
+	// STDOUT
345
+	if r, w, err = os.Pipe(); err != nil {
346
+		return nil, err
347
+	}
348
+	fds = append(fds, r.Fd(), w.Fd())
349
+	p.Stdout, i.Stdout = w, r
350
+	// STDERR
351
+	if r, w, err = os.Pipe(); err != nil {
352
+		return nil, err
353
+	}
354
+	fds = append(fds, r.Fd(), w.Fd())
355
+	p.Stderr, i.Stderr = w, r
356
+	// change ownership of the pipes incase we are in a user namespace
357
+	for _, fd := range fds {
358
+		if err := syscall.Fchown(int(fd), rootuid, rootuid); err != nil {
359
+			return nil, err
360
+		}
361
+	}
362
+	return i, nil
363
+}