@@ -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,108 @@ 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
+ website , err := websiteRepo .GetFirst (commonRepo .WithByID (req .WebsiteID ))
1195
+ if err != nil {
1196
+ return err
1197
+ }
1198
+ runtime , err := runtimeRepo .GetFirst (commonRepo .WithByID (req .RuntimeID ))
1199
+ if err != nil {
1200
+ return err
1201
+ }
1202
+ oldRuntime , err := runtimeRepo .GetFirst (commonRepo .WithByID (req .RuntimeID ))
1203
+ if err != nil {
1204
+ return err
1205
+ }
1206
+ if runtime .Resource == constant .ResourceLocal || oldRuntime .Resource == constant .ResourceLocal {
1207
+ return buserr .New ("ErrPHPResource" )
1208
+ }
1209
+ appInstall , err := appInstallRepo .GetFirst (commonRepo .WithByID (website .AppInstallID ))
1210
+ if err != nil {
1211
+ return err
1212
+ }
1213
+ appDetail , err := appDetailRepo .GetFirst (commonRepo .WithByID (runtime .AppDetailID ))
1214
+ if err != nil {
1215
+ return err
1216
+ }
1217
+
1218
+ envs := make (map [string ]interface {})
1219
+ if err = json .Unmarshal ([]byte (appInstall .Env ), & envs ); err != nil {
1220
+ return err
1221
+ }
1222
+ if out , err := compose .Down (appInstall .GetComposePath ()); err != nil {
1223
+ if out != "" {
1224
+ return errors .New (out )
1225
+ }
1226
+ return err
1227
+ }
1228
+
1229
+ var (
1230
+ busErr error
1231
+ fileOp = files .NewFileOp ()
1232
+ envPath = appInstall .GetEnvPath ()
1233
+ composePath = appInstall .GetComposePath ()
1234
+ confDir = path .Join (appInstall .GetPath (), "conf" )
1235
+ backupConfDir = path .Join (appInstall .GetPath (), "conf_bak" )
1236
+ fpmConfDir = path .Join (confDir , "php-fpm.conf" )
1237
+ phpDir = path .Join (constant .RuntimeDir , runtime .Type , runtime .Name , "php" )
1238
+ oldFmContent , _ = fileOp .GetContent (fpmConfDir )
1239
+ )
1240
+ envParams := make (map [string ]string , len (envs ))
1241
+ handleMap (envs , envParams )
1242
+ envParams ["IMAGE_NAME" ] = runtime .Image
1243
+ defer func () {
1244
+ if busErr != nil {
1245
+ envParams ["IMAGE_NAME" ] = oldRuntime .Image
1246
+ _ = env .Write (envParams , envPath )
1247
+ _ = fileOp .WriteFile (composePath , strings .NewReader (appInstall .DockerCompose ), 0775 )
1248
+ if fileOp .Stat (backupConfDir ) {
1249
+ _ = fileOp .DeleteDir (confDir )
1250
+ _ = fileOp .Rename (backupConfDir , confDir )
1251
+ }
1252
+ }
1253
+ }()
1254
+
1255
+ if busErr = env .Write (envParams , envPath ); busErr != nil {
1256
+ return busErr
1257
+ }
1258
+ if busErr = fileOp .WriteFile (composePath , strings .NewReader (appDetail .DockerCompose ), 0775 ); busErr != nil {
1259
+ return busErr
1260
+ }
1261
+ if ! req .RetainConfig {
1262
+ if busErr = fileOp .Rename (confDir , backupConfDir ); busErr != nil {
1263
+ return busErr
1264
+ }
1265
+ _ = fileOp .CreateDir (confDir , 0755 )
1266
+ if busErr = fileOp .CopyFile (path .Join (phpDir , "php-fpm.conf" ), confDir ); busErr != nil {
1267
+ return busErr
1268
+ }
1269
+ if busErr = fileOp .CopyFile (path .Join (phpDir , "php.ini" ), confDir ); busErr != nil {
1270
+ _ = fileOp .WriteFile (fpmConfDir , bytes .NewReader (oldFmContent ), 0775 )
1271
+ return busErr
1272
+ }
1273
+ }
1274
+ if out , err := compose .Up (appInstall .GetComposePath ()); err != nil {
1275
+ if out != "" {
1276
+ busErr = errors .New (out )
1277
+ return busErr
1278
+ }
1279
+ busErr = err
1280
+ return busErr
1281
+ }
1282
+
1283
+ _ = fileOp .DeleteDir (backupConfDir )
1284
+
1285
+ appInstall .AppDetailId = runtime .AppDetailID
1286
+ appInstall .AppId = appDetail .AppId
1287
+ appInstall .Version = appDetail .Version
1288
+ appInstall .DockerCompose = appDetail .DockerCompose
1289
+
1290
+ _ = appInstallRepo .Save (context .Background (), & appInstall )
1291
+ website .RuntimeID = req .RuntimeID
1292
+ return websiteRepo .Save (context .Background (), & website )
1293
+ }
1294
+
1185
1295
func (w WebsiteService ) UpdateRewriteConfig (req request.NginxRewriteUpdate ) error {
1186
1296
website , err := websiteRepo .GetFirst (commonRepo .WithByID (req .WebsiteID ))
1187
1297
if err != nil {
0 commit comments