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

     1  package libpod
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/hanks177/podman/v4/libpod"
     7  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
     8  	api "github.com/hanks177/podman/v4/pkg/api/types"
     9  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    10  	"github.com/hanks177/podman/v4/pkg/domain/infra/abi"
    11  	"github.com/hanks177/podman/v4/pkg/util"
    12  	"github.com/gorilla/schema"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func GenerateSystemd(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  	query := struct {
    20  		Name             bool     `schema:"useName"`
    21  		New              bool     `schema:"new"`
    22  		NoHeader         bool     `schema:"noHeader"`
    23  		TemplateUnitFile bool     `schema:"templateUnitFile"`
    24  		RestartPolicy    *string  `schema:"restartPolicy"`
    25  		RestartSec       uint     `schema:"restartSec"`
    26  		StopTimeout      uint     `schema:"stopTimeout"`
    27  		StartTimeout     uint     `schema:"startTimeout"`
    28  		ContainerPrefix  *string  `schema:"containerPrefix"`
    29  		PodPrefix        *string  `schema:"podPrefix"`
    30  		Separator        *string  `schema:"separator"`
    31  		Wants            []string `schema:"wants"`
    32  		After            []string `schema:"after"`
    33  		Requires         []string `schema:"requires"`
    34  	}{
    35  		StartTimeout: 0,
    36  		StopTimeout:  util.DefaultContainerConfig().Engine.StopTimeout,
    37  	}
    38  
    39  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    40  		utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    41  		return
    42  	}
    43  
    44  	ContainerPrefix := "container"
    45  	if query.ContainerPrefix != nil {
    46  		ContainerPrefix = *query.ContainerPrefix
    47  	}
    48  
    49  	PodPrefix := "pod"
    50  	if query.PodPrefix != nil {
    51  		PodPrefix = *query.PodPrefix
    52  	}
    53  
    54  	Separator := "-"
    55  	if query.Separator != nil {
    56  		Separator = *query.Separator
    57  	}
    58  
    59  	containerEngine := abi.ContainerEngine{Libpod: runtime}
    60  	options := entities.GenerateSystemdOptions{
    61  		Name:             query.Name,
    62  		New:              query.New,
    63  		NoHeader:         query.NoHeader,
    64  		TemplateUnitFile: query.TemplateUnitFile,
    65  		RestartPolicy:    query.RestartPolicy,
    66  		StartTimeout:     &query.StartTimeout,
    67  		StopTimeout:      &query.StopTimeout,
    68  		ContainerPrefix:  ContainerPrefix,
    69  		PodPrefix:        PodPrefix,
    70  		Separator:        Separator,
    71  		RestartSec:       &query.RestartSec,
    72  		Wants:            query.Wants,
    73  		After:            query.After,
    74  		Requires:         query.Requires,
    75  	}
    76  
    77  	report, err := containerEngine.GenerateSystemd(r.Context(), utils.GetName(r), options)
    78  	if err != nil {
    79  		utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "error generating systemd units"))
    80  		return
    81  	}
    82  
    83  	utils.WriteResponse(w, http.StatusOK, report.Units)
    84  }
    85  
    86  func GenerateKube(w http.ResponseWriter, r *http.Request) {
    87  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    88  	decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
    89  	query := struct {
    90  		Names   []string `schema:"names"`
    91  		Service bool     `schema:"service"`
    92  	}{
    93  		// Defaults would go here.
    94  	}
    95  
    96  	if err := decoder.Decode(&query, r.URL.Query()); err != nil {
    97  		utils.Error(w, http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    98  		return
    99  	}
   100  
   101  	containerEngine := abi.ContainerEngine{Libpod: runtime}
   102  	options := entities.GenerateKubeOptions{Service: query.Service}
   103  	report, err := containerEngine.GenerateKube(r.Context(), query.Names, options)
   104  	if err != nil {
   105  		utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "error generating YAML"))
   106  		return
   107  	}
   108  
   109  	// FIXME: Content-Type is being set as application/x-tar NOT text/vnd.yaml
   110  	// https://mailarchive.ietf.org/arch/msg/media-types/e9ZNC0hDXKXeFlAVRWxLCCaG9GI/
   111  	utils.WriteResponse(w, http.StatusOK, report.Reader)
   112  }