github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/utils/containers.go (about) 1 package utils 2 3 import ( 4 "context" 5 "net/http" 6 "time" 7 8 "github.com/containers/podman/v2/libpod" 9 "github.com/containers/podman/v2/libpod/define" 10 "github.com/containers/podman/v2/pkg/domain/entities" 11 createconfig "github.com/containers/podman/v2/pkg/spec" 12 "github.com/gorilla/schema" 13 "github.com/pkg/errors" 14 ) 15 16 func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) { 17 var ( 18 err error 19 interval time.Duration 20 ) 21 runtime := r.Context().Value("runtime").(*libpod.Runtime) 22 decoder := r.Context().Value("decoder").(*schema.Decoder) 23 query := struct { 24 Interval string `schema:"interval"` 25 Condition string `schema:"condition"` 26 }{ 27 // Override golang default values for types 28 } 29 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 30 Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 31 return 0, err 32 } 33 if _, found := r.URL.Query()["interval"]; found { 34 interval, err = time.ParseDuration(query.Interval) 35 if err != nil { 36 InternalServerError(w, err) 37 return 0, err 38 } 39 } else { 40 interval, err = time.ParseDuration("250ms") 41 if err != nil { 42 InternalServerError(w, err) 43 return 0, err 44 } 45 } 46 condition := define.ContainerStateStopped 47 if _, found := r.URL.Query()["condition"]; found { 48 condition, err = define.StringToContainerStatus(query.Condition) 49 if err != nil { 50 InternalServerError(w, err) 51 return 0, err 52 } 53 } 54 name := GetName(r) 55 con, err := runtime.LookupContainer(name) 56 if err != nil { 57 ContainerNotFound(w, name, err) 58 return 0, err 59 } 60 return con.WaitForConditionWithInterval(interval, condition) 61 } 62 63 func CreateContainer(ctx context.Context, w http.ResponseWriter, runtime *libpod.Runtime, cc *createconfig.CreateConfig) { 64 var pod *libpod.Pod 65 ctr, err := createconfig.CreateContainerFromCreateConfig(ctx, runtime, cc, pod) 66 if err != nil { 67 Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "CreateContainerFromCreateConfig()")) 68 return 69 } 70 71 response := entities.ContainerCreateResponse{ 72 ID: ctr.ID(), 73 Warnings: []string{}} 74 75 WriteResponse(w, http.StatusCreated, response) 76 }