feat: 应用增加重建功能

pull/173/head
zhengkunwang223 2 years ago committed by zhengkunwang223
parent 1ef26aa4eb
commit ff87cd5989

@ -20,7 +20,6 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/docker"
"github.com/1Panel-dev/1Panel/backend/utils/files"
"gopkg.in/yaml.v3"
)
@ -34,7 +33,7 @@ type IAppService interface {
GetApp(key string) (*response.AppDTO, error)
GetAppDetail(appId uint, version string) (response.AppDetailDTO, error)
Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error)
SyncInstalled(installId uint) error
//SyncInstalled(installId uint) error
SyncAppList() error
}
@ -259,94 +258,94 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (
return &appInstall, nil
}
func (a AppService) SyncInstalled(installId uint) error {
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
if err != nil {
return err
}
containerNames, err := getContainerNames(appInstall)
if err != nil {
return err
}
cli, err := docker.NewClient()
if err != nil {
return err
}
containers, err := cli.ListContainersByName(containerNames)
if err != nil {
return err
}
var (
errorContainers []string
notFoundContainers []string
runningContainers []string
)
for _, n := range containers {
if n.State != "running" {
errorContainers = append(errorContainers, n.Names[0])
} else {
runningContainers = append(runningContainers, n.Names[0])
}
}
for _, old := range containerNames {
exist := false
for _, new := range containers {
if common.ExistWithStrArray(old, new.Names) {
exist = true
break
}
}
if !exist {
notFoundContainers = append(notFoundContainers, old)
}
}
containerCount := len(containers)
errCount := len(errorContainers)
notFoundCount := len(notFoundContainers)
normalCount := len(containerNames)
runningCount := len(runningContainers)
if containerCount == 0 {
appInstall.Status = constant.Error
appInstall.Message = "container is not found"
return appInstallRepo.Save(&appInstall)
}
if errCount == 0 && notFoundCount == 0 {
appInstall.Status = constant.Running
return appInstallRepo.Save(&appInstall)
}
if errCount == normalCount {
appInstall.Status = constant.Error
}
if notFoundCount == normalCount {
appInstall.Status = constant.Stopped
}
if runningCount < normalCount {
appInstall.Status = constant.UnHealthy
}
var errMsg strings.Builder
if errCount > 0 {
errMsg.Write([]byte(string(rune(errCount)) + " error containers:"))
for _, e := range errorContainers {
errMsg.Write([]byte(e))
}
errMsg.Write([]byte("\n"))
}
if notFoundCount > 0 {
errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:"))
for _, e := range notFoundContainers {
errMsg.Write([]byte(e))
}
errMsg.Write([]byte("\n"))
}
appInstall.Message = errMsg.String()
return appInstallRepo.Save(&appInstall)
}
//func (a AppService) SyncInstalled(installId uint) error {
// appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
// if err != nil {
// return err
// }
//
// containerNames, err := getContainerNames(appInstall)
// if err != nil {
// return err
// }
//
// cli, err := docker.NewClient()
// if err != nil {
// return err
// }
// containers, err := cli.ListContainersByName(containerNames)
// if err != nil {
// return err
// }
// var (
// errorContainers []string
// notFoundContainers []string
// runningContainers []string
// )
//
// for _, n := range containers {
// if n.State != "running" {
// errorContainers = append(errorContainers, n.Names[0])
// } else {
// runningContainers = append(runningContainers, n.Names[0])
// }
// }
// for _, old := range containerNames {
// exist := false
// for _, new := range containers {
// if common.ExistWithStrArray(old, new.Names) {
// exist = true
// break
// }
// }
// if !exist {
// notFoundContainers = append(notFoundContainers, old)
// }
// }
//
// containerCount := len(containers)
// errCount := len(errorContainers)
// notFoundCount := len(notFoundContainers)
// normalCount := len(containerNames)
// runningCount := len(runningContainers)
//
// if containerCount == 0 {
// appInstall.Status = constant.ContainerNotFound
// appInstall.Message = "container is not found"
// return appInstallRepo.Save(&appInstall)
// }
// if errCount == 0 && notFoundCount == 0 {
// appInstall.Status = constant.Running
// return appInstallRepo.Save(&appInstall)
// }
// if errCount == normalCount {
// appInstall.Status = constant.Error
// }
// if notFoundCount == normalCount {
// appInstall.Status = constant.Stopped
// }
// if runningCount < normalCount {
// appInstall.Status = constant.UnHealthy
// }
//
// var errMsg strings.Builder
// if errCount > 0 {
// errMsg.Write([]byte(string(rune(errCount)) + " error containers:"))
// for _, e := range errorContainers {
// errMsg.Write([]byte(e))
// }
// errMsg.Write([]byte("\n"))
// }
// if notFoundCount > 0 {
// errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:"))
// for _, e := range notFoundContainers {
// errMsg.Write([]byte(e))
// }
// errMsg.Write([]byte("\n"))
// }
// appInstall.Message = errMsg.String()
// return appInstallRepo.Save(&appInstall)
//}
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
res := &response.AppUpdateRes{

@ -159,13 +159,23 @@ func (a AppInstallService) Operate(req request.AppInstalledOperate) error {
}
dockerComposePath := install.GetComposePath()
switch req.Operate {
case constant.Up:
out, err := compose.Up(dockerComposePath)
case constant.Rebuild:
out, err := compose.Down(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
out, err = compose.Up(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
return nil
case constant.Start:
out, err := compose.Start(dockerComposePath)
if err != nil {
return handleErr(install, err, out)
}
install.Status = constant.Running
case constant.Down:
case constant.Stop:
out, err := compose.Stop(dockerComposePath)
if err != nil {
return handleErr(install, err, out)

@ -6,6 +6,7 @@ const (
Error = "Error"
Stopped = "Stopped"
Installing = "Installing"
Syncing = "Syncing"
ContainerPrefix = "1Panel-"
@ -22,10 +23,13 @@ type AppOperate string
var (
Up AppOperate = "up"
Down AppOperate = "down"
Start AppOperate = "start"
Stop AppOperate = "stop"
Restart AppOperate = "restart"
Delete AppOperate = "delete"
Sync AppOperate = "sync"
Backup AppOperate = "backup"
Restore AppOperate = "restore"
Update AppOperate = "update"
Rebuild AppOperate = "rebuild"
)

@ -16,6 +16,11 @@ func Down(filePath string) (string, error) {
return stdout, err
}
func Start(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s start", filePath)
return stdout, err
}
func Stop(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
return stdout, err

@ -859,8 +859,9 @@ export default {
status: 'Status',
container: 'Container',
restart: 'Restart',
up: 'Start',
down: 'Stop',
start: 'Start',
stop: 'Stop',
rebuild: 'Rebuild',
name: 'Name',
description: 'Description',
delete: 'Delete',

@ -864,8 +864,9 @@ export default {
status: '',
container: '',
restart: '',
up: '',
down: '',
start: '',
stop: '',
rebuild: '',
name: '',
description: '',
delete: '',

@ -297,6 +297,12 @@ const buttons = [
openOperate(row, 'sync');
},
},
{
label: i18n.global.t('app.rebuild'),
click: (row: any) => {
openOperate(row, 'rebuild');
},
},
{
label: i18n.global.t('app.restart'),
click: (row: any) => {
@ -304,18 +310,18 @@ const buttons = [
},
},
{
label: i18n.global.t('app.up'),
label: i18n.global.t('app.start'),
click: (row: any) => {
openOperate(row, 'up');
openOperate(row, 'start');
},
disabled: (row: any) => {
return row.status === 'Running';
},
},
{
label: i18n.global.t('app.down'),
label: i18n.global.t('app.stop'),
click: (row: any) => {
openOperate(row, 'down');
openOperate(row, 'stop');
},
disabled: (row: any) => {
return row.status !== 'Running';

Loading…
Cancel
Save