Skip to content

Commit 809d3e3

Browse files
feat: merge from dev
1 parent f6f1989 commit 809d3e3

File tree

32 files changed

+403
-76
lines changed

32 files changed

+403
-76
lines changed

agent/app/api/v2/file.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"path/filepath"
1212
"strconv"
1313
"strings"
14+
"syscall"
1415

1516
"github.com/1Panel-dev/1Panel/agent/app/api/v2/helper"
1617
"github.com/1Panel-dev/1Panel/agent/app/dto"
@@ -338,7 +339,11 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
338339
mode := info.Mode()
339340

340341
fileOp := files.NewFileOp()
341-
342+
stat, ok := info.Sys().(*syscall.Stat_t)
343+
uid, gid := -1, -1
344+
if ok {
345+
uid, gid = int(stat.Uid), int(stat.Gid)
346+
}
342347
success := 0
343348
failures := make(buserr.MultiErr)
344349
for _, file := range uploadFiles {
@@ -351,6 +356,7 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
351356
global.LOG.Error(e)
352357
continue
353358
}
359+
_ = os.Chown(dstDir, uid, gid)
354360
}
355361
tmpFilename := dstFilename + ".tmp"
356362
if err := c.SaveUploadedFile(file, tmpFilename); err != nil {
@@ -378,6 +384,9 @@ func (b *BaseApi) UploadFiles(c *gin.Context) {
378384
} else {
379385
_ = os.Chmod(dstFilename, mode)
380386
}
387+
if uid != -1 && gid != -1 {
388+
_ = os.Chown(dstFilename, uid, gid)
389+
}
381390
success++
382391
}
383392
if success == 0 {

agent/app/dto/backup.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type CommonBackup struct {
1010
DetailName string `json:"detailName"`
1111
Secret string `json:"secret"`
1212
TaskID string `json:"taskID"`
13+
FileName string `json:"fileName"`
1314
}
1415
type CommonRecover struct {
1516
DownloadAccountID uint `json:"downloadAccountID" validate:"required"`

agent/app/dto/response/file.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ type FileWgetRes struct {
3636
}
3737

3838
type FileLineContent struct {
39-
Content string `json:"content"`
40-
End bool `json:"end"`
41-
Path string `json:"path"`
42-
Total int `json:"total"`
39+
Content string `json:"content"`
40+
End bool `json:"end"`
41+
Path string `json:"path"`
42+
Total int `json:"total"`
43+
Lines []string `json:"lines"`
4344
}
4445

4546
type FileExist struct {

agent/app/repo/backup.go

+17
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ type IBackupRepo interface {
1616
CreateRecord(record *model.BackupRecord) error
1717
DeleteRecord(ctx context.Context, opts ...DBOption) error
1818
UpdateRecord(record *model.BackupRecord) error
19+
WithByDetailName(detailName string) DBOption
1920
WithByFileName(fileName string) DBOption
2021
WithByCronID(cronjobID uint) DBOption
22+
WithFileNameStartWith(filePrefix string) DBOption
2123
}
2224

2325
func NewIBackupRepo() IBackupRepo {
@@ -55,6 +57,21 @@ func (u *BackupRepo) WithByFileName(fileName string) DBOption {
5557
}
5658
}
5759

60+
func (u *BackupRepo) WithByDetailName(detailName string) DBOption {
61+
return func(g *gorm.DB) *gorm.DB {
62+
if len(detailName) == 0 {
63+
return g
64+
}
65+
return g.Where("detail_name = ?", detailName)
66+
}
67+
}
68+
69+
func (u *BackupRepo) WithFileNameStartWith(filePrefix string) DBOption {
70+
return func(g *gorm.DB) *gorm.DB {
71+
return g.Where("file_name LIKE ?", filePrefix+"%")
72+
}
73+
}
74+
5875
func (u *BackupRepo) CreateRecord(record *model.BackupRecord) error {
5976
return global.DB.Create(record).Error
6077
}

agent/app/service/app.go

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ func (a AppService) GetAppTags() ([]response.TagDTO, error) {
161161

162162
func (a AppService) GetApp(key string) (*response.AppDTO, error) {
163163
var appDTO response.AppDTO
164+
if key == "postgres" {
165+
key = "postgresql"
166+
}
164167
app, err := appRepo.GetFirst(appRepo.WithKey(key))
165168
if err != nil {
166169
return nil, err

agent/app/service/app_install.go

+23
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ func (a *AppInstallService) GetUpdateVersions(req request.AppUpdateVersion) ([]d
571571
if err != nil {
572572
return versions, err
573573
}
574+
if app.Key == constant.AppMysql {
575+
majorVersion := getMajorVersion(install.Version)
576+
if !strings.HasPrefix(detail.Version, majorVersion) {
577+
continue
578+
}
579+
}
574580
versions = append(versions, dto.AppVersion{
575581
Version: detail.Version,
576582
DetailId: detail.ID,
@@ -740,7 +746,24 @@ func (a *AppInstallService) GetParams(id uint) (*response.AppConfig, error) {
740746
}
741747
}
742748
}
749+
} else if form.Type == "apps" {
750+
if m, ok := form.Child.(map[string]interface{}); ok {
751+
result := make(map[string]string)
752+
for key, value := range m {
753+
if strVal, ok := value.(string); ok {
754+
result[key] = strVal
755+
}
756+
}
757+
if envKey, ok := result["envKey"]; ok {
758+
serviceName := envs[envKey]
759+
if serviceName != nil {
760+
appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithServiceName(serviceName.(string)))
761+
appParam.ShowValue = appInstall.Name
762+
}
763+
}
764+
}
743765
}
766+
744767
params = append(params, appParam)
745768
} else {
746769
params = append(params, response.AppParam{

agent/app/service/app_utils.go

+51-13
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,18 @@ func createLink(ctx context.Context, installTask *task.Task, app model.App, appI
199199
}
200200
case constant.AppRedis:
201201
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
202+
authParam := dto.RedisAuthParam{
203+
RootPassword: "",
204+
}
202205
if password != "" {
203-
authParam := dto.RedisAuthParam{
204-
RootPassword: password.(string),
205-
}
206-
authByte, err := json.Marshal(authParam)
207-
if err != nil {
208-
return err
209-
}
210-
appInstall.Param = string(authByte)
206+
authParam.RootPassword = password.(string)
207+
database.Password = password.(string)
208+
}
209+
authByte, err := json.Marshal(authParam)
210+
if err != nil {
211+
return err
211212
}
212-
database.Password = password.(string)
213+
appInstall.Param = string(authByte)
213214
}
214215
}
215216
return databaseRepo.Create(ctx, database)
@@ -558,11 +559,23 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
558559
)
559560
backUpApp := func(t *task.Task) error {
560561
if req.Backup {
561-
backupRecord, err := NewIBackupService().AppBackup(dto.CommonBackup{Name: install.App.Key, DetailName: install.Name})
562-
if err != nil {
562+
backupService := NewIBackupService()
563+
fileName := fmt.Sprintf("upgrade_backup_%s_%s.tar.gz", install.Name, time.Now().Format(constant.DateTimeSlimLayout)+common.RandStrAndNum(5))
564+
backupRecord, err := backupService.AppBackup(dto.CommonBackup{Name: install.App.Key, DetailName: install.Name, FileName: fileName})
565+
if err == nil {
566+
backups, _ := backupService.ListAppRecords(install.App.Key, install.Name, "upgrade_backup")
567+
if len(backups) > 3 {
568+
backupsToDelete := backups[:len(backups)-3]
569+
var deleteIDs []uint
570+
for _, backup := range backupsToDelete {
571+
deleteIDs = append(deleteIDs, backup.ID)
572+
}
573+
_ = backupService.BatchDeleteRecord(deleteIDs)
574+
}
575+
backupFile = path.Join(global.CONF.System.Backup, backupRecord.FileDir, backupRecord.FileName)
576+
} else {
563577
return buserr.WithNameAndErr("ErrAppBackup", install.Name, err)
564578
}
565-
backupFile = path.Join(global.CONF.System.Backup, backupRecord.FileDir, backupRecord.FileName)
566579
}
567580
return nil
568581
}
@@ -1390,7 +1403,7 @@ func handleInstalled(appInstallList []model.AppInstall, updated bool, sync bool)
13901403
}
13911404

13921405
for _, installed := range appInstallList {
1393-
if updated && (installed.App.Type == "php" || installed.Status == constant.Installing || (installed.App.Key == constant.AppMysql && installed.Version == "5.6.51")) {
1406+
if updated && ignoreUpdate(installed) {
13941407
continue
13951408
}
13961409
if sync && !doNotNeedSync(installed) {
@@ -1691,3 +1704,28 @@ func isHostModel(dockerCompose string) bool {
16911704
}
16921705
return false
16931706
}
1707+
1708+
func getMajorVersion(version string) string {
1709+
parts := strings.Split(version, ".")
1710+
if len(parts) >= 2 {
1711+
return parts[0] + "." + parts[1]
1712+
}
1713+
return version
1714+
}
1715+
1716+
func ignoreUpdate(installed model.AppInstall) bool {
1717+
if installed.App.Type == "php" || installed.Status == constant.Installing {
1718+
return true
1719+
}
1720+
if installed.App.Key == constant.AppMysql {
1721+
majorVersion := getMajorVersion(installed.Version)
1722+
appDetails, _ := appDetailRepo.GetBy(appDetailRepo.WithAppId(installed.App.ID))
1723+
for _, appDetail := range appDetails {
1724+
if strings.HasPrefix(appDetail.Version, majorVersion) && common.CompareVersion(appDetail.Version, installed.Version) {
1725+
return false
1726+
}
1727+
}
1728+
return true
1729+
}
1730+
return false
1731+
}

agent/app/service/backup.go

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type IBackupService interface {
3636
DownloadRecord(info dto.DownloadRecord) (string, error)
3737
DeleteRecordByName(backupType, name, detailName string, withDeleteFile bool) error
3838
BatchDeleteRecord(ids []uint) error
39+
ListAppRecords(name, detailName, fileName string) ([]model.BackupRecord, error)
3940

4041
ListFiles(req dto.OperateByID) []string
4142

@@ -194,6 +195,20 @@ func (u *BackupService) BatchDeleteRecord(ids []uint) error {
194195
return backupRepo.DeleteRecord(context.Background(), commonRepo.WithByIDs(ids))
195196
}
196197

198+
func (u *BackupService) ListAppRecords(name, detailName, fileName string) ([]model.BackupRecord, error) {
199+
records, err := backupRepo.ListRecord(
200+
commonRepo.WithOrderBy("created_at asc"),
201+
commonRepo.WithByName(name),
202+
commonRepo.WithByType("app"),
203+
backupRepo.WithFileNameStartWith(fileName),
204+
backupRepo.WithByDetailName(detailName),
205+
)
206+
if err != nil {
207+
return nil, err
208+
}
209+
return records, err
210+
}
211+
197212
func (u *BackupService) ListFiles(req dto.OperateByID) []string {
198213
var datas []string
199214
account, client, err := NewBackupClientWithID(req.ID)

agent/app/service/backup_app.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func (u *BackupService) AppBackup(req dto.CommonBackup) (*model.BackupRecord, er
3838
itemDir := fmt.Sprintf("app/%s/%s", req.Name, req.DetailName)
3939
backupDir := path.Join(global.CONF.System.Backup, itemDir)
4040

41-
fileName := fmt.Sprintf("%s_%s.tar.gz", req.DetailName, timeNow+common.RandStrAndNum(5))
41+
fileName := req.FileName
42+
if req.FileName == "" {
43+
fileName = fmt.Sprintf("%s_%s.tar.gz", req.DetailName, timeNow+common.RandStrAndNum(5))
44+
}
4245

4346
backupApp := func() (*model.BackupRecord, error) {
4447
if err = handleAppBackup(&install, nil, backupDir, fileName, "", req.Secret, req.TaskID); err != nil {

agent/app/service/container.go

+23
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,29 @@ func calculateNetwork(network map[string]container.NetworkStats) (float64, float
10871087
}
10881088

10891089
func checkImageExist(client *client.Client, imageItem string) bool {
1090+
if client == nil {
1091+
var err error
1092+
client, err = docker.NewDockerClient()
1093+
if err != nil {
1094+
return false
1095+
}
1096+
}
1097+
images, err := client.ImageList(context.Background(), image.ListOptions{})
1098+
if err != nil {
1099+
return false
1100+
}
1101+
1102+
for _, img := range images {
1103+
for _, tag := range img.RepoTags {
1104+
if tag == imageItem || tag == imageItem+":latest" {
1105+
return true
1106+
}
1107+
}
1108+
}
1109+
return false
1110+
}
1111+
1112+
func checkImage(client *client.Client, imageItem string) bool {
10901113
images, err := client.ImageList(context.Background(), image.ListOptions{})
10911114
if err != nil {
10921115
return false

agent/app/service/file.go

+8
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,19 @@ func (f *FileService) ReadLogByLine(req request.FileReadByLineReq) (*response.Fi
467467
if err != nil {
468468
return nil, err
469469
}
470+
if req.Latest && req.Page == 1 && len(lines) < 1000 && total > 1 {
471+
preLines, _, _, err := files.ReadFileByLine(logFilePath, total-1, req.PageSize, false)
472+
if err != nil {
473+
return nil, err
474+
}
475+
lines = append(preLines, lines...)
476+
}
470477
res := &response.FileLineContent{
471478
Content: strings.Join(lines, "\n"),
472479
End: isEndOfFile,
473480
Path: logFilePath,
474481
Total: total,
482+
Lines: lines,
475483
}
476484
return res, nil
477485
}

agent/app/service/website.go

+4
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
378378
switch runtime.Type {
379379
case constant.RuntimePHP:
380380
if runtime.Resource == constant.ResourceAppstore {
381+
if !checkImageExist(nil, runtime.Image) {
382+
return buserr.WithName("ErrImageNotExist", runtime.Name)
383+
}
381384
website.Proxy = fmt.Sprintf("127.0.0.1:%d", runtime.Port)
382385
} else {
383386
website.ProxyType = create.ProxyType
@@ -1085,6 +1088,7 @@ func (w WebsiteService) OpWebsiteHTTPS(ctx context.Context, req request.WebsiteH
10851088
websiteSSL.Provider = constant.Manual
10861089
websiteSSL.PrivateKey = privateKey
10871090
websiteSSL.Pem = certificate
1091+
websiteSSL.Status = constant.SSLReady
10881092

10891093
res.SSL = websiteSSL
10901094
}

agent/cmd/server/nginx_conf/proxy.conf

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ location ^~ /test {
55
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
66
proxy_set_header REMOTE-HOST $remote_addr;
77
proxy_set_header Upgrade $http_upgrade;
8-
proxy_set_header Connection "upgrade";
8+
proxy_set_header Connection $http_connection;
99
proxy_set_header X-Forwarded-Proto $scheme;
1010
proxy_http_version 1.1;
1111

1212
add_header X-Cache $upstream_cache_status;
13+
add_header Cache-Control no-cache;
14+
proxy_ssl_server_name off;
1315
}

agent/i18n/lang/en.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ErrDomainFormat: "{{ .name }} domain format error"
104104
ErrDefaultAlias: "default is a reserved code name, please use another code name"
105105
ErrParentWebsite: "You need to delete the subsite(s) {{ .name }} first"
106106
ErrBuildDirNotFound: "Build directory does not exist"
107+
ErrImageNotExist: "Running environment {{.name}} image does not exist, please re-edit the running environment"
107108

108109
#ssl
109110
ErrSSLCannotDelete: "The certificate {{ .name }} is being used by the website and cannot be removed"

agent/i18n/lang/zh-Hant.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ ErrDomainFormat: "{{ .name }} 域名格式不正確"
104104
ErrDefaultAlias: "default 為保留代號,請使用其他代號"
105105
ErrParentWebsite: "需要先刪除子網站 {{ .name }}"
106106
ErrBuildDirNotFound: "編譯目錄不存在"
107+
ErrImageNotExist: "執行環境 {{.name}} 鏡像不存在,請重新編輯執行環境"
107108

108109
#ssl
109110
ErrSSLCannotDelete: "{{ .name }} 證書正在被網站使用,無法刪除"

agent/i18n/lang/zh.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ ErrDomainFormat: "{{ .name }} 域名格式不正确"
103103
ErrDefaultAlias: "default 为保留代号,请使用其他代号"
104104
ErrParentWebsite: "需要先删除子网站 {{ .name }}"
105105
ErrBuildDirNotFound: "构建目录不存在"
106+
ErrImageNotExist: "运行环境 {{.name}} 镜像不存在,请重新编辑运行环境"
106107

107108
#ssl
108109
ErrSSLCannotDelete: "{{ .name }} 证书正在被网站使用,无法删除"

0 commit comments

Comments
 (0)