|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "os" |
| 6 | + |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/containers/storage" |
| 10 | + "github.com/containers/storage/pkg/archive" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | + "github.com/urfave/cli" |
| 14 | +) |
| 15 | + |
| 16 | +type exportOptions struct { |
| 17 | + output string |
| 18 | + container string |
| 19 | +} |
| 20 | + |
| 21 | +var ( |
| 22 | + exportFlags = []cli.Flag{ |
| 23 | + cli.StringFlag{ |
| 24 | + Name: "output, o", |
| 25 | + Usage: "Write to a file, default is STDOUT", |
| 26 | + Value: "/dev/stdout", |
| 27 | + }, |
| 28 | + } |
| 29 | + exportDescription = "Exports container's filesystem contents as a tar archive" + |
| 30 | + " and saves it on the local machine." |
| 31 | + exportCommand = cli.Command{ |
| 32 | + Name: "export", |
| 33 | + Usage: "Export container's filesystem contents as a tar archive", |
| 34 | + Description: exportDescription, |
| 35 | + Flags: exportFlags, |
| 36 | + Action: exportCmd, |
| 37 | + ArgsUsage: "CONTAINER", |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +// exportCmd saves a container to a tarball on disk |
| 42 | +func exportCmd(c *cli.Context) error { |
| 43 | + args := c.Args() |
| 44 | + if len(args) == 0 { |
| 45 | + return errors.Errorf("container id must be specified") |
| 46 | + } |
| 47 | + if len(args) > 1 { |
| 48 | + return errors.Errorf("too many arguments given, need 1 at most.") |
| 49 | + } |
| 50 | + container := args[0] |
| 51 | + |
| 52 | + config, err := getConfig(c) |
| 53 | + if err != nil { |
| 54 | + return errors.Wrapf(err, "could not get config") |
| 55 | + } |
| 56 | + store, err := getStore(config) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + output := c.String("output") |
| 62 | + if output == "/dev/stdout" { |
| 63 | + file := os.Stdout |
| 64 | + if logrus.IsTerminal(file) { |
| 65 | + return errors.Errorf("refusing to export to terminal. Use -o flag or redirect") |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + opts := exportOptions{ |
| 70 | + output: output, |
| 71 | + container: container, |
| 72 | + } |
| 73 | + |
| 74 | + return exportContainer(store, opts) |
| 75 | +} |
| 76 | + |
| 77 | +// exportContainer exports the contents of a container and saves it as |
| 78 | +// a tarball on disk |
| 79 | +func exportContainer(store storage.Store, opts exportOptions) error { |
| 80 | + mountPoint, err := store.Mount(opts.container, "") |
| 81 | + if err != nil { |
| 82 | + return errors.Wrapf(err, "error finding container %q", opts.container) |
| 83 | + } |
| 84 | + defer func() { |
| 85 | + if err := store.Unmount(opts.container); err != nil { |
| 86 | + fmt.Printf("error unmounting container %q: %v\n", opts.container, err) |
| 87 | + } |
| 88 | + }() |
| 89 | + |
| 90 | + input, err := archive.Tar(mountPoint, archive.Uncompressed) |
| 91 | + if err != nil { |
| 92 | + return errors.Wrapf(err, "error reading container directory %q", opts.container) |
| 93 | + } |
| 94 | + |
| 95 | + outFile, err := os.Create(opts.output) |
| 96 | + if err != nil { |
| 97 | + return errors.Wrapf(err, "error creating file %q", opts.output) |
| 98 | + } |
| 99 | + defer outFile.Close() |
| 100 | + |
| 101 | + _, err = io.Copy(outFile, input) |
| 102 | + return err |
| 103 | +} |
0 commit comments