summaryrefslogtreecommitdiff
path: root/pkg/api/handlers/utils/containers.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/api/handlers/utils/containers.go')
-rw-r--r--pkg/api/handlers/utils/containers.go118
1 files changed, 49 insertions, 69 deletions
diff --git a/pkg/api/handlers/utils/containers.go b/pkg/api/handlers/utils/containers.go
index 2c986db3a..bbe4cee3c 100644
--- a/pkg/api/handlers/utils/containers.go
+++ b/pkg/api/handlers/utils/containers.go
@@ -1,77 +1,33 @@
package utils
import (
- "fmt"
+ "context"
"net/http"
- "syscall"
"time"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/define"
- "github.com/gorilla/mux"
+ createconfig "github.com/containers/libpod/pkg/spec"
"github.com/gorilla/schema"
"github.com/pkg/errors"
)
-func KillContainer(w http.ResponseWriter, r *http.Request) (*libpod.Container, error) {
- runtime := r.Context().Value("runtime").(*libpod.Runtime)
- decoder := r.Context().Value("decorder").(*schema.Decoder)
- query := struct {
- Signal syscall.Signal `schema:"signal"`
- }{
- Signal: syscall.SIGKILL,
- }
- 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 nil, err
- }
- name := mux.Vars(r)["name"]
- con, err := runtime.LookupContainer(name)
- if err != nil {
- ContainerNotFound(w, name, err)
- return nil, err
- }
-
- state, err := con.State()
- if err != nil {
- InternalServerError(w, err)
- return con, err
- }
-
- // If the Container is stopped already, send a 409
- if state == define.ContainerStateStopped || state == define.ContainerStateExited {
- Error(w, fmt.Sprintf("Container %s is not running", name), http.StatusConflict, errors.New(fmt.Sprintf("Cannot kill Container %s, it is not running", name)))
- return con, err
- }
-
- err = con.Kill(uint(query.Signal))
- if err != nil {
- Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrapf(err, "unable to kill Container %s", name))
- }
- return con, err
-}
-
-func RemoveContainer(w http.ResponseWriter, r *http.Request, force, vols bool) {
- runtime := r.Context().Value("runtime").(*libpod.Runtime)
- name := mux.Vars(r)["name"]
- con, err := runtime.LookupContainer(name)
- if err != nil {
- ContainerNotFound(w, name, err)
- return
- }
-
- if err := runtime.RemoveContainer(r.Context(), con, force, vols); err != nil {
- InternalServerError(w, err)
- return
- }
- WriteResponse(w, http.StatusNoContent, "")
+// ContainerCreateResponse is the response struct for creating a container
+type ContainerCreateResponse struct {
+ // ID of the container created
+ ID string `json:"Id"`
+ // Warnings during container creation
+ Warnings []string `json:"Warnings"`
}
func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) {
+ var (
+ err error
+ interval time.Duration
+ )
runtime := r.Context().Value("runtime").(*libpod.Runtime)
decoder := r.Context().Value("decoder").(*schema.Decoder)
- // /{version}/containers/(name)/restart
query := struct {
Interval string `schema:"interval"`
Condition string `schema:"condition"`
@@ -82,25 +38,34 @@ func WaitContainer(w http.ResponseWriter, r *http.Request) (int32, error) {
Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String()))
return 0, err
}
-
- if len(query.Condition) > 0 {
- UnSupportedParameter("condition")
+ 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
+ }
}
-
- name := mux.Vars(r)["name"]
+ condition := define.ContainerStateStopped
+ if _, found := r.URL.Query()["condition"]; found {
+ condition, err = define.StringToContainerStatus(query.Condition)
+ if err != nil {
+ InternalServerError(w, err)
+ return 0, err
+ }
+ }
+ name := GetName(r)
con, err := runtime.LookupContainer(name)
if err != nil {
ContainerNotFound(w, name, err)
return 0, err
}
- if len(query.Interval) > 0 {
- d, err := time.ParseDuration(query.Interval)
- if err != nil {
- Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "Failed to parse %s for interval", query.Interval))
- }
- return con.WaitWithInterval(d)
- }
- return con.Wait()
+ return con.WaitForConditionWithInterval(interval, condition)
}
// GenerateFilterFuncsFromMap is used to generate un-executed functions that can be used to filter
@@ -120,3 +85,18 @@ func GenerateFilterFuncsFromMap(r *libpod.Runtime, filters map[string][]string)
}
return filterFuncs, nil
}
+
+func CreateContainer(ctx context.Context, w http.ResponseWriter, runtime *libpod.Runtime, cc *createconfig.CreateConfig) {
+ var pod *libpod.Pod
+ ctr, err := shared.CreateContainerFromCreateConfig(runtime, cc, ctx, pod)
+ if err != nil {
+ Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "CreateContainerFromCreateConfig()"))
+ return
+ }
+
+ response := ContainerCreateResponse{
+ ID: ctr.ID(),
+ Warnings: []string{}}
+
+ WriteResponse(w, http.StatusCreated, response)
+}