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

add export and import #29

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions commands.go
@@ -1,9 +1,15 @@
package main

import (
"archive/tar"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"sync"
Expand Down Expand Up @@ -450,6 +456,114 @@ 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

machines shouldn't be here – this should just be cli.Subcmd("export".... (I am fixing this in #32!)

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"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be in alphabetical order.

} {
help += fmt.Sprintf(" %-10.10s%s\n", command[0], command[1])
}
Expand Down