Signed-off-by: Antonio Murdaca <me@runcom.ninja>
Antonio Murdaca authored on 2015/03/24 20:25:26... | ... |
@@ -33,7 +33,6 @@ import ( |
33 | 33 |
"github.com/docker/docker/nat" |
34 | 34 |
"github.com/docker/docker/opts" |
35 | 35 |
"github.com/docker/docker/pkg/archive" |
36 |
- "github.com/docker/docker/pkg/common" |
|
37 | 36 |
"github.com/docker/docker/pkg/fileutils" |
38 | 37 |
"github.com/docker/docker/pkg/homedir" |
39 | 38 |
flag "github.com/docker/docker/pkg/mflag" |
... | ... |
@@ -43,6 +42,7 @@ import ( |
43 | 43 |
"github.com/docker/docker/pkg/promise" |
44 | 44 |
"github.com/docker/docker/pkg/resolvconf" |
45 | 45 |
"github.com/docker/docker/pkg/signal" |
46 |
+ "github.com/docker/docker/pkg/stringid" |
|
46 | 47 |
"github.com/docker/docker/pkg/symlink" |
47 | 48 |
"github.com/docker/docker/pkg/term" |
48 | 49 |
"github.com/docker/docker/pkg/timeutils" |
... | ... |
@@ -1165,7 +1165,7 @@ func (cli *DockerCli) CmdHistory(args ...string) error { |
1165 | 1165 |
if *noTrunc { |
1166 | 1166 |
fmt.Fprintf(w, "%s\t", outID) |
1167 | 1167 |
} else { |
1168 |
- fmt.Fprintf(w, "%s\t", common.TruncateID(outID)) |
|
1168 |
+ fmt.Fprintf(w, "%s\t", stringid.TruncateID(outID)) |
|
1169 | 1169 |
} |
1170 | 1170 |
|
1171 | 1171 |
fmt.Fprintf(w, "%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0)))) |
... | ... |
@@ -1180,7 +1180,7 @@ func (cli *DockerCli) CmdHistory(args ...string) error { |
1180 | 1180 |
if *noTrunc { |
1181 | 1181 |
fmt.Fprintln(w, outID) |
1182 | 1182 |
} else { |
1183 |
- fmt.Fprintln(w, common.TruncateID(outID)) |
|
1183 |
+ fmt.Fprintln(w, stringid.TruncateID(outID)) |
|
1184 | 1184 |
} |
1185 | 1185 |
} |
1186 | 1186 |
} |
... | ... |
@@ -1479,7 +1479,7 @@ func (cli *DockerCli) CmdImages(args ...string) error { |
1479 | 1479 |
} |
1480 | 1480 |
|
1481 | 1481 |
if matchName != "" { |
1482 |
- if matchName == image.Get("Id") || matchName == common.TruncateID(image.Get("Id")) { |
|
1482 |
+ if matchName == image.Get("Id") || matchName == stringid.TruncateID(image.Get("Id")) { |
|
1483 | 1483 |
startImage = image |
1484 | 1484 |
} |
1485 | 1485 |
|
... | ... |
@@ -1549,7 +1549,7 @@ func (cli *DockerCli) CmdImages(args ...string) error { |
1549 | 1549 |
for _, out := range outs.Data { |
1550 | 1550 |
outID := out.Get("Id") |
1551 | 1551 |
if !*noTrunc { |
1552 |
- outID = common.TruncateID(outID) |
|
1552 |
+ outID = stringid.TruncateID(outID) |
|
1553 | 1553 |
} |
1554 | 1554 |
|
1555 | 1555 |
repoTags := out.GetList("RepoTags") |
... | ... |
@@ -1629,8 +1629,8 @@ func (cli *DockerCli) printVizNode(noTrunc bool, image *engine.Env, prefix strin |
1629 | 1629 |
imageID = image.Get("Id") |
1630 | 1630 |
parentID = image.Get("ParentId") |
1631 | 1631 |
} else { |
1632 |
- imageID = common.TruncateID(image.Get("Id")) |
|
1633 |
- parentID = common.TruncateID(image.Get("ParentId")) |
|
1632 |
+ imageID = stringid.TruncateID(image.Get("Id")) |
|
1633 |
+ parentID = stringid.TruncateID(image.Get("ParentId")) |
|
1634 | 1634 |
} |
1635 | 1635 |
if parentID == "" { |
1636 | 1636 |
fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", imageID) |
... | ... |
@@ -1649,7 +1649,7 @@ func (cli *DockerCli) printTreeNode(noTrunc bool, image *engine.Env, prefix stri |
1649 | 1649 |
if noTrunc { |
1650 | 1650 |
imageID = image.Get("Id") |
1651 | 1651 |
} else { |
1652 |
- imageID = common.TruncateID(image.Get("Id")) |
|
1652 |
+ imageID = stringid.TruncateID(image.Get("Id")) |
|
1653 | 1653 |
} |
1654 | 1654 |
|
1655 | 1655 |
fmt.Fprintf(cli.out, "%s%s Virtual Size: %s", prefix, imageID, units.HumanSize(float64(image.GetInt64("VirtualSize")))) |
... | ... |
@@ -1757,7 +1757,7 @@ func (cli *DockerCli) CmdPs(args ...string) error { |
1757 | 1757 |
outID := out.Get("Id") |
1758 | 1758 |
|
1759 | 1759 |
if !*noTrunc { |
1760 |
- outID = common.TruncateID(outID) |
|
1760 |
+ outID = stringid.TruncateID(outID) |
|
1761 | 1761 |
} |
1762 | 1762 |
|
1763 | 1763 |
if *quiet { |
... | ... |
@@ -32,8 +32,8 @@ import ( |
32 | 32 |
"github.com/docker/docker/builder/parser" |
33 | 33 |
"github.com/docker/docker/daemon" |
34 | 34 |
"github.com/docker/docker/engine" |
35 |
- "github.com/docker/docker/pkg/common" |
|
36 | 35 |
"github.com/docker/docker/pkg/fileutils" |
36 |
+ "github.com/docker/docker/pkg/stringid" |
|
37 | 37 |
"github.com/docker/docker/pkg/symlink" |
38 | 38 |
"github.com/docker/docker/pkg/tarsum" |
39 | 39 |
"github.com/docker/docker/registry" |
... | ... |
@@ -177,7 +177,7 @@ func (b *Builder) Run(context io.Reader) (string, error) { |
177 | 177 |
} |
178 | 178 |
return "", err |
179 | 179 |
} |
180 |
- fmt.Fprintf(b.OutStream, " ---> %s\n", common.TruncateID(b.image)) |
|
180 |
+ fmt.Fprintf(b.OutStream, " ---> %s\n", stringid.TruncateID(b.image)) |
|
181 | 181 |
if b.Remove { |
182 | 182 |
b.clearTmp() |
183 | 183 |
} |
... | ... |
@@ -187,7 +187,7 @@ func (b *Builder) Run(context io.Reader) (string, error) { |
187 | 187 |
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?") |
188 | 188 |
} |
189 | 189 |
|
190 |
- fmt.Fprintf(b.OutStream, "Successfully built %s\n", common.TruncateID(b.image)) |
|
190 |
+ fmt.Fprintf(b.OutStream, "Successfully built %s\n", stringid.TruncateID(b.image)) |
|
191 | 191 |
return b.image, nil |
192 | 192 |
} |
193 | 193 |
|
... | ... |
@@ -25,10 +25,10 @@ import ( |
25 | 25 |
imagepkg "github.com/docker/docker/image" |
26 | 26 |
"github.com/docker/docker/pkg/archive" |
27 | 27 |
"github.com/docker/docker/pkg/chrootarchive" |
28 |
- "github.com/docker/docker/pkg/common" |
|
29 | 28 |
"github.com/docker/docker/pkg/ioutils" |
30 | 29 |
"github.com/docker/docker/pkg/parsers" |
31 | 30 |
"github.com/docker/docker/pkg/progressreader" |
31 |
+ "github.com/docker/docker/pkg/stringid" |
|
32 | 32 |
"github.com/docker/docker/pkg/symlink" |
33 | 33 |
"github.com/docker/docker/pkg/system" |
34 | 34 |
"github.com/docker/docker/pkg/tarsum" |
... | ... |
@@ -557,7 +557,7 @@ func (b *Builder) create() (*daemon.Container, error) { |
557 | 557 |
} |
558 | 558 |
|
559 | 559 |
b.TmpContainers[c.ID] = struct{}{} |
560 |
- fmt.Fprintf(b.OutStream, " ---> Running in %s\n", common.TruncateID(c.ID)) |
|
560 |
+ fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID)) |
|
561 | 561 |
|
562 | 562 |
if len(config.Cmd) > 0 { |
563 | 563 |
// override the entry point that may have been picked up from the base image |
... | ... |
@@ -753,11 +753,11 @@ func (b *Builder) clearTmp() { |
753 | 753 |
} |
754 | 754 |
|
755 | 755 |
if err := b.Daemon.Rm(tmp); err != nil { |
756 |
- fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %v\n", common.TruncateID(c), err) |
|
756 |
+ fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err) |
|
757 | 757 |
return |
758 | 758 |
} |
759 | 759 |
b.Daemon.DeleteVolumes(tmp.VolumePaths()) |
760 | 760 |
delete(b.TmpContainers, c) |
761 |
- fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", common.TruncateID(c)) |
|
761 |
+ fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", stringid.TruncateID(c)) |
|
762 | 762 |
} |
763 | 763 |
} |
... | ... |
@@ -30,12 +30,12 @@ import ( |
30 | 30 |
"github.com/docker/docker/nat" |
31 | 31 |
"github.com/docker/docker/pkg/archive" |
32 | 32 |
"github.com/docker/docker/pkg/broadcastwriter" |
33 |
- "github.com/docker/docker/pkg/common" |
|
34 | 33 |
"github.com/docker/docker/pkg/directory" |
35 | 34 |
"github.com/docker/docker/pkg/etchosts" |
36 | 35 |
"github.com/docker/docker/pkg/ioutils" |
37 | 36 |
"github.com/docker/docker/pkg/promise" |
38 | 37 |
"github.com/docker/docker/pkg/resolvconf" |
38 |
+ "github.com/docker/docker/pkg/stringid" |
|
39 | 39 |
"github.com/docker/docker/pkg/symlink" |
40 | 40 |
"github.com/docker/docker/pkg/ulimit" |
41 | 41 |
"github.com/docker/docker/runconfig" |
... | ... |
@@ -739,7 +739,7 @@ func (container *Container) Kill() error { |
739 | 739 |
if _, err := container.WaitStop(10 * time.Second); err != nil { |
740 | 740 |
// Ensure that we don't kill ourselves |
741 | 741 |
if pid := container.GetPid(); pid != 0 { |
742 |
- log.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", common.TruncateID(container.ID)) |
|
742 |
+ log.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID)) |
|
743 | 743 |
if err := syscall.Kill(pid, 9); err != nil { |
744 | 744 |
if err != syscall.ESRCH { |
745 | 745 |
return err |
... | ... |
@@ -31,13 +31,13 @@ import ( |
31 | 31 |
"github.com/docker/docker/image" |
32 | 32 |
"github.com/docker/docker/pkg/archive" |
33 | 33 |
"github.com/docker/docker/pkg/broadcastwriter" |
34 |
- "github.com/docker/docker/pkg/common" |
|
35 | 34 |
"github.com/docker/docker/pkg/graphdb" |
36 | 35 |
"github.com/docker/docker/pkg/ioutils" |
37 | 36 |
"github.com/docker/docker/pkg/namesgenerator" |
38 | 37 |
"github.com/docker/docker/pkg/parsers" |
39 | 38 |
"github.com/docker/docker/pkg/parsers/kernel" |
40 | 39 |
"github.com/docker/docker/pkg/resolvconf" |
40 |
+ "github.com/docker/docker/pkg/stringid" |
|
41 | 41 |
"github.com/docker/docker/pkg/sysinfo" |
42 | 42 |
"github.com/docker/docker/pkg/truncindex" |
43 | 43 |
"github.com/docker/docker/runconfig" |
... | ... |
@@ -517,7 +517,7 @@ func (daemon *Daemon) mergeAndVerifyConfig(config *runconfig.Config, img *image. |
517 | 517 |
func (daemon *Daemon) generateIdAndName(name string) (string, string, error) { |
518 | 518 |
var ( |
519 | 519 |
err error |
520 |
- id = common.GenerateRandomID() |
|
520 |
+ id = stringid.GenerateRandomID() |
|
521 | 521 |
) |
522 | 522 |
|
523 | 523 |
if name == "" { |
... | ... |
@@ -562,7 +562,7 @@ func (daemon *Daemon) reserveName(id, name string) (string, error) { |
562 | 562 |
nameAsKnownByUser := strings.TrimPrefix(name, "/") |
563 | 563 |
return "", fmt.Errorf( |
564 | 564 |
"Conflict. The name %q is already in use by container %s. You have to delete (or rename) that container to be able to reuse that name.", nameAsKnownByUser, |
565 |
- common.TruncateID(conflictingContainer.ID)) |
|
565 |
+ stringid.TruncateID(conflictingContainer.ID)) |
|
566 | 566 |
} |
567 | 567 |
} |
568 | 568 |
return name, nil |
... | ... |
@@ -585,7 +585,7 @@ func (daemon *Daemon) generateNewName(id string) (string, error) { |
585 | 585 |
return name, nil |
586 | 586 |
} |
587 | 587 |
|
588 |
- name = "/" + common.TruncateID(id) |
|
588 |
+ name = "/" + stringid.TruncateID(id) |
|
589 | 589 |
if _, err := daemon.containerGraph.Set(name, id); err != nil { |
590 | 590 |
return "", err |
591 | 591 |
} |
... | ... |
@@ -12,9 +12,9 @@ import ( |
12 | 12 |
"github.com/docker/docker/daemon/execdriver/lxc" |
13 | 13 |
"github.com/docker/docker/engine" |
14 | 14 |
"github.com/docker/docker/pkg/broadcastwriter" |
15 |
- "github.com/docker/docker/pkg/common" |
|
16 | 15 |
"github.com/docker/docker/pkg/ioutils" |
17 | 16 |
"github.com/docker/docker/pkg/promise" |
17 |
+ "github.com/docker/docker/pkg/stringid" |
|
18 | 18 |
"github.com/docker/docker/runconfig" |
19 | 19 |
) |
20 | 20 |
|
... | ... |
@@ -141,7 +141,7 @@ func (d *Daemon) ContainerExecCreate(job *engine.Job) engine.Status { |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
execConfig := &execConfig{ |
144 |
- ID: common.GenerateRandomID(), |
|
144 |
+ ID: stringid.GenerateRandomID(), |
|
145 | 145 |
OpenStdin: config.AttachStdin, |
146 | 146 |
OpenStdout: config.AttachStdout, |
147 | 147 |
OpenStderr: config.AttachStderr, |
... | ... |
@@ -34,9 +34,9 @@ import ( |
34 | 34 |
"github.com/docker/docker/daemon/graphdriver" |
35 | 35 |
"github.com/docker/docker/pkg/archive" |
36 | 36 |
"github.com/docker/docker/pkg/chrootarchive" |
37 |
- "github.com/docker/docker/pkg/common" |
|
38 | 37 |
"github.com/docker/docker/pkg/directory" |
39 | 38 |
mountpk "github.com/docker/docker/pkg/mount" |
39 |
+ "github.com/docker/docker/pkg/stringid" |
|
40 | 40 |
"github.com/docker/libcontainer/label" |
41 | 41 |
) |
42 | 42 |
|
... | ... |
@@ -405,7 +405,7 @@ func (a *Driver) Cleanup() error { |
405 | 405 |
|
406 | 406 |
for _, id := range ids { |
407 | 407 |
if err := a.unmount(id); err != nil { |
408 |
- log.Errorf("Unmounting %s: %s", common.TruncateID(id), err) |
|
408 |
+ log.Errorf("Unmounting %s: %s", stringid.TruncateID(id), err) |
|
409 | 409 |
} |
410 | 410 |
} |
411 | 411 |
|
... | ... |
@@ -7,8 +7,8 @@ import ( |
7 | 7 |
"github.com/docker/docker/engine" |
8 | 8 |
"github.com/docker/docker/graph" |
9 | 9 |
"github.com/docker/docker/image" |
10 |
- "github.com/docker/docker/pkg/common" |
|
11 | 10 |
"github.com/docker/docker/pkg/parsers" |
11 |
+ "github.com/docker/docker/pkg/stringid" |
|
12 | 12 |
"github.com/docker/docker/utils" |
13 | 13 |
) |
14 | 14 |
|
... | ... |
@@ -148,11 +148,11 @@ func (daemon *Daemon) canDeleteImage(imgID string, force bool) error { |
148 | 148 |
if imgID == p.ID { |
149 | 149 |
if container.IsRunning() { |
150 | 150 |
if force { |
151 |
- return fmt.Errorf("Conflict, cannot force delete %s because the running container %s is using it, stop it and retry", common.TruncateID(imgID), common.TruncateID(container.ID)) |
|
151 |
+ return fmt.Errorf("Conflict, cannot force delete %s because the running container %s is using it, stop it and retry", stringid.TruncateID(imgID), stringid.TruncateID(container.ID)) |
|
152 | 152 |
} |
153 |
- return fmt.Errorf("Conflict, cannot delete %s because the running container %s is using it, stop it and use -f to force", common.TruncateID(imgID), common.TruncateID(container.ID)) |
|
153 |
+ return fmt.Errorf("Conflict, cannot delete %s because the running container %s is using it, stop it and use -f to force", stringid.TruncateID(imgID), stringid.TruncateID(container.ID)) |
|
154 | 154 |
} else if !force { |
155 |
- return fmt.Errorf("Conflict, cannot delete %s because the container %s is using it, use -f to force", common.TruncateID(imgID), common.TruncateID(container.ID)) |
|
155 |
+ return fmt.Errorf("Conflict, cannot delete %s because the container %s is using it, use -f to force", stringid.TruncateID(imgID), stringid.TruncateID(container.ID)) |
|
156 | 156 |
} |
157 | 157 |
} |
158 | 158 |
return nil |
... | ... |
@@ -8,7 +8,7 @@ import ( |
8 | 8 |
|
9 | 9 |
log "github.com/Sirupsen/logrus" |
10 | 10 |
"github.com/docker/docker/daemon/execdriver" |
11 |
- "github.com/docker/docker/pkg/common" |
|
11 |
+ "github.com/docker/docker/pkg/stringid" |
|
12 | 12 |
"github.com/docker/docker/runconfig" |
13 | 13 |
) |
14 | 14 |
|
... | ... |
@@ -230,7 +230,7 @@ func (m *containerMonitor) shouldRestart(exitCode int) bool { |
230 | 230 |
// the default value of 0 for MaximumRetryCount means that we will not enforce a maximum count |
231 | 231 |
if max := m.restartPolicy.MaximumRetryCount; max != 0 && m.failureCount > max { |
232 | 232 |
log.Debugf("stopping restart of container %s because maximum failure could of %d has been reached", |
233 |
- common.TruncateID(m.container.ID), max) |
|
233 |
+ stringid.TruncateID(m.container.ID), max) |
|
234 | 234 |
return false |
235 | 235 |
} |
236 | 236 |
|
... | ... |
@@ -10,8 +10,8 @@ import ( |
10 | 10 |
"sync" |
11 | 11 |
"time" |
12 | 12 |
|
13 |
- "github.com/docker/docker/pkg/common" |
|
14 | 13 |
"github.com/docker/docker/pkg/ioutils" |
14 |
+ "github.com/docker/docker/pkg/stringutils" |
|
15 | 15 |
) |
16 | 16 |
|
17 | 17 |
// Installer is a standard interface for objects which can "install" themselves |
... | ... |
@@ -78,7 +78,7 @@ func (eng *Engine) RegisterCatchall(catchall Handler) { |
78 | 78 |
func New() *Engine { |
79 | 79 |
eng := &Engine{ |
80 | 80 |
handlers: make(map[string]Handler), |
81 |
- id: common.RandomString(), |
|
81 |
+ id: stringutils.GenerateRandomString(), |
|
82 | 82 |
Stdout: os.Stdout, |
83 | 83 |
Stderr: os.Stderr, |
84 | 84 |
Stdin: os.Stdin, |
... | ... |
@@ -3,23 +3,11 @@ package engine |
3 | 3 |
import ( |
4 | 4 |
"bytes" |
5 | 5 |
"encoding/json" |
6 |
- "math/rand" |
|
7 | 6 |
"testing" |
8 | 7 |
"time" |
9 |
-) |
|
10 |
- |
|
11 |
-const chars = "abcdefghijklmnopqrstuvwxyz" + |
|
12 |
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + |
|
13 |
- "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` " |
|
14 | 8 |
|
15 |
-// RandomString returns random string of specified length |
|
16 |
-func RandomString(length int) string { |
|
17 |
- res := make([]byte, length) |
|
18 |
- for i := 0; i < length; i++ { |
|
19 |
- res[i] = chars[rand.Intn(len(chars))] |
|
20 |
- } |
|
21 |
- return string(res) |
|
22 |
-} |
|
9 |
+ "github.com/docker/docker/pkg/stringutils" |
|
10 |
+) |
|
23 | 11 |
|
24 | 12 |
func TestEnvLenZero(t *testing.T) { |
25 | 13 |
env := &Env{} |
... | ... |
@@ -197,7 +185,7 @@ func TestMultiMap(t *testing.T) { |
197 | 197 |
func testMap(l int) [][2]string { |
198 | 198 |
res := make([][2]string, l) |
199 | 199 |
for i := 0; i < l; i++ { |
200 |
- t := [2]string{RandomString(5), RandomString(20)} |
|
200 |
+ t := [2]string{stringutils.GenerateRandomAsciiString(5), stringutils.GenerateRandomAsciiString(20)} |
|
201 | 201 |
res[i] = t |
202 | 202 |
} |
203 | 203 |
return res |
... | ... |
@@ -17,8 +17,8 @@ import ( |
17 | 17 |
"github.com/docker/docker/daemon/graphdriver" |
18 | 18 |
"github.com/docker/docker/image" |
19 | 19 |
"github.com/docker/docker/pkg/archive" |
20 |
- "github.com/docker/docker/pkg/common" |
|
21 | 20 |
"github.com/docker/docker/pkg/progressreader" |
21 |
+ "github.com/docker/docker/pkg/stringid" |
|
22 | 22 |
"github.com/docker/docker/pkg/truncindex" |
23 | 23 |
"github.com/docker/docker/runconfig" |
24 | 24 |
"github.com/docker/docker/utils" |
... | ... |
@@ -118,7 +118,7 @@ func (graph *Graph) Get(name string) (*image.Image, error) { |
118 | 118 |
// Create creates a new image and registers it in the graph. |
119 | 119 |
func (graph *Graph) Create(layerData archive.ArchiveReader, containerID, containerImage, comment, author string, containerConfig, config *runconfig.Config) (*image.Image, error) { |
120 | 120 |
img := &image.Image{ |
121 |
- ID: common.GenerateRandomID(), |
|
121 |
+ ID: stringid.GenerateRandomID(), |
|
122 | 122 |
Comment: comment, |
123 | 123 |
Created: time.Now().UTC(), |
124 | 124 |
DockerVersion: dockerversion.VERSION, |
... | ... |
@@ -217,7 +217,7 @@ func (graph *Graph) TempLayerArchive(id string, sf *utils.StreamFormatter, outpu |
217 | 217 |
Formatter: sf, |
218 | 218 |
Size: 0, |
219 | 219 |
NewLines: false, |
220 |
- ID: common.TruncateID(id), |
|
220 |
+ ID: stringid.TruncateID(id), |
|
221 | 221 |
Action: "Buffering to disk", |
222 | 222 |
}) |
223 | 223 |
defer progressReader.Close() |
... | ... |
@@ -226,7 +226,7 @@ func (graph *Graph) TempLayerArchive(id string, sf *utils.StreamFormatter, outpu |
226 | 226 |
|
227 | 227 |
// Mktemp creates a temporary sub-directory inside the graph's filesystem. |
228 | 228 |
func (graph *Graph) Mktemp(id string) (string, error) { |
229 |
- dir := path.Join(graph.Root, "_tmp", common.GenerateRandomID()) |
|
229 |
+ dir := path.Join(graph.Root, "_tmp", stringid.GenerateRandomID()) |
|
230 | 230 |
if err := os.MkdirAll(dir, 0700); err != nil { |
231 | 231 |
return "", err |
232 | 232 |
} |
... | ... |
@@ -14,8 +14,8 @@ import ( |
14 | 14 |
"github.com/docker/distribution/digest" |
15 | 15 |
"github.com/docker/docker/engine" |
16 | 16 |
"github.com/docker/docker/image" |
17 |
- "github.com/docker/docker/pkg/common" |
|
18 | 17 |
"github.com/docker/docker/pkg/progressreader" |
18 |
+ "github.com/docker/docker/pkg/stringid" |
|
19 | 19 |
"github.com/docker/docker/registry" |
20 | 20 |
"github.com/docker/docker/utils" |
21 | 21 |
) |
... | ... |
@@ -172,9 +172,9 @@ func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo * |
172 | 172 |
// ensure no two downloads of the same image happen at the same time |
173 | 173 |
if c, err := s.poolAdd("pull", "img:"+img.ID); err != nil { |
174 | 174 |
if c != nil { |
175 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Layer already being pulled by another client. Waiting.", nil)) |
|
175 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Layer already being pulled by another client. Waiting.", nil)) |
|
176 | 176 |
<-c |
177 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) |
|
177 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Download complete", nil)) |
|
178 | 178 |
} else { |
179 | 179 |
log.Debugf("Image (id: %s) pull is already running, skipping: %v", img.ID, err) |
180 | 180 |
} |
... | ... |
@@ -185,12 +185,12 @@ func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo * |
185 | 185 |
} |
186 | 186 |
defer s.poolRemove("pull", "img:"+img.ID) |
187 | 187 |
|
188 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s", img.Tag, repoInfo.CanonicalName), nil)) |
|
188 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s", img.Tag, repoInfo.CanonicalName), nil)) |
|
189 | 189 |
success := false |
190 | 190 |
var lastErr, err error |
191 | 191 |
var is_downloaded bool |
192 | 192 |
for _, ep := range repoInfo.Index.Mirrors { |
193 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, mirror: %s", img.Tag, repoInfo.CanonicalName, ep), nil)) |
|
193 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, mirror: %s", img.Tag, repoInfo.CanonicalName, ep), nil)) |
|
194 | 194 |
if is_downloaded, err = s.pullImage(r, out, img.ID, ep, repoData.Tokens, sf); err != nil { |
195 | 195 |
// Don't report errors when pulling from mirrors. |
196 | 196 |
log.Debugf("Error pulling image (%s) from %s, mirror: %s, %s", img.Tag, repoInfo.CanonicalName, ep, err) |
... | ... |
@@ -202,12 +202,12 @@ func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo * |
202 | 202 |
} |
203 | 203 |
if !success { |
204 | 204 |
for _, ep := range repoData.Endpoints { |
205 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, endpoint: %s", img.Tag, repoInfo.CanonicalName, ep), nil)) |
|
205 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), fmt.Sprintf("Pulling image (%s) from %s, endpoint: %s", img.Tag, repoInfo.CanonicalName, ep), nil)) |
|
206 | 206 |
if is_downloaded, err = s.pullImage(r, out, img.ID, ep, repoData.Tokens, sf); err != nil { |
207 | 207 |
// It's not ideal that only the last error is returned, it would be better to concatenate the errors. |
208 | 208 |
// As the error is also given to the output stream the user will see the error. |
209 | 209 |
lastErr = err |
210 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), fmt.Sprintf("Error pulling image (%s) from %s, endpoint: %s, %s", img.Tag, repoInfo.CanonicalName, ep, err), nil)) |
|
210 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), fmt.Sprintf("Error pulling image (%s) from %s, endpoint: %s, %s", img.Tag, repoInfo.CanonicalName, ep, err), nil)) |
|
211 | 211 |
continue |
212 | 212 |
} |
213 | 213 |
layers_downloaded = layers_downloaded || is_downloaded |
... | ... |
@@ -217,13 +217,13 @@ func (s *TagStore) pullRepository(r *registry.Session, out io.Writer, repoInfo * |
217 | 217 |
} |
218 | 218 |
if !success { |
219 | 219 |
err := fmt.Errorf("Error pulling image (%s) from %s, %v", img.Tag, repoInfo.CanonicalName, lastErr) |
220 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), err.Error(), nil)) |
|
220 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), err.Error(), nil)) |
|
221 | 221 |
if parallel { |
222 | 222 |
errors <- err |
223 | 223 |
return |
224 | 224 |
} |
225 | 225 |
} |
226 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) |
|
226 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Download complete", nil)) |
|
227 | 227 |
|
228 | 228 |
if parallel { |
229 | 229 |
errors <- nil |
... | ... |
@@ -270,7 +270,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
270 | 270 |
if err != nil { |
271 | 271 |
return false, err |
272 | 272 |
} |
273 |
- out.Write(sf.FormatProgress(common.TruncateID(imgID), "Pulling dependent layers", nil)) |
|
273 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(imgID), "Pulling dependent layers", nil)) |
|
274 | 274 |
// FIXME: Try to stream the images? |
275 | 275 |
// FIXME: Launch the getRemoteImage() in goroutines |
276 | 276 |
|
... | ... |
@@ -286,7 +286,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
286 | 286 |
defer s.poolRemove("pull", "layer:"+id) |
287 | 287 |
|
288 | 288 |
if !s.graph.Exists(id) { |
289 |
- out.Write(sf.FormatProgress(common.TruncateID(id), "Pulling metadata", nil)) |
|
289 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), "Pulling metadata", nil)) |
|
290 | 290 |
var ( |
291 | 291 |
imgJSON []byte |
292 | 292 |
imgSize int |
... | ... |
@@ -297,7 +297,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
297 | 297 |
for j := 1; j <= retries; j++ { |
298 | 298 |
imgJSON, imgSize, err = r.GetRemoteImageJSON(id, endpoint, token) |
299 | 299 |
if err != nil && j == retries { |
300 |
- out.Write(sf.FormatProgress(common.TruncateID(id), "Error pulling dependent layers", nil)) |
|
300 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), "Error pulling dependent layers", nil)) |
|
301 | 301 |
return layers_downloaded, err |
302 | 302 |
} else if err != nil { |
303 | 303 |
time.Sleep(time.Duration(j) * 500 * time.Millisecond) |
... | ... |
@@ -306,7 +306,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
306 | 306 |
img, err = image.NewImgJSON(imgJSON) |
307 | 307 |
layers_downloaded = true |
308 | 308 |
if err != nil && j == retries { |
309 |
- out.Write(sf.FormatProgress(common.TruncateID(id), "Error pulling dependent layers", nil)) |
|
309 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), "Error pulling dependent layers", nil)) |
|
310 | 310 |
return layers_downloaded, fmt.Errorf("Failed to parse json: %s", err) |
311 | 311 |
} else if err != nil { |
312 | 312 |
time.Sleep(time.Duration(j) * 500 * time.Millisecond) |
... | ... |
@@ -322,7 +322,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
322 | 322 |
if j > 1 { |
323 | 323 |
status = fmt.Sprintf("Pulling fs layer [retries: %d]", j) |
324 | 324 |
} |
325 |
- out.Write(sf.FormatProgress(common.TruncateID(id), status, nil)) |
|
325 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), status, nil)) |
|
326 | 326 |
layer, err := r.GetRemoteImageLayer(img.ID, endpoint, token, int64(imgSize)) |
327 | 327 |
if uerr, ok := err.(*url.Error); ok { |
328 | 328 |
err = uerr.Err |
... | ... |
@@ -331,7 +331,7 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
331 | 331 |
time.Sleep(time.Duration(j) * 500 * time.Millisecond) |
332 | 332 |
continue |
333 | 333 |
} else if err != nil { |
334 |
- out.Write(sf.FormatProgress(common.TruncateID(id), "Error pulling dependent layers", nil)) |
|
334 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), "Error pulling dependent layers", nil)) |
|
335 | 335 |
return layers_downloaded, err |
336 | 336 |
} |
337 | 337 |
layers_downloaded = true |
... | ... |
@@ -344,21 +344,21 @@ func (s *TagStore) pullImage(r *registry.Session, out io.Writer, imgID, endpoint |
344 | 344 |
Formatter: sf, |
345 | 345 |
Size: imgSize, |
346 | 346 |
NewLines: false, |
347 |
- ID: common.TruncateID(id), |
|
347 |
+ ID: stringid.TruncateID(id), |
|
348 | 348 |
Action: "Downloading", |
349 | 349 |
})) |
350 | 350 |
if terr, ok := err.(net.Error); ok && terr.Timeout() && j < retries { |
351 | 351 |
time.Sleep(time.Duration(j) * 500 * time.Millisecond) |
352 | 352 |
continue |
353 | 353 |
} else if err != nil { |
354 |
- out.Write(sf.FormatProgress(common.TruncateID(id), "Error downloading dependent layers", nil)) |
|
354 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), "Error downloading dependent layers", nil)) |
|
355 | 355 |
return layers_downloaded, err |
356 | 356 |
} else { |
357 | 357 |
break |
358 | 358 |
} |
359 | 359 |
} |
360 | 360 |
} |
361 |
- out.Write(sf.FormatProgress(common.TruncateID(id), "Download complete", nil)) |
|
361 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(id), "Download complete", nil)) |
|
362 | 362 |
} |
363 | 363 |
return layers_downloaded, nil |
364 | 364 |
} |
... | ... |
@@ -478,16 +478,16 @@ func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Wri |
478 | 478 |
} |
479 | 479 |
downloads[i].digest = dgst |
480 | 480 |
|
481 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Pulling fs layer", nil)) |
|
481 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Pulling fs layer", nil)) |
|
482 | 482 |
|
483 | 483 |
downloadFunc := func(di *downloadInfo) error { |
484 | 484 |
log.Debugf("pulling blob %q to V1 img %s", sumStr, img.ID) |
485 | 485 |
|
486 | 486 |
if c, err := s.poolAdd("pull", "img:"+img.ID); err != nil { |
487 | 487 |
if c != nil { |
488 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Layer already being pulled by another client. Waiting.", nil)) |
|
488 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Layer already being pulled by another client. Waiting.", nil)) |
|
489 | 489 |
<-c |
490 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) |
|
490 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Download complete", nil)) |
|
491 | 491 |
} else { |
492 | 492 |
log.Debugf("Image (id: %s) pull is already running, skipping: %v", img.ID, err) |
493 | 493 |
} |
... | ... |
@@ -515,20 +515,20 @@ func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Wri |
515 | 515 |
Formatter: sf, |
516 | 516 |
Size: int(l), |
517 | 517 |
NewLines: false, |
518 |
- ID: common.TruncateID(img.ID), |
|
518 |
+ ID: stringid.TruncateID(img.ID), |
|
519 | 519 |
Action: "Downloading", |
520 | 520 |
})); err != nil { |
521 | 521 |
return fmt.Errorf("unable to copy v2 image blob data: %s", err) |
522 | 522 |
} |
523 | 523 |
|
524 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Verifying Checksum", nil)) |
|
524 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Verifying Checksum", nil)) |
|
525 | 525 |
|
526 | 526 |
if !verifier.Verified() { |
527 | 527 |
log.Infof("Image verification failed: checksum mismatch for %q", di.digest.String()) |
528 | 528 |
verified = false |
529 | 529 |
} |
530 | 530 |
|
531 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Download complete", nil)) |
|
531 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Download complete", nil)) |
|
532 | 532 |
|
533 | 533 |
log.Debugf("Downloaded %s to tempfile %s", img.ID, tmpFile.Name()) |
534 | 534 |
di.tmpFile = tmpFile |
... | ... |
@@ -574,7 +574,7 @@ func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Wri |
574 | 574 |
Out: out, |
575 | 575 |
Formatter: sf, |
576 | 576 |
Size: int(d.length), |
577 |
- ID: common.TruncateID(d.img.ID), |
|
577 |
+ ID: stringid.TruncateID(d.img.ID), |
|
578 | 578 |
Action: "Extracting", |
579 | 579 |
})) |
580 | 580 |
if err != nil { |
... | ... |
@@ -583,10 +583,10 @@ func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Wri |
583 | 583 |
|
584 | 584 |
// FIXME: Pool release here for parallel tag pull (ensures any downloads block until fully extracted) |
585 | 585 |
} |
586 |
- out.Write(sf.FormatProgress(common.TruncateID(d.img.ID), "Pull complete", nil)) |
|
586 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(d.img.ID), "Pull complete", nil)) |
|
587 | 587 |
tagUpdated = true |
588 | 588 |
} else { |
589 |
- out.Write(sf.FormatProgress(common.TruncateID(d.img.ID), "Already exists", nil)) |
|
589 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(d.img.ID), "Already exists", nil)) |
|
590 | 590 |
} |
591 | 591 |
|
592 | 592 |
} |
... | ... |
@@ -16,8 +16,8 @@ import ( |
16 | 16 |
"github.com/docker/distribution/digest" |
17 | 17 |
"github.com/docker/docker/engine" |
18 | 18 |
"github.com/docker/docker/image" |
19 |
- "github.com/docker/docker/pkg/common" |
|
20 | 19 |
"github.com/docker/docker/pkg/progressreader" |
20 |
+ "github.com/docker/docker/pkg/stringid" |
|
21 | 21 |
"github.com/docker/docker/registry" |
22 | 22 |
"github.com/docker/docker/runconfig" |
23 | 23 |
"github.com/docker/docker/utils" |
... | ... |
@@ -139,7 +139,7 @@ func lookupImageOnEndpoint(wg *sync.WaitGroup, r *registry.Session, out io.Write |
139 | 139 |
imagesToPush <- image.id |
140 | 140 |
continue |
141 | 141 |
} |
142 |
- out.Write(sf.FormatStatus("", "Image %s already pushed, skipping", common.TruncateID(image.id))) |
|
142 |
+ out.Write(sf.FormatStatus("", "Image %s already pushed, skipping", stringid.TruncateID(image.id))) |
|
143 | 143 |
} |
144 | 144 |
} |
145 | 145 |
|
... | ... |
@@ -191,7 +191,7 @@ func (s *TagStore) pushImageToEndpoint(endpoint string, out io.Writer, remoteNam |
191 | 191 |
} |
192 | 192 |
} |
193 | 193 |
for _, tag := range tags[id] { |
194 |
- out.Write(sf.FormatStatus("", "Pushing tag for rev [%s] on {%s}", common.TruncateID(id), endpoint+"repositories/"+remoteName+"/tags/"+tag)) |
|
194 |
+ out.Write(sf.FormatStatus("", "Pushing tag for rev [%s] on {%s}", stringid.TruncateID(id), endpoint+"repositories/"+remoteName+"/tags/"+tag)) |
|
195 | 195 |
if err := r.PushRegistryTag(remoteName, id, tag, endpoint, repo.Tokens); err != nil { |
196 | 196 |
return err |
197 | 197 |
} |
... | ... |
@@ -244,7 +244,7 @@ func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep strin |
244 | 244 |
if err != nil { |
245 | 245 |
return "", fmt.Errorf("Cannot retrieve the path for {%s}: %s", imgID, err) |
246 | 246 |
} |
247 |
- out.Write(sf.FormatProgress(common.TruncateID(imgID), "Pushing", nil)) |
|
247 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(imgID), "Pushing", nil)) |
|
248 | 248 |
|
249 | 249 |
imgData := ®istry.ImgData{ |
250 | 250 |
ID: imgID, |
... | ... |
@@ -253,7 +253,7 @@ func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep strin |
253 | 253 |
// Send the json |
254 | 254 |
if err := r.PushImageJSONRegistry(imgData, jsonRaw, ep, token); err != nil { |
255 | 255 |
if err == registry.ErrAlreadyExists { |
256 |
- out.Write(sf.FormatProgress(common.TruncateID(imgData.ID), "Image already pushed, skipping", nil)) |
|
256 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(imgData.ID), "Image already pushed, skipping", nil)) |
|
257 | 257 |
return "", nil |
258 | 258 |
} |
259 | 259 |
return "", err |
... | ... |
@@ -275,7 +275,7 @@ func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep strin |
275 | 275 |
Formatter: sf, |
276 | 276 |
Size: int(layerData.Size), |
277 | 277 |
NewLines: false, |
278 |
- ID: common.TruncateID(imgData.ID), |
|
278 |
+ ID: stringid.TruncateID(imgData.ID), |
|
279 | 279 |
Action: "Pushing", |
280 | 280 |
}), ep, token, jsonRaw) |
281 | 281 |
if err != nil { |
... | ... |
@@ -288,7 +288,7 @@ func (s *TagStore) pushImage(r *registry.Session, out io.Writer, imgID, ep strin |
288 | 288 |
return "", err |
289 | 289 |
} |
290 | 290 |
|
291 |
- out.Write(sf.FormatProgress(common.TruncateID(imgData.ID), "Image successfully pushed", nil)) |
|
291 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(imgData.ID), "Image successfully pushed", nil)) |
|
292 | 292 |
return imgData.Checksum, nil |
293 | 293 |
} |
294 | 294 |
|
... | ... |
@@ -385,7 +385,7 @@ func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, o |
385 | 385 |
// Call mount blob |
386 | 386 |
exists, err = r.HeadV2ImageBlob(endpoint, repoInfo.RemoteName, sumParts[0], sumParts[1], auth) |
387 | 387 |
if err != nil { |
388 |
- out.Write(sf.FormatProgress(common.TruncateID(layer.ID), "Image push failed", nil)) |
|
388 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(layer.ID), "Image push failed", nil)) |
|
389 | 389 |
return err |
390 | 390 |
} |
391 | 391 |
} |
... | ... |
@@ -400,7 +400,7 @@ func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, o |
400 | 400 |
checksum = cs |
401 | 401 |
} |
402 | 402 |
} else { |
403 |
- out.Write(sf.FormatProgress(common.TruncateID(layer.ID), "Image already exists", nil)) |
|
403 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(layer.ID), "Image already exists", nil)) |
|
404 | 404 |
} |
405 | 405 |
m.FSLayers[i] = ®istry.FSLayer{BlobSum: checksum} |
406 | 406 |
m.History[i] = ®istry.ManifestHistory{V1Compatibility: string(jsonData)} |
... | ... |
@@ -443,7 +443,7 @@ func (s *TagStore) pushV2Repository(r *registry.Session, localRepo Repository, o |
443 | 443 |
|
444 | 444 |
// PushV2Image pushes the image content to the v2 registry, first buffering the contents to disk |
445 | 445 |
func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint *registry.Endpoint, imageName string, sf *utils.StreamFormatter, out io.Writer, auth *registry.RequestAuthorization) (string, error) { |
446 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Buffering to Disk", nil)) |
|
446 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Buffering to Disk", nil)) |
|
447 | 447 |
|
448 | 448 |
image, err := s.graph.Get(img.ID) |
449 | 449 |
if err != nil { |
... | ... |
@@ -481,13 +481,13 @@ func (s *TagStore) pushV2Image(r *registry.Session, img *image.Image, endpoint * |
481 | 481 |
Formatter: sf, |
482 | 482 |
Size: int(size), |
483 | 483 |
NewLines: false, |
484 |
- ID: common.TruncateID(img.ID), |
|
484 |
+ ID: stringid.TruncateID(img.ID), |
|
485 | 485 |
Action: "Pushing", |
486 | 486 |
}), auth); err != nil { |
487 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Image push failed", nil)) |
|
487 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Image push failed", nil)) |
|
488 | 488 |
return "", err |
489 | 489 |
} |
490 |
- out.Write(sf.FormatProgress(common.TruncateID(img.ID), "Image successfully pushed", nil)) |
|
490 |
+ out.Write(sf.FormatProgress(stringid.TruncateID(img.ID), "Image successfully pushed", nil)) |
|
491 | 491 |
return dgst.String(), nil |
492 | 492 |
} |
493 | 493 |
|
... | ... |
@@ -13,8 +13,8 @@ import ( |
13 | 13 |
"sync" |
14 | 14 |
|
15 | 15 |
"github.com/docker/docker/image" |
16 |
- "github.com/docker/docker/pkg/common" |
|
17 | 16 |
"github.com/docker/docker/pkg/parsers" |
17 |
+ "github.com/docker/docker/pkg/stringid" |
|
18 | 18 |
"github.com/docker/docker/registry" |
19 | 19 |
"github.com/docker/docker/utils" |
20 | 20 |
"github.com/docker/libtrust" |
... | ... |
@@ -163,7 +163,7 @@ func (store *TagStore) ImageName(id string) string { |
163 | 163 |
if names, exists := store.ByID()[id]; exists && len(names) > 0 { |
164 | 164 |
return names[0] |
165 | 165 |
} |
166 |
- return common.TruncateID(id) |
|
166 |
+ return stringid.TruncateID(id) |
|
167 | 167 |
} |
168 | 168 |
|
169 | 169 |
func (store *TagStore) DeleteAll(id string) error { |
... | ... |
@@ -331,7 +331,7 @@ func (store *TagStore) GetRepoRefs() map[string][]string { |
331 | 331 |
|
332 | 332 |
for name, repository := range store.Repositories { |
333 | 333 |
for tag, id := range repository { |
334 |
- shortID := common.TruncateID(id) |
|
334 |
+ shortID := stringid.TruncateID(id) |
|
335 | 335 |
reporefs[shortID] = append(reporefs[shortID], utils.ImageReference(name, tag)) |
336 | 336 |
} |
337 | 337 |
} |
... | ... |
@@ -22,6 +22,7 @@ import ( |
22 | 22 |
|
23 | 23 |
"github.com/docker/docker/builder/command" |
24 | 24 |
"github.com/docker/docker/pkg/archive" |
25 |
+ "github.com/docker/docker/pkg/stringutils" |
|
25 | 26 |
) |
26 | 27 |
|
27 | 28 |
func TestBuildJSONEmptyRun(t *testing.T) { |
... | ... |
@@ -4420,7 +4421,7 @@ func TestBuildOnBuildOutput(t *testing.T) { |
4420 | 4420 |
} |
4421 | 4421 |
|
4422 | 4422 |
func TestBuildInvalidTag(t *testing.T) { |
4423 |
- name := "abcd:" + makeRandomString(200) |
|
4423 |
+ name := "abcd:" + stringutils.GenerateRandomAlphaOnlyString(200) |
|
4424 | 4424 |
defer deleteImages(name) |
4425 | 4425 |
_, out, err := buildImageWithOut(name, "FROM scratch\nMAINTAINER quux\n", true) |
4426 | 4426 |
// if the error doesnt check for illegal tag name, or the image is built |
... | ... |
@@ -9,7 +9,7 @@ import ( |
9 | 9 |
"testing" |
10 | 10 |
"time" |
11 | 11 |
|
12 |
- "github.com/docker/docker/pkg/common" |
|
12 |
+ "github.com/docker/docker/pkg/stringid" |
|
13 | 13 |
) |
14 | 14 |
|
15 | 15 |
func TestImagesEnsureImageIsListed(t *testing.T) { |
... | ... |
@@ -196,7 +196,7 @@ func TestImagesEnsureDanglingImageOnlyListedOnce(t *testing.T) { |
196 | 196 |
if err != nil { |
197 | 197 |
t.Fatalf("error tagging foobox: %s", err) |
198 | 198 |
} |
199 |
- imageId := common.TruncateID(strings.TrimSpace(out)) |
|
199 |
+ imageId := stringid.TruncateID(strings.TrimSpace(out)) |
|
200 | 200 |
defer deleteImages(imageId) |
201 | 201 |
|
202 | 202 |
// overwrite the tag, making the previous image dangling |
... | ... |
@@ -5,6 +5,8 @@ import ( |
5 | 5 |
"os/exec" |
6 | 6 |
"strings" |
7 | 7 |
"testing" |
8 |
+ |
|
9 |
+ "github.com/docker/docker/pkg/stringutils" |
|
8 | 10 |
) |
9 | 11 |
|
10 | 12 |
// tagging a named image in a new unprefixed repo should work |
... | ... |
@@ -59,7 +61,7 @@ func TestTagInvalidUnprefixedRepo(t *testing.T) { |
59 | 59 |
|
60 | 60 |
// ensure we don't allow the use of invalid tags; these tag operations should fail |
61 | 61 |
func TestTagInvalidPrefixedRepo(t *testing.T) { |
62 |
- long_tag := makeRandomString(121) |
|
62 |
+ long_tag := stringutils.GenerateRandomAlphaOnlyString(121) |
|
63 | 63 |
|
64 | 64 |
invalidTags := []string{"repo:fo$z$", "repo:Foo@3cc", "repo:Foo$3", "repo:Foo*3", "repo:Fo^3", "repo:Foo!3", "repo:%goodbye", "repo:#hashtagit", "repo:F)xcz(", "repo:-foo", "repo:..", long_tag} |
65 | 65 |
|
... | ... |
@@ -22,6 +22,7 @@ import ( |
22 | 22 |
"time" |
23 | 23 |
|
24 | 24 |
"github.com/docker/docker/api" |
25 |
+ "github.com/docker/docker/pkg/stringutils" |
|
25 | 26 |
) |
26 | 27 |
|
27 | 28 |
// Daemon represents a Docker daemon for the testing framework. |
... | ... |
@@ -695,8 +696,8 @@ func (f *remoteFileServer) Close() error { |
695 | 695 |
|
696 | 696 |
func newRemoteFileServer(ctx *FakeContext) (*remoteFileServer, error) { |
697 | 697 |
var ( |
698 |
- image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(makeRandomString(10))) |
|
699 |
- container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(makeRandomString(10))) |
|
698 |
+ image = fmt.Sprintf("fileserver-img-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10))) |
|
699 |
+ container = fmt.Sprintf("fileserver-cnt-%s", strings.ToLower(stringutils.GenerateRandomAlphaOnlyString(10))) |
|
700 | 700 |
) |
701 | 701 |
|
702 | 702 |
// Build the image |
... | ... |
@@ -6,7 +6,6 @@ import ( |
6 | 6 |
"errors" |
7 | 7 |
"fmt" |
8 | 8 |
"io" |
9 |
- "math/rand" |
|
10 | 9 |
"net/http" |
11 | 10 |
"net/http/httptest" |
12 | 11 |
"os" |
... | ... |
@@ -17,6 +16,7 @@ import ( |
17 | 17 |
"syscall" |
18 | 18 |
"time" |
19 | 19 |
|
20 |
+ "github.com/docker/docker/pkg/stringutils" |
|
20 | 21 |
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" |
21 | 22 |
) |
22 | 23 |
|
... | ... |
@@ -301,21 +301,10 @@ func copyWithCP(source, target string) error { |
301 | 301 |
return nil |
302 | 302 |
} |
303 | 303 |
|
304 |
-func makeRandomString(n int) string { |
|
305 |
- // make a really long string |
|
306 |
- letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
|
307 |
- b := make([]byte, n) |
|
308 |
- r := rand.New(rand.NewSource(time.Now().UTC().UnixNano())) |
|
309 |
- for i := range b { |
|
310 |
- b[i] = letters[r.Intn(len(letters))] |
|
311 |
- } |
|
312 |
- return string(b) |
|
313 |
-} |
|
314 |
- |
|
315 | 304 |
// randomUnixTmpDirPath provides a temporary unix path with rand string appended. |
316 | 305 |
// does not create or checks if it exists. |
317 | 306 |
func randomUnixTmpDirPath(s string) string { |
318 |
- return path.Join("/tmp", fmt.Sprintf("%s.%s", s, makeRandomString(10))) |
|
307 |
+ return path.Join("/tmp", fmt.Sprintf("%s.%s", s, stringutils.GenerateRandomAlphaOnlyString(10))) |
|
319 | 308 |
} |
320 | 309 |
|
321 | 310 |
// Reads chunkSize bytes from reader after every interval. |
... | ... |
@@ -12,7 +12,7 @@ import ( |
12 | 12 |
log "github.com/Sirupsen/logrus" |
13 | 13 |
"github.com/docker/docker/api/client" |
14 | 14 |
"github.com/docker/docker/daemon" |
15 |
- "github.com/docker/docker/pkg/common" |
|
15 |
+ "github.com/docker/docker/pkg/stringid" |
|
16 | 16 |
"github.com/docker/docker/pkg/term" |
17 | 17 |
"github.com/kr/pty" |
18 | 18 |
) |
... | ... |
@@ -286,7 +286,7 @@ func TestAttachDetachTruncatedID(t *testing.T) { |
286 | 286 |
ch := make(chan struct{}) |
287 | 287 |
go func() { |
288 | 288 |
defer close(ch) |
289 |
- if err := cli.CmdAttach(common.TruncateID(container.ID)); err != nil { |
|
289 |
+ if err := cli.CmdAttach(stringid.TruncateID(container.ID)); err != nil { |
|
290 | 290 |
if err != io.ErrClosedPipe { |
291 | 291 |
t.Fatal(err) |
292 | 292 |
} |
... | ... |
@@ -2,19 +2,20 @@ package docker |
2 | 2 |
|
3 | 3 |
import ( |
4 | 4 |
"errors" |
5 |
- "github.com/docker/docker/autogen/dockerversion" |
|
6 |
- "github.com/docker/docker/daemon/graphdriver" |
|
7 |
- "github.com/docker/docker/graph" |
|
8 |
- "github.com/docker/docker/image" |
|
9 |
- "github.com/docker/docker/pkg/archive" |
|
10 |
- "github.com/docker/docker/pkg/common" |
|
11 |
- "github.com/docker/docker/utils" |
|
12 | 5 |
"io" |
13 | 6 |
"io/ioutil" |
14 | 7 |
"os" |
15 | 8 |
"path" |
16 | 9 |
"testing" |
17 | 10 |
"time" |
11 |
+ |
|
12 |
+ "github.com/docker/docker/autogen/dockerversion" |
|
13 |
+ "github.com/docker/docker/daemon/graphdriver" |
|
14 |
+ "github.com/docker/docker/graph" |
|
15 |
+ "github.com/docker/docker/image" |
|
16 |
+ "github.com/docker/docker/pkg/archive" |
|
17 |
+ "github.com/docker/docker/pkg/stringid" |
|
18 |
+ "github.com/docker/docker/utils" |
|
18 | 19 |
) |
19 | 20 |
|
20 | 21 |
func TestMount(t *testing.T) { |
... | ... |
@@ -70,7 +71,7 @@ func TestInterruptedRegister(t *testing.T) { |
70 | 70 |
defer nukeGraph(graph) |
71 | 71 |
badArchive, w := io.Pipe() // Use a pipe reader as a fake archive which never yields data |
72 | 72 |
image := &image.Image{ |
73 |
- ID: common.GenerateRandomID(), |
|
73 |
+ ID: stringid.GenerateRandomID(), |
|
74 | 74 |
Comment: "testing", |
75 | 75 |
Created: time.Now(), |
76 | 76 |
} |
... | ... |
@@ -130,7 +131,7 @@ func TestRegister(t *testing.T) { |
130 | 130 |
t.Fatal(err) |
131 | 131 |
} |
132 | 132 |
image := &image.Image{ |
133 |
- ID: common.GenerateRandomID(), |
|
133 |
+ ID: stringid.GenerateRandomID(), |
|
134 | 134 |
Comment: "testing", |
135 | 135 |
Created: time.Now(), |
136 | 136 |
} |
... | ... |
@@ -160,7 +161,7 @@ func TestDeletePrefix(t *testing.T) { |
160 | 160 |
graph, _ := tempGraph(t) |
161 | 161 |
defer nukeGraph(graph) |
162 | 162 |
img := createTestImage(graph, t) |
163 |
- if err := graph.Delete(common.TruncateID(img.ID)); err != nil { |
|
163 |
+ if err := graph.Delete(stringid.TruncateID(img.ID)); err != nil { |
|
164 | 164 |
t.Fatal(err) |
165 | 165 |
} |
166 | 166 |
assertNImages(graph, t, 0) |
... | ... |
@@ -246,19 +247,19 @@ func TestByParent(t *testing.T) { |
246 | 246 |
graph, _ := tempGraph(t) |
247 | 247 |
defer nukeGraph(graph) |
248 | 248 |
parentImage := &image.Image{ |
249 |
- ID: common.GenerateRandomID(), |
|
249 |
+ ID: stringid.GenerateRandomID(), |
|
250 | 250 |
Comment: "parent", |
251 | 251 |
Created: time.Now(), |
252 | 252 |
Parent: "", |
253 | 253 |
} |
254 | 254 |
childImage1 := &image.Image{ |
255 |
- ID: common.GenerateRandomID(), |
|
255 |
+ ID: stringid.GenerateRandomID(), |
|
256 | 256 |
Comment: "child1", |
257 | 257 |
Created: time.Now(), |
258 | 258 |
Parent: parentImage.ID, |
259 | 259 |
} |
260 | 260 |
childImage2 := &image.Image{ |
261 |
- ID: common.GenerateRandomID(), |
|
261 |
+ ID: stringid.GenerateRandomID(), |
|
262 | 262 |
Comment: "child2", |
263 | 263 |
Created: time.Now(), |
264 | 264 |
Parent: parentImage.ID, |
... | ... |
@@ -22,9 +22,9 @@ import ( |
22 | 22 |
"github.com/docker/docker/engine" |
23 | 23 |
"github.com/docker/docker/image" |
24 | 24 |
"github.com/docker/docker/nat" |
25 |
- "github.com/docker/docker/pkg/common" |
|
26 | 25 |
"github.com/docker/docker/pkg/ioutils" |
27 | 26 |
"github.com/docker/docker/pkg/reexec" |
27 |
+ "github.com/docker/docker/pkg/stringid" |
|
28 | 28 |
"github.com/docker/docker/runconfig" |
29 | 29 |
"github.com/docker/docker/utils" |
30 | 30 |
) |
... | ... |
@@ -306,7 +306,7 @@ func TestDaemonCreate(t *testing.T) { |
306 | 306 |
&runconfig.HostConfig{}, |
307 | 307 |
"conflictname", |
308 | 308 |
) |
309 |
- if _, _, err := daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID, Cmd: []string{"ls", "-al"}}, &runconfig.HostConfig{}, testContainer.Name); err == nil || !strings.Contains(err.Error(), common.TruncateID(testContainer.ID)) { |
|
309 |
+ if _, _, err := daemon.Create(&runconfig.Config{Image: GetTestImage(daemon).ID, Cmd: []string{"ls", "-al"}}, &runconfig.HostConfig{}, testContainer.Name); err == nil || !strings.Contains(err.Error(), stringid.TruncateID(testContainer.ID)) { |
|
310 | 310 |
t.Fatalf("Name conflict error doesn't include the correct short id. Message was: %v", err) |
311 | 311 |
} |
312 | 312 |
|
313 | 313 |
deleted file mode 100644 |
... | ... |
@@ -1,47 +0,0 @@ |
1 |
-package common |
|
2 |
- |
|
3 |
-import ( |
|
4 |
- "crypto/rand" |
|
5 |
- "encoding/hex" |
|
6 |
- "io" |
|
7 |
- "strconv" |
|
8 |
-) |
|
9 |
- |
|
10 |
-// TruncateID returns a shorthand version of a string identifier for convenience. |
|
11 |
-// A collision with other shorthands is very unlikely, but possible. |
|
12 |
-// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller |
|
13 |
-// will need to use a langer prefix, or the full-length Id. |
|
14 |
-func TruncateID(id string) string { |
|
15 |
- shortLen := 12 |
|
16 |
- if len(id) < shortLen { |
|
17 |
- shortLen = len(id) |
|
18 |
- } |
|
19 |
- return id[:shortLen] |
|
20 |
-} |
|
21 |
- |
|
22 |
-// GenerateRandomID returns an unique id |
|
23 |
-func GenerateRandomID() string { |
|
24 |
- for { |
|
25 |
- id := make([]byte, 32) |
|
26 |
- if _, err := io.ReadFull(rand.Reader, id); err != nil { |
|
27 |
- panic(err) // This shouldn't happen |
|
28 |
- } |
|
29 |
- value := hex.EncodeToString(id) |
|
30 |
- // if we try to parse the truncated for as an int and we don't have |
|
31 |
- // an error then the value is all numberic and causes issues when |
|
32 |
- // used as a hostname. ref #3869 |
|
33 |
- if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil { |
|
34 |
- continue |
|
35 |
- } |
|
36 |
- return value |
|
37 |
- } |
|
38 |
-} |
|
39 |
- |
|
40 |
-func RandomString() string { |
|
41 |
- id := make([]byte, 32) |
|
42 |
- |
|
43 |
- if _, err := io.ReadFull(rand.Reader, id); err != nil { |
|
44 |
- panic(err) // This shouldn't happen |
|
45 |
- } |
|
46 |
- return hex.EncodeToString(id) |
|
47 |
-} |
48 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,59 +0,0 @@ |
1 |
-package common |
|
2 |
- |
|
3 |
-import ( |
|
4 |
- "testing" |
|
5 |
-) |
|
6 |
- |
|
7 |
-func TestShortenId(t *testing.T) { |
|
8 |
- id := GenerateRandomID() |
|
9 |
- truncID := TruncateID(id) |
|
10 |
- if len(truncID) != 12 { |
|
11 |
- t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) |
|
12 |
- } |
|
13 |
-} |
|
14 |
- |
|
15 |
-func TestShortenIdEmpty(t *testing.T) { |
|
16 |
- id := "" |
|
17 |
- truncID := TruncateID(id) |
|
18 |
- if len(truncID) > len(id) { |
|
19 |
- t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) |
|
20 |
- } |
|
21 |
-} |
|
22 |
- |
|
23 |
-func TestShortenIdInvalid(t *testing.T) { |
|
24 |
- id := "1234" |
|
25 |
- truncID := TruncateID(id) |
|
26 |
- if len(truncID) != len(id) { |
|
27 |
- t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) |
|
28 |
- } |
|
29 |
-} |
|
30 |
- |
|
31 |
-func TestGenerateRandomID(t *testing.T) { |
|
32 |
- id := GenerateRandomID() |
|
33 |
- |
|
34 |
- if len(id) != 64 { |
|
35 |
- t.Fatalf("Id returned is incorrect: %s", id) |
|
36 |
- } |
|
37 |
-} |
|
38 |
- |
|
39 |
-func TestRandomString(t *testing.T) { |
|
40 |
- id := RandomString() |
|
41 |
- if len(id) != 64 { |
|
42 |
- t.Fatalf("Id returned is incorrect: %s", id) |
|
43 |
- } |
|
44 |
-} |
|
45 |
- |
|
46 |
-func TestRandomStringUniqueness(t *testing.T) { |
|
47 |
- repeats := 25 |
|
48 |
- set := make(map[string]struct{}, repeats) |
|
49 |
- for i := 0; i < repeats; i = i + 1 { |
|
50 |
- id := RandomString() |
|
51 |
- if len(id) != 64 { |
|
52 |
- t.Fatalf("Id returned is incorrect: %s", id) |
|
53 |
- } |
|
54 |
- if _, ok := set[id]; ok { |
|
55 |
- t.Fatalf("Random number is repeated") |
|
56 |
- } |
|
57 |
- set[id] = struct{}{} |
|
58 |
- } |
|
59 |
-} |
0 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,38 @@ |
0 |
+package stringid |
|
1 |
+ |
|
2 |
+import ( |
|
3 |
+ "crypto/rand" |
|
4 |
+ "encoding/hex" |
|
5 |
+ "io" |
|
6 |
+ "strconv" |
|
7 |
+) |
|
8 |
+ |
|
9 |
+// TruncateID returns a shorthand version of a string identifier for convenience. |
|
10 |
+// A collision with other shorthands is very unlikely, but possible. |
|
11 |
+// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller |
|
12 |
+// will need to use a langer prefix, or the full-length Id. |
|
13 |
+func TruncateID(id string) string { |
|
14 |
+ shortLen := 12 |
|
15 |
+ if len(id) < shortLen { |
|
16 |
+ shortLen = len(id) |
|
17 |
+ } |
|
18 |
+ return id[:shortLen] |
|
19 |
+} |
|
20 |
+ |
|
21 |
+// GenerateRandomID returns an unique id |
|
22 |
+func GenerateRandomID() string { |
|
23 |
+ for { |
|
24 |
+ id := make([]byte, 32) |
|
25 |
+ if _, err := io.ReadFull(rand.Reader, id); err != nil { |
|
26 |
+ panic(err) // This shouldn't happen |
|
27 |
+ } |
|
28 |
+ value := hex.EncodeToString(id) |
|
29 |
+ // if we try to parse the truncated for as an int and we don't have |
|
30 |
+ // an error then the value is all numberic and causes issues when |
|
31 |
+ // used as a hostname. ref #3869 |
|
32 |
+ if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil { |
|
33 |
+ continue |
|
34 |
+ } |
|
35 |
+ return value |
|
36 |
+ } |
|
37 |
+} |
0 | 38 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,35 @@ |
0 |
+package stringid |
|
1 |
+ |
|
2 |
+import "testing" |
|
3 |
+ |
|
4 |
+func TestGenerateRandomID(t *testing.T) { |
|
5 |
+ id := GenerateRandomID() |
|
6 |
+ |
|
7 |
+ if len(id) != 64 { |
|
8 |
+ t.Fatalf("Id returned is incorrect: %s", id) |
|
9 |
+ } |
|
10 |
+} |
|
11 |
+ |
|
12 |
+func TestShortenId(t *testing.T) { |
|
13 |
+ id := GenerateRandomID() |
|
14 |
+ truncID := TruncateID(id) |
|
15 |
+ if len(truncID) != 12 { |
|
16 |
+ t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) |
|
17 |
+ } |
|
18 |
+} |
|
19 |
+ |
|
20 |
+func TestShortenIdEmpty(t *testing.T) { |
|
21 |
+ id := "" |
|
22 |
+ truncID := TruncateID(id) |
|
23 |
+ if len(truncID) > len(id) { |
|
24 |
+ t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) |
|
25 |
+ } |
|
26 |
+} |
|
27 |
+ |
|
28 |
+func TestShortenIdInvalid(t *testing.T) { |
|
29 |
+ id := "1234" |
|
30 |
+ truncID := TruncateID(id) |
|
31 |
+ if len(truncID) != len(id) { |
|
32 |
+ t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) |
|
33 |
+ } |
|
34 |
+} |
0 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,43 @@ |
0 |
+package stringutils |
|
1 |
+ |
|
2 |
+import ( |
|
3 |
+ "crypto/rand" |
|
4 |
+ "encoding/hex" |
|
5 |
+ "io" |
|
6 |
+ mathrand "math/rand" |
|
7 |
+ "time" |
|
8 |
+) |
|
9 |
+ |
|
10 |
+// Generate 32 chars random string |
|
11 |
+func GenerateRandomString() string { |
|
12 |
+ id := make([]byte, 32) |
|
13 |
+ |
|
14 |
+ if _, err := io.ReadFull(rand.Reader, id); err != nil { |
|
15 |
+ panic(err) // This shouldn't happen |
|
16 |
+ } |
|
17 |
+ return hex.EncodeToString(id) |
|
18 |
+} |
|
19 |
+ |
|
20 |
+// Generate alpha only random stirng with length n |
|
21 |
+func GenerateRandomAlphaOnlyString(n int) string { |
|
22 |
+ // make a really long string |
|
23 |
+ letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") |
|
24 |
+ b := make([]byte, n) |
|
25 |
+ r := mathrand.New(mathrand.NewSource(time.Now().UTC().UnixNano())) |
|
26 |
+ for i := range b { |
|
27 |
+ b[i] = letters[r.Intn(len(letters))] |
|
28 |
+ } |
|
29 |
+ return string(b) |
|
30 |
+} |
|
31 |
+ |
|
32 |
+// Generate Ascii random stirng with length n |
|
33 |
+func GenerateRandomAsciiString(n int) string { |
|
34 |
+ chars := "abcdefghijklmnopqrstuvwxyz" + |
|
35 |
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + |
|
36 |
+ "~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` " |
|
37 |
+ res := make([]byte, n) |
|
38 |
+ for i := 0; i < n; i++ { |
|
39 |
+ res[i] = chars[mathrand.Intn(len(chars))] |
|
40 |
+ } |
|
41 |
+ return string(res) |
|
42 |
+} |
0 | 43 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,25 @@ |
0 |
+package stringutils |
|
1 |
+ |
|
2 |
+import "testing" |
|
3 |
+ |
|
4 |
+func TestRandomString(t *testing.T) { |
|
5 |
+ str := GenerateRandomString() |
|
6 |
+ if len(str) != 64 { |
|
7 |
+ t.Fatalf("Id returned is incorrect: %s", str) |
|
8 |
+ } |
|
9 |
+} |
|
10 |
+ |
|
11 |
+func TestRandomStringUniqueness(t *testing.T) { |
|
12 |
+ repeats := 25 |
|
13 |
+ set := make(map[string]struct{}, repeats) |
|
14 |
+ for i := 0; i < repeats; i = i + 1 { |
|
15 |
+ str := GenerateRandomString() |
|
16 |
+ if len(str) != 64 { |
|
17 |
+ t.Fatalf("Id returned is incorrect: %s", str) |
|
18 |
+ } |
|
19 |
+ if _, ok := set[str]; ok { |
|
20 |
+ t.Fatalf("Random number is repeated") |
|
21 |
+ } |
|
22 |
+ set[str] = struct{}{} |
|
23 |
+ } |
|
24 |
+} |
... | ... |
@@ -4,7 +4,7 @@ import ( |
4 | 4 |
"math/rand" |
5 | 5 |
"testing" |
6 | 6 |
|
7 |
- "github.com/docker/docker/pkg/common" |
|
7 |
+ "github.com/docker/docker/pkg/stringid" |
|
8 | 8 |
) |
9 | 9 |
|
10 | 10 |
// Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix. |
... | ... |
@@ -111,7 +111,7 @@ func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult strin |
111 | 111 |
func BenchmarkTruncIndexAdd100(b *testing.B) { |
112 | 112 |
var testSet []string |
113 | 113 |
for i := 0; i < 100; i++ { |
114 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
114 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
115 | 115 |
} |
116 | 116 |
b.ResetTimer() |
117 | 117 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -127,7 +127,7 @@ func BenchmarkTruncIndexAdd100(b *testing.B) { |
127 | 127 |
func BenchmarkTruncIndexAdd250(b *testing.B) { |
128 | 128 |
var testSet []string |
129 | 129 |
for i := 0; i < 250; i++ { |
130 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
130 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
131 | 131 |
} |
132 | 132 |
b.ResetTimer() |
133 | 133 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -143,7 +143,7 @@ func BenchmarkTruncIndexAdd250(b *testing.B) { |
143 | 143 |
func BenchmarkTruncIndexAdd500(b *testing.B) { |
144 | 144 |
var testSet []string |
145 | 145 |
for i := 0; i < 500; i++ { |
146 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
146 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
147 | 147 |
} |
148 | 148 |
b.ResetTimer() |
149 | 149 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -160,7 +160,7 @@ func BenchmarkTruncIndexGet100(b *testing.B) { |
160 | 160 |
var testSet []string |
161 | 161 |
var testKeys []string |
162 | 162 |
for i := 0; i < 100; i++ { |
163 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
163 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
164 | 164 |
} |
165 | 165 |
index := NewTruncIndex([]string{}) |
166 | 166 |
for _, id := range testSet { |
... | ... |
@@ -184,7 +184,7 @@ func BenchmarkTruncIndexGet250(b *testing.B) { |
184 | 184 |
var testSet []string |
185 | 185 |
var testKeys []string |
186 | 186 |
for i := 0; i < 250; i++ { |
187 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
187 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
188 | 188 |
} |
189 | 189 |
index := NewTruncIndex([]string{}) |
190 | 190 |
for _, id := range testSet { |
... | ... |
@@ -208,7 +208,7 @@ func BenchmarkTruncIndexGet500(b *testing.B) { |
208 | 208 |
var testSet []string |
209 | 209 |
var testKeys []string |
210 | 210 |
for i := 0; i < 500; i++ { |
211 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
211 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
212 | 212 |
} |
213 | 213 |
index := NewTruncIndex([]string{}) |
214 | 214 |
for _, id := range testSet { |
... | ... |
@@ -231,7 +231,7 @@ func BenchmarkTruncIndexGet500(b *testing.B) { |
231 | 231 |
func BenchmarkTruncIndexDelete100(b *testing.B) { |
232 | 232 |
var testSet []string |
233 | 233 |
for i := 0; i < 100; i++ { |
234 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
234 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
235 | 235 |
} |
236 | 236 |
b.ResetTimer() |
237 | 237 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -254,7 +254,7 @@ func BenchmarkTruncIndexDelete100(b *testing.B) { |
254 | 254 |
func BenchmarkTruncIndexDelete250(b *testing.B) { |
255 | 255 |
var testSet []string |
256 | 256 |
for i := 0; i < 250; i++ { |
257 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
257 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
258 | 258 |
} |
259 | 259 |
b.ResetTimer() |
260 | 260 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -277,7 +277,7 @@ func BenchmarkTruncIndexDelete250(b *testing.B) { |
277 | 277 |
func BenchmarkTruncIndexDelete500(b *testing.B) { |
278 | 278 |
var testSet []string |
279 | 279 |
for i := 0; i < 500; i++ { |
280 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
280 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
281 | 281 |
} |
282 | 282 |
b.ResetTimer() |
283 | 283 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -300,7 +300,7 @@ func BenchmarkTruncIndexDelete500(b *testing.B) { |
300 | 300 |
func BenchmarkTruncIndexNew100(b *testing.B) { |
301 | 301 |
var testSet []string |
302 | 302 |
for i := 0; i < 100; i++ { |
303 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
303 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
304 | 304 |
} |
305 | 305 |
b.ResetTimer() |
306 | 306 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -311,7 +311,7 @@ func BenchmarkTruncIndexNew100(b *testing.B) { |
311 | 311 |
func BenchmarkTruncIndexNew250(b *testing.B) { |
312 | 312 |
var testSet []string |
313 | 313 |
for i := 0; i < 250; i++ { |
314 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
314 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
315 | 315 |
} |
316 | 316 |
b.ResetTimer() |
317 | 317 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -322,7 +322,7 @@ func BenchmarkTruncIndexNew250(b *testing.B) { |
322 | 322 |
func BenchmarkTruncIndexNew500(b *testing.B) { |
323 | 323 |
var testSet []string |
324 | 324 |
for i := 0; i < 500; i++ { |
325 |
- testSet = append(testSet, common.GenerateRandomID()) |
|
325 |
+ testSet = append(testSet, stringid.GenerateRandomID()) |
|
326 | 326 |
} |
327 | 327 |
b.ResetTimer() |
328 | 328 |
for i := 0; i < b.N; i++ { |
... | ... |
@@ -334,7 +334,7 @@ func BenchmarkTruncIndexAddGet100(b *testing.B) { |
334 | 334 |
var testSet []string |
335 | 335 |
var testKeys []string |
336 | 336 |
for i := 0; i < 500; i++ { |
337 |
- id := common.GenerateRandomID() |
|
337 |
+ id := stringid.GenerateRandomID() |
|
338 | 338 |
testSet = append(testSet, id) |
339 | 339 |
l := rand.Intn(12) + 12 |
340 | 340 |
testKeys = append(testKeys, id[:l]) |
... | ... |
@@ -359,7 +359,7 @@ func BenchmarkTruncIndexAddGet250(b *testing.B) { |
359 | 359 |
var testSet []string |
360 | 360 |
var testKeys []string |
361 | 361 |
for i := 0; i < 500; i++ { |
362 |
- id := common.GenerateRandomID() |
|
362 |
+ id := stringid.GenerateRandomID() |
|
363 | 363 |
testSet = append(testSet, id) |
364 | 364 |
l := rand.Intn(12) + 12 |
365 | 365 |
testKeys = append(testKeys, id[:l]) |
... | ... |
@@ -384,7 +384,7 @@ func BenchmarkTruncIndexAddGet500(b *testing.B) { |
384 | 384 |
var testSet []string |
385 | 385 |
var testKeys []string |
386 | 386 |
for i := 0; i < 500; i++ { |
387 |
- id := common.GenerateRandomID() |
|
387 |
+ id := stringid.GenerateRandomID() |
|
388 | 388 |
testSet = append(testSet, id) |
389 | 389 |
l := rand.Intn(12) + 12 |
390 | 390 |
testKeys = append(testKeys, id[:l]) |
... | ... |
@@ -21,9 +21,9 @@ import ( |
21 | 21 |
log "github.com/Sirupsen/logrus" |
22 | 22 |
"github.com/docker/docker/autogen/dockerversion" |
23 | 23 |
"github.com/docker/docker/pkg/archive" |
24 |
- "github.com/docker/docker/pkg/common" |
|
25 | 24 |
"github.com/docker/docker/pkg/fileutils" |
26 | 25 |
"github.com/docker/docker/pkg/ioutils" |
26 |
+ "github.com/docker/docker/pkg/stringutils" |
|
27 | 27 |
) |
28 | 28 |
|
29 | 29 |
type KeyValuePair struct { |
... | ... |
@@ -312,7 +312,7 @@ var globalTestID string |
312 | 312 |
// new directory. |
313 | 313 |
func TestDirectory(templateDir string) (dir string, err error) { |
314 | 314 |
if globalTestID == "" { |
315 |
- globalTestID = common.RandomString()[:4] |
|
315 |
+ globalTestID = stringutils.GenerateRandomString()[:4] |
|
316 | 316 |
} |
317 | 317 |
prefix := fmt.Sprintf("docker-test%s-%s-", globalTestID, GetCallerName(2)) |
318 | 318 |
if prefix == "" { |
... | ... |
@@ -9,7 +9,7 @@ import ( |
9 | 9 |
|
10 | 10 |
log "github.com/Sirupsen/logrus" |
11 | 11 |
"github.com/docker/docker/daemon/graphdriver" |
12 |
- "github.com/docker/docker/pkg/common" |
|
12 |
+ "github.com/docker/docker/pkg/stringid" |
|
13 | 13 |
) |
14 | 14 |
|
15 | 15 |
type Repository struct { |
... | ... |
@@ -43,7 +43,7 @@ func (r *Repository) newVolume(path string, writable bool) (*Volume, error) { |
43 | 43 |
var ( |
44 | 44 |
isBindMount bool |
45 | 45 |
err error |
46 |
- id = common.GenerateRandomID() |
|
46 |
+ id = stringid.GenerateRandomID() |
|
47 | 47 |
) |
48 | 48 |
if path != "" { |
49 | 49 |
isBindMount = true |