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"
|
2023-03-15 16:48:26 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2023-03-11 11:32:24 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-12-04 17:20:10 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2022-10-18 18:39:45 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
|
|
|
"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
|
|
|
|
}
|
2023-10-31 14:14:41 +08:00
|
|
|
list, err := client.VolumeList(context.TODO(), volume.ListOptions{})
|
2022-10-18 18:39:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2023-02-07 18:48:32 +08:00
|
|
|
if len(req.Info) != 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
length, count := len(list.Volumes), 0
|
|
|
|
for count < length {
|
2023-02-07 18:48:32 +08:00
|
|
|
if !strings.Contains(list.Volumes[count].Name, req.Info) {
|
|
|
|
list.Volumes = append(list.Volumes[:count], list.Volumes[(count+1):]...)
|
2023-05-30 15:30:57 +08:00
|
|
|
length--
|
2023-02-07 18:48:32 +08:00
|
|
|
} else {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 18:39:45 +08:00
|
|
|
var (
|
|
|
|
data []dto.Volume
|
2023-04-07 11:30:10 +08:00
|
|
|
records []*volume.Volume
|
2022-10-18 18:39:45 +08:00
|
|
|
)
|
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 {
|
2023-04-07 11:30:10 +08:00
|
|
|
records = make([]*volume.Volume, 0)
|
2022-10-18 18:39:45 +08:00
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
|
|
|
}
|
|
|
|
records = list.Volumes[start:end]
|
|
|
|
}
|
|
|
|
|
2024-08-19 18:30:42 +08:00
|
|
|
nyc, _ := time.LoadLocation(common.LoadTimeZoneByCmd())
|
2022-10-18 18:39:45 +08:00
|
|
|
for _, item := range records {
|
|
|
|
tag := make([]string, 0)
|
|
|
|
for _, val := range item.Labels {
|
|
|
|
tag = append(tag, val)
|
|
|
|
}
|
2023-12-04 17:20:10 +08:00
|
|
|
var createTime time.Time
|
|
|
|
if strings.Contains(item.CreatedAt, "Z") {
|
|
|
|
createTime, _ = time.ParseInLocation("2006-01-02T15:04:05Z", item.CreatedAt, nyc)
|
|
|
|
} else if strings.Contains(item.CreatedAt, "+") {
|
|
|
|
createTime, _ = time.ParseInLocation("2006-01-02T15:04:05+08:00", item.CreatedAt, nyc)
|
|
|
|
} else {
|
|
|
|
createTime, _ = time.ParseInLocation("2006-01-02T15:04:05", item.CreatedAt, nyc)
|
2023-02-23 11:39:47 +08:00
|
|
|
}
|
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
|
|
|
|
}
|
2024-04-25 16:45:12 +08:00
|
|
|
defer client.Close()
|
2023-10-31 14:14:41 +08:00
|
|
|
list, err := client.VolumeList(context.TODO(), volume.ListOptions{})
|
2022-10-18 18:39:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-10 22:45:08 +08:00
|
|
|
var datas []dto.Options
|
2022-10-18 18:39:45 +08:00
|
|
|
for _, item := range list.Volumes {
|
2023-07-10 22:45:08 +08:00
|
|
|
datas = append(datas, dto.Options{
|
2022-10-18 18:39:45 +08:00
|
|
|
Option: item.Name,
|
|
|
|
})
|
|
|
|
}
|
2023-07-10 22:45:08 +08:00
|
|
|
sort.Slice(datas, func(i, j int) bool {
|
|
|
|
return datas[i].Option < datas[j].Option
|
|
|
|
})
|
|
|
|
return datas, nil
|
2022-10-18 18:39:45 +08:00
|
|
|
}
|
|
|
|
func (u *ContainerService) DeleteVolume(req dto.BatchDelete) error {
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-25 16:45:12 +08:00
|
|
|
defer client.Close()
|
2022-12-13 18:54:28 +08:00
|
|
|
for _, id := range req.Names {
|
2022-10-18 18:39:45 +08:00
|
|
|
if err := client.VolumeRemove(context.TODO(), id, true); err != nil {
|
2023-03-15 16:48:26 +08:00
|
|
|
if strings.Contains(err.Error(), "volume is in use") {
|
2023-03-16 15:03:44 +08:00
|
|
|
return buserr.WithDetail(constant.ErrInUsed, id, nil)
|
2023-03-15 16:48:26 +08:00
|
|
|
}
|
2022-10-18 18:39:45 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
func (u *ContainerService) CreateVolume(req dto.VolumeCreate) error {
|
2022-10-18 18:39:45 +08:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-25 16:45:12 +08:00
|
|
|
defer client.Close()
|
2023-10-31 15:57:49 +08:00
|
|
|
arg := filters.NewArgs()
|
|
|
|
arg.Add("name", req.Name)
|
|
|
|
vos, _ := client.VolumeList(context.TODO(), volume.ListOptions{Filters: arg})
|
2023-03-11 11:32:24 +08:00
|
|
|
if len(vos.Volumes) != 0 {
|
|
|
|
for _, v := range vos.Volumes {
|
|
|
|
if v.Name == req.Name {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-07 11:30:10 +08:00
|
|
|
options := volume.CreateOptions{
|
2022-10-18 18:39:45 +08:00
|
|
|
Name: req.Name,
|
|
|
|
Driver: req.Driver,
|
|
|
|
DriverOpts: stringsToMap(req.Options),
|
|
|
|
Labels: stringsToMap(req.Labels),
|
|
|
|
}
|
2023-03-15 12:58:56 +08:00
|
|
|
if _, err := client.VolumeCreate(context.TODO(), options); err != nil {
|
2022-10-18 18:39:45 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|