github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/compat/containers_stop.go (about)

     1  package compat
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/hanks177/podman/v4/libpod"
     7  	"github.com/hanks177/podman/v4/libpod/define"
     8  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
     9  	api "github.com/hanks177/podman/v4/pkg/api/types"
    10  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    11  	"github.com/hanks177/podman/v4/pkg/domain/infra/abi"
    12  	"github.com/gorilla/schema"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func StopContainer(w http.ResponseWriter, r *http.Request) {
    17  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    18  	decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
    19  	// Now use the ABI implementation to prevent us from having duplicate
    20  	// code.
    21  	containerEngine := abi.ContainerEngine{Libpod: runtime}
    22  
    23  	// /{version}/containers/(name)/stop
    24  	query := struct {
    25  		Ignore        bool `schema:"ignore"`
    26  		DockerTimeout uint `schema:"t"`
    27  		LibpodTimeout uint `schema:"timeout"`
    28  	}{
    29  		// override any golang type defaults
    30  	}
    31  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    32  		utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    33  		return
    34  	}
    35  
    36  	name := utils.GetName(r)
    37  
    38  	options := entities.StopOptions{
    39  		Ignore: query.Ignore,
    40  	}
    41  	if utils.IsLibpodRequest(r) {
    42  		if _, found := r.URL.Query()["timeout"]; found {
    43  			options.Timeout = &query.LibpodTimeout
    44  		}
    45  	} else {
    46  		if _, found := r.URL.Query()["t"]; found {
    47  			options.Timeout = &query.DockerTimeout
    48  		}
    49  	}
    50  	con, err := runtime.LookupContainer(name)
    51  	if err != nil {
    52  		utils.ContainerNotFound(w, name, err)
    53  		return
    54  	}
    55  	state, err := con.State()
    56  	if err != nil {
    57  		utils.InternalServerError(w, err)
    58  		return
    59  	}
    60  	if state == define.ContainerStateStopped || state == define.ContainerStateExited {
    61  		utils.WriteResponse(w, http.StatusNotModified, nil)
    62  		return
    63  	}
    64  	report, err := containerEngine.ContainerStop(r.Context(), []string{name}, options)
    65  	if err != nil {
    66  		if errors.Cause(err) == define.ErrNoSuchCtr {
    67  			utils.ContainerNotFound(w, name, err)
    68  			return
    69  		}
    70  
    71  		utils.InternalServerError(w, err)
    72  		return
    73  	}
    74  
    75  	if len(report) > 0 && report[0].Err != nil {
    76  		utils.InternalServerError(w, report[0].Err)
    77  		return
    78  	}
    79  
    80  	// Success
    81  	utils.WriteResponse(w, http.StatusNoContent, nil)
    82  }