Skip to content

Commit 605d375

Browse files
committed
feat(website): add batch checkout php version
1 parent 7120faa commit 605d375

23 files changed

Lines changed: 452 additions & 22 deletions

File tree

‎agent/app/api/v2/website.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,3 +1340,16 @@ func (b *BaseApi) BatchSetHttps(c *gin.Context) {
13401340
}
13411341
helper.Success(c)
13421342
}
1343+
1344+
func (b *BaseApi) BatchChangePHPVersion(c *gin.Context) {
1345+
var req request.WebsiteBatchPHPVersionReq
1346+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
1347+
return
1348+
}
1349+
results, err := websiteService.BatchChangePHPVersion(req)
1350+
if err != nil {
1351+
helper.InternalServer(c, err)
1352+
return
1353+
}
1354+
helper.SuccessWithData(c, results)
1355+
}

‎agent/app/dto/request/website.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
)
66

77
type WebsiteSearch struct {
8+
RuntimeType string `json:"runtimeType" validate:"omitempty,oneof=php node java go python dotnet"`
89
dto.PageInfo
910
Name string `json:"name"`
1011
OrderBy string `json:"orderBy" validate:"required,oneof=primary_domain type status createdAt expire_date created_at favorite"`
@@ -393,3 +394,9 @@ type ExecComposerReq struct {
393394
WebsiteID uint `json:"websiteID" validate:"required"`
394395
TaskID string `json:"taskID" validate:"required"`
395396
}
397+
398+
// WebsiteBatchPHPVersionReq changes only existing PHP websites.
399+
type WebsiteBatchPHPVersionReq struct {
400+
IDs []uint `json:"ids" validate:"required,min=1"`
401+
RuntimeID uint `json:"runtimeID" validate:"required"`
402+
}

‎agent/app/dto/response/website.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,10 @@ type Database struct {
128128
From string `json:"from"`
129129
ID uint `json:"id"`
130130
}
131+
132+
type WebsiteBatchPHPVersionResult struct {
133+
ID uint `json:"id"`
134+
Domain string `json:"domain"`
135+
Status string `json:"status"`
136+
Message string `json:"message"`
137+
}

‎agent/app/repo/website.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type IWebsiteRepo interface {
1818
WithDefaultServer() DBOption
1919
WithDomainLike(domain string) DBOption
2020
WithSearchKeyword(keyword string, ids []uint) DBOption
21+
WithRuntimeType(runtimeType string) DBOption
2122
WithRuntimeID(runtimeID uint) DBOption
2223
WithParentID(websiteID uint) DBOption
2324
WithType(websiteType string) DBOption
@@ -60,6 +61,13 @@ func (w *WebsiteRepo) WithAppInstallIds(appInstallIDs []uint) DBOption {
6061
}
6162
}
6263

64+
func (w *WebsiteRepo) WithRuntimeType(runtimeType string) DBOption {
65+
return func(db *gorm.DB) *gorm.DB {
66+
runtimes := db.Session(&gorm.Session{NewDB: true}).Model(&model.Runtime{}).Select("id").Where("type = ?", runtimeType)
67+
return db.Where("type = ? AND runtime_id IN (?)", "runtime", runtimes)
68+
}
69+
}
70+
6371
func (w *WebsiteRepo) WithRuntimeID(runtimeID uint) DBOption {
6472
return func(db *gorm.DB) *gorm.DB {
6573
return db.Where("runtime_id = ?", runtimeID)

‎agent/app/service/website.go‎

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ type IWebsiteService interface {
8282
GetWebsite(id uint) (response.WebsiteDTO, error)
8383

8484
ChangePHPVersion(req request.WebsitePHPVersionReq) error
85+
BatchChangePHPVersion(req request.WebsiteBatchPHPVersionReq) ([]response.WebsiteBatchPHPVersionResult, error)
8586
OperateCrossSiteAccess(req request.CrossSiteAccessOp) error
8687
ExecComposer(req request.ExecComposerReq) error
8788
ChangeGroup(group, newGroup uint) error
@@ -189,6 +190,9 @@ func (w WebsiteService) PageWebsite(req request.WebsiteSearch) (int64, []respons
189190
if req.Type != "" {
190191
opts = append(opts, websiteRepo.WithType(req.Type))
191192
}
193+
if req.RuntimeType != "" {
194+
opts = append(opts, websiteRepo.WithRuntimeType(req.RuntimeType))
195+
}
192196
total, websites, err := websiteRepo.Page(req.Page, req.PageSize, opts...)
193197
if err != nil {
194198
return 0, nil, err
@@ -1417,14 +1421,6 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) (err
14171421
if oldRuntime.Resource == constant.ResourceLocal {
14181422
return buserr.New("ErrPHPResource")
14191423
}
1420-
client, err := docker.NewDockerClient()
1421-
if err != nil {
1422-
return err
1423-
}
1424-
defer client.Close()
1425-
if !checkImageLike(client, oldRuntime.Image) {
1426-
return buserr.WithName("ErrImageNotExist", oldRuntime.Name)
1427-
}
14281424
}
14291425

14301426
if req.RuntimeID > 0 {
@@ -1435,6 +1431,17 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) (err
14351431
if runtime.Resource == constant.ResourceLocal {
14361432
return buserr.New("ErrPHPResource")
14371433
}
1434+
if runtime.Type != constant.RuntimePHP {
1435+
return fmt.Errorf("target runtime must be PHP")
1436+
}
1437+
client, err := docker.NewDockerClient()
1438+
if err != nil {
1439+
return err
1440+
}
1441+
defer client.Close()
1442+
if err := validateWebsitePHPImage(context.Background(), client, runtime.Image, runtime.Name); err != nil {
1443+
return err
1444+
}
14381445
website.RuntimeID = runtime.ID
14391446
website.AppInstallID = 0
14401447
website.Proxy = fmt.Sprintf("127.0.0.1:%s", runtime.Port)
@@ -2677,3 +2684,59 @@ func (w WebsiteService) UpdateStream(req request.StreamUpdate) error {
26772684
website.StreamPorts = req.StreamConfig.StreamPorts
26782685
return websiteRepo.Save(context.Background(), &website)
26792686
}
2687+
2688+
func (w WebsiteService) BatchChangePHPVersion(req request.WebsiteBatchPHPVersionReq) ([]response.WebsiteBatchPHPVersionResult, error) {
2689+
target, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(req.RuntimeID))
2690+
if err != nil {
2691+
return nil, err
2692+
}
2693+
if target.Type != constant.RuntimePHP {
2694+
return nil, fmt.Errorf("target runtime must be PHP")
2695+
}
2696+
if target.Resource == constant.ResourceLocal {
2697+
return nil, buserr.New("ErrPHPResource")
2698+
}
2699+
results := make([]response.WebsiteBatchPHPVersionResult, 0, len(req.IDs))
2700+
seen := make(map[uint]bool)
2701+
for _, id := range req.IDs {
2702+
if seen[id] {
2703+
continue
2704+
}
2705+
seen[id] = true
2706+
result := response.WebsiteBatchPHPVersionResult{ID: id, Status: "failed"}
2707+
website, err := websiteRepo.GetFirst(repo.WithByID(id))
2708+
if err != nil {
2709+
result.Message = err.Error()
2710+
results = append(results, result)
2711+
continue
2712+
}
2713+
result.Domain = website.PrimaryDomain
2714+
if website.Type != constant.Runtime {
2715+
result.Status = "skipped"
2716+
result.Message = "notPHP"
2717+
} else {
2718+
runtime, lookupErr := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
2719+
switch {
2720+
case lookupErr != nil:
2721+
result.Message = lookupErr.Error()
2722+
case runtime.Type != constant.RuntimePHP:
2723+
result.Status = "skipped"
2724+
result.Message = "notPHP"
2725+
case runtime.Resource == constant.ResourceLocal:
2726+
result.Status = "skipped"
2727+
result.Message = "localPHP"
2728+
case website.RuntimeID == req.RuntimeID:
2729+
result.Status = "skipped"
2730+
result.Message = "samePHP"
2731+
default:
2732+
if err := w.ChangePHPVersion(request.WebsitePHPVersionReq{WebsiteID: id, RuntimeID: req.RuntimeID}); err != nil {
2733+
result.Message = err.Error()
2734+
} else {
2735+
result.Status = "success"
2736+
}
2737+
}
2738+
}
2739+
results = append(results, result)
2740+
}
2741+
return results, nil
2742+
}

‎agent/app/service/website_utils.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import (
4141
"github.com/1Panel-dev/1Panel/agent/utils/nginx"
4242
"github.com/1Panel-dev/1Panel/agent/utils/nginx/parser"
4343
"github.com/1Panel-dev/1Panel/agent/utils/re"
44+
"github.com/docker/docker/api/types/image"
45+
"github.com/docker/docker/client"
4446
"github.com/pkg/errors"
4547
"gorm.io/gorm"
4648
)
@@ -2136,3 +2138,23 @@ func getManualWebsiteSSL(req request.WebsiteHTTPSOp) (model.WebsiteSSL, error) {
21362138
websiteSSL.Status = constant.SSLReady
21372139
return websiteSSL, nil
21382140
}
2141+
2142+
type websitePHPImageInspector interface {
2143+
ImageInspect(context.Context, string, ...client.ImageInspectOption) (image.InspectResponse, error)
2144+
}
2145+
2146+
// Let Docker resolve the full reference, including registries, tags, digests and IDs.
2147+
// A missing source image must not prevent switching away from that environment.
2148+
func validateWebsitePHPImage(ctx context.Context, inspector websitePHPImageInspector, reference, name string) error {
2149+
if strings.TrimSpace(reference) == "" {
2150+
return buserr.WithName("ErrImageNotExist", name)
2151+
}
2152+
_, err := inspector.ImageInspect(ctx, reference)
2153+
if client.IsErrNotFound(err) {
2154+
return buserr.WithName("ErrImageNotExist", name)
2155+
}
2156+
if err != nil {
2157+
return fmt.Errorf("inspect PHP runtime %s image %s: %w", name, reference, err)
2158+
}
2159+
return nil
2160+
}

‎agent/router/ro_website.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (a *WebsiteRouter) InitRouter(Router *gin.RouterGroup) {
8787
websiteRouter.POST("/lbs/file", baseApi.UpdateLoadBalanceFile)
8888

8989
websiteRouter.POST("/php/version", baseApi.ChangePHPVersion)
90+
websiteRouter.POST("/php/version/batch", baseApi.BatchChangePHPVersion)
9091

9192
websiteRouter.POST("/realip/config", baseApi.SetRealIPConfig)
9293
websiteRouter.GET("/realip/config/:id", baseApi.GetRealIPConfig)

‎frontend/src/api/interface/website.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export namespace Website {
7373
}
7474

7575
export interface WebSiteSearch extends ReqPage {
76+
type?: string;
77+
runtimeType?: string;
7678
name: string;
7779
orderBy: string;
7880
order: string;
@@ -574,6 +576,18 @@ export namespace Website {
574576
content: string;
575577
}
576578

579+
export interface BatchPHPVersionChange {
580+
ids: number[];
581+
runtimeID: number;
582+
}
583+
584+
export interface BatchPHPVersionResult {
585+
id: number;
586+
domain: string;
587+
status: 'success' | 'failed' | 'skipped';
588+
message: string;
589+
}
590+
577591
export interface PHPVersionChange {
578592
websiteID: number;
579593
runtimeID: number;

‎frontend/src/api/modules/website.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,7 @@ export const importCA = (params: {
466466
}) => {
467467
return http.post(`/websites/ca/import`, params);
468468
};
469+
470+
export const batchChangePHPVersion = (req: Website.BatchPHPVersionChange) => {
471+
return http.post<Website.BatchPHPVersionResult[]>(`/websites/php/version/batch`, req, 600000);
472+
};

‎frontend/src/lang/modules/en.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4625,6 +4625,18 @@ const message = {
46254625
isEdirWarn: 'Changes to docker-compose.yml were detected. Please review the diff.',
46264626
},
46274627
website: {
4628+
batchPHP: 'Switch PHP version',
4629+
batchPHPHelper:
4630+
'Only PHP websites will be changed. Other websites are skipped. Local PHP environments are not supported.',
4631+
batchPHPCount: 'Selected: {0}; PHP websites: {1}; others: {2}.',
4632+
notPHP: 'Not a PHP website',
4633+
localPHP: 'Local PHP is not supported',
4634+
samePHP: 'Already using the target PHP environment',
4635+
batchPHPResult: 'Results',
4636+
batchPHPStatus_success: 'Succeeded',
4637+
batchPHPStatus_failed: 'Failed',
4638+
batchPHPStatus_skipped: 'Skipped',
4639+
46284640
primaryDomain: 'Primary domain',
46294641
otherDomains: 'Other domains',
46304642
static: 'Static',

0 commit comments

Comments
 (0)