summaryrefslogtreecommitdiff
path: root/libpod/runtime_img.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2019-01-27 09:57:44 -0600
committerbaude <bbaude@redhat.com>2019-02-05 10:05:41 -0600
commit64c8fb7c2460eb561c8496f781f26d65443eea59 (patch)
tree19a7bae6cd3bc117fa7b2e9dfe44d3534b4f75ed /libpod/runtime_img.go
parent3554bfce98bc643bd4724340bf2abbaa6373e70c (diff)
downloadpodman-64c8fb7c2460eb561c8496f781f26d65443eea59.tar.gz
podman-64c8fb7c2460eb561c8496f781f26d65443eea59.tar.bz2
podman-64c8fb7c2460eb561c8496f781f26d65443eea59.zip
podman-remote import|export
addition of import and export for the podman-remote client. This includes the ability to send and receive files between the remote-client and the "podman" host using an upgraded varlink connection. Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'libpod/runtime_img.go')
-rw-r--r--libpod/runtime_img.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/libpod/runtime_img.go b/libpod/runtime_img.go
index 66844bb31..c20aa77a3 100644
--- a/libpod/runtime_img.go
+++ b/libpod/runtime_img.go
@@ -3,9 +3,16 @@ package libpod
import (
"context"
"fmt"
+ "github.com/opencontainers/image-spec/specs-go/v1"
+ "io"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "os"
"github.com/containers/buildah/imagebuildah"
"github.com/containers/libpod/libpod/image"
+ "github.com/containers/libpod/pkg/util"
"github.com/containers/storage"
"github.com/pkg/errors"
)
@@ -132,3 +139,75 @@ func (r *Runtime) Build(ctx context.Context, options imagebuildah.BuildOptions,
_, _, err := imagebuildah.BuildDockerfiles(ctx, r.store, options, dockerfiles...)
return err
}
+
+// Import is called as an intermediary to the image library Import
+func (r *Runtime) Import(ctx context.Context, source string, reference string, changes []string, history string, quiet bool) (string, error) {
+ var (
+ writer io.Writer
+ err error
+ )
+
+ ic := v1.ImageConfig{}
+ if len(changes) > 0 {
+ ic, err = util.GetImageConfig(changes)
+ if err != nil {
+ return "", errors.Wrapf(err, "error adding config changes to image %q", source)
+ }
+ }
+
+ hist := []v1.History{
+ {Comment: history},
+ }
+
+ config := v1.Image{
+ Config: ic,
+ History: hist,
+ }
+
+ writer = nil
+ if !quiet {
+ writer = os.Stderr
+ }
+
+ // if source is a url, download it and save to a temp file
+ u, err := url.ParseRequestURI(source)
+ if err == nil && u.Scheme != "" {
+ file, err := downloadFromURL(source)
+ 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 {
+ return "", err
+ }
+ return newImage.ID(), nil
+}
+
+// donwloadFromURL downloads an image in the format "https:/example.com/myimage.tar"
+// and temporarily saves in it /var/tmp/importxyz, which is deleted after the image is imported
+func downloadFromURL(source string) (string, error) {
+ fmt.Printf("Downloading from %q\n", source)
+
+ outFile, err := ioutil.TempFile("/var/tmp", "import")
+ if err != nil {
+ return "", errors.Wrap(err, "error creating file")
+ }
+ defer outFile.Close()
+
+ response, err := http.Get(source)
+ if err != nil {
+ return "", errors.Wrapf(err, "error downloading %q", source)
+ }
+ defer response.Body.Close()
+
+ _, err = io.Copy(outFile, response.Body)
+ if err != nil {
+ return "", errors.Wrapf(err, "error saving %s to %s", source, outFile.Name())
+ }
+
+ return outFile.Name(), nil
+}