Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions agent/app/dto/request/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type AppContainerConfig struct {
Type string `json:"type"`
SpecifyIP string `json:"specifyIP"`
RestartPolicy string `json:"restartPolicy" validate:"omitempty,oneof=always unless-stopped no on-failure"`

KeepServiceName bool `json:"-"`
SkipComposeCommonConfig bool `json:"-"`
UseLifecycleScripts bool `json:"-"`
}

type AppInstalledSearch struct {
Expand Down Expand Up @@ -92,6 +96,8 @@ type AppInstalledOperate struct {
TaskID string `json:"taskID"`
DeleteImage bool `json:"deleteImage"`
Favorite bool `json:"favorite"`

UseLifecycleScripts bool `json:"-"`
}

type AppInstallUpgrade struct {
Expand All @@ -111,11 +117,14 @@ type AppInstallDelete struct {
DeleteDB bool `json:"deleteDB"`
DeleteImage bool `json:"deleteImage"`
TaskID string `json:"taskID"`

UseLifecycleScripts bool `json:"-"`
}

type AppInstalledUpdate struct {
InstallId uint `json:"installId" validate:"required"`
Params map[string]interface{} `json:"params" validate:"required"`
TaskID string `json:"-"`
AppContainerConfig
}

Expand Down
12 changes: 7 additions & 5 deletions agent/app/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,17 @@ func (a AppService) installWithHooks(req request.AppInstallCreate, executeScript
index++
}
newServiceName := strings.ToLower(appInstall.Name)
if app.Limit == 0 && newServiceName != serviceName && len(servicesMap) == 1 {
if app.Limit == 0 && newServiceName != serviceName && len(servicesMap) == 1 && !req.KeepServiceName {
servicesMap[newServiceName] = servicesMap[serviceName]
delete(servicesMap, serviceName)
serviceName = newServiceName
}
appInstall.ServiceName = serviceName

if err = addDockerComposeCommonParam(composeMap, appInstall.ServiceName, req.AppContainerConfig, req.Params); err != nil {
return
if !req.SkipComposeCommonConfig {
if err = addDockerComposeCommonParam(composeMap, appInstall.ServiceName, req.AppContainerConfig, req.Params); err != nil {
return
}
}
var (
composeByte []byte
Expand Down Expand Up @@ -559,7 +561,7 @@ func (a AppService) installWithHooks(req request.AppInstallCreate, executeScript
return err
}
}
if executeScript {
if executeScript || req.UseLifecycleScripts {
if err = runScript(t, appInstall, "init"); err != nil {
return err
}
Expand All @@ -572,7 +574,7 @@ func (a AppService) installWithHooks(req request.AppInstallCreate, executeScript
return err
}
}
if err = upApp(t, appInstall, req.PullImage); err != nil {
if err = upApp(t, appInstall, req.PullImage, req.UseLifecycleScripts); err != nil {
return err
}
updateToolApp(appInstall)
Expand Down
120 changes: 107 additions & 13 deletions agent/app/service/app_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"math"
"net/http"
"os"
Expand All @@ -13,12 +14,14 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/dto/request"
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/app/task"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
Expand Down Expand Up @@ -252,6 +255,9 @@ func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
return buserr.New("ErrInstallDirNotFound")
}
dockerComposePath := install.GetComposePath()
if req.UseLifecycleScripts && (req.Operate == constant.Start || req.Operate == constant.Stop || req.Operate == constant.Restart) {
return operateAppWithLifecycleScripts(install, req, nil)
}
switch req.Operate {
case constant.Rebuild:
return rebuildApp(install)
Expand All @@ -275,12 +281,13 @@ func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
return syncAppInstallStatus(&install, false)
case constant.Delete:
deleteReq := request.AppInstallDelete{
Install: install,
DeleteBackup: req.DeleteBackup,
ForceDelete: req.ForceDelete,
DeleteDB: req.DeleteDB,
DeleteImage: req.DeleteImage,
TaskID: req.TaskID,
Install: install,
DeleteBackup: req.DeleteBackup,
ForceDelete: req.ForceDelete,
DeleteDB: req.DeleteDB,
DeleteImage: req.DeleteImage,
TaskID: req.TaskID,
UseLifecycleScripts: req.UseLifecycleScripts,
}
if err = deleteAppInstall(deleteReq); err != nil && !req.ForceDelete {
return err
Expand Down Expand Up @@ -312,6 +319,70 @@ func (a *AppInstallService) Operate(req request.AppInstalledOperate) error {
}
}

func operateAppWithLifecycleScripts(install model.AppInstall, req request.AppInstalledOperate, onFailure func(error)) error {
taskType := task.TaskUpdate
switch req.Operate {
case constant.Start:
install.Status = constant.StatusStarting
case constant.Restart:
taskType = task.TaskRestart
install.Status = constant.StatusRestarting
case constant.Stop:
install.Status = constant.StatusWaiting
default:
return errors.New("lifecycle script operation not supported")
}
install.Message = ""
if err := appInstallRepo.Save(context.Background(), &install); err != nil {
return err
}

operationTask, err := task.NewTaskWithOps(install.Name, taskType, task.TaskScopeApp, req.TaskID, install.ID)
if err != nil {
return err
}
operation := string(req.Operate)
operationTask.AddSubTaskWithOps(
task.GetTaskName(install.Name, taskType, task.TaskScopeApp),
func(t *task.Task) error {
if err := runScript(t, &install, operation); err != nil {
return err
}
if req.Operate == constant.Stop {
install.Status = constant.StatusStopped
install.Message = ""
return appInstallRepo.Save(context.Background(), &install)
}
containerNames, err := getContainerNames(install)
if err != nil {
return err
}
if len(containerNames) == 0 {
return buserr.WithName("ErrContainerNotFound", install.Name)
}
install.ContainerName = strings.Join(containerNames, ",")
install.Status = constant.StatusRunning
install.Message = ""
return appInstallRepo.Save(context.Background(), &install)
},
nil,
0,
time.Hour,
)
go func() {
if taskErr := operationTask.Execute(); taskErr != nil {
if onFailure != nil {
onFailure(taskErr)
return
}
install.Status = constant.StatusUpErr
install.Message = taskErr.Error()
_ = appInstallRepo.Save(context.Background(), &install)
}
}()
return nil
}

func (a *AppInstallService) UpdateAppConfig(req request.AppConfigUpdate) error {
installed, err := appInstallRepo.GetFirst(repo.WithByID(req.InstallID))
if err != nil {
Expand Down Expand Up @@ -374,8 +445,10 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
return err
}
}
if err = addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil {
return err
if !req.SkipComposeCommonConfig {
if err = addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil {
return err
}
}
composeByte, err := yaml.Marshal(composeMap)
if err != nil {
Expand Down Expand Up @@ -408,7 +481,7 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
if err != nil {
return err
}
backupEnvMaps := oldEnvMaps
backupEnvMaps := maps.Clone(oldEnvMaps)
handleMap(req.Params, oldEnvMaps)
paramByte, err := json.Marshal(oldEnvMaps)
if err != nil {
Expand All @@ -420,13 +493,32 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
}
fileOp := files.NewFileOp()
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(installed.DockerCompose), constant.DirPerm)
if err := rebuildApp(installed); err != nil {
restoreConfig := func(operationErr error) {
_ = env.Write(backupEnvMaps, envPath)
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(backupDockerCompose), constant.DirPerm)
failed := oldInstalled
failed.Status = constant.StatusUpErr
failed.Message = operationErr.Error()
_ = appInstallRepo.Save(context.Background(), &failed)
}
if req.UseLifecycleScripts {
err = operateAppWithLifecycleScripts(installed, request.AppInstalledOperate{
InstallId: installed.ID,
Operate: constant.Restart,
TaskID: req.TaskID,
UseLifecycleScripts: true,
}, restoreConfig)
} else {
err = rebuildApp(installed)
}
if err != nil {
restoreConfig(err)
return err
Comment thread
zhengkunwang223 marked this conversation as resolved.
}
installed.Status = constant.StatusRunning
_ = appInstallRepo.Save(context.Background(), &installed)
if !req.UseLifecycleScripts {
installed.Status = constant.StatusRunning
_ = appInstallRepo.Save(context.Background(), &installed)
}

proxyChanged := hasAppInstallProxyPassChanged(&oldInstalled, &installed)
currentProxy, currentProxyErr := getAppInstallProxyPass(&installed)
Expand Down Expand Up @@ -836,7 +928,9 @@ func (a *AppInstallService) GetParams(id uint) (*response.AppConfig, error) {
}

func syncAppInstallStatus(appInstall *model.AppInstall, force bool) error {
if appInstall.Status == constant.StatusInstalling || appInstall.Status == constant.StatusRebuilding || appInstall.Status == constant.StatusUpgrading || appInstall.Status == constant.StatusUninstalling {
switch appInstall.Status {
case constant.StatusInstalling, constant.StatusRebuilding, constant.StatusUpgrading, constant.StatusUninstalling,
constant.StatusStarting, constant.StatusRestarting, constant.StatusWaiting:
return nil
Comment thread
zhengkunwang223 marked this conversation as resolved.
}
cli, err := docker.NewClient()
Expand Down
40 changes: 30 additions & 10 deletions agent/app/service/app_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,21 @@ func deleteAppInstall(deleteReq request.AppInstallDelete) error {
logStr := i18n.GetMsgByKey("Stop") + i18n.GetMsgByKey("App")
t.Log(logStr)

out, err := compose.Down(install.GetComposePath())
if err != nil && !deleteReq.ForceDelete {
return handleErr(install, err, out)
if deleteReq.UseLifecycleScripts {
if err = runScript(t, &install, "uninstall"); err != nil {
return err
}
} else {
out, err := compose.Down(install.GetComposePath())
if err != nil && !deleteReq.ForceDelete {
return handleErr(install, err, out)
}
if err = runScript(t, &install, "uninstall"); err != nil {
_, _ = compose.Up(install.GetComposePath())
return err
}
}
t.LogSuccess(logStr)
if err = runScript(t, &install, "uninstall"); err != nil {
_, _ = compose.Up(install.GetComposePath())
return err
}
if deleteReq.DeleteImage {
content, err := op.GetContent(install.GetEnvPath())
if err != nil {
Expand Down Expand Up @@ -999,6 +1005,12 @@ func runScript(task *task.Task, appInstall *model.AppInstall, operate string) er
scriptPath = path.Join(workDir, "scripts", "upgrade.sh")
case "uninstall":
scriptPath = path.Join(workDir, "scripts", "uninstall.sh")
case "start":
scriptPath = path.Join(workDir, "scripts", "start.sh")
case "stop":
scriptPath = path.Join(workDir, "scripts", "stop.sh")
case "restart":
scriptPath = path.Join(workDir, "scripts", "restart.sh")
}
fileOp := files.NewFileOp()
if !fileOp.Stat(scriptPath) {
Expand All @@ -1008,7 +1020,11 @@ func runScript(task *task.Task, appInstall *model.AppInstall, operate string) er
logStr := i18n.GetWithName("ExecShell", operate)
task.LogStart(logStr)

cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(10*time.Minute), cmd.WithWorkDir(workDir))
timeout := 10 * time.Minute
if operate == "start" || operate == "restart" {
timeout = time.Hour
}
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(timeout), cmd.WithWorkDir(workDir), cmd.WithTask(*task))
if err := cmdMgr.Run("bash", scriptPath); err != nil {
task.LogFailedWithErr(logStr, err)
return err
Expand Down Expand Up @@ -1043,12 +1059,15 @@ func checkContainerNameIsExist(containerName, appDir string) (bool, error) {
return false, nil
}

func upApp(task *task.Task, appInstall *model.AppInstall, pullImages bool) error {
func upApp(task *task.Task, appInstall *model.AppInstall, pullImages, useLifecycleScripts bool) error {
upProject := func(appInstall *model.AppInstall) (err error) {
var (
out string
errMsg string
)
if useLifecycleScripts {
return runScript(task, appInstall, "start")
}
if pullImages && appInstall.App.Type != "php" {
envByte, err := files.NewFileOp().GetContent(appInstall.GetEnvPath())
if err != nil {
Expand Down Expand Up @@ -1375,7 +1394,8 @@ func handleErr(install model.AppInstall, err error, out string) error {

func doNotNeedSync(installed model.AppInstall) bool {
return installed.Status == constant.StatusInstalling || installed.Status == constant.StatusRebuilding || installed.Status == constant.StatusUpgrading ||
installed.Status == constant.StatusSyncing || installed.Status == constant.StatusUninstalling || installed.Status == constant.StatusInstallErr
installed.Status == constant.StatusSyncing || installed.Status == constant.StatusUninstalling || installed.Status == constant.StatusInstallErr ||
installed.Status == constant.StatusStarting || installed.Status == constant.StatusRestarting || installed.Status == constant.StatusWaiting
}

func synAppInstall(containers map[string]container.Summary, appInstall *model.AppInstall, force bool) {
Expand Down
Loading
Loading