github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/compat/containers_stop.go (about) 1 package compat 2 3 import ( 4 "net/http" 5 6 "github.com/containers/podman/v2/libpod" 7 "github.com/containers/podman/v2/libpod/define" 8 "github.com/containers/podman/v2/pkg/api/handlers/utils" 9 "github.com/gorilla/schema" 10 "github.com/pkg/errors" 11 ) 12 13 func StopContainer(w http.ResponseWriter, r *http.Request) { 14 runtime := r.Context().Value("runtime").(*libpod.Runtime) 15 decoder := r.Context().Value("decoder").(*schema.Decoder) 16 17 // /{version}/containers/(name)/stop 18 query := struct { 19 Timeout int `schema:"t"` 20 }{ 21 // override any golang type defaults 22 } 23 if err := decoder.Decode(&query, r.URL.Query()); err != nil { 24 utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest, 25 errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String())) 26 return 27 } 28 29 name := utils.GetName(r) 30 con, err := runtime.LookupContainer(name) 31 if err != nil { 32 utils.ContainerNotFound(w, name, err) 33 return 34 } 35 36 state, err := con.State() 37 if err != nil { 38 utils.InternalServerError(w, errors.Wrapf(err, "unable to get state for Container %s", name)) 39 return 40 } 41 // If the Container is stopped already, send a 304 42 if state == define.ContainerStateStopped || state == define.ContainerStateExited { 43 utils.WriteResponse(w, http.StatusNotModified, "") 44 return 45 } 46 47 var stopError error 48 if query.Timeout > 0 { 49 stopError = con.StopWithTimeout(uint(query.Timeout)) 50 } else { 51 stopError = con.Stop() 52 } 53 if stopError != nil { 54 utils.InternalServerError(w, errors.Wrapf(stopError, "failed to stop %s", name)) 55 return 56 } 57 58 // Success 59 utils.WriteResponse(w, http.StatusNoContent, "") 60 }