github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/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/hanks177/podman/v4/libpod"
    10  	"github.com/hanks177/podman/v4/libpod/define"
    11  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
    12  	api "github.com/hanks177/podman/v4/pkg/api/types"
    13  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    14  	"github.com/hanks177/podman/v4/pkg/domain/entities/types"
    15  	"github.com/hanks177/podman/v4/version"
    16  	"github.com/pkg/errors"
    17  	"github.com/sirupsen/logrus"
    18  )
    19  
    20  func VersionHandler(w http.ResponseWriter, r *http.Request) {
    21  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    22  
    23  	running, err := define.GetVersion()
    24  	if err != nil {
    25  		utils.Error(w, http.StatusInternalServerError, err)
    26  		return
    27  	}
    28  
    29  	info, err := runtime.Info()
    30  	if err != nil {
    31  		utils.Error(w, http.StatusInternalServerError, errors.Wrapf(err, "failed to obtain system memory info"))
    32  		return
    33  	}
    34  
    35  	components := []types.ComponentVersion{{
    36  		Name:    "Podman Engine",
    37  		Version: running.Version,
    38  		Details: map[string]string{
    39  			"APIVersion":    version.APIVersion[version.Libpod][version.CurrentAPI].String(),
    40  			"Arch":          goRuntime.GOARCH,
    41  			"BuildTime":     time.Unix(running.Built, 0).Format(time.RFC3339),
    42  			"Experimental":  "false",
    43  			"GitCommit":     running.GitCommit,
    44  			"GoVersion":     running.GoVersion,
    45  			"KernelVersion": info.Host.Kernel,
    46  			"MinAPIVersion": version.APIVersion[version.Libpod][version.MinimalAPI].String(),
    47  			"Os":            goRuntime.GOOS,
    48  		},
    49  	}}
    50  
    51  	if conmon, oci, err := runtime.DefaultOCIRuntime().RuntimeInfo(); err != nil {
    52  		logrus.Warnf("Failed to retrieve Conmon and OCI Information: %q", err.Error())
    53  	} else {
    54  		additional := []types.ComponentVersion{
    55  			{
    56  				Name:    "Conmon",
    57  				Version: conmon.Version,
    58  				Details: map[string]string{
    59  					"Package": conmon.Package,
    60  				},
    61  			},
    62  			{
    63  				Name:    fmt.Sprintf("OCI Runtime (%s)", oci.Name),
    64  				Version: oci.Version,
    65  				Details: map[string]string{
    66  					"Package": oci.Package,
    67  				},
    68  			},
    69  		}
    70  		components = append(components, additional...)
    71  	}
    72  
    73  	apiVersion := version.APIVersion[version.Compat][version.CurrentAPI]
    74  	minVersion := version.APIVersion[version.Compat][version.MinimalAPI]
    75  
    76  	utils.WriteResponse(w, http.StatusOK, entities.ComponentVersion{
    77  		Version: types.Version{
    78  			Platform: struct {
    79  				Name string
    80  			}{
    81  				Name: fmt.Sprintf("%s/%s/%s-%s", goRuntime.GOOS, goRuntime.GOARCH, info.Host.Distribution.Distribution, info.Host.Distribution.Version),
    82  			},
    83  			APIVersion:    fmt.Sprintf("%d.%d", apiVersion.Major, apiVersion.Minor),
    84  			Arch:          components[0].Details["Arch"],
    85  			BuildTime:     components[0].Details["BuildTime"],
    86  			Components:    components,
    87  			Experimental:  false,
    88  			GitCommit:     components[0].Details["GitCommit"],
    89  			GoVersion:     components[0].Details["GoVersion"],
    90  			KernelVersion: components[0].Details["KernelVersion"],
    91  			MinAPIVersion: fmt.Sprintf("%d.%d", minVersion.Major, minVersion.Minor),
    92  			Os:            components[0].Details["Os"],
    93  			Version:       components[0].Version,
    94  		},
    95  	})
    96  }