github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/compat/version.go (about)

     1  package compat
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	goRuntime "runtime"
     7  	"time"
     8  
     9  	"github.com/containers/libpod/libpod"
    10  	"github.com/containers/libpod/libpod/define"
    11  	"github.com/containers/libpod/pkg/api/handlers"
    12  	"github.com/containers/libpod/pkg/api/handlers/utils"
    13  	docker "github.com/docker/docker/api/types"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  func VersionHandler(w http.ResponseWriter, r *http.Request) {
    18  	// 200 ok
    19  	// 500 internal
    20  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    21  
    22  	versionInfo, err := define.GetVersion()
    23  	if err != nil {
    24  		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, err)
    25  		return
    26  	}
    27  
    28  	infoData, err := runtime.Info()
    29  	if err != nil {
    30  		utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrapf(err, "Failed to obtain system memory info"))
    31  		return
    32  	}
    33  	components := []docker.ComponentVersion{{
    34  		Name:    "Podman Engine",
    35  		Version: versionInfo.Version,
    36  		Details: map[string]string{
    37  			"APIVersion":    handlers.DefaultApiVersion,
    38  			"Arch":          goRuntime.GOARCH,
    39  			"BuildTime":     time.Unix(versionInfo.Built, 0).Format(time.RFC3339),
    40  			"Experimental":  "true",
    41  			"GitCommit":     versionInfo.GitCommit,
    42  			"GoVersion":     versionInfo.GoVersion,
    43  			"KernelVersion": infoData.Host.Kernel,
    44  			"MinAPIVersion": handlers.MinimalApiVersion,
    45  			"Os":            goRuntime.GOOS,
    46  		},
    47  	}}
    48  
    49  	utils.WriteResponse(w, http.StatusOK, handlers.Version{Version: docker.Version{
    50  		Platform: struct {
    51  			Name string
    52  		}{
    53  			Name: fmt.Sprintf("%s/%s/%s-%s", goRuntime.GOOS, goRuntime.GOARCH, infoData.Host.Distribution.Distribution, infoData.Host.Distribution.Version),
    54  		},
    55  		APIVersion:    components[0].Details["APIVersion"],
    56  		Arch:          components[0].Details["Arch"],
    57  		BuildTime:     components[0].Details["BuildTime"],
    58  		Components:    components,
    59  		Experimental:  true,
    60  		GitCommit:     components[0].Details["GitCommit"],
    61  		GoVersion:     components[0].Details["GoVersion"],
    62  		KernelVersion: components[0].Details["KernelVersion"],
    63  		MinAPIVersion: components[0].Details["MinAPIVersion"],
    64  		Os:            components[0].Details["Os"],
    65  		Version:       components[0].Version,
    66  	}})
    67  }