mirror of
https://github.com/containers/podman.git
synced 2025-06-21 17:38:12 +08:00
Merge pull request #6751 from vrothberg/fix-6744
podman run/create: support all transports
This commit is contained in:
@ -6,11 +6,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/libpod/libpod/define"
|
|
||||||
|
|
||||||
"github.com/containers/common/pkg/config"
|
"github.com/containers/common/pkg/config"
|
||||||
|
"github.com/containers/image/v5/storage"
|
||||||
|
"github.com/containers/image/v5/transports/alltransports"
|
||||||
"github.com/containers/libpod/cmd/podman/common"
|
"github.com/containers/libpod/cmd/podman/common"
|
||||||
"github.com/containers/libpod/cmd/podman/registry"
|
"github.com/containers/libpod/cmd/podman/registry"
|
||||||
|
"github.com/containers/libpod/libpod/define"
|
||||||
"github.com/containers/libpod/pkg/domain/entities"
|
"github.com/containers/libpod/pkg/domain/entities"
|
||||||
"github.com/containers/libpod/pkg/errorhandling"
|
"github.com/containers/libpod/pkg/errorhandling"
|
||||||
"github.com/containers/libpod/pkg/specgen"
|
"github.com/containers/libpod/pkg/specgen"
|
||||||
@ -108,12 +109,15 @@ func create(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imageName := args[0]
|
||||||
if !cliVals.RootFS {
|
if !cliVals.RootFS {
|
||||||
if err := pullImage(args[0]); err != nil {
|
name, err := pullImage(args[0])
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
imageName = name
|
||||||
}
|
}
|
||||||
s := specgen.NewSpecGenerator(args[0], cliVals.RootFS)
|
s := specgen.NewSpecGenerator(imageName, cliVals.RootFS)
|
||||||
if err := common.FillOutSpecGen(s, &cliVals, args); err != nil {
|
if err := common.FillOutSpecGen(s, &cliVals, args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -211,30 +215,44 @@ func createInit(c *cobra.Command) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pullImage(imageName string) error {
|
func pullImage(imageName string) (string, error) {
|
||||||
br, err := registry.ImageEngine().Exists(registry.GetContext(), imageName)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pullPolicy, err := config.ValidatePullPolicy(cliVals.Pull)
|
pullPolicy, err := config.ValidatePullPolicy(cliVals.Pull)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
if !br.Value || pullPolicy == config.PullImageAlways {
|
|
||||||
|
// Check if the image is missing and hence if we need to pull it.
|
||||||
|
imageMissing := true
|
||||||
|
imageRef, err := alltransports.ParseImageName(imageName)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
// Assume we specified a local image withouth the explicit storage transport.
|
||||||
|
fallthrough
|
||||||
|
|
||||||
|
case imageRef.Transport().Name() == storage.Transport.Name():
|
||||||
|
br, err := registry.ImageEngine().Exists(registry.GetContext(), imageName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
imageMissing = !br.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
if imageMissing || pullPolicy == config.PullImageAlways {
|
||||||
if pullPolicy == config.PullImageNever {
|
if pullPolicy == config.PullImageNever {
|
||||||
return errors.Wrapf(define.ErrNoSuchImage, "unable to find a name and tag match for %s in repotags", imageName)
|
return "", errors.Wrapf(define.ErrNoSuchImage, "unable to find a name and tag match for %s in repotags", imageName)
|
||||||
}
|
}
|
||||||
_, pullErr := registry.ImageEngine().Pull(registry.GetContext(), imageName, entities.ImagePullOptions{
|
pullReport, pullErr := registry.ImageEngine().Pull(registry.GetContext(), imageName, entities.ImagePullOptions{
|
||||||
Authfile: cliVals.Authfile,
|
Authfile: cliVals.Authfile,
|
||||||
Quiet: cliVals.Quiet,
|
Quiet: cliVals.Quiet,
|
||||||
OverrideArch: cliVals.OverrideArch,
|
OverrideArch: cliVals.OverrideArch,
|
||||||
OverrideOS: cliVals.OverrideOS,
|
OverrideOS: cliVals.OverrideOS,
|
||||||
})
|
})
|
||||||
if pullErr != nil {
|
if pullErr != nil {
|
||||||
return pullErr
|
return "", pullErr
|
||||||
}
|
}
|
||||||
|
imageName = pullReport.Images[0]
|
||||||
}
|
}
|
||||||
return nil
|
return imageName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func openCidFile(cidfile string) (*os.File, error) {
|
func openCidFile(cidfile string) (*os.File, error) {
|
||||||
|
@ -125,10 +125,13 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imageName := args[0]
|
||||||
if !cliVals.RootFS {
|
if !cliVals.RootFS {
|
||||||
if err := pullImage(args[0]); err != nil {
|
name, err := pullImage(args[0])
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
imageName = name
|
||||||
}
|
}
|
||||||
|
|
||||||
if cliVals.Replace {
|
if cliVals.Replace {
|
||||||
@ -166,7 +169,7 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
runOpts.Detach = cliVals.Detach
|
runOpts.Detach = cliVals.Detach
|
||||||
runOpts.DetachKeys = cliVals.DetachKeys
|
runOpts.DetachKeys = cliVals.DetachKeys
|
||||||
cliVals.PreserveFDs = runOpts.PreserveFDs
|
cliVals.PreserveFDs = runOpts.PreserveFDs
|
||||||
s := specgen.NewSpecGenerator(args[0], cliVals.RootFS)
|
s := specgen.NewSpecGenerator(imageName, cliVals.RootFS)
|
||||||
if err := common.FillOutSpecGen(s, &cliVals, args); err != nil {
|
if err := common.FillOutSpecGen(s, &cliVals, args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -196,7 +199,7 @@ func run(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if runRmi {
|
if runRmi {
|
||||||
_, rmErrors := registry.ImageEngine().Remove(registry.GetContext(), []string{args[0]}, entities.ImageRemoveOptions{})
|
_, rmErrors := registry.ImageEngine().Remove(registry.GetContext(), []string{imageName}, entities.ImageRemoveOptions{})
|
||||||
if len(rmErrors) > 0 {
|
if len(rmErrors) > 0 {
|
||||||
logrus.Errorf("%s", errors.Wrapf(errorhandling.JoinErrors(rmErrors), "failed removing image"))
|
logrus.Errorf("%s", errors.Wrapf(errorhandling.JoinErrors(rmErrors), "failed removing image"))
|
||||||
}
|
}
|
||||||
|
@ -201,4 +201,45 @@ echo $rand | 0 | $rand
|
|||||||
"podman will not overwrite existing cidfile"
|
"podman will not overwrite existing cidfile"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "podman run docker-archive" {
|
||||||
|
# Create an image that, when run, outputs a random magic string
|
||||||
|
expect=$(random_string 20)
|
||||||
|
run_podman run --name myc --entrypoint="[\"/bin/echo\",\"$expect\"]" $IMAGE
|
||||||
|
is "$output" "$expect" "podman run --entrypoint echo-randomstring"
|
||||||
|
|
||||||
|
# Save it as a tar archive
|
||||||
|
run_podman commit myc myi
|
||||||
|
archive=$PODMAN_TMPDIR/archive.tar
|
||||||
|
run_podman save myi -o $archive
|
||||||
|
is "$output" "" "podman save"
|
||||||
|
|
||||||
|
# Clean up image and container from container storage...
|
||||||
|
run_podman rmi myi
|
||||||
|
run_podman rm myc
|
||||||
|
|
||||||
|
# ... then confirm we can run from archive. This re-imports the image
|
||||||
|
# and runs it, producing our random string as the last line.
|
||||||
|
run_podman run docker-archive:$archive
|
||||||
|
is "${lines[0]}" "Getting image source signatures" "podman run docker-archive, first line of output"
|
||||||
|
is "$output" ".*Copying blob" "podman run docker-archive"
|
||||||
|
is "$output" ".*Copying config" "podman run docker-archive"
|
||||||
|
is "$output" ".*Writing manifest" "podman run docker-archive"
|
||||||
|
is "${lines[-1]}" "$expect" "podman run docker-archive: expected random string output"
|
||||||
|
|
||||||
|
# Clean up container as well as re-imported image
|
||||||
|
run_podman rm -a
|
||||||
|
run_podman rmi myi
|
||||||
|
|
||||||
|
# Repeat the above, with podman-create and podman-start.
|
||||||
|
run_podman create docker-archive:$archive
|
||||||
|
cid=${lines[-1]}
|
||||||
|
|
||||||
|
run_podman start --attach $cid
|
||||||
|
is "$output" "$expect" "'podman run' of 'podman-create docker-archive'"
|
||||||
|
|
||||||
|
# Clean up.
|
||||||
|
run_podman rm $cid
|
||||||
|
run_podman rmi myi
|
||||||
|
}
|
||||||
|
|
||||||
# vim: filetype=sh
|
# vim: filetype=sh
|
||||||
|
Reference in New Issue
Block a user