github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/libpod/generate.go (about)

     1  package libpod
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/containers/podman/v2/libpod"
     7  	"github.com/containers/podman/v2/pkg/api/handlers/utils"
     8  	"github.com/containers/podman/v2/pkg/domain/entities"
     9  	"github.com/containers/podman/v2/pkg/domain/infra/abi"
    10  	"github.com/containers/podman/v2/pkg/util"
    11  	"github.com/gorilla/schema"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
    16  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    17  	decoder := r.Context().Value("decoder").(*schema.Decoder)
    18  	query := struct {
    19  		Name            bool   `schema:"useName"`
    20  		New             bool   `schema:"new"`
    21  		RestartPolicy   string `schema:"restartPolicy"`
    22  		StopTimeout     uint   `schema:"stopTimeout"`
    23  		ContainerPrefix string `schema:"containerPrefix"`
    24  		PodPrefix       string `schema:"podPrefix"`
    25  		Separator       string `schema:"separator"`
    26  	}{
    27  		RestartPolicy:   "on-failure",
    28  		StopTimeout:     util.DefaultContainerConfig().Engine.StopTimeout,
    29  		ContainerPrefix: "container",
    30  		PodPrefix:       "pod",
    31  		Separator:       "-",
    32  	}
    33  
    34  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    35  		utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
    36  			errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    37  		return
    38  	}
    39  
    40  	containerEngine := abi.ContainerEngine{Libpod: runtime}
    41  	options := entities.GenerateSystemdOptions{
    42  		Name:            query.Name,
    43  		New:             query.New,
    44  		RestartPolicy:   query.RestartPolicy,
    45  		StopTimeout:     &query.StopTimeout,
    46  		ContainerPrefix: query.ContainerPrefix,
    47  		PodPrefix:       query.PodPrefix,
    48  		Separator:       query.Separator,
    49  	}
    50  	report, err := containerEngine.GenerateSystemd(r.Context(), utils.GetName(r), options)
    51  	if err != nil {
    52  		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "error generating systemd units"))
    53  		return
    54  	}
    55  
    56  	utils.WriteResponse(w, http.StatusOK, report.Units)
    57  }
    58  
    59  func GenerateKube(w http.ResponseWriter, r *http.Request) {
    60  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    61  	decoder := r.Context().Value("decoder").(*schema.Decoder)
    62  	query := struct {
    63  		Service bool `schema:"service"`
    64  	}{
    65  		// Defaults would go here.
    66  	}
    67  
    68  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    69  		utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
    70  			errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    71  		return
    72  	}
    73  
    74  	containerEngine := abi.ContainerEngine{Libpod: runtime}
    75  	options := entities.GenerateKubeOptions{Service: query.Service}
    76  	report, err := containerEngine.GenerateKube(r.Context(), utils.GetName(r), options)
    77  	if err != nil {
    78  		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "error generating YAML"))
    79  		return
    80  	}
    81  
    82  	utils.WriteResponse(w, http.StatusOK, report.Reader)
    83  }