2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 12:40:25 +08:00

fix: compose中的错误

This commit is contained in:
耗子
2025-03-21 02:14:48 +08:00
parent f4533c9662
commit 28d699c886

View File

@@ -4,7 +4,6 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/tnb-labs/panel/internal/app"
"github.com/tnb-labs/panel/internal/biz"
@@ -20,64 +19,59 @@ func NewContainerComposeRepo() biz.ContainerComposeRepo {
// List 列出所有编排文件名
func (r *containerComposeRepo) List() ([]string, error) {
dir := filepath.Join(app.Root, "server", "compose")
var files []string
err := filepath.Walk(dir, func(path string, d fs.FileInfo, err error) error {
var names []string
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if !d.IsDir() {
return nil
}
if ext := filepath.Ext(path); ext == ".yml" || ext == ".yaml" {
files = append(files, strings.TrimSuffix(filepath.Base(path), ext))
}
names = append(names, filepath.Base(path))
return nil
})
if err != nil {
return nil, err
}
return files, nil
return names, nil
}
// Get 获取编排文件内容
func (r *containerComposeRepo) Get(name string) (string, error) {
dir := filepath.Join(app.Root, "server", "compose")
path := filepath.Join(dir, name+".yml")
content, err := os.ReadFile(path)
content, err := os.ReadFile(filepath.Join(app.Root, "server", "compose", name, "docker-compose.yml"))
return string(content), err
}
// Create 创建编排文件
func (r *containerComposeRepo) Create(name, compose string) error {
dir := filepath.Join(app.Root, "server", "compose")
path := filepath.Join(dir, name+".yml")
return os.WriteFile(path, []byte(compose), 0644)
dir := filepath.Join(app.Root, "server", "compose", name)
if err := os.MkdirAll(dir, 0644); err != nil {
return err
}
return os.WriteFile(filepath.Join(dir, "docker-compose.yml"), []byte(compose), 0644)
}
// Up 启动编排
func (r *containerComposeRepo) Up(name string, force bool) error {
dir := filepath.Join(app.Root, "server", "compose")
path := filepath.Join(dir, name+".yml")
file := filepath.Join(app.Root, "server", "compose", name, "docker-compose.yml")
cmd := "docker compose -f %s up -d"
if force {
cmd += " --pull always" // 强制拉取镜像
}
_, err := shell.Execf(cmd, path)
_, err := shell.Execf(cmd, file)
return err
}
// Down 停止编排
func (r *containerComposeRepo) Down(name string) error {
dir := filepath.Join(app.Root, "server", "compose")
path := filepath.Join(dir, name+".yml")
_, err := shell.Execf("docker compose -f %s down", path)
file := filepath.Join(app.Root, "server", "compose", name, "docker-compose.yml")
_, err := shell.Execf("docker compose -f %s down", file)
return err
}
// Remove 删除编排
func (r *containerComposeRepo) Remove(name string) error {
dir := filepath.Join(app.Root, "server", "compose")
path := filepath.Join(dir, name+".yml")
return os.Remove(path)
dir := filepath.Join(app.Root, "server", "compose", name)
return os.RemoveAll(dir)
}