mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
* feat(openamt): add AMT Devices information in Environments view [INT-8] (#6169) * feat(openamt): add AMT Devices Ouf of Band Managamenet actions [INT-9] (#6171) * feat(openamt): add AMT Devices KVM Connection [INT-10] (#6179) * feat(openamt): Enhance the Environments MX to activate OpenAMT on compatible environments [INT-7] (#6196) * feat(openamt): Enable KVM by default [INT-25] (#6228) * feat(fdo): implement the FDO configuration settings INT-19 (#6238) feat(fdo): implement the FDO configuration settings INT-19 * feat(fdo): implement Owner client INT-17 (#6231) feat(fdo): implement Owner client INT-17 * feat(openamt): hide wireless config in OpenAMT form (#6250) * feat(openamt): Increase OpenAMT timeouts [INT-30] (#6253) * feat(openamt): Disable the ability to use KVM and OOB actions on a MPS disconnected device [INT-36] (#6254) * feat(fdo): add import device UI [INT-20] (#6240) feat(fdo): add import device UI INT-20 * refactor(fdo): fix develop merge issues * feat(openamt): Do not fetch OpenAMT details for an unassociated Edge endpoint (#6273) * fix(intel): Fix switches params (#6282) * feat(openamt): preload existing AMT settings (#6283) * feat(openamt): Better UI/UX for AMT activation loading [INT-39] (#6290) * feat(openamt): Remove wireless config related code [INT-41] (#6291) * yarn install * feat(openamt): change kvm redirection for pop up, always enable features [INT-37] (#6292) * feat(openamt): change kvm redirection for pop up, always enable features [INT-37] (#6293) * feat(openmt): use .ts services with axios for OpenAMT (#6312) * Minor code cleanup. * fix(fdo): move the FDO client code to the hostmanagement folder INT-44 (#6345) * refactor(intel): Add Edge Compute Settings view (#6351) * feat(fdo): add FDO profiles INT-22 (#6363) feat(fdo): add FDO profiles INT-22 * fix(fdo): fix incorrect profile URL INT-45 (#6377) * fixed husky version * fix go.mod with go mod tidy * feat(edge): migrate OpenAMT devices views to Edge Devices [EE-2322] (#6373) * feat(intel): OpenAMT UI/UX adjustments (#6394) * only allow edge agent as edge device * show all edge agent environments on Edge Devices view * feat(fdo): add the ability to import multiple ownership vouchers at once EE-2324 (#6395) * fix(edge): settings edge compute alert (#6402) * remove pagination, add useMemo for devices result array (#6409) * feat(edge): minor Edge Devices (AMT) UI fixes (#6410) * chore(eslint): fix versions * chore(app): reformat codebase * change add edge agent modal behaviour, fix yarn.lock * fix use pagination * remove extractedTranslations folder * feat(edge): add FDO Profiles Datatable [EE-2406] (#6415) * feat(edge): add KVM workaround tooltip (#6441) * feat(edge): Add default FDO profile (#6450) * feat(edge): add settings to disable trust on first connect and enforce Edge ID INT-1 EE-2410 (#6429) Co-authored-by: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io> Co-authored-by: andres-portainer <andres-portainer@users.noreply.github.com> Co-authored-by: Chaim Lev-Ari <chiptus@gmail.com>
224 lines
6.2 KiB
Go
224 lines
6.2 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 environment(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 environment(endpoint)
|
|
func (snapshotter *Snapshotter) CreateSnapshot(endpoint *portainer.Endpoint) (*portainer.DockerSnapshot, error) {
|
|
cli, err := snapshotter.clientFactory.CreateClient(endpoint, "", nil)
|
|
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] [environment: %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] [environment: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotNodes(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot Swarm nodes] [environment: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
}
|
|
|
|
err = snapshotContainers(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot containers] [environment: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotImages(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot images] [environment: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotVolumes(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot volumes] [environment: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotNetworks(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot networks] [environment: %s] [err: %s]", endpoint.Name, err)
|
|
}
|
|
|
|
err = snapshotVersion(snapshot, cli)
|
|
if err != nil {
|
|
log.Printf("[WARN] [docker,snapshot] [message: unable to snapshot engine version] [environment: %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
|
|
}
|