Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Commit

Permalink
sign off
Browse files Browse the repository at this point in the history
Signed-off-by: Wei-Ting Kuo <waitingkuo0527@gmail.com>
  • Loading branch information
waitingkuo committed Dec 6, 2014
1 parent 961e490 commit 85484c2
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
115 changes: 115 additions & 0 deletions commands.go
Expand Up @@ -8,6 +8,12 @@ import (
"strings"
"sync"
"text/tabwriter"
"path"
"path/filepath"
"archive/tar"
"bytes"
"io"
"io/ioutil"

log "github.com/Sirupsen/logrus"
flag "github.com/docker/docker/pkg/mflag"
Expand Down Expand Up @@ -450,6 +456,115 @@ func (cli *DockerCli) CmdActive(args ...string) error {

}

func (cli * DockerCli) CmdExport(args ...string) error {
cmd := cli.Subcmd("machines export", "[NAME]", "Export a machine to a tarfile")
if err := cmd.Parse(args); err != nil {
return err
}

store := NewStore()

if cmd.NArg() == 0 {
cmd.Usage()
return nil
}

hostName := cmd.Arg(0)
hostPath := path.Join(store.Path, hostName)

files, err := ioutil.ReadDir(hostPath)
if err != nil {
return err
}

buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)

for _, file := range files {

hdr := &tar.Header{
Name: file.Name(),
Mode: int64(file.Mode()),
Size: file.Size(),
}
if err := tw.WriteHeader(hdr); err != nil {
return err
}

fileO, err := os.Open(path.Join(hostPath, file.Name()))
if err != nil{
return err
}

_, err = io.Copy(tw, fileO)
if err != nil{
return err
}
}

output := hostName + ".tar"
err = ioutil.WriteFile(output, buf.Bytes(), 0600)
if err != nil {
return err
}

return nil

}

func (cli * DockerCli) CmdImport(args ...string) error {
cmd := cli.Subcmd("machines import", "[TARFILE]", "Import a machine from a tarfile")
if err := cmd.Parse(args); err != nil {
return err
}

if cmd.NArg() == 0 {
cmd.Usage()
return nil
}

store := NewStore()

tarName := cmd.Arg(0)
hostName := strings.TrimSuffix(tarName, filepath.Ext(tarName))

curDir, err := os.Getwd()
tarPath := path.Join(curDir, tarName)
data, err := ioutil.ReadFile(tarPath)
if err != nil {
return err
}

r := bytes.NewReader(data)
tr := tar.NewReader(r)

os.MkdirAll(path.Join(store.Path, hostName), 0700)

// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, tr); err != nil {
return err
}

filePath := path.Join(store.Path, hostName, hdr.Name)
err = ioutil.WriteFile(filePath, buf.Bytes(), 0600)
if err != nil {
return err
}
}

return nil
}


func (cli *DockerCli) CmdInspect(args ...string) error {
cmd := cli.Subcmd("machines inspect", "[NAME]", "Get detailed information about a machine")
if err := cmd.Parse(args); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -36,6 +36,8 @@ func main() {
{"stop", "Stop a machine"},
{"upgrade", "Upgrade a machine to the latest version of Docker"},
{"url", "Get the URL of a machine"},
{"export", "Export a machine"},
{"import", "Import a machine"},
} {
help += fmt.Sprintf(" %-10.10s%s\n", command[0], command[1])
}
Expand Down

0 comments on commit 85484c2

Please sign in to comment.