diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-03-11 11:08:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-11 11:08:11 -0700 |
commit | b8863b260a7885981def2b02b5acc7c9a209e0c6 (patch) | |
tree | 6d0beca2f7cd0d680ce60c393f291421a5db8dfa /libpod/runtime_img.go | |
parent | 6421208e0f6ff1fba58eafdab12e897b5ed12e3b (diff) | |
parent | 135b670a2e7d8197a3b46cb147da1946a957f43c (diff) | |
download | podman-b8863b260a7885981def2b02b5acc7c9a209e0c6.tar.gz podman-b8863b260a7885981def2b02b5acc7c9a209e0c6.tar.bz2 podman-b8863b260a7885981def2b02b5acc7c9a209e0c6.zip |
Merge pull request #2611 from nalind/buffer-stdin-import
Buffer stdin to a file when importing "-"
Diffstat (limited to 'libpod/runtime_img.go')
-rw-r--r-- | libpod/runtime_img.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libpod/runtime_img.go b/libpod/runtime_img.go index 451c2ebe7..02f925fc6 100644 --- a/libpod/runtime_img.go +++ b/libpod/runtime_img.go @@ -14,6 +14,7 @@ import ( "github.com/containers/libpod/pkg/util" "github.com/containers/storage" "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/containers/image/directory" dockerarchive "github.com/containers/image/docker/archive" @@ -183,6 +184,15 @@ func (r *Runtime) Import(ctx context.Context, source string, reference string, c defer os.Remove(file) source = file } + // if it's stdin, buffer it, too + if source == "-" { + file, err := downloadFromFile(os.Stdin) + if err != nil { + return "", err + } + defer os.Remove(file) + source = file + } newImage, err := r.imageRuntime.Import(ctx, source, reference, writer, image.SigningOptions{}, config) if err != nil { @@ -216,6 +226,25 @@ func downloadFromURL(source string) (string, error) { return outFile.Name(), nil } +// donwloadFromFile reads all of the content from the reader and temporarily +// saves in it /var/tmp/importxyz, which is deleted after the image is imported +func downloadFromFile(reader *os.File) (string, error) { + outFile, err := ioutil.TempFile("/var/tmp", "import") + if err != nil { + return "", errors.Wrap(err, "error creating file") + } + defer outFile.Close() + + logrus.Debugf("saving %s to %s", reader.Name(), outFile.Name()) + + _, err = io.Copy(outFile, reader) + if err != nil { + return "", errors.Wrapf(err, "error saving %s to %s", reader.Name(), outFile.Name()) + } + + return outFile.Name(), nil +} + // LoadImage loads a container image into local storage func (r *Runtime) LoadImage(ctx context.Context, name, inputFile string, writer io.Writer, signaturePolicy string) (string, error) { var newImages []*image.Image |