2016-09-23 20:43:03 +00:00
|
|
|
// Copyright 2013 go-dockerclient authors. All rights reserved.
|
2016-02-12 18:02:16 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package docker
|
|
|
|
|
2016-03-25 00:42:00 +00:00
|
|
|
import (
|
2018-11-29 22:08:16 +00:00
|
|
|
"context"
|
2016-03-25 00:42:00 +00:00
|
|
|
"encoding/json"
|
2017-02-15 01:34:05 +00:00
|
|
|
"net"
|
2020-03-22 08:25:46 +00:00
|
|
|
"net/http"
|
2016-03-25 00:42:00 +00:00
|
|
|
"strings"
|
2016-09-23 20:43:03 +00:00
|
|
|
|
2017-02-15 01:34:05 +00:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-03-25 00:42:00 +00:00
|
|
|
)
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
// Version returns version information about the docker server.
|
|
|
|
//
|
2017-02-15 01:34:05 +00:00
|
|
|
// See https://goo.gl/mU7yje for more details.
|
2016-02-12 18:02:16 +00:00
|
|
|
func (c *Client) Version() (*Env, error) {
|
2020-03-22 08:25:46 +00:00
|
|
|
return c.VersionWithContext(context.TODO())
|
2018-11-29 22:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VersionWithContext returns version information about the docker server.
|
|
|
|
func (c *Client) VersionWithContext(ctx context.Context) (*Env, error) {
|
2020-03-22 08:25:46 +00:00
|
|
|
resp, err := c.do(http.MethodGet, "/version", doOptions{context: ctx})
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var env Env
|
|
|
|
if err := env.Decode(resp.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &env, nil
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:42:00 +00:00
|
|
|
// DockerInfo contains information about the Docker server
|
|
|
|
//
|
|
|
|
// See https://goo.gl/bHUoz9 for more details.
|
2020-03-22 08:25:46 +00:00
|
|
|
//nolint:golint
|
2016-03-25 00:42:00 +00:00
|
|
|
type DockerInfo struct {
|
|
|
|
ID string
|
|
|
|
Containers int
|
|
|
|
ContainersRunning int
|
|
|
|
ContainersPaused int
|
|
|
|
ContainersStopped int
|
|
|
|
Images int
|
|
|
|
Driver string
|
|
|
|
DriverStatus [][2]string
|
|
|
|
SystemStatus [][2]string
|
|
|
|
Plugins PluginsInfo
|
2016-09-23 20:43:03 +00:00
|
|
|
NFd int
|
2016-03-25 00:42:00 +00:00
|
|
|
NGoroutines int
|
|
|
|
SystemTime string
|
|
|
|
ExecutionDriver string
|
|
|
|
LoggingDriver string
|
|
|
|
CgroupDriver string
|
|
|
|
NEventsListener int
|
|
|
|
KernelVersion string
|
|
|
|
OperatingSystem string
|
|
|
|
OSType string
|
|
|
|
Architecture string
|
|
|
|
IndexServerAddress string
|
2017-02-15 01:34:05 +00:00
|
|
|
RegistryConfig *ServiceConfig
|
2017-11-14 23:27:57 +00:00
|
|
|
SecurityOptions []string
|
2016-03-25 00:42:00 +00:00
|
|
|
NCPU int
|
|
|
|
MemTotal int64
|
|
|
|
DockerRootDir string
|
|
|
|
HTTPProxy string `json:"HttpProxy"`
|
|
|
|
HTTPSProxy string `json:"HttpsProxy"`
|
|
|
|
NoProxy string
|
|
|
|
Name string
|
|
|
|
Labels []string
|
|
|
|
ServerVersion string
|
|
|
|
ClusterStore string
|
2018-11-29 22:08:16 +00:00
|
|
|
Runtimes map[string]Runtime
|
2016-03-25 00:42:00 +00:00
|
|
|
ClusterAdvertise string
|
2017-08-10 16:19:19 +00:00
|
|
|
Isolation string
|
|
|
|
InitBinary string
|
|
|
|
DefaultRuntime string
|
2016-09-23 20:43:03 +00:00
|
|
|
Swarm swarm.Info
|
2020-03-22 08:25:46 +00:00
|
|
|
LiveRestoreEnabled bool
|
|
|
|
MemoryLimit bool
|
|
|
|
SwapLimit bool
|
|
|
|
KernelMemory bool
|
|
|
|
CPUCfsPeriod bool `json:"CpuCfsPeriod"`
|
|
|
|
CPUCfsQuota bool `json:"CpuCfsQuota"`
|
|
|
|
CPUShares bool
|
|
|
|
CPUSet bool
|
|
|
|
IPv4Forwarding bool
|
|
|
|
BridgeNfIptables bool
|
|
|
|
BridgeNfIP6tables bool `json:"BridgeNfIp6tables"`
|
|
|
|
Debug bool
|
|
|
|
OomKillDisable bool
|
|
|
|
ExperimentalBuild bool
|
2016-03-25 00:42:00 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 22:08:16 +00:00
|
|
|
// Runtime describes an OCI runtime
|
|
|
|
//
|
|
|
|
// for more information, see: https://dockr.ly/2NKM8qq
|
|
|
|
type Runtime struct {
|
|
|
|
Path string
|
|
|
|
Args []string `json:"runtimeArgs"`
|
|
|
|
}
|
|
|
|
|
2016-03-25 00:42:00 +00:00
|
|
|
// PluginsInfo is a struct with the plugins registered with the docker daemon
|
|
|
|
//
|
|
|
|
// for more information, see: https://goo.gl/bHUoz9
|
|
|
|
type PluginsInfo struct {
|
|
|
|
// List of Volume plugins registered
|
|
|
|
Volume []string
|
|
|
|
// List of Network plugins registered
|
|
|
|
Network []string
|
|
|
|
// List of Authorization plugins registered
|
|
|
|
Authorization []string
|
|
|
|
}
|
|
|
|
|
2017-02-15 01:34:05 +00:00
|
|
|
// ServiceConfig stores daemon registry services configuration.
|
|
|
|
//
|
|
|
|
// for more information, see: https://goo.gl/7iFFDz
|
|
|
|
type ServiceConfig struct {
|
|
|
|
InsecureRegistryCIDRs []*NetIPNet
|
|
|
|
IndexConfigs map[string]*IndexInfo
|
|
|
|
Mirrors []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetIPNet is the net.IPNet type, which can be marshalled and
|
|
|
|
// unmarshalled to JSON.
|
|
|
|
//
|
|
|
|
// for more information, see: https://goo.gl/7iFFDz
|
|
|
|
type NetIPNet net.IPNet
|
|
|
|
|
|
|
|
// MarshalJSON returns the JSON representation of the IPNet.
|
|
|
|
//
|
|
|
|
func (ipnet *NetIPNet) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal((*net.IPNet)(ipnet).String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON sets the IPNet from a byte array of JSON.
|
|
|
|
//
|
|
|
|
func (ipnet *NetIPNet) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
var ipnetStr string
|
|
|
|
if err = json.Unmarshal(b, &ipnetStr); err == nil {
|
|
|
|
var cidr *net.IPNet
|
|
|
|
if _, cidr, err = net.ParseCIDR(ipnetStr); err == nil {
|
|
|
|
*ipnet = NetIPNet(*cidr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// IndexInfo contains information about a registry.
|
|
|
|
//
|
|
|
|
// for more information, see: https://goo.gl/7iFFDz
|
|
|
|
type IndexInfo struct {
|
|
|
|
Name string
|
|
|
|
Mirrors []string
|
|
|
|
Secure bool
|
|
|
|
Official bool
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:02:16 +00:00
|
|
|
// Info returns system-wide information about the Docker server.
|
|
|
|
//
|
|
|
|
// See https://goo.gl/ElTHi2 for more details.
|
2016-03-25 00:42:00 +00:00
|
|
|
func (c *Client) Info() (*DockerInfo, error) {
|
2020-03-22 08:25:46 +00:00
|
|
|
resp, err := c.do(http.MethodGet, "/info", doOptions{})
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2016-03-25 00:42:00 +00:00
|
|
|
var info DockerInfo
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
|
2016-02-12 18:02:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRepositoryTag gets the name of the repository and returns it splitted
|
2016-09-23 20:43:03 +00:00
|
|
|
// in two parts: the repository and the tag. It ignores the digest when it is
|
|
|
|
// present.
|
2016-02-12 18:02:16 +00:00
|
|
|
//
|
|
|
|
// Some examples:
|
|
|
|
//
|
|
|
|
// localhost.localdomain:5000/samalba/hipache:latest -> localhost.localdomain:5000/samalba/hipache, latest
|
|
|
|
// localhost.localdomain:5000/samalba/hipache -> localhost.localdomain:5000/samalba/hipache, ""
|
2016-09-23 20:43:03 +00:00
|
|
|
// busybox:latest@sha256:4a731fb46adc5cefe3ae374a8b6020fc1b6ad667a279647766e9a3cd89f6fa92 -> busybox, latest
|
2016-02-12 18:02:16 +00:00
|
|
|
func ParseRepositoryTag(repoTag string) (repository string, tag string) {
|
2016-09-23 20:43:03 +00:00
|
|
|
parts := strings.SplitN(repoTag, "@", 2)
|
|
|
|
repoTag = parts[0]
|
2016-02-12 18:02:16 +00:00
|
|
|
n := strings.LastIndex(repoTag, ":")
|
|
|
|
if n < 0 {
|
|
|
|
return repoTag, ""
|
|
|
|
}
|
|
|
|
if tag := repoTag[n+1:]; !strings.Contains(tag, "/") {
|
|
|
|
return repoTag[:n], tag
|
|
|
|
}
|
|
|
|
return repoTag, ""
|
|
|
|
}
|