Skip to content

Commit b4dafda

Browse files
committed
feat: set app Web access address during website deployment
1 parent 97fafa7 commit b4dafda

5 files changed

Lines changed: 58 additions & 39 deletions

File tree

‎agent/app/repo/app_install.go‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type IAppInstallRepo interface {
3131
GetFirst(opts ...DBOption) (model.AppInstall, error)
3232
Create(ctx context.Context, install *model.AppInstall) error
3333
Save(ctx context.Context, install *model.AppInstall) error
34-
UpdateStatus(install *model.AppInstall) error
34+
UpdateWebUI(id uint, address string) error
35+
UpdateStatus(install *model.AppInstall, matchUpdatedAt bool) error
3536
DeleteBy(opts ...DBOption) error
3637
Delete(ctx context.Context, install model.AppInstall) error
3738
Page(page, size int, opts ...DBOption) (int64, []model.AppInstall, error)
@@ -47,9 +48,20 @@ func NewIAppInstallRepo() IAppInstallRepo {
4748
return &AppInstallRepo{}
4849
}
4950

50-
func (a *AppInstallRepo) UpdateStatus(install *model.AppInstall) error {
51-
return getDb().Model(&model.AppInstall{}).Where("id = ? AND updated_at = ?", install.ID, install.UpdatedAt).
52-
Updates(map[string]interface{}{"status": install.Status, "message": install.Message, "container_name": install.ContainerName}).Error
51+
func (a *AppInstallRepo) UpdateWebUI(id uint, address string) error {
52+
result := getDb().Model(&model.AppInstall{}).Where("id = ?", id).Update("web_ui", address)
53+
if result.Error == nil && result.RowsAffected == 0 {
54+
return gorm.ErrRecordNotFound
55+
}
56+
return result.Error
57+
}
58+
59+
func (a *AppInstallRepo) UpdateStatus(install *model.AppInstall, matchUpdatedAt bool) error {
60+
db := getDb().Model(&model.AppInstall{}).Where("id = ?", install.ID)
61+
if matchUpdatedAt {
62+
db = db.Where("updated_at = ?", install.UpdatedAt)
63+
}
64+
return db.Updates(map[string]interface{}{"status": install.Status, "message": install.Message, "container_name": install.ContainerName}).Error
5365
}
5466

5567
func (a *AppInstallRepo) WithDetailIdsIn(detailIds []uint) DBOption {
@@ -151,7 +163,7 @@ func (a *AppInstallRepo) Create(ctx context.Context, install *model.AppInstall)
151163
}
152164

153165
func (a *AppInstallRepo) Save(ctx context.Context, install *model.AppInstall) error {
154-
return getTx(ctx).Omit("App").Save(&install).Error
166+
return getTx(ctx).Omit("App").Save(install).Error
155167
}
156168

157169
func (a *AppInstallRepo) DeleteBy(opts ...DBOption) error {

‎agent/app/service/app.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ func (a AppService) InstallWithHooks(req request.AppInstallCreate, executeScript
599599
if _, err = SyncLocalModelAccount(*appInstall, 0, modelAccountBaseURL, nil, false, true, nil); err != nil {
600600
appInstall.Status = constant.StatusInstallErr
601601
appInstall.Message = err.Error()
602-
_ = appInstallRepo.Save(context.Background(), appInstall)
602+
_ = appInstallRepo.UpdateStatus(appInstall, false)
603603
return
604604
}
605605
}
@@ -608,7 +608,7 @@ func (a AppService) InstallWithHooks(req request.AppInstallCreate, executeScript
608608
if err != nil {
609609
appInstall.Status = constant.StatusInstallErr
610610
appInstall.Message = err.Error()
611-
if saveErr := appInstallRepo.Save(context.Background(), appInstall); saveErr != nil {
611+
if saveErr := appInstallRepo.UpdateStatus(appInstall, false); saveErr != nil {
612612
err = fmt.Errorf("%w; save failed install status: %v", err, saveErr)
613613
}
614614
return
@@ -650,7 +650,7 @@ func (a AppService) InstallWithHooks(req request.AppInstallCreate, executeScript
650650
handleAppStatus := func(t *task.Task) {
651651
appInstall.Status = constant.StatusUpErr
652652
appInstall.Message = installTask.Task.ErrorMsg
653-
_ = appInstallRepo.Save(context.Background(), appInstall)
653+
_ = appInstallRepo.UpdateStatus(appInstall, false)
654654
}
655655

656656
installTimeout := time.Hour
@@ -666,7 +666,7 @@ func (a AppService) InstallWithHooks(req request.AppInstallCreate, executeScript
666666
if strings.Contains(taskErr.Error(), "Timeout") && strings.Contains(taskErr.Error(), "Pulling") {
667667
appInstall.Message = buserr.New("PullImageTimeout").Error() + appInstall.Message
668668
}
669-
_ = appInstallRepo.Save(context.Background(), appInstall)
669+
_ = appInstallRepo.UpdateStatus(appInstall, false)
670670
}
671671
}()
672672

‎agent/app/service/app_install.go‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,7 @@ func operateAppTask(install model.AppInstall, req request.AppInstalledOperate, o
455455
}
456456

457457
func (a *AppInstallService) UpdateAppConfig(req request.AppConfigUpdate) error {
458-
installed, err := appInstallRepo.GetFirst(repo.WithByID(req.InstallID))
459-
if err != nil {
460-
return err
461-
}
462-
installed.WebUI = ""
463-
if req.WebUI != "" {
464-
installed.WebUI = req.WebUI
465-
}
466-
return appInstallRepo.Save(context.Background(), &installed)
458+
return appInstallRepo.UpdateWebUI(req.InstallID, req.WebUI)
467459
}
468460

469461
func (a *AppInstallService) UpdateSort(req request.AppInstallSort) error {

‎agent/app/service/app_utils.go‎

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,26 +1209,20 @@ func upApp(task *task.Task, appInstall *model.AppInstall, pullImages, useLifecyc
12091209
task.LogSuccess(logStr)
12101210
return
12111211
}
1212-
exist, _ := appInstallRepo.GetFirst(repo.WithByID(appInstall.ID))
1213-
if exist.ID > 0 {
1214-
containerNames, err := getContainerNames(*appInstall)
1215-
if err == nil {
1216-
if len(containerNames) > 0 {
1217-
appInstall.ContainerName = strings.Join(containerNames, ",")
1218-
}
1219-
_ = appInstallRepo.Save(context.Background(), appInstall)
1220-
}
1212+
if containerNames, err := getContainerNames(*appInstall); err == nil && len(containerNames) > 0 {
1213+
appInstall.ContainerName = strings.Join(containerNames, ",")
1214+
_ = appInstallRepo.BatchUpdateBy(map[string]interface{}{"container_name": appInstall.ContainerName}, repo.WithByID(appInstall.ID))
12211215
}
12221216
if err := upProject(appInstall); err != nil {
12231217
if appInstall.Message == "" {
12241218
appInstall.Message = err.Error()
12251219
}
12261220
appInstall.Status = constant.StatusUpErr
1227-
_ = appInstallRepo.Save(context.Background(), appInstall)
1221+
_ = appInstallRepo.UpdateStatus(appInstall, false)
12281222
return err
12291223
} else {
12301224
appInstall.Status = constant.StatusRunning
1231-
_ = appInstallRepo.Save(context.Background(), appInstall)
1225+
_ = appInstallRepo.UpdateStatus(appInstall, false)
12321226
return nil
12331227
}
12341228
}
@@ -1525,11 +1519,7 @@ func synAppInstall(containers map[string]container.Summary, appInstall *model.Ap
15251519
}
15261520
appInstall.Status = constant.StatusError
15271521
appInstall.Message = buserr.WithName("ErrContainerNotFound", strings.Join(containerNames, ",")).Error()
1528-
if appInstall.App.Key == "ollama" || appInstall.App.Key == "vllm" {
1529-
_ = appInstallRepo.UpdateStatus(appInstall)
1530-
} else {
1531-
_ = appInstallRepo.Save(context.Background(), appInstall)
1532-
}
1522+
_ = appInstallRepo.UpdateStatus(appInstall, true)
15331523
return
15341524
}
15351525
notFoundNames := make([]string, 0)
@@ -1588,11 +1578,7 @@ func synAppInstall(containers map[string]container.Summary, appInstall *model.Ap
15881578
appInstall.Message = msg
15891579
appInstall.Status = constant.StatusUnHealthy
15901580
}
1591-
if appInstall.App.Key == "ollama" || appInstall.App.Key == "vllm" {
1592-
_ = appInstallRepo.UpdateStatus(appInstall)
1593-
} else {
1594-
_ = appInstallRepo.Save(context.Background(), appInstall)
1595-
}
1581+
_ = appInstallRepo.UpdateStatus(appInstall, true)
15961582
}
15971583

15981584
func handleInstalled(appInstallList []model.AppInstall, updated, sync, checkUpdate bool) ([]response.AppInstallDTO, error) {

‎agent/app/service/website.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path"
1313
"reflect"
1414
"regexp"
15+
"slices"
1516
"strconv"
1617
"strings"
1718
"syscall"
@@ -279,6 +280,25 @@ func newWebsiteCreateHTTPSOp(sslID uint) request.WebsiteHTTPSOp {
279280
}
280281
}
281282

283+
func deploymentWebAddress(protocol string, domains []model.WebsiteDomain, httpPort, httpsPort int) string {
284+
host := strings.Trim(domains[0].Domain, "[]")
285+
port := domains[0].Port
286+
standardPort := 80
287+
if protocol == constant.ProtocolHTTPS {
288+
standardPort = 443
289+
plan := buildWebsiteTLSPlan(domains, httpPort, httpsPort)
290+
if !slices.Contains(plan.httpsPorts, port) {
291+
port = plan.redirectPort
292+
}
293+
}
294+
if port != standardPort {
295+
host = net.JoinHostPort(host, strconv.Itoa(port))
296+
} else if strings.Contains(host, ":") {
297+
host = "[" + host + "]"
298+
}
299+
return strings.ToLower(protocol) + "://" + host
300+
}
301+
282302
func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error) {
283303
websiteListenMutation.Lock()
284304
defer websiteListenMutation.Unlock()
@@ -594,6 +614,15 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
594614
}
595615
}
596616

617+
if website.Type == constant.Deployment {
618+
address := deploymentWebAddress(website.Protocol, domains, defaultHttpPort, nginxInstall.HttpsPort)
619+
if err := tx.Model(&model.AppInstall{}).
620+
Where("id = ? AND (web_ui IS NULL OR web_ui = '')", website.AppInstallID).
621+
Update("web_ui", address).Error; err != nil {
622+
return err
623+
}
624+
}
625+
597626
if err := tx.Commit().Error; err != nil {
598627
return err
599628
}

0 commit comments

Comments
 (0)