This commit is contained in:
Solomon Hykes 2013-03-09 19:44:09 -08:00
parent 93ba6dd82b
commit c59fff422f
21 changed files with 273 additions and 303 deletions

View File

@ -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

View File

@ -15,7 +15,6 @@ type Termios struct {
Ospeed uintptr
}
const (
// Input flags
inpck = 0x010
@ -74,7 +73,7 @@ const (
ONOCR = 0x20
ONOEOT = 0x8
OPOST = 0x1
RENB = 0x1000
RENB = 0x1000
PARMRK = 0x8
PARODD = 0x2000
@ -136,12 +135,9 @@ func MakeRaw(fd int) (*State, error) {
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
}

View File

@ -1,6 +1,7 @@
package docker
import (
"./fs"
"bytes"
"encoding/json"
"errors"
@ -14,7 +15,6 @@ import (
"strings"
"syscall"
"time"
"./fs"
)
var sysInitPath string

View File

@ -1,6 +1,7 @@
package docker
import (
"./fs"
"container/list"
"fmt"
"io/ioutil"
@ -8,7 +9,6 @@ import (
"os"
"path"
"sort"
"./fs"
)
type Docker struct {

View File

@ -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() {
}
}
}

View File

@ -1,12 +1,12 @@
package docker
import (
"./fs"
"io"
"io/ioutil"
"log"
"os"
"testing"
"io"
"./fs"
)
const testLayerPath string = "/var/lib/docker/docker-ut.tar"
@ -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)

View File

@ -1,9 +1,9 @@
package main
import (
"flag"
".."
"../server"
"flag"
"log"
)

View File

@ -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
}

View File

@ -2,8 +2,8 @@ package fs
import (
"fmt"
"path/filepath"
"os"
"path/filepath"
"strings"
)

View File

@ -1,15 +1,15 @@
package fs
import (
"../future"
"errors"
"path"
"path/filepath"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"fmt"
"../future"
"path"
"path/filepath"
)
type LayerStore struct {
@ -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)

View File

@ -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
*/

View File

@ -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)
}

View File

@ -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

View File

@ -1,9 +1,9 @@
package fs
import (
"../fake"
"errors"
"fmt"
"../fake"
"io/ioutil"
"os"
"testing"

View File

@ -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
}

View File

@ -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
}
func New(root string) (*Store, error) {
abspath, err := filepath.Abs(root)
if err != nil {
@ -79,7 +77,6 @@ func (store *Store) Create(name string, source string, layers ...string) (*Image
return image, nil
}
// Index
type Index struct {
@ -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,7 +282,7 @@ 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:]...)
}
}
}
@ -301,7 +298,7 @@ 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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -1,15 +1,15 @@
package server
import (
".."
"../fs"
"../future"
"../rcli"
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
".."
"../future"
"../fs"
"../rcli"
"io"
"net/http"
"net/url"