From c59fff422fdc61c1c994b44fd83e4173a2f0530a Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 9 Mar 2013 19:44:09 -0800 Subject: [PATCH 1/4] gofmt --- client/client.go | 4 +- client/term.go | 172 ++++++++++++++++++++-------------------- container.go | 6 +- docker.go | 12 +-- docker/docker.go | 3 +- docker_test.go | 8 +- dockerd/dockerd.go | 2 +- fake/fake.go | 16 ++-- fs/changes.go | 194 ++++++++++++++++++++++----------------------- fs/layers.go | 16 ++-- fs/layers_test.go | 9 +-- fs/mount_linux.go | 1 - fs/store.go | 5 +- fs/store_test.go | 2 +- future/future.go | 25 +++--- image/image.go | 57 +++++++------ mount_linux.go | 1 - rcli/http.go | 9 +-- rcli/tcp.go | 12 ++- rcli/types.go | 10 +-- server/server.go | 12 +-- 21 files changed, 273 insertions(+), 303 deletions(-) diff --git a/client/client.go b/client/client.go index 5a8aac3807..4c4ea1c5e3 100644 --- a/client/client.go +++ b/client/client.go @@ -1,8 +1,8 @@ package client import ( - "github.com/dotcloud/docker/rcli" "github.com/dotcloud/docker/future" + "github.com/dotcloud/docker/rcli" "io" "io/ioutil" "log" @@ -112,7 +112,7 @@ func InteractiveMode(scripts ...string) error { return err } io.WriteString(rcfile, "enable -n help\n") - os.Setenv("PATH", tmp + ":" + os.Getenv("PATH")) + os.Setenv("PATH", tmp+":"+os.Getenv("PATH")) os.Setenv("PS1", "\\h docker> ") shell := exec.Command("/bin/bash", append([]string{"--rcfile", rcfile.Name()}, scripts...)...) shell.Stdin = os.Stdin diff --git a/client/term.go b/client/term.go index ed52be96b4..a988d0d796 100644 --- a/client/term.go +++ b/client/term.go @@ -15,7 +15,6 @@ type Termios struct { Ospeed uintptr } - const ( // Input flags inpck = 0x010 @@ -35,113 +34,110 @@ const ( ) const ( - HUPCL = 0x4000 - ICANON = 0x100 - ICRNL = 0x100 - IEXTEN = 0x400 - BRKINT = 0x2 - CFLUSH = 0xf - CLOCAL = 0x8000 - CREAD = 0x800 - CS5 = 0x0 - CS6 = 0x100 - CS7 = 0x200 - CS8 = 0x300 - CSIZE = 0x300 - CSTART = 0x11 - CSTATUS = 0x14 - CSTOP = 0x13 - CSTOPB = 0x400 - CSUSP = 0x1a - IGNBRK = 0x1 - IGNCR = 0x80 - IGNPAR = 0x4 - IMAXBEL = 0x2000 - INLCR = 0x40 - INPCK = 0x10 - ISIG = 0x80 - ISTRIP = 0x20 - IUTF8 = 0x4000 - IXANY = 0x800 - IXOFF = 0x400 - IXON = 0x200 - NOFLSH = 0x80000000 - OCRNL = 0x10 - OFDEL = 0x20000 - OFILL = 0x80 - ONLCR = 0x2 - ONLRET = 0x40 - ONOCR = 0x20 - ONOEOT = 0x8 - OPOST = 0x1 -RENB = 0x1000 - PARMRK = 0x8 - PARODD = 0x2000 + HUPCL = 0x4000 + ICANON = 0x100 + ICRNL = 0x100 + IEXTEN = 0x400 + BRKINT = 0x2 + CFLUSH = 0xf + CLOCAL = 0x8000 + CREAD = 0x800 + CS5 = 0x0 + CS6 = 0x100 + CS7 = 0x200 + CS8 = 0x300 + CSIZE = 0x300 + CSTART = 0x11 + CSTATUS = 0x14 + CSTOP = 0x13 + CSTOPB = 0x400 + CSUSP = 0x1a + IGNBRK = 0x1 + IGNCR = 0x80 + IGNPAR = 0x4 + IMAXBEL = 0x2000 + INLCR = 0x40 + INPCK = 0x10 + ISIG = 0x80 + ISTRIP = 0x20 + IUTF8 = 0x4000 + IXANY = 0x800 + IXOFF = 0x400 + IXON = 0x200 + NOFLSH = 0x80000000 + OCRNL = 0x10 + OFDEL = 0x20000 + OFILL = 0x80 + ONLCR = 0x2 + ONLRET = 0x40 + ONOCR = 0x20 + ONOEOT = 0x8 + OPOST = 0x1 + RENB = 0x1000 + PARMRK = 0x8 + PARODD = 0x2000 - TOSTOP = 0x400000 - VDISCARD = 0xf - VDSUSP = 0xb - VEOF = 0x0 - VEOL = 0x1 - VEOL2 = 0x2 - VERASE = 0x3 - VINTR = 0x8 - VKILL = 0x5 - VLNEXT = 0xe - VMIN = 0x10 - VQUIT = 0x9 - VREPRINT = 0x6 - VSTART = 0xc - VSTATUS = 0x12 - VSTOP = 0xd - VSUSP = 0xa - VT0 = 0x0 - VT1 = 0x10000 - VTDLY = 0x10000 - VTIME = 0x11 - ECHO = 0x00000008 + TOSTOP = 0x400000 + VDISCARD = 0xf + VDSUSP = 0xb + VEOF = 0x0 + VEOL = 0x1 + VEOL2 = 0x2 + VERASE = 0x3 + VINTR = 0x8 + VKILL = 0x5 + VLNEXT = 0xe + VMIN = 0x10 + VQUIT = 0x9 + VREPRINT = 0x6 + VSTART = 0xc + VSTATUS = 0x12 + VSTOP = 0xd + VSUSP = 0xa + VT0 = 0x0 + VT1 = 0x10000 + VTDLY = 0x10000 + VTIME = 0x11 + ECHO = 0x00000008 - PENDIN = 0x20000000 + PENDIN = 0x20000000 ) type State struct { - termios Termios + termios Termios } // IsTerminal returns true if the given file descriptor is a terminal. func IsTerminal(fd int) bool { - var termios Termios - _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(getTermios), uintptr(unsafe.Pointer(&termios)), 0, 0, 0) - return err == 0 + var termios Termios + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(getTermios), uintptr(unsafe.Pointer(&termios)), 0, 0, 0) + return err == 0 } // MakeRaw put the terminal connected to the given file descriptor into raw // mode and returns the previous state of the terminal so that it can be // restored. func MakeRaw(fd int) (*State, error) { - var oldState State - if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 { - return nil, err - } + var oldState State + if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(getTermios), uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 { + return nil, err + } - newState := oldState.termios - newState.Iflag &^= ISTRIP | INLCR | IGNCR | IXON | IXOFF - newState.Iflag |= ICRNL - newState.Oflag |= ONLCR - newState.Lflag &^= ECHO | ICANON | ISIG - if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(setTermios), uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { - return nil, err - } + newState := oldState.termios + newState.Iflag &^= ISTRIP | INLCR | IGNCR | IXON | IXOFF + newState.Iflag |= ICRNL + newState.Oflag |= ONLCR + newState.Lflag &^= ECHO | ICANON | ISIG + if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(setTermios), uintptr(unsafe.Pointer(&newState)), 0, 0, 0); err != 0 { + return nil, err + } - return &oldState, nil + return &oldState, nil } - // Restore restores the terminal connected to the given file descriptor to a // previous state. func Restore(fd int, state *State) error { - _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(setTermios), uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0) - return err + _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), uintptr(setTermios), uintptr(unsafe.Pointer(&state.termios)), 0, 0, 0) + return err } - - diff --git a/container.go b/container.go index 3f727726d5..5a4559f720 100644 --- a/container.go +++ b/container.go @@ -1,6 +1,7 @@ package docker import ( + "./fs" "bytes" "encoding/json" "errors" @@ -14,7 +15,6 @@ import ( "strings" "syscall" "time" - "./fs" ) var sysInitPath string @@ -35,7 +35,7 @@ type Container struct { Config *Config Mountpoint *fs.Mountpoint State *State - Image string + Image string SysInitPath string lxcConfigPath string @@ -69,7 +69,7 @@ func createContainer(id string, root string, command string, args []string, imag Path: command, Args: args, Config: config, - Image: image.Id, + Image: image.Id, Mountpoint: mountpoint, State: newState(), diff --git a/docker.go b/docker.go index b5ee2bb848..105b1ea7da 100644 --- a/docker.go +++ b/docker.go @@ -1,6 +1,7 @@ package docker import ( + "./fs" "container/list" "fmt" "io/ioutil" @@ -8,14 +9,13 @@ import ( "os" "path" "sort" - "./fs" ) type Docker struct { - root string - repository string - containers *list.List - Store *fs.Store + root string + repository string + containers *list.List + Store *fs.Store } func (docker *Docker) List() []*Container { @@ -117,7 +117,7 @@ func NewFromDirectory(root string) (*Docker, error) { root: root, repository: path.Join(root, "containers"), containers: list.New(), - Store: store, + Store: store, } if err := os.MkdirAll(docker.repository, 0700); err != nil && !os.IsExist(err) { diff --git a/docker/docker.go b/docker/docker.go index efc93620a4..fa9011defa 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -2,10 +2,10 @@ package main import ( "flag" + "github.com/dotcloud/docker/client" "log" "os" "path" - "github.com/dotcloud/docker/client" ) func main() { @@ -27,4 +27,3 @@ func main() { } } } - diff --git a/docker_test.go b/docker_test.go index c612c4f60b..befc0a9a3f 100644 --- a/docker_test.go +++ b/docker_test.go @@ -1,17 +1,17 @@ package docker import ( + "./fs" + "io" "io/ioutil" "log" "os" "testing" - "io" - "./fs" ) const testLayerPath string = "/var/lib/docker/docker-ut.tar" -func layerArchive(tarfile string) (io.Reader, error) { +func layerArchive(tarfile string) (io.Reader, error) { // FIXME: need to close f somewhere f, err := os.Open(tarfile) if err != nil { @@ -57,7 +57,7 @@ func newTestDocker() (*Docker, error) { return docker, nil } -func GetTestImage(docker *Docker) (*fs.Image) { +func GetTestImage(docker *Docker) *fs.Image { imgs, err := docker.Store.Images() if err != nil { panic(err) diff --git a/dockerd/dockerd.go b/dockerd/dockerd.go index 7fff1eb7dd..22ae8df8e0 100644 --- a/dockerd/dockerd.go +++ b/dockerd/dockerd.go @@ -1,9 +1,9 @@ package main import ( - "flag" ".." "../server" + "flag" "log" ) diff --git a/fake/fake.go b/fake/fake.go index b3ec46310e..598ff6a813 100644 --- a/fake/fake.go +++ b/fake/fake.go @@ -1,20 +1,19 @@ package fake import ( - "bytes" - "math/rand" - "io" "archive/tar" - "os/exec" + "bytes" "github.com/kr/pty" + "io" + "math/rand" + "os/exec" ) - func FakeTar() (io.Reader, error) { content := []byte("Hello world!\n") buf := new(bytes.Buffer) tw := tar.NewWriter(buf) - for _, name := range []string {"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} { + for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} { hdr := new(tar.Header) hdr.Size = int64(len(content)) hdr.Name = name @@ -27,7 +26,6 @@ func FakeTar() (io.Reader, error) { return buf, nil } - func WriteFakeTar(dst io.Writer) error { if data, err := FakeTar(); err != nil { return err @@ -37,7 +35,6 @@ func WriteFakeTar(dst io.Writer) error { return nil } - func RandomBytesChanged() uint { return uint(rand.Int31n(24 * 1024 * 1024)) } @@ -54,7 +51,6 @@ func ContainerRunning() bool { return false } - func StartCommand(cmd *exec.Cmd, interactive bool) (io.WriteCloser, io.ReadCloser, error) { if interactive { term, err := pty.Start(cmd) @@ -76,5 +72,3 @@ func StartCommand(cmd *exec.Cmd, interactive bool) (io.WriteCloser, io.ReadClose } return stdin, stdout, nil } - - diff --git a/fs/changes.go b/fs/changes.go index 5c3523f3f3..9bd315ad82 100644 --- a/fs/changes.go +++ b/fs/changes.go @@ -1,129 +1,129 @@ package fs import ( - "fmt" - "path/filepath" - "os" - "strings" + "fmt" + "os" + "path/filepath" + "strings" ) type ChangeType int const ( - ChangeModify = iota - ChangeAdd - ChangeDelete + ChangeModify = iota + ChangeAdd + ChangeDelete ) type Change struct { - Path string - Kind ChangeType + Path string + Kind ChangeType } func (change *Change) String() string { - var kind string - switch change.Kind { - case ChangeModify: - kind = "C" - case ChangeAdd: - kind = "A" - case ChangeDelete: - kind = "D" - } - return fmt.Sprintf("%s %s", kind, change.Path) + var kind string + switch change.Kind { + case ChangeModify: + kind = "C" + case ChangeAdd: + kind = "A" + case ChangeDelete: + kind = "D" + } + return fmt.Sprintf("%s %s", kind, change.Path) } func (store *Store) Changes(mp *Mountpoint) ([]Change, error) { - var changes []Change - image, err := store.Get(mp.Image) - if err != nil { - return nil, err - } - layers, err := image.layers() - if err != nil { - return nil, err - } + var changes []Change + image, err := store.Get(mp.Image) + if err != nil { + return nil, err + } + layers, err := image.layers() + if err != nil { + return nil, err + } - err = filepath.Walk(mp.Rw, func(path string, f os.FileInfo, err error) error { - if err != nil { - return err - } + err = filepath.Walk(mp.Rw, func(path string, f os.FileInfo, err error) error { + if err != nil { + return err + } - // Rebase path - path, err = filepath.Rel(mp.Rw, path) - if err != nil { - return err - } - path = filepath.Join("/", path) + // Rebase path + path, err = filepath.Rel(mp.Rw, path) + if err != nil { + return err + } + path = filepath.Join("/", path) - // Skip root - if path == "/" { - return nil - } + // Skip root + if path == "/" { + return nil + } - // Skip AUFS metadata - if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched { - return err - } + // Skip AUFS metadata + if matched, err := filepath.Match("/.wh..wh.*", path); err != nil || matched { + return err + } - change := Change{ - Path: path, - } + change := Change{ + Path: path, + } - // Find out what kind of modification happened - file := filepath.Base(path) - // If there is a whiteout, then the file was removed - if strings.HasPrefix(file, ".wh.") { - originalFile := strings.TrimLeft(file, ".wh.") - change.Path = filepath.Join(filepath.Dir(path), originalFile) - change.Kind = ChangeDelete - } else { - // Otherwise, the file was added - change.Kind = ChangeAdd + // Find out what kind of modification happened + file := filepath.Base(path) + // If there is a whiteout, then the file was removed + if strings.HasPrefix(file, ".wh.") { + originalFile := strings.TrimLeft(file, ".wh.") + change.Path = filepath.Join(filepath.Dir(path), originalFile) + change.Kind = ChangeDelete + } else { + // Otherwise, the file was added + change.Kind = ChangeAdd - // ...Unless it already existed in a top layer, in which case, it's a modification - for _, layer := range layers { - stat, err := os.Stat(filepath.Join(layer, path)) - if err != nil && !os.IsNotExist(err) { - return err - } - if err == nil { - // The file existed in the top layer, so that's a modification + // ...Unless it already existed in a top layer, in which case, it's a modification + for _, layer := range layers { + stat, err := os.Stat(filepath.Join(layer, path)) + if err != nil && !os.IsNotExist(err) { + return err + } + if err == nil { + // The file existed in the top layer, so that's a modification - // However, if it's a directory, maybe it wasn't actually modified. - // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar - if stat.IsDir() && f.IsDir() { - if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() { - // Both directories are the same, don't record the change - return nil - } - } - change.Kind = ChangeModify - break - } - } - } + // However, if it's a directory, maybe it wasn't actually modified. + // If you modify /foo/bar/baz, then /foo will be part of the changed files only because it's the parent of bar + if stat.IsDir() && f.IsDir() { + if f.Size() == stat.Size() && f.Mode() == stat.Mode() && f.ModTime() == stat.ModTime() { + // Both directories are the same, don't record the change + return nil + } + } + change.Kind = ChangeModify + break + } + } + } - // Record change - changes = append(changes, change) - return nil - }) - if err != nil { - return nil, err - } - return changes, nil + // Record change + changes = append(changes, change) + return nil + }) + if err != nil { + return nil, err + } + return changes, nil } // Reset removes all changes to the filesystem, reverting it to its initial state. func (mp *Mountpoint) Reset() error { - if err := os.RemoveAll(mp.Rw); err != nil { - return err - } - // We removed the RW directory itself along with its content: let's re-create an empty one. - if err := mp.createFolders(); err != nil { - return err - } - return nil + if err := os.RemoveAll(mp.Rw); err != nil { + return err + } + // We removed the RW directory itself along with its content: let's re-create an empty one. + if err := mp.createFolders(); err != nil { + return err + } + return nil } // Open opens the named file for reading. @@ -141,4 +141,4 @@ func (mp *Mountpoint) Reset() error { // return nil, err // } // return ioutil.ReadDir(filepath.Join(fs.RootFS, dirname)) -// } \ No newline at end of file +// } diff --git a/fs/layers.go b/fs/layers.go index bbb0ee2014..d094ba569b 100644 --- a/fs/layers.go +++ b/fs/layers.go @@ -1,25 +1,25 @@ package fs import ( + "../future" "errors" - "path" - "path/filepath" + "fmt" "io" "io/ioutil" "os" "os/exec" - "fmt" - "../future" + "path" + "path/filepath" ) type LayerStore struct { - Root string + Root string } type Compression uint32 const ( - Uncompressed Compression = iota + Uncompressed Compression = iota Bzip2 Gzip ) @@ -80,10 +80,9 @@ func (store *LayerStore) Init() error { return os.Mkdir(store.Root, 0700) } - func (store *LayerStore) Mktemp() (string, error) { tmpName := future.RandomId() - tmpPath := path.Join(store.Root, "tmp-" + tmpName) + tmpPath := path.Join(store.Root, "tmp-"+tmpName) if err := os.Mkdir(tmpPath, 0700); err != nil { return "", err } @@ -94,7 +93,6 @@ func (store *LayerStore) layerPath(id string) string { return path.Join(store.Root, id) } - func (store *LayerStore) AddLayer(id string, archive Archive, stderr io.Writer, compression Compression) (string, error) { if _, err := os.Stat(store.layerPath(id)); err == nil { return "", errors.New("Layer already exists: " + id) diff --git a/fs/layers_test.go b/fs/layers_test.go index 3d8e9e32a7..8d47dc4b9f 100644 --- a/fs/layers_test.go +++ b/fs/layers_test.go @@ -1,14 +1,12 @@ package fs import ( - "io/ioutil" - "testing" - "os" "github.com/dotcloud/docker/fake" + "io/ioutil" + "os" + "testing" ) - - func TestLayersInit(t *testing.T) { store := tempStore(t) defer os.RemoveAll(store.Root) @@ -54,7 +52,6 @@ func TestAddLayerDuplicate(t *testing.T) { } } - /* * HELPER FUNCTIONS */ diff --git a/fs/mount_linux.go b/fs/mount_linux.go index 6dd2a39af8..b36888f75c 100644 --- a/fs/mount_linux.go +++ b/fs/mount_linux.go @@ -2,7 +2,6 @@ package fs import "syscall" - func mount(source string, target string, fstype string, flags uintptr, data string) (err error) { return syscall.Mount(source, target, fstype, flags, data) } diff --git a/fs/store.go b/fs/store.go index 67b0f2b3b4..697919a356 100644 --- a/fs/store.go +++ b/fs/store.go @@ -10,9 +10,9 @@ import ( "io" "os" "path" + "path/filepath" "syscall" "time" - "path/filepath" ) type Store struct { @@ -168,7 +168,6 @@ type Image struct { store *Store `db:"-"` } - func (image *Image) Copy(pth string) (*Image, error) { if err := image.store.orm.Insert(&Path{Path: pth, Image: image.Id}); err != nil { return nil, err @@ -198,7 +197,7 @@ func (image *Image) Mountpoint(root, rw string) (*Mountpoint, error) { func (image *Image) layers() ([]string, error) { var list []string - var err error + var err error currentImg := image for currentImg != nil { if layer := image.store.layers.Get(image.Id); layer != "" { diff --git a/fs/store_test.go b/fs/store_test.go index 954c83d865..d5715d1ba0 100644 --- a/fs/store_test.go +++ b/fs/store_test.go @@ -1,9 +1,9 @@ package fs import ( + "../fake" "errors" "fmt" - "../fake" "io/ioutil" "os" "testing" diff --git a/future/future.go b/future/future.go index a0efacc03c..cc7771cf97 100644 --- a/future/future.go +++ b/future/future.go @@ -1,12 +1,12 @@ package future import ( - "crypto/sha256" - "io" - "fmt" - "time" "bytes" + "crypto/sha256" + "fmt" + "io" "math/rand" + "time" ) func Seed() { @@ -30,18 +30,18 @@ func HumanDuration(d time.Duration) string { return "About a minute" } else if minutes < 60 { return fmt.Sprintf("%d minutes", minutes) - } else if hours := int(d.Hours()); hours == 1{ + } else if hours := int(d.Hours()); hours == 1 { return "About an hour" } else if hours < 48 { return fmt.Sprintf("%d hours", hours) - } else if hours < 24 * 7 * 2 { - return fmt.Sprintf("%d days", hours / 24) - } else if hours < 24 * 30 * 3 { - return fmt.Sprintf("%d weeks", hours / 24 / 7) - } else if hours < 24 * 365 * 2 { - return fmt.Sprintf("%d months", hours / 24 / 30) + } else if hours < 24*7*2 { + return fmt.Sprintf("%d days", hours/24) + } else if hours < 24*30*3 { + return fmt.Sprintf("%d weeks", hours/24/7) + } else if hours < 24*365*2 { + return fmt.Sprintf("%d months", hours/24/30) } - return fmt.Sprintf("%d years", d.Hours() / 24 / 365) + return fmt.Sprintf("%d years", d.Hours()/24/365) } func randomBytes() io.Reader { @@ -60,4 +60,3 @@ func Go(f func() error) chan error { }() return ch } - diff --git a/image/image.go b/image/image.go index 82c88b2a05..5e7d274256 100644 --- a/image/image.go +++ b/image/image.go @@ -1,27 +1,25 @@ package image import ( + "encoding/json" + "errors" + "github.com/dotcloud/docker/future" "io" "io/ioutil" - "encoding/json" - "time" + "os" "path" "path/filepath" - "errors" "sort" - "os" - "github.com/dotcloud/docker/future" "strings" + "time" ) - type Store struct { *Index - Root string - Layers *LayerStore + Root string + Layers *LayerStore } - func New(root string) (*Store, error) { abspath, err := filepath.Abs(root) if err != nil { @@ -38,8 +36,8 @@ func New(root string) (*Store, error) { return nil, err } return &Store{ - Root: abspath, - Index: NewIndex(path.Join(root, "index.json")), + Root: abspath, + Index: NewIndex(path.Join(root, "index.json")), Layers: layers, }, nil } @@ -47,7 +45,7 @@ func New(root string) (*Store, error) { type Compression uint32 const ( - Uncompressed Compression = iota + Uncompressed Compression = iota Bzip2 Gzip ) @@ -79,20 +77,19 @@ func (store *Store) Create(name string, source string, layers ...string) (*Image return image, nil } - // Index type Index struct { - Path string - ByName map[string]*History - ById map[string]*Image + Path string + ByName map[string]*History + ById map[string]*Image } func NewIndex(path string) *Index { return &Index{ - Path: path, + Path: path, ByName: make(map[string]*History), - ById: make(map[string]*Image), + ById: make(map[string]*Image), } } @@ -222,7 +219,7 @@ func (index *Index) Names() []string { if err := index.load(); err != nil { return []string{} } - var names[]string + var names []string for name := range index.ByName { names = append(names, name) } @@ -285,23 +282,23 @@ func (history *History) Add(image *Image) { func (history *History) Del(id string) { for idx, image := range *history { if image.Id == id { - *history = append((*history)[:idx], (*history)[idx + 1:]...) + *history = append((*history)[:idx], (*history)[idx+1:]...) } } } type Image struct { - Id string // Globally unique identifier - Layers []string // Absolute paths - Created time.Time - Parent string + Id string // Globally unique identifier + Layers []string // Absolute paths + Created time.Time + Parent string } func (image *Image) IdParts() (string, string) { if len(image.Id) < 8 { return "", image.Id } - hash := image.Id[len(image.Id)-8:len(image.Id)] + hash := image.Id[len(image.Id)-8 : len(image.Id)] name := image.Id[:len(image.Id)-9] return name, hash } @@ -322,7 +319,7 @@ func generateImageId(name string, layers []string) (string, error) { for _, layer := range layers { ids += path.Base(layer) } - if h, err := future.ComputeId(strings.NewReader(ids)); err != nil { + if h, err := future.ComputeId(strings.NewReader(ids)); err != nil { return "", err } else { hash = h @@ -337,9 +334,9 @@ func NewImage(name string, layers []string, parent string) (*Image, error) { return nil, err } return &Image{ - Id: id, - Layers: layers, - Created: time.Now(), - Parent: parent, + Id: id, + Layers: layers, + Created: time.Now(), + Parent: parent, }, nil } diff --git a/mount_linux.go b/mount_linux.go index a5a24e8480..0efb253003 100644 --- a/mount_linux.go +++ b/mount_linux.go @@ -2,7 +2,6 @@ package docker import "syscall" - func mount(source string, target string, fstype string, flags uintptr, data string) (err error) { return syscall.Mount(source, target, fstype, flags, data) } diff --git a/rcli/http.go b/rcli/http.go index e6cb5657d9..cc8d3b149e 100644 --- a/rcli/http.go +++ b/rcli/http.go @@ -1,13 +1,12 @@ package rcli import ( + "fmt" "net/http" "net/url" "path" - "fmt" ) - // Use this key to encode an RPC call into an URL, // eg. domain.tld/path/to/method?q=get_user&q=gordon const ARG_URL_KEY = "q" @@ -16,18 +15,16 @@ func URLToCall(u *url.URL) (method string, args []string) { return path.Base(u.Path), u.Query()[ARG_URL_KEY] } - func ListenAndServeHTTP(addr string, service Service) error { return http.ListenAndServe(addr, http.HandlerFunc( - func (w http.ResponseWriter, r *http.Request) { + func(w http.ResponseWriter, r *http.Request) { cmd, args := URLToCall(r.URL) if err := call(service, r.Body, &AutoFlush{w}, append([]string{cmd}, args...)...); err != nil { - fmt.Fprintf(w, "Error: " + err.Error() + "\n") + fmt.Fprintf(w, "Error: "+err.Error()+"\n") } })) } - type AutoFlush struct { http.ResponseWriter } diff --git a/rcli/tcp.go b/rcli/tcp.go index 0a06d459ce..869a3bcdb6 100644 --- a/rcli/tcp.go +++ b/rcli/tcp.go @@ -1,13 +1,13 @@ package rcli import ( + "bufio" + "encoding/json" + "fmt" "io" "io/ioutil" - "net" "log" - "fmt" - "encoding/json" - "bufio" + "net" ) // Connect to a remote endpoint using protocol `proto` and address `addr`, @@ -44,7 +44,7 @@ func ListenAndServe(proto, addr string, service Service) error { go func() { if err := Serve(conn, service); err != nil { log.Printf("Error: " + err.Error() + "\n") - fmt.Fprintf(conn, "Error: " + err.Error() + "\n") + fmt.Fprintf(conn, "Error: "+err.Error()+"\n") } conn.Close() }() @@ -53,7 +53,6 @@ func ListenAndServe(proto, addr string, service Service) error { return nil } - // Parse an rcli call on a new connection, and pass it to `service` if it // is valid. func Serve(conn io.ReadWriter, service Service) error { @@ -68,4 +67,3 @@ func Serve(conn io.ReadWriter, service Service) error { } return nil } - diff --git a/rcli/types.go b/rcli/types.go index b8572cd896..52079291b6 100644 --- a/rcli/types.go +++ b/rcli/types.go @@ -8,13 +8,13 @@ package rcli // are the usual suspects. import ( + "errors" + "flag" "fmt" "io" - "reflect" - "flag" "log" + "reflect" "strings" - "errors" ) type Service interface { @@ -25,7 +25,6 @@ type Service interface { type Cmd func(io.ReadCloser, io.Writer, ...string) error type CmdMethod func(Service, io.ReadCloser, io.Writer, ...string) error - func call(service Service, stdin io.ReadCloser, stdout io.Writer, args ...string) error { if len(args) == 0 { args = []string{"help"} @@ -63,7 +62,7 @@ func getMethod(service Service, name string) Cmd { return nil } } - methodName := "Cmd"+strings.ToUpper(name[:1])+strings.ToLower(name[1:]) + methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:]) method, exists := reflect.TypeOf(service).MethodByName(methodName) if !exists { return nil @@ -91,4 +90,3 @@ func Subcmd(output io.Writer, name, signature, description string) *flag.FlagSet } return flags } - diff --git a/server/server.go b/server/server.go index e5df5ec5ef..5dffe0979c 100644 --- a/server/server.go +++ b/server/server.go @@ -1,15 +1,15 @@ package server import ( + ".." + "../fs" + "../future" + "../rcli" "bufio" "bytes" "encoding/json" "errors" "fmt" - ".." - "../future" - "../fs" - "../rcli" "io" "net/http" "net/url" @@ -269,8 +269,8 @@ func (srv *Server) CmdInspect(stdin io.ReadCloser, stdout io.Writer, args ...str var obj interface{} if container := srv.containers.Get(name); container != nil { obj = container - //} else if image, err := srv.images.List(name); image != nil { - // obj = image + //} else if image, err := srv.images.List(name); image != nil { + // obj = image } else { return errors.New("No such container or image: " + name) } From 4474cd5677751ee5acac0525953eea583f797a1b Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 9 Mar 2013 19:49:09 -0800 Subject: [PATCH 2/4] Fixed a bug which caused docker to crash at startup while restoring existing containers --- container.go | 10 +++++++++- docker.go | 3 +-- fs/store.go | 8 ++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/container.go b/container.go index 5a4559f720..58ece78f58 100644 --- a/container.go +++ b/container.go @@ -100,17 +100,25 @@ func createContainer(id string, root string, command string, args []string, imag return container, nil } -func loadContainer(containerPath string) (*Container, error) { +func loadContainer(store *fs.Store, containerPath string) (*Container, error) { data, err := ioutil.ReadFile(path.Join(containerPath, "config.json")) if err != nil { return nil, err } + mountpoint, err := store.FetchMountpoint( + path.Join(containerPath, "rootfs"), + path.Join(containerPath, "rw"), + ) + if err != nil { + return nil, err + } container := &Container{ stdout: newWriteBroadcaster(), stderr: newWriteBroadcaster(), stdoutLog: new(bytes.Buffer), stderrLog: new(bytes.Buffer), lxcConfigPath: path.Join(containerPath, "config.lxc"), + Mountpoint: mountpoint, } if err := json.Unmarshal(data, container); err != nil { return nil, err diff --git a/docker.go b/docker.go index 105b1ea7da..50b38dbed4 100644 --- a/docker.go +++ b/docker.go @@ -92,12 +92,11 @@ func (docker *Docker) restore() error { return err } for _, v := range dir { - container, err := loadContainer(path.Join(docker.repository, v.Name())) + container, err := loadContainer(docker.Store, path.Join(docker.repository, v.Name())) if err != nil { log.Printf("Failed to load container %v: %v", v.Name(), err) continue } - container.Mountpoint.Store = docker.Store docker.containers.PushBack(container) } return nil diff --git a/fs/store.go b/fs/store.go index 697919a356..1ced3722fe 100644 --- a/fs/store.go +++ b/fs/store.go @@ -227,7 +227,7 @@ func (image *Image) Mountpoints() ([]*Mountpoint, error) { func (image *Image) Mount(root, rw string) (*Mountpoint, error) { var mountpoint *Mountpoint - if mp, err := image.fetchMountpoint(root, rw); err != nil { + if mp, err := image.store.FetchMountpoint(root, rw); err != nil { return nil, err } else if mp == nil { mountpoint, err = image.Mountpoint(root, rw) @@ -345,8 +345,8 @@ func (mp *Mountpoint) Deregister() error { return err } -func (image *Image) fetchMountpoint(root, rw string) (*Mountpoint, error) { - res, err := image.store.orm.Select(Mountpoint{}, "select * from mountpoints where Image=? and Root=? and Rw=?", image.Id, root, rw) +func (store *Store) FetchMountpoint(root, rw string) (*Mountpoint, error) { + res, err := store.orm.Select(Mountpoint{}, "select * from mountpoints where Root=? and Rw=?", root, rw) if err != nil { return nil, err } else if len(res) < 1 || res[0] == nil { @@ -354,7 +354,7 @@ func (image *Image) fetchMountpoint(root, rw string) (*Mountpoint, error) { } mp := res[0].(*Mountpoint) - mp.Store = image.store + mp.Store = store return mp, nil } From b5b2f005eb679d0224db407ad1e9320be361640a Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 9 Mar 2013 19:56:33 -0800 Subject: [PATCH 3/4] Fixed a bug which caused dockerd to not create its DB if an empty /var/lib/docker/images existed; fixed a bug which caused dockerd to not create missing tables in the images DB. --- fs/store.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/store.go b/fs/store.go index 1ced3722fe..ed70532ab4 100644 --- a/fs/store.go +++ b/fs/store.go @@ -4,9 +4,9 @@ import ( "database/sql" "errors" "fmt" - "github.com/coopernurse/gorp" "github.com/dotcloud/docker/future" _ "github.com/mattn/go-sqlite3" + "github.com/shykes/gorp" //Forked to implement CreateTablesOpts "io" "os" "path" @@ -29,8 +29,6 @@ func New(root string) (*Store, error) { if err := os.Mkdir(root, 0700); err != nil && !os.IsExist(err) { return nil, err - } else if os.IsExist(err) { - isNewStore = false } db, err := sql.Open("sqlite3", path.Join(root, "db")) if err != nil { @@ -42,7 +40,7 @@ func New(root string) (*Store, error) { orm.AddTableWithName(Mountpoint{}, "mountpoints").SetKeys(false, "Root") orm.AddTableWithName(Tag{}, "tags").SetKeys(false, "TagName") if isNewStore { - if err := orm.CreateTables(); err != nil { + if err := orm.CreateTablesOpts(true); err != nil { return nil, err } } From dd0227b700503e03fce0e9e9ea3539c438e9c0cb Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Sat, 9 Mar 2013 20:17:31 -0800 Subject: [PATCH 4/4] Removed deprecated docker/image package --- image/image.go | 342 ------------------------------------------------- 1 file changed, 342 deletions(-) delete mode 100644 image/image.go diff --git a/image/image.go b/image/image.go deleted file mode 100644 index 5e7d274256..0000000000 --- a/image/image.go +++ /dev/null @@ -1,342 +0,0 @@ -package image - -import ( - "encoding/json" - "errors" - "github.com/dotcloud/docker/future" - "io" - "io/ioutil" - "os" - "path" - "path/filepath" - "sort" - "strings" - "time" -) - -type Store struct { - *Index - Root string - Layers *LayerStore -} - -func New(root string) (*Store, error) { - abspath, err := filepath.Abs(root) - if err != nil { - return nil, err - } - if err := os.MkdirAll(abspath, 0700); err != nil && !os.IsExist(err) { - return nil, err - } - layers, err := NewLayerStore(path.Join(root, "layers")) - if err != nil { - return nil, err - } - if err := layers.Init(); err != nil { - return nil, err - } - return &Store{ - Root: abspath, - Index: NewIndex(path.Join(root, "index.json")), - Layers: layers, - }, nil -} - -type Compression uint32 - -const ( - Uncompressed Compression = iota - Bzip2 - Gzip -) - -func (store *Store) Import(name string, archive io.Reader, stderr io.Writer, parent *Image, compression Compression) (*Image, error) { - layer, err := store.Layers.AddLayer(archive, stderr, compression) - if err != nil { - return nil, err - } - layers := []string{layer} - if parent != nil { - layers = append(layers, parent.Layers...) - } - var parentId string - if parent != nil { - parentId = parent.Id - } - return store.Create(name, parentId, layers...) -} - -func (store *Store) Create(name string, source string, layers ...string) (*Image, error) { - image, err := NewImage(name, layers, source) - if err != nil { - return nil, err - } - if err := store.Index.Add(name, image); err != nil { - return nil, err - } - return image, nil -} - -// Index - -type Index struct { - Path string - ByName map[string]*History - ById map[string]*Image -} - -func NewIndex(path string) *Index { - return &Index{ - Path: path, - ByName: make(map[string]*History), - ById: make(map[string]*Image), - } -} - -func (index *Index) Exists(id string) bool { - _, exists := index.ById[id] - return exists -} - -func (index *Index) Find(idOrName string) *Image { - // Load - if err := index.load(); err != nil { - return nil - } - // Lookup by ID - if image, exists := index.ById[idOrName]; exists { - return image - } - // Lookup by name - if history, exists := index.ByName[idOrName]; exists && history.Len() > 0 { - return (*history)[0] - } - return nil -} - -func (index *Index) Add(name string, image *Image) error { - // Load - if err := index.load(); err != nil { - return err - } - if _, exists := index.ByName[name]; !exists { - index.ByName[name] = new(History) - } else { - // If this image is already the latest version, don't add it. - if (*index.ByName[name])[0].Id == image.Id { - return nil - } - } - index.ByName[name].Add(image) - index.ById[image.Id] = image - // Save - if err := index.save(); err != nil { - return err - } - return nil -} - -func (index *Index) Copy(srcNameOrId, dstName string) (*Image, error) { - if srcNameOrId == "" || dstName == "" { - return nil, errors.New("Illegal image name") - } - // Load - if err := index.load(); err != nil { - return nil, err - } - src := index.Find(srcNameOrId) - if src == nil { - return nil, errors.New("No such image: " + srcNameOrId) - } - dst, err := NewImage(dstName, src.Layers, src.Id) - if err != nil { - return nil, err - } - if err := index.Add(dstName, dst); err != nil { - return nil, err - } - // Save - if err := index.save(); err != nil { - return nil, err - } - return dst, nil -} - -func (index *Index) Rename(oldName, newName string) error { - // Load - if err := index.load(); err != nil { - return err - } - if _, exists := index.ByName[oldName]; !exists { - return errors.New("Can't rename " + oldName + ": no such image.") - } - if _, exists := index.ByName[newName]; exists { - return errors.New("Can't rename to " + newName + ": name is already in use.") - } - index.ByName[newName] = index.ByName[oldName] - delete(index.ByName, oldName) - // Change the ID of all images, since they include the name - for _, image := range *index.ByName[newName] { - if id, err := generateImageId(newName, image.Layers); err != nil { - return err - } else { - oldId := image.Id - image.Id = id - index.ById[id] = image - delete(index.ById, oldId) - } - } - // Save - if err := index.save(); err != nil { - return err - } - return nil -} - -// Delete deletes all images with the name `name` -func (index *Index) Delete(name string) error { - // Load - if err := index.load(); err != nil { - return err - } - if _, exists := index.ByName[name]; !exists { - return errors.New("No such image: " + name) - } - // Remove from index lookup - for _, image := range *index.ByName[name] { - delete(index.ById, image.Id) - } - // Remove from name lookup - delete(index.ByName, name) - // Save - if err := index.save(); err != nil { - return err - } - return nil -} - -func (index *Index) Names() []string { - if err := index.load(); err != nil { - return []string{} - } - var names []string - for name := range index.ByName { - names = append(names, name) - } - sort.Strings(names) - return names -} - -func (index *Index) load() error { - jsonData, err := ioutil.ReadFile(index.Path) - if err != nil { - if os.IsNotExist(err) { - return nil - } - return err - } - path := index.Path - if err := json.Unmarshal(jsonData, index); err != nil { - return err - } - index.Path = path - return nil -} - -func (index *Index) save() error { - jsonData, err := json.Marshal(index) - if err != nil { - return err - } - if err := ioutil.WriteFile(index.Path, jsonData, 0600); err != nil { - return err - } - return nil -} - -// History wraps an array of images so they can be sorted by date (most recent first) - -type History []*Image - -func (history *History) Len() int { - return len(*history) -} - -func (history *History) Less(i, j int) bool { - images := *history - return images[j].Created.Before(images[i].Created) -} - -func (history *History) Swap(i, j int) { - images := *history - tmp := images[i] - images[i] = images[j] - images[j] = tmp -} - -func (history *History) Add(image *Image) { - *history = append(*history, image) - sort.Sort(history) -} - -func (history *History) Del(id string) { - for idx, image := range *history { - if image.Id == id { - *history = append((*history)[:idx], (*history)[idx+1:]...) - } - } -} - -type Image struct { - Id string // Globally unique identifier - Layers []string // Absolute paths - Created time.Time - Parent string -} - -func (image *Image) IdParts() (string, string) { - if len(image.Id) < 8 { - return "", image.Id - } - hash := image.Id[len(image.Id)-8 : len(image.Id)] - name := image.Id[:len(image.Id)-9] - return name, hash -} - -func (image *Image) IdIsFinal() bool { - return len(image.Layers) == 1 -} - -func generateImageId(name string, layers []string) (string, error) { - if len(layers) == 0 { - return "", errors.New("No layers provided.") - } - var hash string - if len(layers) == 1 { - hash = path.Base(layers[0]) - } else { - var ids string - for _, layer := range layers { - ids += path.Base(layer) - } - if h, err := future.ComputeId(strings.NewReader(ids)); err != nil { - return "", err - } else { - hash = h - } - } - return name + ":" + hash, nil -} - -func NewImage(name string, layers []string, parent string) (*Image, error) { - id, err := generateImageId(name, layers) - if err != nil { - return nil, err - } - return &Image{ - Id: id, - Layers: layers, - Created: time.Now(), - Parent: parent, - }, nil -}