podman
196 строк · 5.3 Кб
1// Copyright 2013 go-dockerclient authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package docker
6
7import (
8"context"
9"encoding/json"
10"net"
11"net/http"
12"strings"
13
14"github.com/docker/docker/api/types/swarm"
15)
16
17// Version returns version information about the docker server.
18//
19// See https://goo.gl/mU7yje for more details.
20func (c *Client) Version() (*Env, error) {
21return c.VersionWithContext(context.TODO())
22}
23
24// VersionWithContext returns version information about the docker server.
25func (c *Client) VersionWithContext(ctx context.Context) (*Env, error) {
26resp, err := c.do(http.MethodGet, "/version", doOptions{context: ctx})
27if err != nil {
28return nil, err
29}
30defer resp.Body.Close()
31var env Env
32if err := env.Decode(resp.Body); err != nil {
33return nil, err
34}
35return &env, nil
36}
37
38// DockerInfo contains information about the Docker server
39//
40// See https://goo.gl/bHUoz9 for more details.
41type DockerInfo struct {
42ID string
43Containers int
44ContainersRunning int
45ContainersPaused int
46ContainersStopped int
47Images int
48Driver string
49DriverStatus [][2]string
50SystemStatus [][2]string
51Plugins PluginsInfo
52NFd int
53NGoroutines int
54SystemTime string
55ExecutionDriver string
56LoggingDriver string
57CgroupDriver string
58NEventsListener int
59KernelVersion string
60OperatingSystem string
61OSType string
62Architecture string
63IndexServerAddress string
64RegistryConfig *ServiceConfig
65SecurityOptions []string
66NCPU int
67MemTotal int64
68DockerRootDir string
69HTTPProxy string `json:"HttpProxy"`
70HTTPSProxy string `json:"HttpsProxy"`
71NoProxy string
72Name string
73Labels []string
74ServerVersion string
75ClusterStore string
76Runtimes map[string]Runtime
77ClusterAdvertise string
78Isolation string
79InitBinary string
80DefaultRuntime string
81Swarm swarm.Info
82LiveRestoreEnabled bool
83MemoryLimit bool
84SwapLimit bool
85KernelMemory bool
86CPUCfsPeriod bool `json:"CpuCfsPeriod"`
87CPUCfsQuota bool `json:"CpuCfsQuota"`
88CPUShares bool
89CPUSet bool
90IPv4Forwarding bool
91BridgeNfIptables bool
92BridgeNfIP6tables bool `json:"BridgeNfIp6tables"`
93Debug bool
94OomKillDisable bool
95ExperimentalBuild bool
96}
97
98// Runtime describes an OCI runtime
99//
100// for more information, see: https://dockr.ly/2NKM8qq
101type Runtime struct {
102Path string
103Args []string `json:"runtimeArgs"`
104}
105
106// PluginsInfo is a struct with the plugins registered with the docker daemon
107//
108// for more information, see: https://goo.gl/bHUoz9
109type PluginsInfo struct {
110// List of Volume plugins registered
111Volume []string
112// List of Network plugins registered
113Network []string
114// List of Authorization plugins registered
115Authorization []string
116}
117
118// ServiceConfig stores daemon registry services configuration.
119//
120// for more information, see: https://goo.gl/7iFFDz
121type ServiceConfig struct {
122InsecureRegistryCIDRs []*NetIPNet
123IndexConfigs map[string]*IndexInfo
124Mirrors []string
125}
126
127// NetIPNet is the net.IPNet type, which can be marshalled and
128// unmarshalled to JSON.
129//
130// for more information, see: https://goo.gl/7iFFDz
131type NetIPNet net.IPNet
132
133// MarshalJSON returns the JSON representation of the IPNet.
134func (ipnet *NetIPNet) MarshalJSON() ([]byte, error) {
135return json.Marshal((*net.IPNet)(ipnet).String())
136}
137
138// UnmarshalJSON sets the IPNet from a byte array of JSON.
139func (ipnet *NetIPNet) UnmarshalJSON(b []byte) (err error) {
140var ipnetStr string
141if err = json.Unmarshal(b, &ipnetStr); err == nil {
142var cidr *net.IPNet
143if _, cidr, err = net.ParseCIDR(ipnetStr); err == nil {
144*ipnet = NetIPNet(*cidr)
145}
146}
147return
148}
149
150// IndexInfo contains information about a registry.
151//
152// for more information, see: https://goo.gl/7iFFDz
153type IndexInfo struct {
154Name string
155Mirrors []string
156Secure bool
157Official bool
158}
159
160// Info returns system-wide information about the Docker server.
161//
162// See https://goo.gl/ElTHi2 for more details.
163func (c *Client) Info() (*DockerInfo, error) {
164resp, err := c.do(http.MethodGet, "/info", doOptions{})
165if err != nil {
166return nil, err
167}
168defer resp.Body.Close()
169var info DockerInfo
170if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
171return nil, err
172}
173return &info, nil
174}
175
176// ParseRepositoryTag gets the name of the repository and returns it splitted
177// in two parts: the repository and the tag. It ignores the digest when it is
178// present.
179//
180// Some examples:
181//
182// localhost.localdomain:5000/samalba/hipache:latest -> localhost.localdomain:5000/samalba/hipache, latest
183// localhost.localdomain:5000/samalba/hipache -> localhost.localdomain:5000/samalba/hipache, ""
184// busybox:latest@sha256:4a731fb46adc5cefe3ae374a8b6020fc1b6ad667a279647766e9a3cd89f6fa92 -> busybox, latest
185func ParseRepositoryTag(repoTag string) (repository string, tag string) {
186parts := strings.SplitN(repoTag, "@", 2)
187repoTag = parts[0]
188n := strings.LastIndex(repoTag, ":")
189if n < 0 {
190return repoTag, ""
191}
192if tag := repoTag[n+1:]; !strings.Contains(tag, "/") {
193return repoTag[:n], tag
194}
195return repoTag, ""
196}
197