|
|
@ -9,7 +9,6 @@ import (
|
|
|
|
"encoding/pem"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/task"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"reflect"
|
|
|
@ -207,34 +206,13 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
|
|
|
|
if exist, _ := websiteRepo.GetBy(websiteRepo.WithAlias(alias)); len(exist) > 0 {
|
|
|
|
if exist, _ := websiteRepo.GetBy(websiteRepo.WithAlias(alias)); len(exist) > 0 {
|
|
|
|
return buserr.New(constant.ErrAliasIsExist)
|
|
|
|
return buserr.New(constant.ErrAliasIsExist)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defaultHttpPort := nginxInstall.HttpPort
|
|
|
|
defaultHttpPort := nginxInstall.HttpPort
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
existInstall model.AppInstall
|
|
|
|
|
|
|
|
runtime *model.Runtime
|
|
|
|
|
|
|
|
appInstall *model.AppInstall
|
|
|
|
|
|
|
|
proxy string
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch create.Type {
|
|
|
|
|
|
|
|
case constant.Deployment:
|
|
|
|
|
|
|
|
if create.AppType != constant.NewApp {
|
|
|
|
|
|
|
|
existInstall, err = appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case constant.Runtime:
|
|
|
|
|
|
|
|
runtime, err = runtimeRepo.GetFirst(commonRepo.WithByID(create.RuntimeID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
otherDomains []model.WebsiteDomain
|
|
|
|
otherDomains []model.WebsiteDomain
|
|
|
|
domains []model.WebsiteDomain
|
|
|
|
domains []model.WebsiteDomain
|
|
|
@ -266,35 +244,78 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
|
|
|
|
IPV6: create.IPV6,
|
|
|
|
IPV6: create.IPV6,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
websiteTask, err := task.NewTask("创建网站", "website")
|
|
|
|
var (
|
|
|
|
if err != nil {
|
|
|
|
appInstall *model.AppInstall
|
|
|
|
return err
|
|
|
|
runtime *model.Runtime
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
if website.AppInstallID > 0 {
|
|
|
|
|
|
|
|
req := request.AppInstalledOperate{
|
|
|
|
|
|
|
|
InstallId: website.AppInstallID,
|
|
|
|
|
|
|
|
Operate: constant.Delete,
|
|
|
|
|
|
|
|
ForceDelete: true,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := NewIAppInstalledService().Operate(req); err != nil {
|
|
|
|
|
|
|
|
global.LOG.Errorf(err.Error())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
var proxy string
|
|
|
|
|
|
|
|
|
|
|
|
switch create.Type {
|
|
|
|
switch create.Type {
|
|
|
|
case constant.Deployment:
|
|
|
|
case constant.Deployment:
|
|
|
|
if create.AppType == constant.NewApp {
|
|
|
|
if create.AppType == constant.NewApp {
|
|
|
|
deleteApp := func() {
|
|
|
|
var (
|
|
|
|
if website.AppInstallID > 0 {
|
|
|
|
req request.AppInstallCreate
|
|
|
|
req := request.AppInstalledOperate{
|
|
|
|
install *model.AppInstall
|
|
|
|
InstallId: website.AppInstallID,
|
|
|
|
)
|
|
|
|
Operate: constant.Delete,
|
|
|
|
req.Name = create.AppInstall.Name
|
|
|
|
ForceDelete: true,
|
|
|
|
req.AppDetailId = create.AppInstall.AppDetailId
|
|
|
|
}
|
|
|
|
req.Params = create.AppInstall.Params
|
|
|
|
if err := NewIAppInstalledService().Operate(req); err != nil {
|
|
|
|
req.AppContainerConfig = create.AppInstall.AppContainerConfig
|
|
|
|
global.LOG.Errorf(err.Error())
|
|
|
|
tx, installCtx := getTxAndContext()
|
|
|
|
}
|
|
|
|
install, err = NewIAppService().Install(installCtx, req)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
installApp := func() error {
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
appInstall = install
|
|
|
|
|
|
|
|
website.AppInstallID = install.ID
|
|
|
|
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
var install model.AppInstall
|
|
|
|
|
|
|
|
install, err = appInstallRepo.GetFirst(commonRepo.WithByID(create.AppInstallID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
appInstall = &install
|
|
|
|
|
|
|
|
website.AppInstallID = appInstall.ID
|
|
|
|
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case constant.Runtime:
|
|
|
|
|
|
|
|
runtime, err = runtimeRepo.GetFirst(commonRepo.WithByID(create.RuntimeID))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
website.RuntimeID = runtime.ID
|
|
|
|
|
|
|
|
switch runtime.Type {
|
|
|
|
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
|
|
|
|
if runtime.Resource == constant.ResourceAppstore {
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
req request.AppInstallCreate
|
|
|
|
req request.AppInstallCreate
|
|
|
|
install *model.AppInstall
|
|
|
|
install *model.AppInstall
|
|
|
|
)
|
|
|
|
)
|
|
|
|
req.Name = create.AppInstall.Name
|
|
|
|
reg, _ := regexp.Compile(`[^a-z0-9_-]+`)
|
|
|
|
|
|
|
|
req.Name = reg.ReplaceAllString(strings.ToLower(alias), "")
|
|
|
|
req.AppDetailId = create.AppInstall.AppDetailId
|
|
|
|
req.AppDetailId = create.AppInstall.AppDetailId
|
|
|
|
req.Params = create.AppInstall.Params
|
|
|
|
req.Params = create.AppInstall.Params
|
|
|
|
|
|
|
|
req.Params["IMAGE_NAME"] = runtime.Image
|
|
|
|
req.AppContainerConfig = create.AppInstall.AppContainerConfig
|
|
|
|
req.AppContainerConfig = create.AppInstall.AppContainerConfig
|
|
|
|
|
|
|
|
req.Params["PANEL_WEBSITE_DIR"] = path.Join(nginxInstall.GetPath(), "/www")
|
|
|
|
tx, installCtx := getTxAndContext()
|
|
|
|
tx, installCtx := getTxAndContext()
|
|
|
|
install, err = NewIAppService().Install(installCtx, req)
|
|
|
|
install, err = NewIAppService().Install(installCtx, req)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -302,47 +323,9 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
tx.Commit()
|
|
|
|
appInstall = install
|
|
|
|
|
|
|
|
website.AppInstallID = install.ID
|
|
|
|
website.AppInstallID = install.ID
|
|
|
|
|
|
|
|
appInstall = install
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
websiteTask.AddSubTask("安装应用", installApp, deleteApp)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
appInstall = &existInstall
|
|
|
|
|
|
|
|
website.AppInstallID = appInstall.ID
|
|
|
|
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case constant.Runtime:
|
|
|
|
|
|
|
|
website.RuntimeID = runtime.ID
|
|
|
|
|
|
|
|
switch runtime.Type {
|
|
|
|
|
|
|
|
case constant.RuntimePHP:
|
|
|
|
|
|
|
|
if runtime.Resource == constant.ResourceAppstore {
|
|
|
|
|
|
|
|
installApp := func() error {
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
|
|
|
req request.AppInstallCreate
|
|
|
|
|
|
|
|
install *model.AppInstall
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
reg, _ := regexp.Compile(`[^a-z0-9_-]+`)
|
|
|
|
|
|
|
|
req.Name = reg.ReplaceAllString(strings.ToLower(alias), "")
|
|
|
|
|
|
|
|
req.AppDetailId = create.AppInstall.AppDetailId
|
|
|
|
|
|
|
|
req.Params = create.AppInstall.Params
|
|
|
|
|
|
|
|
req.Params["IMAGE_NAME"] = runtime.Image
|
|
|
|
|
|
|
|
req.AppContainerConfig = create.AppInstall.AppContainerConfig
|
|
|
|
|
|
|
|
req.Params["PANEL_WEBSITE_DIR"] = path.Join(nginxInstall.GetPath(), "/www")
|
|
|
|
|
|
|
|
tx, installCtx := getTxAndContext()
|
|
|
|
|
|
|
|
install, err = NewIAppService().Install(installCtx, req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
website.AppInstallID = install.ID
|
|
|
|
|
|
|
|
appInstall = install
|
|
|
|
|
|
|
|
website.Proxy = fmt.Sprintf("127.0.0.1:%d", appInstall.HttpPort)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
websiteTask.AddSubTask("创建运行环境", installApp, nil)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
website.ProxyType = create.ProxyType
|
|
|
|
website.ProxyType = create.ProxyType
|
|
|
|
if website.ProxyType == constant.RuntimeProxyUnix {
|
|
|
|
if website.ProxyType == constant.RuntimeProxyUnix {
|
|
|
@ -358,43 +341,36 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
configNginx := func() error {
|
|
|
|
if err = configDefaultNginx(website, domains, appInstall, runtime); err != nil {
|
|
|
|
if err = configDefaultNginx(website, domains, appInstall, runtime); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = createWafConfig(website, domains); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if err = websiteRepo.Create(ctx, website); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range domains {
|
|
|
|
|
|
|
|
domains[i].WebsiteID = website.ID
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
websiteTask.AddSubTask("配置 OpenResty", configNginx, nil)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(create.FtpUser) != 0 && len(create.FtpPassword) != 0 {
|
|
|
|
if len(create.FtpUser) != 0 && len(create.FtpPassword) != 0 {
|
|
|
|
createFtp := func() error {
|
|
|
|
indexDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
|
|
|
|
indexDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
|
|
|
|
itemID, err := NewIFtpService().Create(dto.FtpCreate{User: create.FtpUser, Password: create.FtpPassword, Path: indexDir})
|
|
|
|
itemID, err := NewIFtpService().Create(dto.FtpCreate{User: create.FtpUser, Password: create.FtpPassword, Path: indexDir})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("create ftp for website failed, err: %v", err)
|
|
|
|
websiteTask.Logger.Printf("create ftp for website failed, err: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
website.FtpID = itemID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
websiteTask.AddSubTask("创建 FTP", createFtp, nil)
|
|
|
|
website.FtpID = itemID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return websiteTask.Execute()
|
|
|
|
if err = createWafConfig(website, domains); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if err = websiteRepo.Create(ctx, website); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range domains {
|
|
|
|
|
|
|
|
domains[i].WebsiteID = website.ID
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = websiteDomainRepo.BatchCreate(ctx, domains); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (w WebsiteService) OpWebsite(req request.WebsiteOp) error {
|
|
|
|
func (w WebsiteService) OpWebsite(req request.WebsiteOp) error {
|
|
|
|