Skip to content

Commit

Permalink
feat: 移除docker包
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent 247421c commit e7bd3aa
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 181 deletions.
24 changes: 0 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23
require (
github.com/bddjr/hlfhr v1.1.2
github.com/beevik/ntp v1.4.3
github.com/docker/docker v27.3.1+incompatible
github.com/expr-lang/expr v1.16.9
github.com/glebarez/sqlite v1.11.0
github.com/go-chi/chi/v5 v5.1.0
Expand Down Expand Up @@ -52,31 +51,22 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bodgit/plumbing v1.2.0 // indirect
github.com/bodgit/sevenzip v1.3.0 // indirect
github.com/bodgit/windows v1.0.0 // indirect
github.com/connesc/cipherio v0.2.1 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/devhaozi/huaweicloud-sdk-go-v3 v0.0.0-20241018211007-bbebb6de5db7 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-rat/securecookie v1.0.1 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
github.com/gofiber/schema v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -90,16 +80,10 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/nwaples/rardecode/v2 v2.0.0-beta.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.597 // indirect
Expand All @@ -108,18 +92,10 @@ require (
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go4.org v0.0.0-20200411211856-f5505b9728dd // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
modernc.org/libc v1.60.1 // indirect
modernc.org/mathutil v1.6.0 // indirect
Expand Down
81 changes: 0 additions & 81 deletions go.sum

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions internal/biz/container_volume.go
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
}
47 changes: 42 additions & 5 deletions internal/data/container_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,47 @@ func (r *containerNetworkRepo) List() ([]types.ContainerNetwork, error) {
if err != nil {
return nil, fmt.Errorf("inspect failed: %w", err)
}
var inspect []types.ContainerNetworkInspect
var inspect []struct {
Name string `json:"Name"`
Id string `json:"Id"`
Created time.Time `json:"Created"`
Scope string `json:"Scope"`
Driver string `json:"Driver"`
EnableIPv6 bool `json:"EnableIPv6"`
IPAM struct {
Driver string `json:"Driver"`
Options map[string]string `json:"Options"`
Config []struct {
Subnet string `json:"Subnet"`
IPRange string `json:"IPRange"`
Gateway string `json:"Gateway"`
AuxAddress map[string]string `json:"AuxiliaryAddresses"`
} `json:"Config"`
} `json:"IPAM"`
Internal bool `json:"Internal"`
Attachable bool `json:"Attachable"`
Ingress bool `json:"Ingress"`
ConfigOnly bool `json:"ConfigOnly"`
Options map[string]string `json:"Options"`
Labels map[string]string `json:"Labels"`
}
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")
}

var ipamConfigs []types.ContainerNetworkIPAMConfig
for _, ipam := range inspect[0].IPAM.Config {
ipamConfigs = append(ipamConfigs, types.ContainerNetworkIPAMConfig{
Subnet: ipam.Subnet,
IPRange: ipam.IPRange,
Gateway: ipam.Gateway,
AuxAddress: ipam.AuxAddress,
})
}

createdAt, _ := time.Parse("2006-01-02 15:04:05 -0700 MST", item.CreatedAt)
networks = append(networks, types.ContainerNetwork{
ID: item.ID,
Expand All @@ -78,9 +111,13 @@ func (r *containerNetworkRepo) List() ([]types.ContainerNetwork, error) {
Ingress: cast.ToBool(inspect[0].Ingress),
Scope: item.Scope,
CreatedAt: createdAt,
IPAM: inspect[0].IPAM,
Options: types.MapToKV(inspect[0].Options),
Labels: types.SliceToKV(strings.Split(item.Labels, ",")),
IPAM: types.ContainerNetworkIPAM{
Driver: inspect[0].IPAM.Driver,
Options: types.MapToKV(inspect[0].IPAM.Options),
Config: ipamConfigs,
},
Options: types.MapToKV(inspect[0].Options),
Labels: types.SliceToKV(strings.Split(item.Labels, ",")),
})
}

Expand Down Expand Up @@ -120,7 +157,7 @@ func (r *containerNetworkRepo) Create(req *request.ContainerNetworkCreate) (stri

// Remove 删除网络
func (r *containerNetworkRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(10*time.Second, "%s network rm %s", r.cmd, id)
_, err := shell.ExecfWithTimeout(10*time.Second, "%s network rm -f %s", r.cmd, id)
return err
}

Expand Down
96 changes: 73 additions & 23 deletions internal/data/container_volume.go
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
}
29 changes: 3 additions & 26 deletions internal/service/container_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/internal/data"
"github.com/TheTNB/panel/internal/http/request"
"github.com/TheTNB/panel/pkg/str"
)

type ContainerVolumeService struct {
Expand All @@ -30,31 +29,9 @@ func (s *ContainerVolumeService) List(w http.ResponseWriter, r *http.Request) {

paged, total := Paginate(r, volumes)

items := make([]any, 0)
for _, item := range paged {
var usage any
if item.UsageData != nil {
usage = map[string]any{
"ref_count": item.UsageData.RefCount,
"size": str.FormatBytes(float64(item.UsageData.Size)),
}
}
items = append(items, map[string]any{
"id": item.Name,
"created": item.CreatedAt,
"driver": item.Driver,
"mount": item.Mountpoint,
"labels": item.Labels,
"options": item.Options,
"scope": item.Scope,
"status": item.Status,
"usage": usage,
})
}

Success(w, chix.M{
"total": total,
"items": items,
"items": paged,
})
}

Expand All @@ -65,13 +42,13 @@ func (s *ContainerVolumeService) Create(w http.ResponseWriter, r *http.Request)
return
}

volume, err := s.containerVolumeRepo.Create(req)
name, err := s.containerVolumeRepo.Create(req)
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}

Success(w, volume.Name)
Success(w, name)

}

Expand Down
20 changes: 2 additions & 18 deletions pkg/types/container_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type ContainerNetwork struct {
// ContainerNetworkIPAM represents IP Address Management
type ContainerNetworkIPAM struct {
Driver string
Options map[string]string // Per network IPAM driver options
Options []KV
Config []ContainerNetworkIPAMConfig
}

Expand All @@ -29,21 +29,5 @@ type ContainerNetworkIPAMConfig struct {
Subnet string `json:"subnet"`
IPRange string `json:"ip_range"`
Gateway string `json:"gateway"`
AuxAddress map[string]string `json:"AuxiliaryAddresses,omitempty"`
}

type ContainerNetworkInspect struct {
Name string `json:"Name"`
Id string `json:"Id"`
Created time.Time `json:"Created"`
Scope string `json:"Scope"`
Driver string `json:"Driver"`
EnableIPv6 bool `json:"EnableIPv6"`
IPAM ContainerNetworkIPAM
Internal bool `json:"Internal"`
Attachable bool `json:"Attachable"`
Ingress bool `json:"Ingress"`
ConfigOnly bool `json:"ConfigOnly"`
Options map[string]string `json:"Options"`
Labels map[string]string `json:"Labels"`
AuxAddress map[string]string `json:"aux_address"`
}
Loading

0 comments on commit e7bd3aa

Please sign in to comment.