* feat(license): add liblicense dep * feat(license): add bolt license service * feat(license): introduce license service * feat(license): validate license before adding * feat(license): aggregate info after changing of licenses * feat(http): implement http handlers * feat(license-management): introduce license service * feat(licenses): introduce empty view * feat(license-management): add datatable * feat(licenses): show license info * fix(license): inject services * feat(licenses): add buttons to buy/renew license * feat(licenses): introduce add license route * feat(licenses): add license form * feat(license): datatable * feat(license): show more details about license * refactor(license): rename components name * feat(licenses): show expiration date * feat(license): introduce init license route * feat(license): validate license * feat(license): save licenses * feat(bouncer): check if license is valid on restricted * feat(bouncer): remove license check on api * feat(home): add node warning * feat(licenses): remove license * feat(licenses): listen to info changes * feat(license): show license expiration message * feat(license): block regular users from licenses view * feat(license): prevent removing of last license * fix(license): show message when failed delete * feat(license): remove trial license when applying oneoff * feat(license): hide the number of nodes for trial * feat(auth): disable login if license is invalid * feat(licenses): add confirmation before removal of license * feat(nodes): count nodes in env * feat(license): show message if nodes exceed allowed * feat(deps): update liblicense * feat(licenses): show validation errors * feat(license): use information panel for node info * fix(license): reload license data on remove * fix(license): always send list of failed keys * fix(license): rename buttons * feat(license): replace icon * feat(license): add link to licenses page in add license * fix(licenses): show green valid icon * fix(licenses): rename expires at * fix(licenses): rename Attach to add * fix(licenses): show license type label * feat(license): aggregate revoked info * chore(deps): update liblicense * fix(license): remove space * fix(sidebar): align icon * fix(license): change info layout * feat(license): aggregate only valid licenses * fix(licenses): move add license to a new line * style(license): remove console * refactor(license): move license line to component * feat(license): check server validation * fix(licenses): check form validation before submit * feat(licenses): send only invalid licenses * fix(license): hide panels when not needed * feat(licnese): receive a single license on init * refactor(header): move header to module * feat(license): move license panel to header * fix(header): set min height * fix(home): show node warning only if subscription * feat(licenses): minor UI updates * feat(licenses): minor UI update * feat(licenses-datatable): add copy button * fix(licenses-datatable): show date without hours * feat(license): show expiration message * fix(users): get user info only on restriced access * fix(license): clear check for single license Co-authored-by: Anthony Lapenna <lapenna.anthony@gmail.com>
225 lines
6.1 KiB
Go
225 lines
6.1 KiB
Go
package docker
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/client"
|
|
"github.com/portainer/portainer/api"
|
|
)
|
|
|
|
// Snapshotter represents a service used to create endpoint snapshots
|
|
type Snapshotter struct {
|
|
clientFactory *ClientFactory
|
|
}
|
|
|
|
// NewSnapshotter returns a new Snapshotter instance
|
|
func NewSnapshotter(clientFactory *ClientFactory) *Snapshotter {
|
|
return &Snapshotter{
|
|
clientFactory: clientFactory,
|
|
}
|
|
}
|
|
|
|
// CreateSnapshot creates a snapshot of a specific Docker endpoint
|
|
func (snapshotter *Snapshotter) CreateSnapshot(endpoint *portainer.Endpoint) (*portainer.DockerSnapshot, error) {
|
|
cli, err := snapshotter.clientFactory.CreateClient(endpoint, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cli.Close()
|
|
|
|
return snapshot(cli, endpoint)
|
|
}
|
|
|
|
func snapshot(cli *client.Client, endpoint *portainer.Endpoint) (*portainer.DockerSnapshot, error) {
|
|
_, err := cli.Ping(context.Background())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
snapshot := &portainer.DockerSnapshot{
|
|
StackCount: 0,
|
|
}
|
|
|
|
err = snapshotInfo(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot engine information] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
if snapshot.Swarm {
|
|
err = snapshotSwarmServices(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot Swarm services] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotNodes(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot Swarm nodes] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
}
|
|
|
|
err = snapshotContainers(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot containers] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotImages(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot images] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotVolumes(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot volumes] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotNetworks(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot networks] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotVersion(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot engine version] [endpoint: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
snapshot.Time = time.Now().Unix()
|
|
return snapshot, nil
|
|
}
|
|
|
|
func snapshotInfo(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
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
|
|
snapshot.SnapshotRaw.Info = info
|
|
return nil
|
|
}
|
|
|
|
func snapshotNodes(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
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
|
|
snapshot.NodeCount = len(nodes)
|
|
return nil
|
|
}
|
|
|
|
func snapshotSwarmServices(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
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
|
|
}
|
|
|
|
func snapshotContainers(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
runningContainers := 0
|
|
stoppedContainers := 0
|
|
healthyContainers := 0
|
|
unhealthyContainers := 0
|
|
stacks := make(map[string]struct{})
|
|
for _, container := range containers {
|
|
if container.State == "exited" {
|
|
stoppedContainers++
|
|
} else if container.State == "running" {
|
|
runningContainers++
|
|
}
|
|
|
|
if strings.Contains(container.Status, "(healthy)") {
|
|
healthyContainers++
|
|
} else if strings.Contains(container.Status, "(unhealthy)") {
|
|
unhealthyContainers++
|
|
}
|
|
|
|
for k, v := range container.Labels {
|
|
if k == "com.docker.compose.project" {
|
|
stacks[v] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
snapshot.RunningContainerCount = runningContainers
|
|
snapshot.StoppedContainerCount = stoppedContainers
|
|
snapshot.HealthyContainerCount = healthyContainers
|
|
snapshot.UnhealthyContainerCount = unhealthyContainers
|
|
snapshot.StackCount += len(stacks)
|
|
snapshot.SnapshotRaw.Containers = containers
|
|
return nil
|
|
}
|
|
|
|
func snapshotImages(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
snapshot.ImageCount = len(images)
|
|
snapshot.SnapshotRaw.Images = images
|
|
return nil
|
|
}
|
|
|
|
func snapshotVolumes(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
volumes, err := cli.VolumeList(context.Background(), filters.Args{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
snapshot.VolumeCount = len(volumes.Volumes)
|
|
snapshot.SnapshotRaw.Volumes = volumes
|
|
return nil
|
|
}
|
|
|
|
func snapshotNetworks(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
networks, err := cli.NetworkList(context.Background(), types.NetworkListOptions{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snapshot.SnapshotRaw.Networks = networks
|
|
return nil
|
|
}
|
|
|
|
func snapshotVersion(snapshot *portainer.DockerSnapshot, cli *client.Client) error {
|
|
version, err := cli.ServerVersion(context.Background())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snapshot.SnapshotRaw.Version = version
|
|
return nil
|
|
}
|