-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
package biz | ||
|
||
import ( | ||
"github.com/docker/docker/api/types/volume" | ||
|
||
"github.com/TheTNB/panel/internal/http/request" | ||
"github.com/TheTNB/panel/pkg/types" | ||
) | ||
|
||
type ContainerVolumeRepo interface { | ||
List() ([]*volume.Volume, error) | ||
Create(req *request.ContainerVolumeCreate) (volume.Volume, error) | ||
List() ([]types.ContainerVolume, error) | ||
Create(req *request.ContainerVolumeCreate) (string, error) | ||
Remove(id string) error | ||
Prune() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,107 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/docker/api/types/filters" | ||
"github.com/docker/docker/api/types/volume" | ||
"github.com/docker/docker/client" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/TheTNB/panel/internal/biz" | ||
"github.com/TheTNB/panel/internal/http/request" | ||
"github.com/TheTNB/panel/pkg/shell" | ||
"github.com/TheTNB/panel/pkg/types" | ||
) | ||
|
||
type containerVolumeRepo struct { | ||
client *client.Client | ||
cmd string | ||
} | ||
|
||
func NewContainerVolumeRepo(sock ...string) biz.ContainerVolumeRepo { | ||
if len(sock) == 0 { | ||
sock = append(sock, "/run/podman/podman.sock") | ||
func NewContainerVolumeRepo(cmd ...string) biz.ContainerVolumeRepo { | ||
if len(cmd) == 0 { | ||
cmd = append(cmd, "docker") | ||
} | ||
cli, _ := client.NewClientWithOpts(client.WithHost("unix://"+sock[0]), client.WithAPIVersionNegotiation()) | ||
return &containerVolumeRepo{ | ||
client: cli, | ||
cmd: cmd[0], | ||
} | ||
} | ||
|
||
// List 列出存储卷 | ||
func (r *containerVolumeRepo) List() ([]*volume.Volume, error) { | ||
volumes, err := r.client.VolumeList(context.Background(), volume.ListOptions{}) | ||
func (r *containerVolumeRepo) List() ([]types.ContainerVolume, error) { | ||
output, err := shell.ExecfWithTimeout(10*time.Second, "%s volume ls --format json", r.cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return volumes.Volumes, err | ||
lines := strings.Split(output, "\n") | ||
|
||
var volumes []types.ContainerVolume | ||
for _, line := range lines { | ||
if line == "" { | ||
continue | ||
} | ||
|
||
var item struct { | ||
Availability string `json:"Availability"` | ||
Driver string `json:"Driver"` | ||
Group string `json:"Group"` | ||
Labels string `json:"Labels"` | ||
Links string `json:"Links"` | ||
Mountpoint string `json:"Mountpoint"` | ||
Name string `json:"Name"` | ||
Scope string `json:"Scope"` | ||
Size string `json:"Size"` | ||
Status string `json:"Status"` | ||
} | ||
if err = json.Unmarshal([]byte(line), &item); err != nil { | ||
return nil, fmt.Errorf("unmarshal failed: %w", err) | ||
} | ||
|
||
output, err = shell.ExecfWithTimeout(10*time.Second, "%s volume inspect %s", r.cmd, item.Name) | ||
if err != nil { | ||
return nil, fmt.Errorf("inspect failed: %w", err) | ||
} | ||
var inspect []struct { | ||
CreatedAt time.Time `json:"CreatedAt"` | ||
Driver string `json:"Driver"` | ||
Labels map[string]string `json:"Labels"` | ||
Mountpoint string `json:"Mountpoint"` | ||
Name string `json:"Name"` | ||
Options map[string]string `json:"Options"` | ||
Scope string `json:"Scope"` | ||
} | ||
if err = json.Unmarshal([]byte(output), &inspect); err != nil { | ||
return nil, fmt.Errorf("unmarshal inspect failed: %w", err) | ||
} | ||
if len(inspect) == 0 { | ||
return nil, fmt.Errorf("inspect empty") | ||
} | ||
|
||
volumes = append(volumes, types.ContainerVolume{ | ||
Name: item.Name, | ||
Driver: item.Driver, | ||
Scope: item.Scope, | ||
MountPoint: item.Mountpoint, | ||
CreatedAt: inspect[0].CreatedAt, | ||
Options: types.MapToKV(inspect[0].Options), | ||
Labels: types.SliceToKV(strings.Split(item.Labels, ",")), | ||
}) | ||
} | ||
|
||
return volumes, nil | ||
} | ||
|
||
// Create 创建存储卷 | ||
func (r *containerVolumeRepo) Create(req *request.ContainerVolumeCreate) (volume.Volume, error) { | ||
return r.client.VolumeCreate(context.Background(), volume.CreateOptions{ | ||
Name: req.Name, | ||
Driver: req.Driver, | ||
DriverOpts: types.KVToMap(req.Options), | ||
Labels: types.KVToMap(req.Labels), | ||
}) | ||
func (r *containerVolumeRepo) Create(req *request.ContainerVolumeCreate) (string, error) { | ||
return "", nil | ||
} | ||
|
||
// Remove 删除存储卷 | ||
func (r *containerVolumeRepo) Remove(id string) error { | ||
return r.client.VolumeRemove(context.Background(), id, true) | ||
_, err := shell.ExecfWithTimeout(10*time.Second, "%s volume rm -f %s", r.cmd, id) | ||
return err | ||
} | ||
|
||
// Prune 清理未使用的存储卷 | ||
func (r *containerVolumeRepo) Prune() error { | ||
_, err := r.client.VolumesPrune(context.Background(), filters.NewArgs()) | ||
_, err := shell.ExecfWithTimeout(10*time.Second, "%s volume prune -f", r.cmd) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.