Skip to content

Commit

Permalink
Merge branch 'UGREEN-DEV' into UGREEN
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyLTY committed Mar 16, 2024
2 parents 543c88b + eaed395 commit fe69b00
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 97 deletions.
8 changes: 8 additions & 0 deletions internal/svc/servicecontext.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package svc

import (
"github.com/docker/docker/client"
"github.com/flosch/pongo2"
loader "github.com/nathan-osman/pongo2-embed-loader"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/config"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/module"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"sync"
)
Expand All @@ -20,6 +22,7 @@ type ServiceContext struct {
HubImageInfo *module.ImageUpdateData
IndexCheckMiddleware rest.Middleware
ProgressStore ProgressStoreType
DockerClient *client.Client
mu sync.Mutex
}

Expand All @@ -35,11 +38,16 @@ type TaskProgress struct {
type ProgressStoreType map[string]TaskProgress

func NewServiceContext(c config.Config, loaders *loader.Loader) *ServiceContext {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logx.Errorf("Unable to create docker client: %s", err)
}
return &ServiceContext{
Config: c,
Template: pongo2.NewSet("", loaders),
HubImageInfo: module.NewImageCheck(),
ProgressStore: make(ProgressStoreType),
DockerClient: cli,
}
}

Expand Down
9 changes: 2 additions & 7 deletions internal/utiles/backupcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
dockerBackend "github.com/docker/docker/api/types/backend"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
"os"
Expand All @@ -14,19 +13,15 @@ import (
)

func BackupContainer(ctx *svc.ServiceContext) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
containerList, err := GetContainerList(ctx)
if err != nil {
return err
}
var backupList []dockerBackend.ContainerCreateConfig
for i, v := range containerList {
containerID := containerList[i].ID
cli.NegotiateAPIVersion(context.TODO())
inspectedContainer, err := cli.ContainerInspect(context.TODO(), containerID)
ctx.DockerClient.NegotiateAPIVersion(context.TODO())
inspectedContainer, err := ctx.DockerClient.ContainerInspect(context.TODO(), containerID)
if err != nil {
logx.Error("获取容器信息失败" + err.Error())
return err
Expand Down
7 changes: 1 addition & 6 deletions internal/utiles/containerInspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ package utiles
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
)

func GetContainerInspect(ctx *svc.ServiceContext, id string) (types.ContainerJSON, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return types.ContainerJSON{}, err
}
inspectedContainer, err := cli.ContainerInspect(context.TODO(), id)
inspectedContainer, err := ctx.DockerClient.ContainerInspect(context.TODO(), id)
if err != nil {
return types.ContainerJSON{}, err
}
Expand Down
8 changes: 1 addition & 7 deletions internal/utiles/getcontainerlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ package utiles
import (
"context"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
MyType "github.com/onlyLTY/dockerCopilot/UGREEN/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)

func GetContainerList(ctx *svc.ServiceContext) ([]MyType.Container, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logx.Errorf("connect to docker error: %v", err)
return nil, err
}
// 获取所有容器(包括停止的容器)
dockerContainerList, err := cli.ContainerList(context.Background(), container.ListOptions{
dockerContainerList, err := ctx.DockerClient.ContainerList(context.Background(), container.ListOptions{
All: true, // 设置为true来获取所有容器
})
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions internal/utiles/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
MyType "github.com/onlyLTY/dockerCopilot/UGREEN/internal/types"
"log"
"strings"
)

func GetImagesList(ctx *svc.ServiceContext) ([]MyType.Image, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Fatalf("Unable to create docker client: %s", err)
}
var imagesList []MyType.Image
dockerImages, err := cli.ImageList(context.Background(), types.ImageListOptions{})
dockerImages, err := ctx.DockerClient.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
log.Fatalf("Unable to fetch docker images: %s", err)
}
Expand Down
9 changes: 1 addition & 8 deletions internal/utiles/removeimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ package utiles
import (
"context"
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"log"
)

func RemoveImage(ctx *svc.ServiceContext, imageID string, force bool) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Fatalf("Failed to create Docker client: %s", err)
return err
}
_, err = cli.ImageRemove(context.Background(), imageID, dockerTypes.ImageRemoveOptions{Force: force})
_, err := ctx.DockerClient.ImageRemove(context.Background(), imageID, dockerTypes.ImageRemoveOptions{Force: force})
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions internal/utiles/renamecontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package utiles

import (
"context"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)

func RenameContainer(ctx *svc.ServiceContext, id string, newName string) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logx.Errorf("connect to docker error: %v", err)
return err
}
err = cli.ContainerRename(context.TODO(), id, newName)
err := ctx.DockerClient.ContainerRename(context.TODO(), id, newName)
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions internal/utiles/restartcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@ package utiles
import (
"context"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)

func RestartContainer(ctx *svc.ServiceContext, id string) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logx.Errorf("connect to docker error: %v", err)
return err
}
timeout := 10
signal := "SIGINT"
stopOptions := container.StopOptions{
Signal: signal,
Timeout: &timeout,
}
err = cli.ContainerRestart(context.Background(), id, stopOptions)
err := ctx.DockerClient.ContainerRestart(context.Background(), id, stopOptions)
if err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions internal/utiles/restorecontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
docker "github.com/docker/docker/api/types"
dockerBackend "github.com/docker/docker/api/types/backend"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
"os"
Expand Down Expand Up @@ -49,30 +48,26 @@ func RestoreContainer(ctx *svc.ServiceContext, filename string, taskID string) e
oldProgress.IsDone = true
ctx.UpdateProgress(taskID, oldProgress)
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
for i, containerInfo := range configList {
info := "正在恢复第" + strconv.Itoa(i+1) + "个容器"
oldProgress.Percentage = int(float64(i) / float64(len(configList)) * 100)
oldProgress.Message = info
oldProgress.DetailMsg = info
ctx.UpdateProgress(taskID, oldProgress)
cli.NegotiateAPIVersion(context.TODO())
ctx.DockerClient.NegotiateAPIVersion(context.TODO())
if err != nil {
backupList = append(backupList, "出现错误"+err.Error())
logx.Error("Failed to inspect container: %s", err)
return err
}
reader, err := cli.ImagePull(context.TODO(), containerInfo.Config.Image, docker.ImagePullOptions{})
reader, err := ctx.DockerClient.ImagePull(context.TODO(), containerInfo.Config.Image, docker.ImagePullOptions{})
if err != nil {
backupList = append(backupList, containerInfo.Config.Image+"拉取镜像出现错误"+err.Error())
logx.Errorf("Failed to pull image: %s", err)
continue
}
decodePullResp(reader, ctx, taskID)
_, err = cli.ContainerCreate(context.TODO(), containerInfo.Config, containerInfo.HostConfig, containerInfo.NetworkingConfig, nil, containerInfo.Name)
_, err = ctx.DockerClient.ContainerCreate(context.TODO(), containerInfo.Config, containerInfo.HostConfig, containerInfo.NetworkingConfig, nil, containerInfo.Name)
if err != nil {
logx.Error("Failed to create container: %s", err)
info = "正在恢复第" + strconv.Itoa(i+1) + "个容器"
Expand Down
7 changes: 1 addition & 6 deletions internal/utiles/startcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package utiles
import (
"context"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
)

func StartContainer(ctx *svc.ServiceContext, id string) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
startOptions := container.StartOptions{}
err = cli.ContainerStart(context.Background(), id, startOptions)
err := ctx.DockerClient.ContainerStart(context.Background(), id, startOptions)
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions internal/utiles/stopcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@ package utiles
import (
"context"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)

func StopContainer(ctx *svc.ServiceContext, id string) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logx.Errorf("connect to docker error: %v", err)
return err
}
timeout := 10
signal := "SIGINT"
stopOptions := container.StopOptions{
Signal: signal,
Timeout: &timeout,
}
err = cli.ContainerStop(context.Background(), id, stopOptions)
err := ctx.DockerClient.ContainerStop(context.Background(), id, stopOptions)
if err != nil {
return err
}
Expand Down
32 changes: 8 additions & 24 deletions internal/utiles/updatecontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
dockerMsgType "github.com/docker/docker/pkg/jsonmessage"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
Expand All @@ -34,14 +33,6 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
IsDone: false,
}
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
oldTaskProgress.Message = "连接Docker失败"
oldTaskProgress.DetailMsg = err.Error()
oldTaskProgress.IsDone = true
ctx.UpdateProgress(taskID, oldTaskProgress)
return err
}
timeout := 10
signal := "SIGINT"

Expand All @@ -50,15 +41,8 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
oldTaskProgress.Percentage = 10
oldTaskProgress.DetailMsg = "正在拉取新镜像"
ctx.UpdateProgress(taskID, oldTaskProgress)
cli.NegotiateAPIVersion(context.TODO())
if err != nil {
oldTaskProgress.Message = "获取Docker API版本失败"
oldTaskProgress.DetailMsg = err.Error()
oldTaskProgress.IsDone = true
ctx.UpdateProgress(taskID, oldTaskProgress)
return err
}
reader, err := cli.ImagePull(context.TODO(), imageNameAndTag, dockerTypes.ImagePullOptions{})
ctx.DockerClient.NegotiateAPIVersion(context.TODO())
reader, err := ctx.DockerClient.ImagePull(context.TODO(), imageNameAndTag, dockerTypes.ImagePullOptions{})
if err != nil {
oldTaskProgress.Message = "拉取镜像失败"
oldTaskProgress.DetailMsg = err.Error()
Expand Down Expand Up @@ -88,7 +72,7 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
Signal: signal,
Timeout: &timeout,
}
err = cli.ContainerStop(context.Background(), id, stopOptions)
err = ctx.DockerClient.ContainerStop(context.Background(), id, stopOptions)
if err != nil {
oldTaskProgress.Message = "停止容器失败"
oldTaskProgress.DetailMsg = err.Error()
Expand All @@ -105,7 +89,7 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
oldTaskProgress.DetailMsg = "正在重命名旧容器"
ctx.UpdateProgress(taskID, oldTaskProgress)
currentDate := time.Now().Format("2006-01-02-15-04-05")
err = cli.ContainerRename(context.Background(), id, name+"-"+currentDate)
err = ctx.DockerClient.ContainerRename(context.Background(), id, name+"-"+currentDate)
if err != nil {
oldTaskProgress.Message = "重命名旧容器失败"
oldTaskProgress.DetailMsg = err.Error()
Expand All @@ -120,7 +104,7 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
oldTaskProgress.Message = "正在创建新容器"
oldTaskProgress.DetailMsg = "正在创建新容器"
ctx.UpdateProgress(taskID, oldTaskProgress)
inspectedContainer, err := cli.ContainerInspect(context.TODO(), id)
inspectedContainer, err := ctx.DockerClient.ContainerInspect(context.TODO(), id)
if err != nil {
oldTaskProgress.Message = "获取容器信息失败"
oldTaskProgress.DetailMsg = err.Error()
Expand All @@ -138,7 +122,7 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
EndpointsConfig: inspectedContainer.NetworkSettings.Networks,
}
containerName := name
_, err = cli.ContainerCreate(context.TODO(), config, hostConfig, networkingConfig, nil, containerName)
_, err = ctx.DockerClient.ContainerCreate(context.TODO(), config, hostConfig, networkingConfig, nil, containerName)
if err != nil {
oldTaskProgress.Message = "创建新容器失败"
oldTaskProgress.DetailMsg = err.Error()
Expand All @@ -153,7 +137,7 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
oldTaskProgress.Message = "正在启动新容器以及删除旧容器(如果不保留旧容器)"
oldTaskProgress.DetailMsg = "正在启动新容器以及删除旧容器(如果不保留旧容器)"
ctx.UpdateProgress(taskID, oldTaskProgress)
err = cli.ContainerStart(context.Background(), containerName, container.StartOptions{
err = ctx.DockerClient.ContainerStart(context.Background(), containerName, container.StartOptions{
CheckpointID: "",
CheckpointDir: "",
})
Expand All @@ -165,7 +149,7 @@ func UpdateContainer(ctx *svc.ServiceContext, id string, name string, imageNameA
return err
}
if delOldContainer {
err = cli.ContainerRemove(context.Background(), id, container.RemoveOptions{})
err = ctx.DockerClient.ContainerRemove(context.Background(), id, container.RemoveOptions{})
if err != nil {
oldTaskProgress.Message = "删除旧容器失败"
oldTaskProgress.DetailMsg = err.Error()
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.3-UGREEN
v2.0.4-UGREEN

0 comments on commit fe69b00

Please sign in to comment.