Skip to content

Commit

Permalink
Refactor pkg/common, Fixes moby#11599
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <[email protected]>
  • Loading branch information
Antonio Murdaca committed Mar 24, 2015
1 parent 05c23ca commit b80fae7
Show file tree
Hide file tree
Showing 32 changed files with 215 additions and 196 deletions.
18 changes: 9 additions & 9 deletions api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/docker/docker/nat"
"github.com/docker/docker/opts"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/homedir"
flag "github.com/docker/docker/pkg/mflag"
Expand All @@ -43,6 +42,7 @@ import (
"github.com/docker/docker/pkg/promise"
"github.com/docker/docker/pkg/resolvconf"
"github.com/docker/docker/pkg/signal"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/term"
"github.com/docker/docker/pkg/timeutils"
Expand Down Expand Up @@ -1165,7 +1165,7 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
if *noTrunc {
fmt.Fprintf(w, "%s\t", outID)
} else {
fmt.Fprintf(w, "%s\t", common.TruncateID(outID))
fmt.Fprintf(w, "%s\t", stringid.TruncateID(outID))
}

fmt.Fprintf(w, "%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))))
Expand All @@ -1180,7 +1180,7 @@ func (cli *DockerCli) CmdHistory(args ...string) error {
if *noTrunc {
fmt.Fprintln(w, outID)
} else {
fmt.Fprintln(w, common.TruncateID(outID))
fmt.Fprintln(w, stringid.TruncateID(outID))
}
}
}
Expand Down Expand Up @@ -1479,7 +1479,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
}

if matchName != "" {
if matchName == image.Get("Id") || matchName == common.TruncateID(image.Get("Id")) {
if matchName == image.Get("Id") || matchName == stringid.TruncateID(image.Get("Id")) {
startImage = image
}

Expand Down Expand Up @@ -1549,7 +1549,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
for _, out := range outs.Data {
outID := out.Get("Id")
if !*noTrunc {
outID = common.TruncateID(outID)
outID = stringid.TruncateID(outID)
}

repoTags := out.GetList("RepoTags")
Expand Down Expand Up @@ -1629,8 +1629,8 @@ func (cli *DockerCli) printVizNode(noTrunc bool, image *engine.Env, prefix strin
imageID = image.Get("Id")
parentID = image.Get("ParentId")
} else {
imageID = common.TruncateID(image.Get("Id"))
parentID = common.TruncateID(image.Get("ParentId"))
imageID = stringid.TruncateID(image.Get("Id"))
parentID = stringid.TruncateID(image.Get("ParentId"))
}
if parentID == "" {
fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", imageID)
Expand All @@ -1649,7 +1649,7 @@ func (cli *DockerCli) printTreeNode(noTrunc bool, image *engine.Env, prefix stri
if noTrunc {
imageID = image.Get("Id")
} else {
imageID = common.TruncateID(image.Get("Id"))
imageID = stringid.TruncateID(image.Get("Id"))
}

fmt.Fprintf(cli.out, "%s%s Virtual Size: %s", prefix, imageID, units.HumanSize(float64(image.GetInt64("VirtualSize"))))
Expand Down Expand Up @@ -1757,7 +1757,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
outID := out.Get("Id")

if !*noTrunc {
outID = common.TruncateID(outID)
outID = stringid.TruncateID(outID)
}

if *quiet {
Expand Down
6 changes: 3 additions & 3 deletions builder/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"github.com/docker/docker/builder/parser"
"github.com/docker/docker/daemon"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/tarsum"
"github.com/docker/docker/registry"
Expand Down Expand Up @@ -177,7 +177,7 @@ func (b *Builder) Run(context io.Reader) (string, error) {
}
return "", err
}
fmt.Fprintf(b.OutStream, " ---> %s\n", common.TruncateID(b.image))
fmt.Fprintf(b.OutStream, " ---> %s\n", stringid.TruncateID(b.image))
if b.Remove {
b.clearTmp()
}
Expand All @@ -187,7 +187,7 @@ func (b *Builder) Run(context io.Reader) (string, error) {
return "", fmt.Errorf("No image was generated. Is your Dockerfile empty?")
}

fmt.Fprintf(b.OutStream, "Successfully built %s\n", common.TruncateID(b.image))
fmt.Fprintf(b.OutStream, "Successfully built %s\n", stringid.TruncateID(b.image))
return b.image, nil
}

Expand Down
8 changes: 4 additions & 4 deletions builder/internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
imagepkg "github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/chrootarchive"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/progressreader"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/system"
"github.com/docker/docker/pkg/tarsum"
Expand Down Expand Up @@ -557,7 +557,7 @@ func (b *Builder) create() (*daemon.Container, error) {
}

b.TmpContainers[c.ID] = struct{}{}
fmt.Fprintf(b.OutStream, " ---> Running in %s\n", common.TruncateID(c.ID))
fmt.Fprintf(b.OutStream, " ---> Running in %s\n", stringid.TruncateID(c.ID))

if len(config.Cmd) > 0 {
// override the entry point that may have been picked up from the base image
Expand Down Expand Up @@ -753,11 +753,11 @@ func (b *Builder) clearTmp() {
}

if err := b.Daemon.Rm(tmp); err != nil {
fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %v\n", common.TruncateID(c), err)
fmt.Fprintf(b.OutStream, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
return
}
b.Daemon.DeleteVolumes(tmp.VolumePaths())
delete(b.TmpContainers, c)
fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", common.TruncateID(c))
fmt.Fprintf(b.OutStream, "Removing intermediate container %s\n", stringid.TruncateID(c))
}
}
4 changes: 2 additions & 2 deletions daemon/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
"github.com/docker/docker/nat"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/broadcastwriter"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/directory"
"github.com/docker/docker/pkg/etchosts"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/promise"
"github.com/docker/docker/pkg/resolvconf"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/symlink"
"github.com/docker/docker/pkg/ulimit"
"github.com/docker/docker/runconfig"
Expand Down Expand Up @@ -739,7 +739,7 @@ func (container *Container) Kill() error {
if _, err := container.WaitStop(10 * time.Second); err != nil {
// Ensure that we don't kill ourselves
if pid := container.GetPid(); pid != 0 {
log.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", common.TruncateID(container.ID))
log.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
if err := syscall.Kill(pid, 9); err != nil {
if err != syscall.ESRCH {
return err
Expand Down
8 changes: 4 additions & 4 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import (
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/broadcastwriter"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/graphdb"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/namesgenerator"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/parsers/kernel"
"github.com/docker/docker/pkg/resolvconf"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/sysinfo"
"github.com/docker/docker/pkg/truncindex"
"github.com/docker/docker/runconfig"
Expand Down Expand Up @@ -517,7 +517,7 @@ func (daemon *Daemon) mergeAndVerifyConfig(config *runconfig.Config, img *image.
func (daemon *Daemon) generateIdAndName(name string) (string, string, error) {
var (
err error
id = common.GenerateRandomID()
id = stringid.GenerateRandomID()
)

if name == "" {
Expand Down Expand Up @@ -562,7 +562,7 @@ func (daemon *Daemon) reserveName(id, name string) (string, error) {
nameAsKnownByUser := strings.TrimPrefix(name, "/")
return "", fmt.Errorf(
"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,
common.TruncateID(conflictingContainer.ID))
stringid.TruncateID(conflictingContainer.ID))
}
}
return name, nil
Expand All @@ -585,7 +585,7 @@ func (daemon *Daemon) generateNewName(id string) (string, error) {
return name, nil
}

name = "/" + common.TruncateID(id)
name = "/" + stringid.TruncateID(id)
if _, err := daemon.containerGraph.Set(name, id); err != nil {
return "", err
}
Expand Down
4 changes: 2 additions & 2 deletions daemon/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/docker/docker/daemon/execdriver/lxc"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/broadcastwriter"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/promise"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/runconfig"
)

Expand Down Expand Up @@ -141,7 +141,7 @@ func (d *Daemon) ContainerExecCreate(job *engine.Job) engine.Status {
}

execConfig := &execConfig{
ID: common.GenerateRandomID(),
ID: stringid.GenerateRandomID(),
OpenStdin: config.AttachStdin,
OpenStdout: config.AttachStdout,
OpenStderr: config.AttachStderr,
Expand Down
4 changes: 2 additions & 2 deletions daemon/graphdriver/aufs/aufs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import (
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/chrootarchive"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/directory"
mountpk "github.com/docker/docker/pkg/mount"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/libcontainer/label"
)

Expand Down Expand Up @@ -405,7 +405,7 @@ func (a *Driver) Cleanup() error {

for _, id := range ids {
if err := a.unmount(id); err != nil {
log.Errorf("Unmounting %s: %s", common.TruncateID(id), err)
log.Errorf("Unmounting %s: %s", stringid.TruncateID(id), err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions daemon/image_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/docker/docker/engine"
"github.com/docker/docker/graph"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/parsers"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/utils"
)

Expand Down Expand Up @@ -148,11 +148,11 @@ func (daemon *Daemon) canDeleteImage(imgID string, force bool) error {
if imgID == p.ID {
if container.IsRunning() {
if force {
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))
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))
}
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))
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))
} else if !force {
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))
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))
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions daemon/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

log "github.com/Sirupsen/logrus"
"github.com/docker/docker/daemon/execdriver"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/runconfig"
)

Expand Down Expand Up @@ -230,7 +230,7 @@ func (m *containerMonitor) shouldRestart(exitCode int) bool {
// the default value of 0 for MaximumRetryCount means that we will not enforce a maximum count
if max := m.restartPolicy.MaximumRetryCount; max != 0 && m.failureCount > max {
log.Debugf("stopping restart of container %s because maximum failure could of %d has been reached",
common.TruncateID(m.container.ID), max)
stringid.TruncateID(m.container.ID), max)
return false
}

Expand Down
4 changes: 2 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"sync"
"time"

"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/ioutils"
"github.com/docker/docker/pkg/stringutils"
)

// Installer is a standard interface for objects which can "install" themselves
Expand Down Expand Up @@ -78,7 +78,7 @@ func (eng *Engine) RegisterCatchall(catchall Handler) {
func New() *Engine {
eng := &Engine{
handlers: make(map[string]Handler),
id: common.RandomString(),
id: stringutils.GenerateRandomString(),
Stdout: os.Stdout,
Stderr: os.Stderr,
Stdin: os.Stdin,
Expand Down
18 changes: 3 additions & 15 deletions engine/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@ package engine
import (
"bytes"
"encoding/json"
"math/rand"
"testing"
"time"
)

const chars = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"~!@#$%^&*()-_+={}[]\\|<,>.?/\"';:` "

// RandomString returns random string of specified length
func RandomString(length int) string {
res := make([]byte, length)
for i := 0; i < length; i++ {
res[i] = chars[rand.Intn(len(chars))]
}
return string(res)
}
"github.com/docker/docker/pkg/stringutils"
)

func TestEnvLenZero(t *testing.T) {
env := &Env{}
Expand Down Expand Up @@ -197,7 +185,7 @@ func TestMultiMap(t *testing.T) {
func testMap(l int) [][2]string {
res := make([][2]string, l)
for i := 0; i < l; i++ {
t := [2]string{RandomString(5), RandomString(20)}
t := [2]string{stringutils.GenerateRandomAsciiString(5), stringutils.GenerateRandomAsciiString(20)}
res[i] = t
}
return res
Expand Down
8 changes: 4 additions & 4 deletions graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/image"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/common"
"github.com/docker/docker/pkg/progressreader"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/truncindex"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/utils"
Expand Down Expand Up @@ -118,7 +118,7 @@ func (graph *Graph) Get(name string) (*image.Image, error) {
// Create creates a new image and registers it in the graph.
func (graph *Graph) Create(layerData archive.ArchiveReader, containerID, containerImage, comment, author string, containerConfig, config *runconfig.Config) (*image.Image, error) {
img := &image.Image{
ID: common.GenerateRandomID(),
ID: stringid.GenerateRandomID(),
Comment: comment,
Created: time.Now().UTC(),
DockerVersion: dockerversion.VERSION,
Expand Down Expand Up @@ -217,7 +217,7 @@ func (graph *Graph) TempLayerArchive(id string, sf *utils.StreamFormatter, outpu
Formatter: sf,
Size: 0,
NewLines: false,
ID: common.TruncateID(id),
ID: stringid.TruncateID(id),
Action: "Buffering to disk",
})
defer progressReader.Close()
Expand All @@ -226,7 +226,7 @@ func (graph *Graph) TempLayerArchive(id string, sf *utils.StreamFormatter, outpu

// Mktemp creates a temporary sub-directory inside the graph's filesystem.
func (graph *Graph) Mktemp(id string) (string, error) {
dir := path.Join(graph.Root, "_tmp", common.GenerateRandomID())
dir := path.Join(graph.Root, "_tmp", stringid.GenerateRandomID())
if err := os.MkdirAll(dir, 0700); err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit b80fae7

Please sign in to comment.