mirror of https://github.com/containers/podman.git
kpod_wait
Convert to libpod container backend Signed-off-by: baude <bbaude@redhat.com> Closes: #70 Approved by: rhatdan
This commit is contained in:
parent
99f905243b
commit
52ea0deee6
|
@ -5,7 +5,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/projectatomic/libpod/libkpod"
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,23 +30,23 @@ func waitCmd(c *cli.Context) error {
|
||||||
return errors.Errorf("you must provide at least one container name or id")
|
return errors.Errorf("you must provide at least one container name or id")
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := getConfig(c)
|
runtime, err := getRuntime(c)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "error creating libpod runtime")
|
||||||
|
}
|
||||||
|
defer runtime.Shutdown(false)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "could not get config")
|
return errors.Wrapf(err, "could not get config")
|
||||||
}
|
}
|
||||||
server, err := libkpod.New(config)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "could not get container server")
|
|
||||||
}
|
|
||||||
defer server.Shutdown()
|
|
||||||
err = server.Update()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrapf(err, "could not update list of containers")
|
|
||||||
}
|
|
||||||
|
|
||||||
var lastError error
|
var lastError error
|
||||||
for _, container := range c.Args() {
|
for _, container := range c.Args() {
|
||||||
returnCode, err := server.ContainerWait(container)
|
ctr, err := runtime.LookupContainer(container)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "unable to find container %s", container)
|
||||||
|
}
|
||||||
|
returnCode, err := ctr.Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if lastError != nil {
|
if lastError != nil {
|
||||||
fmt.Fprintln(os.Stderr, lastError)
|
fmt.Fprintln(os.Stderr, lastError)
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
crioAnnotations "github.com/projectatomic/libpod/pkg/annotations"
|
crioAnnotations "github.com/projectatomic/libpod/pkg/annotations"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/ulule/deepcopier"
|
"github.com/ulule/deepcopier"
|
||||||
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/client-go/tools/remotecommand"
|
"k8s.io/client-go/tools/remotecommand"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -639,3 +640,35 @@ func (c *Container) Export(path string) error {
|
||||||
func (c *Container) Commit() (*storage.Image, error) {
|
func (c *Container) Commit() (*storage.Image, error) {
|
||||||
return nil, ErrNotImplemented
|
return nil, ErrNotImplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait blocks on a container to exit and returns its exit code
|
||||||
|
func (c *Container) Wait() (int32, error) {
|
||||||
|
err := wait.PollImmediateInfinite(1,
|
||||||
|
func() (bool, error) {
|
||||||
|
stopped, err := c.isStopped()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if !stopped {
|
||||||
|
return false, nil
|
||||||
|
} else { // nolint
|
||||||
|
return true, nil // nolint
|
||||||
|
} // nolint
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
exitCode := c.state.ExitCode
|
||||||
|
return exitCode, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) isStopped() (bool, error) {
|
||||||
|
c.lock.Lock()
|
||||||
|
defer c.lock.Unlock()
|
||||||
|
err := c.syncContainer()
|
||||||
|
if err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
return c.state.State == ContainerStateStopped, nil
|
||||||
|
}
|
||||||
|
|
|
@ -2,74 +2,31 @@
|
||||||
|
|
||||||
load helpers
|
load helpers
|
||||||
|
|
||||||
IMAGE="redis:alpine"
|
|
||||||
function setup() {
|
function setup() {
|
||||||
copy_images
|
copy_images
|
||||||
}
|
}
|
||||||
|
|
||||||
# Returns the POD ID
|
|
||||||
function pod_run_from_template(){
|
|
||||||
#1=name, 2=uid, 3=namespace) {
|
|
||||||
NAME=$1 CUID=$2 NAMESPACE=$3 envsubst < ${TESTDATA}/template_sandbox_config.json > ${TESTDIR}/pod-${1}.json
|
|
||||||
crioctl pod run --config ${TESTDIR}/pod-${1}.json
|
|
||||||
}
|
|
||||||
|
|
||||||
# Returns the container ID
|
|
||||||
function container_create_from_template() {
|
|
||||||
#1=name, 2=image, 3=command, 4=id) {
|
|
||||||
NAME=$1 IMAGE=$2 COMMAND=$3 envsubst < ${TESTDATA}/template_container_config.json > ${TESTDIR}/ctr-${1}.json
|
|
||||||
crioctl ctr create --config ${TESTDIR}/ctr-${1}.json --pod "$4"
|
|
||||||
}
|
|
||||||
|
|
||||||
function container_start() {
|
|
||||||
#1=id
|
|
||||||
crioctl ctr start --id "$1"
|
|
||||||
|
|
||||||
}
|
|
||||||
@test "wait on a bogus container" {
|
@test "wait on a bogus container" {
|
||||||
skip "Needs to be converted to kpod run"
|
run ${KPOD_BINARY} ${KPOD_OPTIONS} wait 12343
|
||||||
start_crio
|
|
||||||
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait 12343
|
|
||||||
echo $output
|
echo $output
|
||||||
|
echo $status
|
||||||
[ "$status" -eq 1 ]
|
[ "$status" -eq 1 ]
|
||||||
stop_crio
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "wait on a stopped container" {
|
@test "wait on a stopped container" {
|
||||||
skip "Needs to be converted to kpod run"
|
run ${KPOD_BINARY} ${KPOD_OPTIONS} run -d ${ALPINE} ls
|
||||||
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} pull docker.io/library/busybox:latest
|
|
||||||
echo $output
|
echo $output
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
start_crio
|
ctr_id=${output}
|
||||||
pod_id=$( pod_run_from_template "test" "test" "test1-1" )
|
|
||||||
echo $pod_id
|
|
||||||
ctr_id=$(container_create_from_template "test-CTR" "docker.io/library/busybox:latest" '["ls"]' "${pod_id}")
|
|
||||||
echo $ctr_id
|
|
||||||
container_start $ctr_id
|
|
||||||
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait $ctr_id
|
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait $ctr_id
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
cleanup_ctrs
|
|
||||||
cleanup_pods
|
|
||||||
stop_crio
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "wait on a sleeping container" {
|
@test "wait on a sleeping container" {
|
||||||
skip "Needs to be converted to kpod run"
|
run ${KPOD_BINARY} ${KPOD_OPTIONS} run -d ${ALPINE} sleep 10
|
||||||
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} pull docker.io/library/busybox:latest
|
|
||||||
echo $output
|
|
||||||
[ "$status" -eq 0 ]
|
|
||||||
start_crio
|
|
||||||
pod_id=$( pod_run_from_template "test" "test" "test1-1" )
|
|
||||||
echo $pod_id
|
|
||||||
ctr_id=$(container_create_from_template "test-CTR" "docker.io/library/busybox:latest" '["sleep", "5"]' "${pod_id}")
|
|
||||||
echo $ctr_id
|
|
||||||
run container_start $ctr_id
|
|
||||||
echo $output
|
echo $output
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
|
ctr_id=${output}
|
||||||
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait $ctr_id
|
run bash -c ${KPOD_BINARY} ${KPOD_OPTIONS} wait $ctr_id
|
||||||
echo $output
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
cleanup_ctrs
|
|
||||||
cleanup_pods
|
|
||||||
stop_crio
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue