@@ -5,9 +5,12 @@ import (
5
5
"bytes"
6
6
"context"
7
7
"crypto/x509"
8
+ "encoding/json"
8
9
"encoding/pem"
9
10
"errors"
10
11
"fmt"
12
+ "github.com/1Panel-dev/1Panel/backend/utils/compose"
13
+ "github.com/1Panel-dev/1Panel/backend/utils/env"
11
14
"os"
12
15
"path"
13
16
"reflect"
@@ -54,20 +57,25 @@ type IWebsiteService interface {
54
57
CreateWebsiteDomain (create request.WebsiteDomainCreate ) (model.WebsiteDomain , error )
55
58
GetWebsiteDomain (websiteId uint ) ([]model.WebsiteDomain , error )
56
59
DeleteWebsiteDomain (domainId uint ) error
60
+
57
61
GetNginxConfigByScope (req request.NginxScopeReq ) (* response.WebsiteNginxConfig , error )
58
62
UpdateNginxConfigByScope (req request.NginxConfigUpdate ) error
59
63
GetWebsiteNginxConfig (websiteId uint , configType string ) (response.FileInfo , error )
64
+ UpdateNginxConfigFile (req request.WebsiteNginxUpdate ) error
60
65
GetWebsiteHTTPS (websiteId uint ) (response.WebsiteHTTPS , error )
61
66
OpWebsiteHTTPS (ctx context.Context , req request.WebsiteHTTPSOp ) (* response.WebsiteHTTPS , error )
67
+ OpWebsiteLog (req request.WebsiteLogReq ) (* response.WebsiteLog , error )
68
+ ChangeDefaultServer (id uint ) error
62
69
PreInstallCheck (req request.WebsiteInstallCheckReq ) ([]response.WebsitePreInstallCheck , error )
70
+
63
71
GetWafConfig (req request.WebsiteWafReq ) (response.WebsiteWafConfig , error )
64
72
UpdateWafConfig (req request.WebsiteWafUpdate ) error
65
- UpdateNginxConfigFile (req request.WebsiteNginxUpdate ) error
66
- OpWebsiteLog (req request.WebsiteLogReq ) (* response.WebsiteLog , error )
67
- ChangeDefaultServer (id uint ) error
73
+
68
74
GetPHPConfig (id uint ) (* response.PHPConfig , error )
69
75
UpdatePHPConfig (req request.WebsitePHPConfigUpdate ) error
70
76
UpdatePHPConfigFile (req request.WebsitePHPFileUpdate ) error
77
+ ChangePHPVersion (req request.WebsitePHPVersionReq ) error
78
+
71
79
GetRewriteConfig (req request.NginxRewriteReq ) (* response.NginxRewriteRes , error )
72
80
UpdateRewriteConfig (req request.NginxRewriteUpdate ) error
73
81
UpdateSiteDir (req request.WebsiteUpdateDir ) error
@@ -1036,7 +1044,7 @@ func (w WebsiteService) GetPHPConfig(id uint) (*response.PHPConfig, error) {
1036
1044
phpConfigPath := path .Join (appInstall .GetPath (), "conf" , "php.ini" )
1037
1045
fileOp := files .NewFileOp ()
1038
1046
if ! fileOp .Stat (phpConfigPath ) {
1039
- return nil , buserr .WithDetail ( constant . ErrFileCanNotRead , " php.ini" , nil )
1047
+ return nil , buserr .WithMap ( "ErrFileNotFound" , map [ string ] interface {}{ "name" : " php.ini"} , nil )
1040
1048
}
1041
1049
params := make (map [string ]string )
1042
1050
configFile , err := fileOp .OpenFile (phpConfigPath )
@@ -1090,7 +1098,7 @@ func (w WebsiteService) UpdatePHPConfig(req request.WebsitePHPConfigUpdate) (err
1090
1098
phpConfigPath := path .Join (appInstall .GetPath (), "conf" , "php.ini" )
1091
1099
fileOp := files .NewFileOp ()
1092
1100
if ! fileOp .Stat (phpConfigPath ) {
1093
- return buserr .WithDetail ( constant . ErrFileCanNotRead , " php.ini" , nil )
1101
+ return buserr .WithMap ( "ErrFileNotFound" , map [ string ] interface {}{ "name" : " php.ini"} , nil )
1094
1102
}
1095
1103
configFile , err := fileOp .OpenFile (phpConfigPath )
1096
1104
if err != nil {
@@ -1182,6 +1190,109 @@ func (w WebsiteService) UpdatePHPConfigFile(req request.WebsitePHPFileUpdate) er
1182
1190
return nil
1183
1191
}
1184
1192
1193
+ func (w WebsiteService ) ChangePHPVersion (req request.WebsitePHPVersionReq ) error {
1194
+
1195
+ website , err := websiteRepo .GetFirst (commonRepo .WithByID (req .WebsiteID ))
1196
+ if err != nil {
1197
+ return err
1198
+ }
1199
+ runtime , err := runtimeRepo .GetFirst (commonRepo .WithByID (req .RuntimeID ))
1200
+ if err != nil {
1201
+ return err
1202
+ }
1203
+ oldRuntime , err := runtimeRepo .GetFirst (commonRepo .WithByID (req .RuntimeID ))
1204
+ if err != nil {
1205
+ return err
1206
+ }
1207
+ if runtime .Resource == constant .ResourceLocal || oldRuntime .Resource == constant .ResourceLocal {
1208
+ return buserr .New ("ErrPHPResource" )
1209
+ }
1210
+ appInstall , err := appInstallRepo .GetFirst (commonRepo .WithByID (website .AppInstallID ))
1211
+ if err != nil {
1212
+ return err
1213
+ }
1214
+ appDetail , err := appDetailRepo .GetFirst (commonRepo .WithByID (runtime .AppDetailID ))
1215
+ if err != nil {
1216
+ return err
1217
+ }
1218
+
1219
+ envs := make (map [string ]interface {})
1220
+ if err = json .Unmarshal ([]byte (appInstall .Env ), & envs ); err != nil {
1221
+ return err
1222
+ }
1223
+ if out , err := compose .Down (appInstall .GetComposePath ()); err != nil {
1224
+ if out != "" {
1225
+ return errors .New (out )
1226
+ }
1227
+ return err
1228
+ }
1229
+
1230
+ var (
1231
+ busErr error
1232
+ fileOp = files .NewFileOp ()
1233
+ envPath = appInstall .GetEnvPath ()
1234
+ composePath = appInstall .GetComposePath ()
1235
+ confDir = path .Join (appInstall .GetPath (), "conf" )
1236
+ backupConfDir = path .Join (appInstall .GetPath (), "conf_bak" )
1237
+ fpmConfDir = path .Join (confDir , "php-fpm.conf" )
1238
+ phpDir = path .Join (constant .RuntimeDir , runtime .Type , runtime .Name , "php" )
1239
+ oldFmContent , _ = fileOp .GetContent (fpmConfDir )
1240
+ )
1241
+ envParams := make (map [string ]string , len (envs ))
1242
+ handleMap (envs , envParams )
1243
+ envParams ["IMAGE_NAME" ] = runtime .Image
1244
+ defer func () {
1245
+ if busErr != nil {
1246
+ envParams ["IMAGE_NAME" ] = oldRuntime .Image
1247
+ _ = env .Write (envParams , envPath )
1248
+ _ = fileOp .WriteFile (composePath , strings .NewReader (appInstall .DockerCompose ), 0775 )
1249
+ if fileOp .Stat (backupConfDir ) {
1250
+ _ = fileOp .DeleteDir (confDir )
1251
+ _ = fileOp .Rename (backupConfDir , confDir )
1252
+ }
1253
+ }
1254
+ }()
1255
+
1256
+ if busErr = env .Write (envParams , envPath ); busErr != nil {
1257
+ return busErr
1258
+ }
1259
+ if busErr = fileOp .WriteFile (composePath , strings .NewReader (appDetail .DockerCompose ), 0775 ); busErr != nil {
1260
+ return busErr
1261
+ }
1262
+ if ! req .RetainConfig {
1263
+ if busErr = fileOp .Rename (confDir , backupConfDir ); busErr != nil {
1264
+ return busErr
1265
+ }
1266
+ _ = fileOp .CreateDir (confDir , 0755 )
1267
+ if busErr = fileOp .CopyFile (path .Join (phpDir , "php-fpm.conf" ), confDir ); busErr != nil {
1268
+ return busErr
1269
+ }
1270
+ if busErr = fileOp .CopyFile (path .Join (phpDir , "php.ini" ), confDir ); busErr != nil {
1271
+ _ = fileOp .WriteFile (fpmConfDir , bytes .NewReader (oldFmContent ), 0775 )
1272
+ return busErr
1273
+ }
1274
+ }
1275
+ if out , err := compose .Up (appInstall .GetComposePath ()); err != nil {
1276
+ if out != "" {
1277
+ busErr = errors .New (out )
1278
+ return busErr
1279
+ }
1280
+ busErr = err
1281
+ return busErr
1282
+ }
1283
+
1284
+ _ = fileOp .DeleteDir (backupConfDir )
1285
+
1286
+ appInstall .AppDetailId = runtime .AppDetailID
1287
+ appInstall .AppId = appDetail .AppId
1288
+ appInstall .Version = appDetail .Version
1289
+ appInstall .DockerCompose = appDetail .DockerCompose
1290
+
1291
+ _ = appInstallRepo .Save (context .Background (), & appInstall )
1292
+ website .RuntimeID = req .RuntimeID
1293
+ return websiteRepo .Save (context .Background (), & website )
1294
+ }
1295
+
1185
1296
func (w WebsiteService ) UpdateRewriteConfig (req request.NginxRewriteUpdate ) error {
1186
1297
website , err := websiteRepo .GetFirst (commonRepo .WithByID (req .WebsiteID ))
1187
1298
if err != nil {
0 commit comments