Skip to content

Commit f71ff7d

Browse files
feat: Restrict abnormal database selection during application installation (#8059)
1 parent f242f71 commit f71ff7d

File tree

28 files changed

+132
-97
lines changed

28 files changed

+132
-97
lines changed

agent/app/dto/response/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ type AppService struct {
141141
Value string `json:"value"`
142142
Config interface{} `json:"config"`
143143
From string `json:"from"`
144+
Status string `json:"status"`
144145
}
145146

146147
type AppParam struct {

agent/app/service/ai.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func LoadContainerName() (string, error) {
358358
if err != nil {
359359
return "", fmt.Errorf("ollama service is not found, err: %v", err)
360360
}
361-
if ollamaBaseInfo.Status != constant.Running {
361+
if ollamaBaseInfo.Status != constant.StatusRunning {
362362
return "", fmt.Errorf("container %s of ollama is not running, please check and retry!", ollamaBaseInfo.ContainerName)
363363
}
364364
return ollamaBaseInfo.ContainerName, nil

agent/app/service/app.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,15 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
373373
return
374374
}
375375
}
376+
if hostName, ok := req.Params["PANEL_DB_HOST"]; ok {
377+
database, _ := databaseRepo.Get(repo.WithByName(hostName.(string)))
378+
if database.AppInstallID > 0 {
379+
databaseInstall, _ := appInstallRepo.GetFirst(repo.WithByID(database.AppInstallID))
380+
if databaseInstall.Status != constant.StatusRunning {
381+
return nil, buserr.WithName("ErrAppIsDown", databaseInstall.Name)
382+
}
383+
}
384+
}
376385
if app.Key == "openresty" && app.Resource == "remote" && common.CompareVersion(appDetail.Version, "1.27") {
377386
if dir, ok := req.Params["WEBSITE_DIR"]; ok {
378387
siteDir := dir.(string)
@@ -421,7 +430,7 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
421430
AppId: appDetail.AppId,
422431
AppDetailId: appDetail.ID,
423432
Version: appDetail.Version,
424-
Status: constant.Installing,
433+
Status: constant.StatusInstalling,
425434
HttpPort: httpPort,
426435
HttpsPort: httpsPort,
427436
App: app,
@@ -536,7 +545,7 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
536545
}
537546

538547
handleAppStatus := func(t *task.Task) {
539-
appInstall.Status = constant.UpErr
548+
appInstall.Status = constant.StatusUpErr
540549
appInstall.Message = installTask.Task.ErrorMsg
541550
_ = appInstallRepo.Save(context.Background(), appInstall)
542551
}
@@ -545,7 +554,7 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
545554

546555
go func() {
547556
if taskErr := installTask.Execute(); taskErr != nil {
548-
appInstall.Status = constant.InstallErr
557+
appInstall.Status = constant.StatusInstallErr
549558
appInstall.Message = taskErr.Error()
550559
if strings.Contains(taskErr.Error(), "Timeout") && strings.Contains(taskErr.Error(), "Pulling") {
551560
appInstall.Message = buserr.New("PullImageTimeout").Error() + appInstall.Message
@@ -808,7 +817,7 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
808817
if err != nil {
809818
return nil, err
810819
}
811-
if setting.AppStoreSyncStatus == constant.Syncing {
820+
if setting.AppStoreSyncStatus == constant.StatusSyncing {
812821
res.IsSyncing = true
813822
return res, nil
814823
}
@@ -931,7 +940,7 @@ func (a AppService) SyncAppListFromRemote(taskID string) (err error) {
931940
list = updateRes.AppList
932941
}
933942
settingService := NewISettingService()
934-
_ = settingService.Update("AppStoreSyncStatus", constant.Syncing)
943+
_ = settingService.Update("AppStoreSyncStatus", constant.StatusSyncing)
935944

936945
setting, err := settingService.GetSettingInfo()
937946
if err != nil {
@@ -1156,7 +1165,7 @@ func (a AppService) SyncAppListFromRemote(taskID string) (err error) {
11561165
}
11571166
tx.Commit()
11581167

1159-
_ = settingService.Update("AppStoreSyncStatus", constant.SyncSuccess)
1168+
_ = settingService.Update("AppStoreSyncStatus", constant.StatusSyncSuccess)
11601169
_ = settingService.Update("AppStoreLastModified", strconv.Itoa(list.LastModified))
11611170
t.Log(i18n.GetMsgByKey("AppStoreSyncSuccess"))
11621171
return nil
@@ -1165,7 +1174,7 @@ func (a AppService) SyncAppListFromRemote(taskID string) (err error) {
11651174
go func() {
11661175
if err = syncTask.Execute(); err != nil {
11671176
_ = NewISettingService().Update("AppStoreLastModified", "0")
1168-
_ = NewISettingService().Update("AppStoreSyncStatus", constant.Error)
1177+
_ = NewISettingService().Update("AppStoreSyncStatus", constant.StatusError)
11691178
}
11701179
}()
11711180

agent/app/service/app_install.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,11 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error {
406406
_ = fileOp.WriteFile(installed.GetComposePath(), strings.NewReader(backupDockerCompose), constant.DirPerm)
407407
return err
408408
}
409-
installed.Status = constant.Running
409+
installed.Status = constant.StatusRunning
410410
_ = appInstallRepo.Save(context.Background(), &installed)
411411

412412
website, _ := websiteRepo.GetFirst(websiteRepo.WithAppInstallId(installed.ID))
413-
if changePort && website.ID != 0 && website.Status == constant.Running {
413+
if changePort && website.ID != 0 && website.Status == constant.StatusRunning {
414414
go func() {
415415
nginxInstall, err := getNginxFull(&website)
416416
if err != nil {
@@ -455,9 +455,9 @@ func (a *AppInstallService) SyncAll(systemInit bool) error {
455455
return err
456456
}
457457
for _, i := range allList {
458-
if i.Status == constant.Installing || i.Status == constant.Upgrading || i.Status == constant.Rebuilding || i.Status == constant.Uninstalling {
458+
if i.Status == constant.StatusInstalling || i.Status == constant.StatusUpgrading || i.Status == constant.StatusRebuilding || i.Status == constant.StatusUninstalling {
459459
if systemInit {
460-
i.Status = constant.Error
460+
i.Status = constant.StatusError
461461
i.Message = "1Panel restart causes the task to terminate"
462462
_ = appInstallRepo.Save(context.Background(), &i)
463463
}
@@ -498,8 +498,10 @@ func (a *AppInstallService) GetServices(key string) ([]response.AppService, erro
498498
}
499499
service.Config = paramMap
500500
service.From = constant.AppResourceLocal
501+
service.Status = install.Status
501502
} else {
502503
service.From = constant.AppResourceRemote
504+
service.Status = constant.StatusRunning
503505
}
504506
res = append(res, service)
505507
}
@@ -508,7 +510,7 @@ func (a *AppInstallService) GetServices(key string) ([]response.AppService, erro
508510
if err != nil {
509511
return nil, err
510512
}
511-
installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID), appInstallRepo.WithStatus(constant.Running))
513+
installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID), appInstallRepo.WithStatus(constant.StatusRunning))
512514
if err != nil {
513515
return nil, err
514516
}
@@ -521,6 +523,7 @@ func (a *AppInstallService) GetServices(key string) ([]response.AppService, erro
521523
Label: install.Name,
522524
Value: install.ServiceName,
523525
Config: paramMap,
526+
Status: install.Status,
524527
})
525528
}
526529
}
@@ -774,7 +777,7 @@ func (a *AppInstallService) GetParams(id uint) (*response.AppConfig, error) {
774777
}
775778

776779
func syncAppInstallStatus(appInstall *model.AppInstall, force bool) error {
777-
if appInstall.Status == constant.Installing || appInstall.Status == constant.Rebuilding || appInstall.Status == constant.Upgrading || appInstall.Status == constant.Uninstalling {
780+
if appInstall.Status == constant.StatusInstalling || appInstall.Status == constant.StatusRebuilding || appInstall.Status == constant.StatusUpgrading || appInstall.Status == constant.StatusUninstalling {
778781
return nil
779782
}
780783
cli, err := docker.NewClient()

agent/app/service/app_utils.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ func deleteAppInstall(deleteReq request.AppInstallDelete) error {
333333
}
334334

335335
uninstall := func(t *task.Task) error {
336-
install.Status = constant.Uninstalling
336+
install.Status = constant.StatusUninstalling
337337
_ = appInstallRepo.Save(context.Background(), &install)
338338
dir, _ := os.Stat(appDir)
339339
if dir != nil {
@@ -434,7 +434,7 @@ func deleteAppInstall(deleteReq request.AppInstallDelete) error {
434434
uninstallTask.AddSubTask(task.GetTaskName(install.Name, task.TaskUninstall, task.TaskScopeApp), uninstall, nil)
435435
go func() {
436436
if err := uninstallTask.Execute(); err != nil && !deleteReq.ForceDelete {
437-
install.Status = constant.Error
437+
install.Status = constant.StatusError
438438
_ = appInstallRepo.Save(context.Background(), &install)
439439
}
440440
}()
@@ -554,7 +554,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
554554
if err != nil {
555555
return err
556556
}
557-
install.Status = constant.Upgrading
557+
install.Status = constant.StatusUpgrading
558558

559559
var (
560560
upErr error
@@ -747,7 +747,7 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
747747
return err
748748
}
749749
t.LogSuccess(logStr)
750-
install.Status = constant.Running
750+
install.Status = constant.StatusRunning
751751
return appInstallRepo.Save(context.Background(), &install)
752752
}
753753

@@ -769,8 +769,8 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
769769
err = upgradeTask.Execute()
770770
if err != nil {
771771
existInstall, _ := appInstallRepo.GetFirst(repo.WithByID(req.InstallID))
772-
if existInstall.ID > 0 && existInstall.Status != constant.Running {
773-
existInstall.Status = constant.UpgradeErr
772+
if existInstall.ID > 0 && existInstall.Status != constant.StatusRunning {
773+
existInstall.Status = constant.StatusUpgradeErr
774774
existInstall.Message = err.Error()
775775
_ = appInstallRepo.Save(context.Background(), &existInstall)
776776
}
@@ -889,7 +889,7 @@ func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.App
889889
defer func() {
890890
if err != nil {
891891
if appInstall != nil {
892-
appInstall.Status = constant.DownloadErr
892+
appInstall.Status = constant.StatusDownloadErr
893893
appInstall.Message = err.Error()
894894
}
895895
}
@@ -1105,18 +1105,18 @@ func upApp(task *task.Task, appInstall *model.AppInstall, pullImages bool) error
11051105
if appInstall.Message == "" {
11061106
appInstall.Message = err.Error()
11071107
}
1108-
appInstall.Status = constant.UpErr
1108+
appInstall.Status = constant.StatusUpErr
11091109
_ = appInstallRepo.Save(context.Background(), appInstall)
11101110
return err
11111111
} else {
1112-
appInstall.Status = constant.Running
1112+
appInstall.Status = constant.StatusRunning
11131113
_ = appInstallRepo.Save(context.Background(), appInstall)
11141114
return nil
11151115
}
11161116
}
11171117

11181118
func rebuildApp(appInstall model.AppInstall) error {
1119-
appInstall.Status = constant.Rebuilding
1119+
appInstall.Status = constant.StatusRebuilding
11201120
_ = appInstallRepo.Save(context.Background(), &appInstall)
11211121
go func() {
11221122
dockerComposePath := appInstall.GetComposePath()
@@ -1137,7 +1137,7 @@ func rebuildApp(appInstall model.AppInstall) error {
11371137
}
11381138
appInstall.ContainerName = strings.Join(containerNames, ",")
11391139

1140-
appInstall.Status = constant.Running
1140+
appInstall.Status = constant.StatusRunning
11411141
_ = appInstallRepo.Save(context.Background(), &appInstall)
11421142
}()
11431143
return nil
@@ -1336,24 +1336,24 @@ func handleErr(install model.AppInstall, err error, out string) error {
13361336
install.Message = out
13371337
reErr = errors.New(out)
13381338
}
1339-
install.Status = constant.UpErr
1339+
install.Status = constant.StatusUpErr
13401340
_ = appInstallRepo.Save(context.Background(), &install)
13411341
return reErr
13421342
}
13431343

13441344
func doNotNeedSync(installed model.AppInstall) bool {
1345-
return installed.Status == constant.Installing || installed.Status == constant.Rebuilding || installed.Status == constant.Upgrading ||
1346-
installed.Status == constant.Syncing || installed.Status == constant.Uninstalling || installed.Status == constant.InstallErr
1345+
return installed.Status == constant.StatusInstalling || installed.Status == constant.StatusRebuilding || installed.Status == constant.StatusUpgrading ||
1346+
installed.Status == constant.StatusSyncing || installed.Status == constant.StatusUninstalling || installed.Status == constant.StatusInstallErr
13471347
}
13481348

13491349
func synAppInstall(containers map[string]types.Container, appInstall *model.AppInstall, force bool) {
13501350
oldStatus := appInstall.Status
13511351
containerNames := strings.Split(appInstall.ContainerName, ",")
13521352
if len(containers) == 0 {
1353-
if appInstall.Status == constant.UpErr && !force {
1353+
if appInstall.Status == constant.StatusUpErr && !force {
13541354
return
13551355
}
1356-
appInstall.Status = constant.Error
1356+
appInstall.Status = constant.StatusError
13571357
appInstall.Message = buserr.WithName("ErrContainerNotFound", strings.Join(containerNames, ",")).Error()
13581358
_ = appInstallRepo.Save(context.Background(), appInstall)
13591359
return
@@ -1384,21 +1384,21 @@ func synAppInstall(containers map[string]types.Container, appInstall *model.AppI
13841384
}
13851385
switch {
13861386
case exitedCount == total:
1387-
appInstall.Status = constant.Stopped
1387+
appInstall.Status = constant.StatusStopped
13881388
case runningCount == total:
1389-
appInstall.Status = constant.Running
1390-
if oldStatus == constant.Running {
1389+
appInstall.Status = constant.StatusRunning
1390+
if oldStatus == constant.StatusRunning {
13911391
return
13921392
}
13931393
case restartingCount == total:
1394-
appInstall.Status = constant.Restating
1394+
appInstall.Status = constant.StatusRestarting
13951395
case pausedCount == total:
1396-
appInstall.Status = constant.Paused
1396+
appInstall.Status = constant.StatusPaused
13971397
case len(notFoundNames) == total:
1398-
if appInstall.Status == constant.UpErr && !force {
1398+
if appInstall.Status == constant.StatusUpErr && !force {
13991399
return
14001400
}
1401-
appInstall.Status = constant.Error
1401+
appInstall.Status = constant.StatusError
14021402
appInstall.Message = buserr.WithName("ErrContainerNotFound", strings.Join(notFoundNames, ",")).Error()
14031403
default:
14041404
var msg string
@@ -1412,7 +1412,7 @@ func synAppInstall(containers map[string]types.Container, appInstall *model.AppI
14121412
msg = buserr.New("ErrAppWarn").Error()
14131413
}
14141414
appInstall.Message = msg
1415-
appInstall.Status = constant.UnHealthy
1415+
appInstall.Status = constant.StatusUnHealthy
14161416
}
14171417
_ = appInstallRepo.Save(context.Background(), appInstall)
14181418
}
@@ -1761,7 +1761,7 @@ func getMajorVersion(version string) string {
17611761
}
17621762

17631763
func ignoreUpdate(installed model.AppInstall) bool {
1764-
if installed.App.Type == "php" || installed.Status == constant.Installing {
1764+
if installed.App.Type == "php" || installed.Status == constant.StatusInstalling {
17651765
return true
17661766
}
17671767
if installed.App.Key == constant.AppMysql {

agent/app/service/backup_record.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ func (u *BackupRecordService) LoadRecordSize(req dto.SearchForSize) ([]dto.Recor
224224
default:
225225
_, records, err := backupRepo.PageRecord(
226226
req.Page, req.PageSize,
227+
repo.WithOrderBy("created_at desc"),
227228
repo.WithByName(req.Name),
228229
repo.WithByType(req.Type),
229230
repo.WithByDetailName(req.DetailName),

agent/app/service/docker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ type logOption struct {
5555
func (u *DockerService) LoadDockerStatus() string {
5656
client, err := docker.NewDockerClient()
5757
if err != nil {
58-
return constant.Stopped
58+
return constant.StatusStopped
5959
}
6060
defer client.Close()
6161
if _, err := client.Ping(context.Background()); err != nil {
62-
return constant.Stopped
62+
return constant.StatusStopped
6363
}
6464

6565
return constant.StatusRunning

agent/app/service/snapshot_recover.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ func recoverAppData(src string, itemHelper *snapRecoverHelper) error {
303303
var wg sync.WaitGroup
304304
for i := 0; i < len(appInstalls); i++ {
305305
wg.Add(1)
306-
appInstalls[i].Status = constant.Rebuilding
306+
appInstalls[i].Status = constant.StatusRebuilding
307307
_ = appInstallRepo.Save(context.Background(), &appInstalls[i])
308308
go func(app model.AppInstall) {
309309
defer wg.Done()
310310
dockerComposePath := app.GetComposePath()
311311
_, _ = compose.Up(dockerComposePath)
312-
app.Status = constant.Running
312+
app.Status = constant.StatusRunning
313313
_ = appInstallRepo.Save(context.Background(), &app)
314314
}(appInstalls[i])
315315
}

agent/app/service/website.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]r
11531153
Version: install.Version,
11541154
AppName: install.App.Name,
11551155
})
1156-
if install.Status != constant.Running {
1156+
if install.Status != constant.StatusRunning {
11571157
showErr = true
11581158
}
11591159
}

agent/app/service/website_ssl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (w WebsiteSSLService) ObtainSSL(apply request.WebsiteSSLApply) error {
377377

378378
func handleError(websiteSSL *model.WebsiteSSL, err error) {
379379
if websiteSSL.Status == constant.SSLInit || websiteSSL.Status == constant.SSLError {
380-
websiteSSL.Status = constant.Error
380+
websiteSSL.Status = constant.StatusError
381381
} else {
382382
websiteSSL.Status = constant.SSLApplyError
383383
}

0 commit comments

Comments
 (0)