2022-10-10 15:10:53 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-10-11 16:27:58 +08:00
|
|
|
"context"
|
2022-10-10 15:10:53 +08:00
|
|
|
"encoding/json"
|
2023-03-02 13:54:07 +08:00
|
|
|
"fmt"
|
2023-04-12 14:22:29 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
2023-04-06 18:32:16 +08:00
|
|
|
"github.com/compose-spec/compose-go/types"
|
2023-03-29 14:58:28 +08:00
|
|
|
"github.com/subosito/gotenv"
|
2022-10-18 18:39:45 +08:00
|
|
|
"math"
|
|
|
|
"os"
|
2023-04-03 17:47:23 +08:00
|
|
|
"os/exec"
|
2022-10-18 18:39:45 +08:00
|
|
|
"path"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-13 18:37:15 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/env"
|
|
|
|
|
2023-01-30 21:05:20 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
2023-03-28 10:52:17 +08:00
|
|
|
composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-10-10 22:56:42 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-10-10 15:10:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DatabaseOp string
|
|
|
|
|
|
|
|
var (
|
|
|
|
Add DatabaseOp = "add"
|
|
|
|
Delete DatabaseOp = "delete"
|
|
|
|
)
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func checkPort(key string, params map[string]interface{}) (int, error) {
|
|
|
|
port, ok := params[key]
|
|
|
|
if ok {
|
|
|
|
portN := int(math.Ceil(port.(float64)))
|
2023-02-07 16:29:54 +08:00
|
|
|
|
|
|
|
oldInstalled, _ := appInstallRepo.ListBy(appInstallRepo.WithPort(portN))
|
|
|
|
if len(oldInstalled) > 0 {
|
|
|
|
var apps []string
|
|
|
|
for _, install := range oldInstalled {
|
|
|
|
apps = append(apps, install.App.Name)
|
|
|
|
}
|
|
|
|
return portN, buserr.WithMap(constant.ErrPortInOtherApp, map[string]interface{}{"port": portN, "apps": apps}, nil)
|
|
|
|
}
|
2022-10-11 16:27:58 +08:00
|
|
|
if common.ScanPort(portN) {
|
2023-02-07 16:29:54 +08:00
|
|
|
return portN, buserr.WithDetail(constant.ErrPortInUsed, portN, nil)
|
2022-10-11 16:27:58 +08:00
|
|
|
} else {
|
|
|
|
return portN, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error {
|
|
|
|
var dbConfig dto.AppDatabase
|
|
|
|
if app.Type == "runtime" {
|
|
|
|
var authParam dto.AuthParam
|
|
|
|
paramByte, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(paramByte, &authParam); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-26 11:42:37 +08:00
|
|
|
if authParam.RootPassword != "" {
|
|
|
|
authByte, err := json.Marshal(authParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appInstall.Param = string(authByte)
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 16:21:17 +08:00
|
|
|
if app.Type == "website" || app.Type == "tool" {
|
2022-10-11 16:27:58 +08:00
|
|
|
paramByte, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(paramByte, &dbConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:36:32 +08:00
|
|
|
if !reflect.DeepEqual(dbConfig, dto.AppDatabase{}) && dbConfig.ServiceName != "" {
|
2022-10-11 16:27:58 +08:00
|
|
|
dbInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithServiceName(dbConfig.ServiceName))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-08 23:36:32 +08:00
|
|
|
var resourceId uint
|
|
|
|
if dbConfig.DbName != "" && dbConfig.DbUser != "" && dbConfig.Password != "" {
|
|
|
|
iMysqlRepo := repo.NewIMysqlRepo()
|
|
|
|
oldMysqlDb, _ := iMysqlRepo.Get(commonRepo.WithByName(dbConfig.DbName))
|
|
|
|
resourceId = oldMysqlDb.ID
|
|
|
|
if oldMysqlDb.ID > 0 {
|
|
|
|
if oldMysqlDb.Username != dbConfig.DbUser || oldMysqlDb.Password != dbConfig.Password {
|
|
|
|
return buserr.New(constant.ErrDbUserNotValid)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var createMysql dto.MysqlDBCreate
|
|
|
|
createMysql.Name = dbConfig.DbName
|
|
|
|
createMysql.Username = dbConfig.DbUser
|
|
|
|
createMysql.Format = "utf8mb4"
|
|
|
|
createMysql.Permission = "%"
|
|
|
|
createMysql.Password = dbConfig.Password
|
|
|
|
mysqldb, err := NewIMysqlService().Create(ctx, createMysql)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resourceId = mysqldb.ID
|
2023-03-06 17:24:00 +08:00
|
|
|
}
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
|
|
|
var installResource model.AppInstallResource
|
2023-03-06 17:24:00 +08:00
|
|
|
installResource.ResourceId = resourceId
|
2022-10-11 16:27:58 +08:00
|
|
|
installResource.AppInstallId = appInstall.ID
|
|
|
|
installResource.LinkId = dbInstall.ID
|
|
|
|
installResource.Key = dbInstall.App.Key
|
|
|
|
if err := appInstallResourceRepo.Create(ctx, &installResource); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
func handleAppInstallErr(ctx context.Context, install *model.AppInstall) error {
|
|
|
|
op := files.NewFileOp()
|
|
|
|
appDir := install.GetPath()
|
|
|
|
dir, _ := os.Stat(appDir)
|
|
|
|
if dir != nil {
|
|
|
|
_, _ = compose.Down(install.GetComposePath())
|
|
|
|
if err := op.DeleteDir(appDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := deleteLink(ctx, install, true, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:22:29 +08:00
|
|
|
func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete bool, deleteDB bool) error {
|
2022-10-17 16:32:31 +08:00
|
|
|
op := files.NewFileOp()
|
|
|
|
appDir := install.GetPath()
|
|
|
|
dir, _ := os.Stat(appDir)
|
|
|
|
if dir != nil {
|
|
|
|
out, err := compose.Down(install.GetComposePath())
|
2023-02-07 16:29:54 +08:00
|
|
|
if err != nil && !forceDelete {
|
2022-10-17 16:32:31 +08:00
|
|
|
return handleErr(install, err, out)
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 14:22:29 +08:00
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
defer tx.Rollback()
|
2023-03-10 16:22:48 +08:00
|
|
|
if err := appInstallRepo.Delete(ctx, install); err != nil {
|
2022-10-17 16:32:31 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-03-10 16:22:48 +08:00
|
|
|
if err := deleteLink(ctx, &install, deleteDB, forceDelete); err != nil && !forceDelete {
|
2022-10-17 16:32:31 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-04-12 14:22:29 +08:00
|
|
|
_ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("app"), commonRepo.WithByName(install.App.Key), backupRepo.WithByDetailName(install.Name))
|
|
|
|
_ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType(install.App.Key))
|
|
|
|
if install.App.Key == constant.AppMysql {
|
|
|
|
_ = mysqlRepo.DeleteAll(ctx)
|
|
|
|
}
|
2023-03-02 13:54:07 +08:00
|
|
|
uploadDir := fmt.Sprintf("%s/1panel/uploads/app/%s/%s", global.CONF.System.BaseDir, install.App.Key, install.Name)
|
|
|
|
if _, err := os.Stat(uploadDir); err == nil {
|
|
|
|
_ = os.RemoveAll(uploadDir)
|
|
|
|
}
|
2022-12-21 11:32:52 +08:00
|
|
|
if deleteBackup {
|
2023-04-12 14:22:29 +08:00
|
|
|
localDir, _ := loadLocalDir()
|
2023-03-02 13:54:07 +08:00
|
|
|
backupDir := fmt.Sprintf("%s/app/%s/%s", localDir, install.App.Key, install.Name)
|
|
|
|
if _, err := os.Stat(backupDir); err == nil {
|
|
|
|
_ = os.RemoveAll(backupDir)
|
|
|
|
}
|
|
|
|
global.LOG.Infof("delete app %s-%s backups successful", install.App.Key, install.Name)
|
2022-10-17 16:32:31 +08:00
|
|
|
}
|
2023-04-12 14:22:29 +08:00
|
|
|
_ = op.DeleteDir(appDir)
|
|
|
|
tx.Commit()
|
2022-10-17 16:32:31 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 16:22:48 +08:00
|
|
|
func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, forceDelete bool) error {
|
2022-10-11 16:27:58 +08:00
|
|
|
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
|
|
|
|
if len(resources) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, re := range resources {
|
2022-12-27 16:30:25 +08:00
|
|
|
mysqlService := NewIMysqlService()
|
2023-03-06 17:24:00 +08:00
|
|
|
if re.Key == "mysql" && deleteDB {
|
2022-11-18 17:50:52 +08:00
|
|
|
database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId))
|
|
|
|
if reflect.DeepEqual(database, model.DatabaseMysql{}) {
|
2022-10-11 16:27:58 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-12-27 16:30:25 +08:00
|
|
|
if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{
|
2023-03-10 16:22:48 +08:00
|
|
|
ID: database.ID,
|
|
|
|
ForceDelete: forceDelete,
|
|
|
|
}); err != nil && !forceDelete {
|
2022-10-11 16:27:58 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return appInstallResourceRepo.DeleteBy(ctx, appInstallResourceRepo.WithAppInstallId(install.ID))
|
|
|
|
}
|
|
|
|
|
2023-04-07 16:46:11 +08:00
|
|
|
func upgradeInstall(installId uint, detailId uint) error {
|
2022-10-13 18:56:53 +08:00
|
|
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
detail, err := appDetailRepo.GetFirst(commonRepo.WithByID(detailId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if install.Version == detail.Version {
|
2022-10-17 16:32:31 +08:00
|
|
|
return errors.New("two version is same")
|
2022-10-13 18:56:53 +08:00
|
|
|
}
|
2023-02-21 19:06:24 +08:00
|
|
|
if err := NewIBackupService().AppBackup(dto.CommonBackup{Name: install.App.Key, DetailName: install.Name}); err != nil {
|
2022-10-13 18:56:53 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-04-03 17:47:23 +08:00
|
|
|
|
|
|
|
detailDir := path.Join(constant.ResourceDir, "apps", install.App.Key, "versions", detail.Version)
|
2023-04-27 15:30:15 +08:00
|
|
|
if install.App.Resource == constant.AppResourceLocal {
|
|
|
|
detailDir = path.Join(constant.ResourceDir, "localApps", strings.TrimPrefix(install.App.Key, "local"), "versions", detail.Version)
|
|
|
|
}
|
|
|
|
|
2023-04-03 17:47:23 +08:00
|
|
|
cmd := exec.Command("/bin/bash", "-c", fmt.Sprintf("cp -rf %s/* %s", detailDir, install.GetPath()))
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
if stdout != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if out, err := compose.Down(install.GetComposePath()); err != nil {
|
|
|
|
if out != "" {
|
|
|
|
return errors.New(out)
|
|
|
|
}
|
2022-10-13 18:56:53 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
install.DockerCompose = detail.DockerCompose
|
|
|
|
install.Version = detail.Version
|
2023-03-09 13:37:39 +08:00
|
|
|
install.AppDetailId = detailId
|
2022-10-17 16:32:31 +08:00
|
|
|
|
2022-10-13 18:56:53 +08:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if err := fileOp.WriteFile(install.GetComposePath(), strings.NewReader(install.DockerCompose), 0775); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-03 17:47:23 +08:00
|
|
|
if out, err := compose.Up(install.GetComposePath()); err != nil {
|
|
|
|
if out != "" {
|
|
|
|
return errors.New(out)
|
|
|
|
}
|
2022-10-13 18:56:53 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-04-06 00:09:58 +08:00
|
|
|
return appInstallRepo.Save(context.Background(), &install)
|
2022-10-13 18:56:53 +08:00
|
|
|
}
|
|
|
|
|
2022-10-11 16:27:58 +08:00
|
|
|
func getContainerNames(install model.AppInstall) ([]string, error) {
|
2023-03-29 14:58:28 +08:00
|
|
|
envStr, err := coverEnvJsonToStr(install.Env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-09 22:32:12 +08:00
|
|
|
project, err := composeV2.GetComposeProject(install.Name, install.GetPath(), []byte(install.DockerCompose), []byte(envStr), true)
|
2022-10-11 16:27:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-07 16:46:11 +08:00
|
|
|
containerMap := make(map[string]struct{})
|
|
|
|
containerMap[install.ContainerName] = struct{}{}
|
2022-10-11 16:27:58 +08:00
|
|
|
for _, service := range project.AllServices() {
|
2023-02-23 10:43:45 +08:00
|
|
|
if service.ContainerName == "${CONTAINER_NAME}" || service.ContainerName == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-07 16:46:11 +08:00
|
|
|
containerMap[service.ContainerName] = struct{}{}
|
|
|
|
}
|
|
|
|
var containerNames []string
|
|
|
|
for k := range containerMap {
|
|
|
|
containerNames = append(containerNames, k)
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
|
|
|
return containerNames, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 14:58:28 +08:00
|
|
|
func coverEnvJsonToStr(envJson string) (string, error) {
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
_ = json.Unmarshal([]byte(envJson), &envMap)
|
|
|
|
newEnvMap := make(map[string]string, len(envMap))
|
|
|
|
handleMap(envMap, newEnvMap)
|
|
|
|
envStr, err := gotenv.Marshal(newEnvMap)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return envStr, nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:45:00 +08:00
|
|
|
func checkLimit(app model.App) error {
|
2022-10-10 22:56:42 +08:00
|
|
|
if app.Limit > 0 {
|
2023-02-07 16:29:54 +08:00
|
|
|
installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID))
|
2022-10-10 22:56:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(installs) >= app.Limit {
|
2022-12-04 12:47:19 +08:00
|
|
|
return buserr.New(constant.ErrAppLimit)
|
2022-10-10 22:56:42 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 16:45:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRequiredAndLimit(app model.App) error {
|
|
|
|
if err := checkLimit(app); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-10 22:56:42 +08:00
|
|
|
if app.Required != "" {
|
|
|
|
var requiredArray []string
|
|
|
|
if err := json.Unmarshal([]byte(app.Required), &requiredArray); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, key := range requiredArray {
|
|
|
|
if key == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
requireApp, err := appRepo.GetFirst(appRepo.WithKey(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
details, err := appDetailRepo.GetBy(appDetailRepo.WithAppId(requireApp.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var detailIds []uint
|
|
|
|
for _, d := range details {
|
|
|
|
detailIds = append(detailIds, d.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = appInstallRepo.GetFirst(appInstallRepo.WithDetailIdsIn(detailIds))
|
|
|
|
if err != nil {
|
2023-02-07 16:29:54 +08:00
|
|
|
return buserr.WithDetail(constant.ErrAppRequired, requireApp.Name, nil)
|
2022-10-10 22:56:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:46:38 +08:00
|
|
|
func handleMap(params map[string]interface{}, envParams map[string]string) {
|
|
|
|
for k, v := range params {
|
|
|
|
switch t := v.(type) {
|
|
|
|
case string:
|
|
|
|
envParams[k] = t
|
|
|
|
case float64:
|
|
|
|
envParams[k] = strconv.FormatFloat(t, 'f', -1, 32)
|
|
|
|
default:
|
|
|
|
envParams[k] = t.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 14:02:14 +08:00
|
|
|
func copyAppData(key, version, installName string, params map[string]interface{}, isLocal bool) (err error) {
|
2022-10-10 15:10:53 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2023-04-08 14:02:14 +08:00
|
|
|
appResourceDir := constant.AppResourceDir
|
2022-12-02 10:31:07 +08:00
|
|
|
installAppDir := path.Join(constant.AppInstallDir, key)
|
2023-04-08 14:02:14 +08:00
|
|
|
appKey := key
|
|
|
|
if isLocal {
|
|
|
|
appResourceDir = constant.LocalAppResourceDir
|
|
|
|
appKey = strings.TrimPrefix(key, "local")
|
|
|
|
installAppDir = path.Join(constant.LocalAppInstallDir, appKey)
|
|
|
|
}
|
|
|
|
resourceDir := path.Join(appResourceDir, appKey, "versions", version)
|
2022-12-04 12:47:19 +08:00
|
|
|
|
2022-12-02 10:31:07 +08:00
|
|
|
if !fileOp.Stat(installAppDir) {
|
|
|
|
if err = fileOp.CreateDir(installAppDir, 0755); err != nil {
|
2022-11-20 22:49:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 10:31:07 +08:00
|
|
|
appDir := path.Join(installAppDir, installName)
|
|
|
|
if fileOp.Stat(appDir) {
|
|
|
|
if err = fileOp.DeleteDir(appDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-10 15:10:53 +08:00
|
|
|
}
|
2022-12-04 12:47:19 +08:00
|
|
|
if err = fileOp.Copy(resourceDir, installAppDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
versionDir := path.Join(installAppDir, version)
|
|
|
|
if err = fileOp.Rename(versionDir, appDir); err != nil {
|
2022-10-10 15:10:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
envPath := path.Join(appDir, ".env")
|
|
|
|
|
|
|
|
envParams := make(map[string]string, len(params))
|
2022-10-13 16:46:38 +08:00
|
|
|
handleMap(params, envParams)
|
2023-03-09 14:24:59 +08:00
|
|
|
if err = env.Write(envParams, envPath); err != nil {
|
2022-10-10 15:10:53 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-31 12:16:14 +08:00
|
|
|
// 处理文件夹权限等问题
|
2023-04-11 14:38:27 +08:00
|
|
|
func upAppPre(app model.App, appInstall *model.AppInstall) error {
|
2023-03-31 12:16:14 +08:00
|
|
|
if app.Key == "nexus" {
|
|
|
|
dataPath := path.Join(appInstall.GetPath(), "data")
|
|
|
|
if err := files.NewFileOp().Chown(dataPath, 200, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
func getServiceFromInstall(appInstall *model.AppInstall) (service *composeV2.ComposeService, err error) {
|
2023-04-07 16:46:11 +08:00
|
|
|
var (
|
|
|
|
project *types.Project
|
|
|
|
envStr string
|
|
|
|
)
|
|
|
|
envStr, err = coverEnvJsonToStr(appInstall.Env)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-09 22:32:12 +08:00
|
|
|
project, err = composeV2.GetComposeProject(appInstall.Name, appInstall.GetPath(), []byte(appInstall.DockerCompose), []byte(envStr), true)
|
2023-04-07 16:46:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
service, err = composeV2.NewComposeService()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
service.SetProject(project)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
func upApp(appInstall *model.AppInstall) {
|
|
|
|
upProject := func(appInstall *model.AppInstall) (err error) {
|
2023-04-06 18:32:16 +08:00
|
|
|
if err == nil {
|
2023-04-07 16:46:11 +08:00
|
|
|
var composeService *composeV2.ComposeService
|
|
|
|
composeService, err = getServiceFromInstall(appInstall)
|
2023-04-10 17:30:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-06 18:32:16 +08:00
|
|
|
err = composeService.ComposeUp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return
|
2022-10-10 15:10:53 +08:00
|
|
|
} else {
|
2023-04-06 18:32:16 +08:00
|
|
|
return
|
2022-10-10 15:10:53 +08:00
|
|
|
}
|
2023-04-06 18:32:16 +08:00
|
|
|
}
|
|
|
|
if err := upProject(appInstall); err != nil {
|
2022-10-10 15:10:53 +08:00
|
|
|
appInstall.Status = constant.Error
|
2023-04-06 18:32:16 +08:00
|
|
|
appInstall.Message = err.Error()
|
2022-10-10 15:10:53 +08:00
|
|
|
} else {
|
|
|
|
appInstall.Status = constant.Running
|
2023-04-06 16:38:16 +08:00
|
|
|
}
|
|
|
|
exist, _ := appInstallRepo.GetFirst(commonRepo.WithByID(appInstall.ID))
|
|
|
|
if exist.ID > 0 {
|
2023-04-11 14:38:27 +08:00
|
|
|
_ = appInstallRepo.Save(context.Background(), appInstall)
|
2022-10-10 15:10:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:04:22 +08:00
|
|
|
func rebuildApp(appInstall model.AppInstall) error {
|
|
|
|
dockerComposePath := appInstall.GetComposePath()
|
|
|
|
out, err := compose.Down(dockerComposePath)
|
|
|
|
if err != nil {
|
|
|
|
return handleErr(appInstall, err, out)
|
|
|
|
}
|
|
|
|
out, err = compose.Up(dockerComposePath)
|
|
|
|
if err != nil {
|
|
|
|
return handleErr(appInstall, err, out)
|
|
|
|
}
|
|
|
|
return syncById(appInstall.ID)
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:10:53 +08:00
|
|
|
func getAppDetails(details []model.AppDetail, versions []string) map[string]model.AppDetail {
|
|
|
|
appDetails := make(map[string]model.AppDetail, len(details))
|
|
|
|
for _, old := range details {
|
|
|
|
old.Status = constant.AppTakeDown
|
|
|
|
appDetails[old.Version] = old
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range versions {
|
|
|
|
detail, ok := appDetails[v]
|
|
|
|
if ok {
|
|
|
|
detail.Status = constant.AppNormal
|
|
|
|
appDetails[v] = detail
|
|
|
|
} else {
|
|
|
|
appDetails[v] = model.AppDetail{
|
|
|
|
Version: v,
|
|
|
|
Status: constant.AppNormal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return appDetails
|
|
|
|
}
|
|
|
|
|
2023-04-08 14:02:14 +08:00
|
|
|
func getApps(oldApps []model.App, items []dto.AppDefine, isLocal bool) map[string]model.App {
|
2022-10-10 15:10:53 +08:00
|
|
|
apps := make(map[string]model.App, len(oldApps))
|
|
|
|
for _, old := range oldApps {
|
|
|
|
old.Status = constant.AppTakeDown
|
|
|
|
apps[old.Key] = old
|
|
|
|
}
|
|
|
|
for _, item := range items {
|
2023-04-08 14:02:14 +08:00
|
|
|
key := item.Key
|
|
|
|
if isLocal {
|
|
|
|
key = "local" + key
|
|
|
|
}
|
|
|
|
app, ok := apps[key]
|
2022-10-10 15:10:53 +08:00
|
|
|
if !ok {
|
|
|
|
app = model.App{}
|
|
|
|
}
|
2023-04-08 14:02:14 +08:00
|
|
|
if isLocal {
|
|
|
|
app.Resource = constant.AppResourceLocal
|
|
|
|
} else {
|
|
|
|
app.Resource = constant.AppResourceRemote
|
|
|
|
}
|
2022-10-10 15:10:53 +08:00
|
|
|
app.Name = item.Name
|
2022-11-22 14:32:45 +08:00
|
|
|
app.Limit = item.Limit
|
2023-04-08 14:02:14 +08:00
|
|
|
app.Key = key
|
2023-02-17 16:21:13 +08:00
|
|
|
app.ShortDescZh = item.ShortDescZh
|
|
|
|
app.ShortDescEn = item.ShortDescEn
|
2023-01-30 17:24:46 +08:00
|
|
|
app.Website = item.Website
|
|
|
|
app.Document = item.Document
|
|
|
|
app.Github = item.Github
|
2022-10-10 15:10:53 +08:00
|
|
|
app.Type = item.Type
|
|
|
|
app.CrossVersionUpdate = item.CrossVersionUpdate
|
|
|
|
app.Required = item.GetRequired()
|
|
|
|
app.Status = constant.AppNormal
|
2023-04-08 14:02:14 +08:00
|
|
|
apps[key] = app
|
2022-10-10 15:10:53 +08:00
|
|
|
}
|
|
|
|
return apps
|
|
|
|
}
|
2022-10-11 16:27:58 +08:00
|
|
|
|
|
|
|
func handleErr(install model.AppInstall, err error, out string) error {
|
|
|
|
reErr := err
|
|
|
|
install.Message = err.Error()
|
|
|
|
if out != "" {
|
|
|
|
install.Message = out
|
|
|
|
reErr = errors.New(out)
|
2023-03-07 15:43:12 +08:00
|
|
|
install.Status = constant.Error
|
2022-10-11 16:27:58 +08:00
|
|
|
}
|
2023-04-06 00:09:58 +08:00
|
|
|
_ = appInstallRepo.Save(context.Background(), &install)
|
2022-10-11 16:27:58 +08:00
|
|
|
return reErr
|
|
|
|
}
|
2022-10-14 14:48:55 +08:00
|
|
|
|
2023-02-14 11:17:02 +08:00
|
|
|
func getAppFromRepo(downloadPath, version string) error {
|
2023-02-23 09:31:00 +08:00
|
|
|
downloadUrl := downloadPath
|
2023-02-10 18:07:29 +08:00
|
|
|
appDir := constant.AppResourceDir
|
2023-02-14 11:17:02 +08:00
|
|
|
|
|
|
|
global.LOG.Infof("download file from %s", downloadUrl)
|
2022-10-14 14:48:55 +08:00
|
|
|
fileOp := files.NewFileOp()
|
2022-12-12 12:01:33 +08:00
|
|
|
if _, err := fileOp.CopyAndBackup(appDir); err != nil {
|
2022-10-14 14:48:55 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-02-10 18:07:29 +08:00
|
|
|
packagePath := path.Join(constant.ResourceDir, path.Base(downloadUrl))
|
|
|
|
if err := fileOp.DownloadFile(downloadUrl, packagePath); err != nil {
|
2022-10-14 14:48:55 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-02-10 18:07:29 +08:00
|
|
|
if err := fileOp.Decompress(packagePath, constant.ResourceDir, files.TarGz); err != nil {
|
2022-10-14 14:48:55 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-02-14 11:17:02 +08:00
|
|
|
_ = NewISettingService().Update("AppStoreVersion", version)
|
2022-10-14 14:48:55 +08:00
|
|
|
defer func() {
|
|
|
|
_ = fileOp.DeleteFile(packagePath)
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-22 14:22:25 +08:00
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
func handleInstalled(appInstallList []model.AppInstall, updated bool) ([]response.AppInstalledDTO, error) {
|
2022-12-14 15:08:21 +08:00
|
|
|
var res []response.AppInstalledDTO
|
2023-01-16 15:30:24 +08:00
|
|
|
for _, installed := range appInstallList {
|
2023-05-10 14:32:23 +08:00
|
|
|
if installed.App.Type == "php" {
|
|
|
|
continue
|
|
|
|
}
|
2022-12-14 15:08:21 +08:00
|
|
|
installDTO := response.AppInstalledDTO{
|
2022-11-22 14:22:25 +08:00
|
|
|
AppInstall: installed,
|
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(installed.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
details, err := appDetailRepo.GetBy(appDetailRepo.WithAppId(app.ID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var versions []string
|
|
|
|
for _, detail := range details {
|
|
|
|
versions = append(versions, detail.Version)
|
|
|
|
}
|
|
|
|
versions = common.GetSortedVersions(versions)
|
|
|
|
lastVersion := versions[0]
|
|
|
|
if common.IsCrossVersion(installed.Version, lastVersion) {
|
|
|
|
installDTO.CanUpdate = app.CrossVersionUpdate
|
|
|
|
} else {
|
|
|
|
installDTO.CanUpdate = common.CompareVersion(lastVersion, installed.Version)
|
|
|
|
}
|
2023-01-16 15:30:24 +08:00
|
|
|
if updated {
|
|
|
|
if installDTO.CanUpdate {
|
|
|
|
res = append(res, installDTO)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = append(res, installDTO)
|
|
|
|
}
|
2022-11-22 14:22:25 +08:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-11-24 10:28:39 +08:00
|
|
|
|
|
|
|
func getAppInstallByKey(key string) (model.AppInstall, error) {
|
|
|
|
app, err := appRepo.GetFirst(appRepo.WithKey(key))
|
|
|
|
if err != nil {
|
|
|
|
return model.AppInstall{}, err
|
|
|
|
}
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(app.ID))
|
|
|
|
if err != nil {
|
|
|
|
return model.AppInstall{}, err
|
|
|
|
}
|
|
|
|
return appInstall, nil
|
|
|
|
}
|
2022-12-09 16:03:00 +08:00
|
|
|
|
2023-04-11 14:38:27 +08:00
|
|
|
func updateToolApp(installed *model.AppInstall) {
|
2022-12-09 16:03:00 +08:00
|
|
|
tooKey, ok := dto.AppToolMap[installed.App.Key]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toolInstall, _ := getAppInstallByKey(tooKey)
|
|
|
|
if reflect.DeepEqual(toolInstall, model.AppInstall{}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
paramMap := make(map[string]string)
|
|
|
|
_ = json.Unmarshal([]byte(installed.Param), ¶mMap)
|
|
|
|
envMap := make(map[string]interface{})
|
|
|
|
_ = json.Unmarshal([]byte(toolInstall.Env), &envMap)
|
|
|
|
if password, ok := paramMap["PANEL_DB_ROOT_PASSWORD"]; ok {
|
|
|
|
envMap["PANEL_DB_ROOT_PASSWORD"] = password
|
|
|
|
}
|
|
|
|
if _, ok := envMap["PANEL_REDIS_HOST"]; ok {
|
|
|
|
envMap["PANEL_REDIS_HOST"] = installed.ServiceName
|
|
|
|
}
|
|
|
|
if _, ok := envMap["PANEL_DB_HOST"]; ok {
|
|
|
|
envMap["PANEL_DB_HOST"] = installed.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
envPath := path.Join(toolInstall.GetPath(), ".env")
|
|
|
|
contentByte, err := json.Marshal(envMap)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
envFileMap := make(map[string]string)
|
|
|
|
handleMap(envMap, envFileMap)
|
2023-03-09 14:24:59 +08:00
|
|
|
if err = env.Write(envFileMap, envPath); err != nil {
|
2022-12-09 16:03:00 +08:00
|
|
|
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toolInstall.Env = string(contentByte)
|
2023-04-06 00:09:58 +08:00
|
|
|
if err := appInstallRepo.Save(context.Background(), &toolInstall); err != nil {
|
2022-12-09 16:03:00 +08:00
|
|
|
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if out, err := compose.Down(toolInstall.GetComposePath()); err != nil {
|
|
|
|
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if out, err := compose.Up(toolInstall.GetComposePath()); err != nil {
|
|
|
|
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|