2018-07-11 08:39:20 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-20 04:13:44 +00:00
|
|
|
"log"
|
2020-02-04 18:59:29 +00:00
|
|
|
"strings"
|
2018-07-11 08:39:20 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2022-07-17 23:02:14 +00:00
|
|
|
_container "github.com/docker/docker/api/types/container"
|
2018-07-11 08:39:20 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/client"
|
2022-07-17 23:02:14 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2018-07-11 08:39:20 +00:00
|
|
|
)
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// Snapshotter represents a service used to create environment(endpoint) snapshots
|
2020-07-05 23:21:03 +00:00
|
|
|
type Snapshotter struct {
|
|
|
|
clientFactory *ClientFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSnapshotter returns a new Snapshotter instance
|
|
|
|
func NewSnapshotter(clientFactory *ClientFactory) *Snapshotter {
|
|
|
|
return &Snapshotter{
|
|
|
|
clientFactory: clientFactory,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// CreateSnapshot creates a snapshot of a specific Docker environment(endpoint)
|
2020-07-05 23:21:03 +00:00
|
|
|
func (snapshotter *Snapshotter) CreateSnapshot(endpoint *portainer.Endpoint) (*portainer.DockerSnapshot, error) {
|
2022-01-23 19:48:04 +00:00
|
|
|
cli, err := snapshotter.clientFactory.CreateClient(endpoint, "", nil)
|
2020-07-05 23:21:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
return snapshot(cli, endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
func snapshot(cli *client.Client, endpoint *portainer.Endpoint) (*portainer.DockerSnapshot, error) {
|
2018-07-11 08:39:20 +00:00
|
|
|
_, err := cli.Ping(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
snapshot := &portainer.DockerSnapshot{
|
2018-07-11 08:39:20 +00:00
|
|
|
StackCount: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snapshotInfo(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot engine information] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if snapshot.Swarm {
|
|
|
|
err = snapshotSwarmServices(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot Swarm services] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
2018-08-13 19:20:56 +00:00
|
|
|
|
|
|
|
err = snapshotNodes(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot Swarm nodes] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-08-13 19:20:56 +00:00
|
|
|
}
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = snapshotContainers(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot containers] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = snapshotImages(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot images] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = snapshotVolumes(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot volumes] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-28 09:27:06 +00:00
|
|
|
err = snapshotNetworks(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot networks] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-10-28 09:27:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = snapshotVersion(snapshot, cli)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot engine version] [environment: %s] [err: %s]", endpoint.Name, err)
|
2018-10-28 09:27:06 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
snapshot.Time = time.Now().Unix()
|
|
|
|
return snapshot, nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotInfo(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-07-11 08:39:20 +00:00
|
|
|
info, err := cli.Info(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.Swarm = info.Swarm.ControlAvailable
|
|
|
|
snapshot.DockerVersion = info.ServerVersion
|
|
|
|
snapshot.TotalCPU = info.NCPU
|
|
|
|
snapshot.TotalMemory = info.MemTotal
|
2018-10-28 09:27:06 +00:00
|
|
|
snapshot.SnapshotRaw.Info = info
|
2018-07-11 08:39:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotNodes(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-08-13 19:20:56 +00:00
|
|
|
nodes, err := cli.NodeList(context.Background(), types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var nanoCpus int64
|
|
|
|
var totalMem int64
|
|
|
|
for _, node := range nodes {
|
|
|
|
nanoCpus += node.Description.Resources.NanoCPUs
|
|
|
|
totalMem += node.Description.Resources.MemoryBytes
|
|
|
|
}
|
|
|
|
snapshot.TotalCPU = int(nanoCpus / 1e9)
|
|
|
|
snapshot.TotalMemory = totalMem
|
2021-03-04 15:42:47 +00:00
|
|
|
snapshot.NodeCount = len(nodes)
|
2018-08-13 19:20:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotSwarmServices(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-07-11 08:39:20 +00:00
|
|
|
stacks := make(map[string]struct{})
|
|
|
|
|
|
|
|
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
for k, v := range service.Spec.Labels {
|
|
|
|
if k == "com.docker.stack.namespace" {
|
|
|
|
stacks[v] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.ServiceCount = len(services)
|
|
|
|
snapshot.StackCount += len(stacks)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotContainers(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-07-11 08:39:20 +00:00
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
runningContainers := 0
|
|
|
|
stoppedContainers := 0
|
2020-02-04 18:59:29 +00:00
|
|
|
healthyContainers := 0
|
|
|
|
unhealthyContainers := 0
|
2018-07-11 08:39:20 +00:00
|
|
|
stacks := make(map[string]struct{})
|
2022-07-17 23:02:14 +00:00
|
|
|
gpuUseSet := make(map[string]struct{})
|
|
|
|
gpuUseAll := false
|
2018-07-11 08:39:20 +00:00
|
|
|
for _, container := range containers {
|
|
|
|
if container.State == "exited" {
|
|
|
|
stoppedContainers++
|
|
|
|
} else if container.State == "running" {
|
|
|
|
runningContainers++
|
2022-07-17 23:02:14 +00:00
|
|
|
|
|
|
|
// snapshot GPUs
|
|
|
|
response, err := cli.ContainerInspect(context.Background(), container.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var gpuOptions *_container.DeviceRequest = nil
|
|
|
|
for _, deviceRequest := range response.HostConfig.Resources.DeviceRequests {
|
|
|
|
if deviceRequest.Driver == "nvidia" || deviceRequest.Capabilities[0][0] == "gpu" {
|
|
|
|
gpuOptions = &deviceRequest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if gpuOptions != nil {
|
|
|
|
if gpuOptions.Count == -1 {
|
|
|
|
gpuUseAll = true
|
|
|
|
}
|
|
|
|
for _, id := range gpuOptions.DeviceIDs {
|
|
|
|
gpuUseSet[id] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 08:39:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 18:59:29 +00:00
|
|
|
if strings.Contains(container.Status, "(healthy)") {
|
|
|
|
healthyContainers++
|
|
|
|
} else if strings.Contains(container.Status, "(unhealthy)") {
|
|
|
|
unhealthyContainers++
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
for k, v := range container.Labels {
|
|
|
|
if k == "com.docker.compose.project" {
|
|
|
|
stacks[v] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 23:02:14 +00:00
|
|
|
gpuUseList := make([]string, 0, len(gpuUseSet))
|
|
|
|
for gpuUse := range gpuUseSet {
|
|
|
|
gpuUseList = append(gpuUseList, gpuUse)
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.GpuUseAll = gpuUseAll
|
|
|
|
snapshot.GpuUseList = gpuUseList
|
|
|
|
|
2018-07-11 08:39:20 +00:00
|
|
|
snapshot.RunningContainerCount = runningContainers
|
|
|
|
snapshot.StoppedContainerCount = stoppedContainers
|
2020-02-04 18:59:29 +00:00
|
|
|
snapshot.HealthyContainerCount = healthyContainers
|
|
|
|
snapshot.UnhealthyContainerCount = unhealthyContainers
|
2018-07-11 08:39:20 +00:00
|
|
|
snapshot.StackCount += len(stacks)
|
2018-10-28 09:27:06 +00:00
|
|
|
snapshot.SnapshotRaw.Containers = containers
|
2018-07-11 08:39:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotImages(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-07-11 08:39:20 +00:00
|
|
|
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.ImageCount = len(images)
|
2018-10-28 09:27:06 +00:00
|
|
|
snapshot.SnapshotRaw.Images = images
|
2018-07-11 08:39:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotVolumes(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-07-11 08:39:20 +00:00
|
|
|
volumes, err := cli.VolumeList(context.Background(), filters.Args{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.VolumeCount = len(volumes.Volumes)
|
2018-10-28 09:27:06 +00:00
|
|
|
snapshot.SnapshotRaw.Volumes = volumes
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotNetworks(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-10-28 09:27:06 +00:00
|
|
|
networks, err := cli.NetworkList(context.Background(), types.NetworkListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snapshot.SnapshotRaw.Networks = networks
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:21:03 +00:00
|
|
|
func snapshotVersion(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
2018-10-28 09:27:06 +00:00
|
|
|
version, err := cli.ServerVersion(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snapshot.SnapshotRaw.Version = version
|
2018-07-11 08:39:20 +00:00
|
|
|
return nil
|
|
|
|
}
|