1Panel/backend/app/service/container_volume.go

118 lines
2.8 KiB
Go
Raw Normal View History

2022-10-18 18:39:45 +08:00
package service
import (
"context"
2022-12-06 18:24:26 +08:00
"sort"
2023-02-07 18:48:32 +08:00
"strings"
2022-10-18 18:39:45 +08:00
"time"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
)
2023-02-07 18:48:32 +08:00
func (u *ContainerService) PageVolume(req dto.SearchWithPage) (int64, interface{}, error) {
2022-10-18 18:39:45 +08:00
client, err := docker.NewDockerClient()
if err != nil {
return 0, nil, err
}
list, err := client.VolumeList(context.TODO(), filters.NewArgs())
if err != nil {
return 0, nil, err
}
2023-02-07 18:48:32 +08:00
if len(req.Info) != 0 {
lenth, count := len(list.Volumes), 0
for count < lenth {
if !strings.Contains(list.Volumes[count].Name, req.Info) {
list.Volumes = append(list.Volumes[:count], list.Volumes[(count+1):]...)
lenth--
} else {
count++
}
}
}
2022-10-18 18:39:45 +08:00
var (
data []dto.Volume
records []*types.Volume
)
2022-12-06 18:24:26 +08:00
sort.Slice(list.Volumes, func(i, j int) bool {
return list.Volumes[i].CreatedAt > list.Volumes[j].CreatedAt
})
2022-10-18 18:39:45 +08:00
total, start, end := len(list.Volumes), (req.Page-1)*req.PageSize, req.Page*req.PageSize
if start > total {
records = make([]*types.Volume, 0)
} else {
if end >= total {
end = total
}
records = list.Volumes[start:end]
}
for _, item := range records {
tag := make([]string, 0)
for _, val := range item.Labels {
tag = append(tag, val)
}
2023-02-23 11:39:47 +08:00
if len(item.CreatedAt) > 19 {
item.CreatedAt = item.CreatedAt[0:19]
}
createTime, _ := time.Parse("2006-01-02T15:04:05", item.CreatedAt)
2022-10-18 18:39:45 +08:00
data = append(data, dto.Volume{
CreatedAt: createTime,
Name: item.Name,
Driver: item.Driver,
Mountpoint: item.Mountpoint,
Labels: tag,
})
}
return int64(total), data, nil
}
func (u *ContainerService) ListVolume() ([]dto.Options, error) {
client, err := docker.NewDockerClient()
if err != nil {
return nil, err
}
list, err := client.VolumeList(context.TODO(), filters.NewArgs())
if err != nil {
return nil, err
}
var data []dto.Options
for _, item := range list.Volumes {
data = append(data, dto.Options{
Option: item.Name,
})
}
return data, nil
}
func (u *ContainerService) DeleteVolume(req dto.BatchDelete) error {
client, err := docker.NewDockerClient()
if err != nil {
return err
}
for _, id := range req.Names {
2022-10-18 18:39:45 +08:00
if err := client.VolumeRemove(context.TODO(), id, true); err != nil {
return err
}
}
return nil
}
func (u *ContainerService) CreateVolume(req dto.VolumeCreat) error {
client, err := docker.NewDockerClient()
if err != nil {
return err
}
options := volume.VolumeCreateBody{
Name: req.Name,
Driver: req.Driver,
DriverOpts: stringsToMap(req.Options),
Labels: stringsToMap(req.Labels),
}
if _, err := client.VolumeCreate(context.TODO(), options); err != nil {
return err
}
return nil
}