From 96d1e9bb5a269936afeb9748a894f6995b17e0a4 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 31 Oct 2013 16:57:45 -0700 Subject: [PATCH] Move archive.go to sub package --- api.go | 3 ++- archive/MAINTAINERS | 1 + archive.go => archive/archive.go | 2 +- archive_test.go => archive/archive_test.go | 2 +- buildfile.go | 9 +++++---- buildfile_test.go | 3 ++- commands.go | 9 +++++---- container.go | 17 +++++++++-------- graph.go | 11 ++++++----- graph_test.go | 3 ++- image.go | 9 +++++---- server.go | 10 +++++----- 12 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 archive/MAINTAINERS rename archive.go => archive/archive.go (99%) rename archive_test.go => archive/archive_test.go (99%) diff --git a/api.go b/api.go index 638e78a713..854198f1f0 100644 --- a/api.go +++ b/api.go @@ -5,6 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/auth" "github.com/dotcloud/docker/utils" "github.com/gorilla/mux" @@ -905,7 +906,7 @@ func postBuild(srv *Server, version float64, w http.ResponseWriter, r *http.Requ return fmt.Errorf("Error trying to use git: %s (%s)", err, output) } - c, err := Tar(root, Bzip2) + c, err := archive.Tar(root, archive.Bzip2) if err != nil { return err } diff --git a/archive/MAINTAINERS b/archive/MAINTAINERS new file mode 100644 index 0000000000..1e998f8ac1 --- /dev/null +++ b/archive/MAINTAINERS @@ -0,0 +1 @@ +Michael Crosby (@crosbymichael) diff --git a/archive.go b/archive/archive.go similarity index 99% rename from archive.go rename to archive/archive.go index bb019fb033..97deb6c7b7 100644 --- a/archive.go +++ b/archive/archive.go @@ -1,4 +1,4 @@ -package docker +package archive import ( "archive/tar" diff --git a/archive_test.go b/archive/archive_test.go similarity index 99% rename from archive_test.go rename to archive/archive_test.go index 9a0a8e1b9e..4abb73890a 100644 --- a/archive_test.go +++ b/archive/archive_test.go @@ -1,4 +1,4 @@ -package docker +package archive import ( "bytes" diff --git a/buildfile.go b/buildfile.go index 931395efec..41828f33d2 100644 --- a/buildfile.go +++ b/buildfile.go @@ -3,6 +3,7 @@ package docker import ( "encoding/json" "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -291,17 +292,17 @@ func (b *buildFile) addContext(container *Container, orig, dest string) error { return fmt.Errorf("%s: no such file or directory", orig) } if fi.IsDir() { - if err := CopyWithTar(origPath, destPath); err != nil { + if err := archive.CopyWithTar(origPath, destPath); err != nil { return err } // First try to unpack the source as an archive - } else if err := UntarPath(origPath, destPath); err != nil { + } else if err := archive.UntarPath(origPath, destPath); err != nil { utils.Debugf("Couldn't untar %s to %s: %s", origPath, destPath, err) // If that fails, just copy it as a regular file if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil { return err } - if err := CopyWithTar(origPath, destPath); err != nil { + if err := archive.CopyWithTar(origPath, destPath); err != nil { return err } } @@ -473,7 +474,7 @@ func (b *buildFile) Build(context io.Reader) (string, error) { if err != nil { return "", err } - if err := Untar(context, name); err != nil { + if err := archive.Untar(context, name); err != nil { return "", err } defer os.RemoveAll(name) diff --git a/buildfile_test.go b/buildfile_test.go index 1af457158a..c504b81aaa 100644 --- a/buildfile_test.go +++ b/buildfile_test.go @@ -2,6 +2,7 @@ package docker import ( "fmt" + "github.com/dotcloud/docker/archive" "io/ioutil" "net" "net/http" @@ -12,7 +13,7 @@ import ( // mkTestContext generates a build context from the contents of the provided dockerfile. // This context is suitable for use as an argument to BuildFile.Build() -func mkTestContext(dockerfile string, files [][2]string, t *testing.T) Archive { +func mkTestContext(dockerfile string, files [][2]string, t *testing.T) archive.Archive { context, err := mkBuildContext(dockerfile, files) if err != nil { t.Fatal(err) diff --git a/commands.go b/commands.go index 0e06f47947..4c9f9aedc2 100644 --- a/commands.go +++ b/commands.go @@ -9,6 +9,7 @@ import ( "errors" "flag" "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/auth" "github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/term" @@ -137,7 +138,7 @@ func (cli *DockerCli) CmdInsert(args ...string) error { // mkBuildContext returns an archive of an empty context with the contents // of `dockerfile` at the path ./Dockerfile -func mkBuildContext(dockerfile string, files [][2]string) (Archive, error) { +func mkBuildContext(dockerfile string, files [][2]string) (archive.Archive, error) { buf := new(bytes.Buffer) tw := tar.NewWriter(buf) files = append(files, [2]string{"Dockerfile", dockerfile}) @@ -175,7 +176,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error { } var ( - context Archive + context archive.Archive isRemote bool err error ) @@ -194,7 +195,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error { if _, err := os.Stat(cmd.Arg(0)); err != nil { return err } - context, err = Tar(cmd.Arg(0), Uncompressed) + context, err = archive.Tar(cmd.Arg(0), archive.Uncompressed) } var body io.Reader // Setup an upload progress bar @@ -1773,7 +1774,7 @@ func (cli *DockerCli) CmdCp(args ...string) error { if statusCode == 200 { r := bytes.NewReader(data) - if err := Untar(r, copyData.HostPath); err != nil { + if err := archive.Untar(r, copyData.HostPath); err != nil { return err } } diff --git a/container.go b/container.go index 9448932674..e67c8f5b0b 100644 --- a/container.go +++ b/container.go @@ -6,6 +6,7 @@ import ( "errors" "flag" "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/term" "github.com/dotcloud/docker/utils" "github.com/kr/pty" @@ -817,7 +818,7 @@ func (container *Container) Start(hostConfig *HostConfig) (err error) { } if len(srcList) == 0 { // If the source volume is empty copy files from the root into the volume - if err := CopyWithTar(rootVolPath, srcPath); err != nil { + if err := archive.CopyWithTar(rootVolPath, srcPath); err != nil { return err } @@ -1337,23 +1338,23 @@ func (container *Container) Resize(h, w int) error { return term.SetWinsize(pty.Fd(), &term.Winsize{Height: uint16(h), Width: uint16(w)}) } -func (container *Container) ExportRw() (Archive, error) { - return Tar(container.rwPath(), Uncompressed) +func (container *Container) ExportRw() (archive.Archive, error) { + return archive.Tar(container.rwPath(), archive.Uncompressed) } func (container *Container) RwChecksum() (string, error) { - rwData, err := Tar(container.rwPath(), Xz) + rwData, err := archive.Tar(container.rwPath(), archive.Xz) if err != nil { return "", err } return utils.HashData(rwData) } -func (container *Container) Export() (Archive, error) { +func (container *Container) Export() (archive.Archive, error) { if err := container.EnsureMounted(); err != nil { return nil, err } - return Tar(container.RootfsPath(), Uncompressed) + return archive.Tar(container.RootfsPath(), archive.Uncompressed) } func (container *Container) WaitTimeout(timeout time.Duration) error { @@ -1488,7 +1489,7 @@ func (container *Container) GetSize() (int64, int64) { return sizeRw, sizeRootfs } -func (container *Container) Copy(resource string) (Archive, error) { +func (container *Container) Copy(resource string) (archive.Archive, error) { if err := container.EnsureMounted(); err != nil { return nil, err } @@ -1506,7 +1507,7 @@ func (container *Container) Copy(resource string) (Archive, error) { filter = []string{path.Base(basePath)} basePath = path.Dir(basePath) } - return TarFilter(basePath, Uncompressed, filter) + return archive.TarFilter(basePath, archive.Uncompressed, filter) } // Returns true if the container exposes a certain port diff --git a/graph.go b/graph.go index d4ce12e67b..f10c8e1bd9 100644 --- a/graph.go +++ b/graph.go @@ -2,6 +2,7 @@ package docker import ( "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -94,7 +95,7 @@ func (graph *Graph) Get(name string) (*Image, error) { } // Create creates a new image and registers it in the graph. -func (graph *Graph) Create(layerData Archive, container *Container, comment, author string, config *Config) (*Image, error) { +func (graph *Graph) Create(layerData archive.Archive, container *Container, comment, author string, config *Config) (*Image, error) { img := &Image{ ID: GenerateID(), Comment: comment, @@ -117,7 +118,7 @@ func (graph *Graph) Create(layerData Archive, container *Container, comment, aut // Register imports a pre-existing image into the graph. // FIXME: pass img as first argument -func (graph *Graph) Register(jsonData []byte, layerData Archive, img *Image) error { +func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Image) error { if err := ValidateID(img.ID); err != nil { return err } @@ -146,7 +147,7 @@ func (graph *Graph) Register(jsonData []byte, layerData Archive, img *Image) err // The archive is stored on disk and will be automatically deleted as soon as has been read. // If output is not nil, a human-readable progress bar will be written to it. // FIXME: does this belong in Graph? How about MktempFile, let the caller use it for archives? -func (graph *Graph) TempLayerArchive(id string, compression Compression, sf *utils.StreamFormatter, output io.Writer) (*TempArchive, error) { +func (graph *Graph) TempLayerArchive(id string, compression archive.Compression, sf *utils.StreamFormatter, output io.Writer) (*archive.TempArchive, error) { image, err := graph.Get(id) if err != nil { return nil, err @@ -155,11 +156,11 @@ func (graph *Graph) TempLayerArchive(id string, compression Compression, sf *uti if err != nil { return nil, err } - archive, err := image.TarLayer(compression) + a, err := image.TarLayer(compression) if err != nil { return nil, err } - return NewTempArchive(utils.ProgressReader(ioutil.NopCloser(archive), 0, output, sf.FormatProgress("", "Buffering to disk", "%v/%v (%v)"), sf, true), tmp.Root) + return archive.NewTempArchive(utils.ProgressReader(ioutil.NopCloser(a), 0, output, sf.FormatProgress("", "Buffering to disk", "%v/%v (%v)"), sf, true), tmp.Root) } // Mktemp creates a temporary sub-directory inside the graph's filesystem. diff --git a/graph_test.go b/graph_test.go index 471016938d..1102129aba 100644 --- a/graph_test.go +++ b/graph_test.go @@ -4,6 +4,7 @@ import ( "archive/tar" "bytes" "errors" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -301,7 +302,7 @@ func tempGraph(t *testing.T) *Graph { return graph } -func testArchive(t *testing.T) Archive { +func testArchive(t *testing.T) archive.Archive { archive, err := fakeTar() if err != nil { t.Fatal(err) diff --git a/image.go b/image.go index a62724803f..94cccaac67 100644 --- a/image.go +++ b/image.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/utils" "io" "io/ioutil" @@ -72,7 +73,7 @@ func LoadImage(root string) (*Image, error) { return img, nil } -func StoreImage(img *Image, jsonData []byte, layerData Archive, root string) error { +func StoreImage(img *Image, jsonData []byte, layerData archive.Archive, root string) error { // Check that root doesn't already exist if _, err := os.Stat(root); err == nil { return fmt.Errorf("Image %s already exists", img.ID) @@ -89,7 +90,7 @@ func StoreImage(img *Image, jsonData []byte, layerData Archive, root string) err if layerData != nil { start := time.Now() utils.Debugf("Start untar layer") - if err := Untar(layerData, layer); err != nil { + if err := archive.Untar(layerData, layer); err != nil { return err } utils.Debugf("Untar time: %vs", time.Now().Sub(start).Seconds()) @@ -162,12 +163,12 @@ func MountAUFS(ro []string, rw string, target string) error { } // TarLayer returns a tar archive of the image's filesystem layer. -func (image *Image) TarLayer(compression Compression) (Archive, error) { +func (image *Image) TarLayer(compression archive.Compression) (archive.Archive, error) { layerPath, err := image.layer() if err != nil { return nil, err } - return Tar(layerPath, compression) + return archive.Tar(layerPath, compression) } func (image *Image) Mount(root, rw string) error { diff --git a/server.go b/server.go index 815ed3fe7c..cdf2751226 100644 --- a/server.go +++ b/server.go @@ -5,11 +5,12 @@ import ( "encoding/json" "errors" "fmt" + "github.com/dotcloud/docker/archive" "github.com/dotcloud/docker/auth" + "github.com/dotcloud/docker/engine" "github.com/dotcloud/docker/gograph" "github.com/dotcloud/docker/registry" "github.com/dotcloud/docker/utils" - "github.com/dotcloud/docker/engine" "io" "io/ioutil" "log" @@ -17,14 +18,14 @@ import ( "net/url" "os" "os/exec" + "os/signal" "path" "path/filepath" "runtime" "strings" "sync" - "time" "syscall" - "os/signal" + "time" ) func (srv *Server) Close() error { @@ -92,7 +93,6 @@ func (srv *Server) Daemon() error { return nil } - func (srv *Server) DockerVersion() APIVersion { return APIVersion{ Version: VERSION, @@ -902,7 +902,7 @@ func (srv *Server) pushImage(r *registry.Registry, out io.Writer, remote, imgID, return "", err } - layerData, err := srv.runtime.graph.TempLayerArchive(imgID, Uncompressed, sf, out) + layerData, err := srv.runtime.graph.TempLayerArchive(imgID, archive.Uncompressed, sf, out) if err != nil { return "", fmt.Errorf("Failed to generate layer archive: %s", err) }