podman

Форк
0
191 строка · 4.8 Кб
1
package buildah
2

3
import (
4
	"bufio"
5
	"bytes"
6
	"fmt"
7
	"os"
8
	"runtime"
9
	"strconv"
10
	"strings"
11

12
	internalUtil "github.com/containers/buildah/internal/util"
13
	putil "github.com/containers/buildah/pkg/util"
14
	"github.com/containers/buildah/util"
15
	"github.com/containers/common/pkg/cgroups"
16
	"github.com/containers/storage"
17
	"github.com/containers/storage/pkg/system"
18
	"github.com/containers/storage/pkg/unshare"
19
	v1 "github.com/opencontainers/image-spec/specs-go/v1"
20
	"github.com/sirupsen/logrus"
21
)
22

23
// InfoData holds the info type, i.e store, host etc and the data for each type
24
type InfoData struct {
25
	Type string
26
	Data map[string]interface{}
27
}
28

29
// Info returns the store and host information
30
func Info(store storage.Store) ([]InfoData, error) {
31
	info := []InfoData{}
32
	// get host information
33
	hostInfo := hostInfo()
34
	info = append(info, InfoData{Type: "host", Data: hostInfo})
35

36
	// get store information
37
	storeInfo, err := storeInfo(store)
38
	if err != nil {
39
		logrus.Error(err, "error getting store info")
40
	}
41
	info = append(info, InfoData{Type: "store", Data: storeInfo})
42
	return info, nil
43
}
44

45
func hostInfo() map[string]interface{} {
46
	info := map[string]interface{}{}
47
	ps := internalUtil.NormalizePlatform(v1.Platform{OS: runtime.GOOS, Architecture: runtime.GOARCH})
48
	info["os"] = ps.OS
49
	info["arch"] = ps.Architecture
50
	info["variant"] = ps.Variant
51
	info["cpus"] = runtime.NumCPU()
52
	info["rootless"] = unshare.IsRootless()
53

54
	unified, err := cgroups.IsCgroup2UnifiedMode()
55
	if err != nil {
56
		logrus.Error(err, "err reading cgroups mode")
57
	}
58
	cgroupVersion := "v1"
59
	ociruntime := util.Runtime()
60
	if unified {
61
		cgroupVersion = "v2"
62
	}
63
	info["CgroupVersion"] = cgroupVersion
64
	info["OCIRuntime"] = ociruntime
65

66
	mi, err := system.ReadMemInfo()
67
	if err != nil {
68
		logrus.Error(err, "err reading memory info")
69
		info["MemTotal"] = ""
70
		info["MemFree"] = ""
71
		info["SwapTotal"] = ""
72
		info["SwapFree"] = ""
73
	} else {
74
		info["MemTotal"] = mi.MemTotal
75
		info["MemFree"] = mi.MemFree
76
		info["SwapTotal"] = mi.SwapTotal
77
		info["SwapFree"] = mi.SwapFree
78
	}
79
	hostDistributionInfo := getHostDistributionInfo()
80
	info["Distribution"] = map[string]interface{}{
81
		"distribution": hostDistributionInfo["Distribution"],
82
		"version":      hostDistributionInfo["Version"],
83
	}
84

85
	kv, err := putil.ReadKernelVersion()
86
	if err != nil {
87
		logrus.Error(err, "error reading kernel version")
88
	}
89
	info["kernel"] = kv
90

91
	upDuration, err := putil.ReadUptime()
92
	if err != nil {
93
		logrus.Error(err, "error reading up time")
94
	}
95

96
	hoursFound := false
97
	var timeBuffer bytes.Buffer
98
	var hoursBuffer bytes.Buffer
99
	for _, elem := range upDuration.String() {
100
		timeBuffer.WriteRune(elem)
101
		if elem == 'h' || elem == 'm' {
102
			timeBuffer.WriteRune(' ')
103
			if elem == 'h' {
104
				hoursFound = true
105
			}
106
		}
107
		if !hoursFound {
108
			hoursBuffer.WriteRune(elem)
109
		}
110
	}
111

112
	info["uptime"] = timeBuffer.String()
113
	if hoursFound {
114
		hours, err := strconv.ParseFloat(hoursBuffer.String(), 64)
115
		if err == nil {
116
			days := hours / 24
117
			info["uptime"] = fmt.Sprintf("%s (Approximately %.2f days)", info["uptime"], days)
118
		}
119
	}
120

121
	host, err := os.Hostname()
122
	if err != nil {
123
		logrus.Error(err, "error getting hostname")
124
	}
125
	info["hostname"] = host
126

127
	return info
128
}
129

130
// top-level "store" info
131
func storeInfo(store storage.Store) (map[string]interface{}, error) {
132
	// lets say storage driver in use, number of images, number of containers
133
	info := map[string]interface{}{}
134
	info["GraphRoot"] = store.GraphRoot()
135
	info["RunRoot"] = store.RunRoot()
136
	info["GraphDriverName"] = store.GraphDriverName()
137
	info["GraphOptions"] = store.GraphOptions()
138
	statusPairs, err := store.Status()
139
	if err != nil {
140
		return nil, err
141
	}
142
	status := map[string]string{}
143
	for _, pair := range statusPairs {
144
		status[pair[0]] = pair[1]
145
	}
146
	info["GraphStatus"] = status
147
	images, err := store.Images()
148
	if err != nil {
149
		logrus.Error(err, "error getting number of images")
150
	}
151
	info["ImageStore"] = map[string]interface{}{
152
		"number": len(images),
153
	}
154

155
	containers, err := store.Containers()
156
	if err != nil {
157
		logrus.Error(err, "error getting number of containers")
158
	}
159
	info["ContainerStore"] = map[string]interface{}{
160
		"number": len(containers),
161
	}
162

163
	return info, nil
164
}
165

166
// getHostDistributionInfo returns a map containing the host's distribution and version
167
func getHostDistributionInfo() map[string]string {
168
	dist := make(map[string]string)
169

170
	// Populate values in case we cannot find the values
171
	// or the file
172
	dist["Distribution"] = "unknown"
173
	dist["Version"] = "unknown"
174

175
	f, err := os.Open("/etc/os-release")
176
	if err != nil {
177
		return dist
178
	}
179
	defer f.Close()
180

181
	l := bufio.NewScanner(f)
182
	for l.Scan() {
183
		if strings.HasPrefix(l.Text(), "ID=") {
184
			dist["Distribution"] = strings.TrimPrefix(l.Text(), "ID=")
185
		}
186
		if strings.HasPrefix(l.Text(), "VERSION_ID=") {
187
			dist["Version"] = strings.Trim(strings.TrimPrefix(l.Text(), "VERSION_ID="), "\"")
188
		}
189
	}
190
	return dist
191
}
192

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.