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

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

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

@ -16,6 +16,11 @@ func Down(filePath string) (string, error) {
return stdout, err 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) { func Stop(filePath string) (string, error) {
stdout, err := cmd.Execf("docker-compose -f %s stop", filePath) stdout, err := cmd.Execf("docker-compose -f %s stop", filePath)
return stdout, err return stdout, err

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

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

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

Loading…
Cancel
Save