github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/varlinkapi/system.go (about)

     1  // +build varlink
     2  
     3  package varlinkapi
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"os"
     9  	goruntime "runtime"
    10  	"time"
    11  
    12  	"github.com/containers/image/v5/pkg/sysregistriesv2"
    13  	"github.com/containers/libpod/libpod/define"
    14  	iopodman "github.com/containers/libpod/pkg/varlink"
    15  	"github.com/sirupsen/logrus"
    16  )
    17  
    18  // GetVersion ...
    19  func (i *VarlinkAPI) GetVersion(call iopodman.VarlinkCall) error {
    20  	versionInfo, err := define.GetVersion()
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	return call.ReplyGetVersion(
    26  		versionInfo.Version,
    27  		versionInfo.GoVersion,
    28  		versionInfo.GitCommit,
    29  		time.Unix(versionInfo.Built, 0).Format(time.RFC3339),
    30  		versionInfo.OsArch,
    31  		versionInfo.RemoteAPIVersion,
    32  	)
    33  }
    34  
    35  // GetInfo returns details about the podman host and its stores
    36  func (i *VarlinkAPI) GetInfo(call iopodman.VarlinkCall) error {
    37  	versionInfo, err := define.GetVersion()
    38  	if err != nil {
    39  		return err
    40  	}
    41  	podmanInfo := iopodman.PodmanInfo{}
    42  	info, err := i.Runtime.Info()
    43  	if err != nil {
    44  		return call.ReplyErrorOccurred(err.Error())
    45  	}
    46  
    47  	distribution := iopodman.InfoDistribution{
    48  		Distribution: info.Host.Distribution.Distribution,
    49  		Version:      info.Host.Distribution.Version,
    50  	}
    51  	infoHost := iopodman.InfoHost{
    52  		Buildah_version: info.Host.BuildahVersion,
    53  		Distribution:    distribution,
    54  		Mem_free:        info.Host.MemFree,
    55  		Mem_total:       info.Host.MemTotal,
    56  		Swap_free:       info.Host.SwapFree,
    57  		Swap_total:      info.Host.SwapTotal,
    58  		Arch:            info.Host.Arch,
    59  		Cpus:            int64(info.Host.CPUs),
    60  		Hostname:        info.Host.Hostname,
    61  		Kernel:          info.Host.Kernel,
    62  		Os:              info.Host.OS,
    63  		Uptime:          info.Host.Uptime,
    64  		Eventlogger:     info.Host.EventLogger,
    65  	}
    66  	podmanInfo.Host = infoHost
    67  	pmaninfo := iopodman.InfoPodmanBinary{
    68  		Compiler:       goruntime.Compiler,
    69  		Go_version:     goruntime.Version(),
    70  		Podman_version: versionInfo.Version,
    71  		Git_commit:     versionInfo.GitCommit,
    72  	}
    73  
    74  	graphStatus := iopodman.InfoGraphStatus{
    75  		Backing_filesystem:  info.Store.GraphStatus["Backing Filesystem"],
    76  		Native_overlay_diff: info.Store.GraphStatus["Native Overlay Diff"],
    77  		Supports_d_type:     info.Store.GraphStatus["Supports d_type"],
    78  	}
    79  	infoStore := iopodman.InfoStore{
    80  		Graph_driver_name:    info.Store.GraphDriverName,
    81  		Containers:           int64(info.Store.ContainerStore.Number),
    82  		Images:               int64(info.Store.ImageStore.Number),
    83  		Run_root:             info.Store.RunRoot,
    84  		Graph_root:           info.Store.GraphRoot,
    85  		Graph_driver_options: fmt.Sprintf("%v", info.Store.GraphOptions),
    86  		Graph_status:         graphStatus,
    87  	}
    88  
    89  	// Registry information if any is stored as the second list item
    90  	for key, val := range info.Registries {
    91  		if key == "search" {
    92  			podmanInfo.Registries.Search = val.([]string)
    93  			continue
    94  		}
    95  		regData := val.(sysregistriesv2.Registry)
    96  		if regData.Insecure {
    97  			podmanInfo.Registries.Insecure = append(podmanInfo.Registries.Insecure, key)
    98  		}
    99  		if regData.Blocked {
   100  			podmanInfo.Registries.Blocked = append(podmanInfo.Registries.Blocked, key)
   101  		}
   102  	}
   103  	podmanInfo.Store = infoStore
   104  	podmanInfo.Podman = pmaninfo
   105  	return call.ReplyGetInfo(podmanInfo)
   106  }
   107  
   108  // GetVersion ...
   109  func (i *VarlinkAPI) Reset(call iopodman.VarlinkCall) error {
   110  	if err := i.Runtime.Reset(context.TODO()); err != nil {
   111  		logrus.Errorf("Reset Failed: %v", err)
   112  		if err := call.ReplyErrorOccurred(err.Error()); err != nil {
   113  			logrus.Errorf("Failed to send ReplyErrorOccurred: %v", err)
   114  		}
   115  		os.Exit(define.ExecErrorCodeGeneric)
   116  	}
   117  	if err := call.ReplyReset(); err != nil {
   118  		logrus.Errorf("Failed to send ReplyReset: %v", err)
   119  		os.Exit(define.ExecErrorCodeGeneric)
   120  	}
   121  	os.Exit(0)
   122  	return nil
   123  }