summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/utils/containers.go
blob: fac237f8798279ef0acf04f2786689a9fe5f6371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package utils

import (
	"net/http"
	"time"

	"github.com/containers/podman/v2/libpod"
	"github.com/containers/podman/v2/libpod/define"
	"github.com/containers/podman/v2/pkg/domain/entities"
	"github.com/containers/podman/v2/pkg/domain/infra/abi"
	"github.com/gorilla/schema"
	"github.com/pkg/errors"
)

func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) {
	var (
		err      error
		interval time.Duration
	)
	runtime := r.Context().Value("runtime").(*libpod.Runtime)
	// Now use the ABI implementation to prevent us from having duplicate
	// code.
	containerEngine := abi.ContainerEngine{Libpod: runtime}
	decoder := r.Context().Value("decoder").(*schema.Decoder)
	query := struct {
		Interval  string                 `schema:"interval"`
		Condition define.ContainerStatus `schema:"condition"`
	}{
		// Override golang default values for types
	}
	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
		Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
		return 0, err
	}
	options := entities.WaitOptions{
		Condition: define.ContainerStateStopped,
	}
	name := GetName(r)
	if _, found := r.URL.Query()["interval"]; found {
		interval, err = time.ParseDuration(query.Interval)
		if err != nil {
			InternalServerError(w, err)
			return 0, err
		}
	} else {
		interval, err = time.ParseDuration("250ms")
		if err != nil {
			InternalServerError(w, err)
			return 0, err
		}
	}
	options.Interval = interval

	if _, found := r.URL.Query()["condition"]; found {
		options.Condition = query.Condition
	}

	report, err := containerEngine.ContainerWait(r.Context(), []string{name}, options)
	if err != nil {
		return 0, err
	}
	if len(report) == 0 {
		InternalServerError(w, errors.New("No reports returned"))
		return 0, err
	}
	return report[0].ExitCode, report[0].Error
}