2022-10-10 07:10:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-10-11 08:27:58 +00:00
|
|
|
"context"
|
2023-05-31 06:51:01 +00:00
|
|
|
"encoding/base64"
|
2022-10-10 07:10:53 +00:00
|
|
|
"encoding/json"
|
2023-03-02 05:54:07 +00:00
|
|
|
"fmt"
|
2022-10-18 10:39:45 +00:00
|
|
|
"math"
|
2023-05-18 08:48:19 +00:00
|
|
|
"net/http"
|
2022-10-18 10:39:45 +00:00
|
|
|
"os"
|
2023-04-03 09:47:23 +00:00
|
|
|
"os/exec"
|
2022-10-18 10:39:45 +00:00
|
|
|
"path"
|
|
|
|
"reflect"
|
2023-05-18 08:48:19 +00:00
|
|
|
"regexp"
|
2022-10-18 10:39:45 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2024-05-29 03:05:09 +00:00
|
|
|
httpUtil "github.com/1Panel-dev/1Panel/backend/utils/http"
|
2024-04-25 08:45:12 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/xpack"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
|
2023-09-12 09:48:11 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
|
2023-07-27 07:52:25 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/i18n"
|
|
|
|
"github.com/subosito/gotenv"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
2023-03-13 10:37:15 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/env"
|
|
|
|
|
2023-01-30 13:05:20 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
|
2022-10-17 08:32:31 +00: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 02:52:17 +00:00
|
|
|
composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-10-10 14:56:42 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-10-10 07:10:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DatabaseOp string
|
|
|
|
|
|
|
|
var (
|
|
|
|
Add DatabaseOp = "add"
|
|
|
|
Delete DatabaseOp = "delete"
|
|
|
|
)
|
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
func checkPort(key string, params map[string]interface{}) (int, error) {
|
|
|
|
port, ok := params[key]
|
|
|
|
if ok {
|
2023-07-10 06:47:08 +00:00
|
|
|
portN := 0
|
|
|
|
var err error
|
|
|
|
switch p := port.(type) {
|
|
|
|
case string:
|
|
|
|
portN, err = strconv.Atoi(p)
|
|
|
|
if err != nil {
|
|
|
|
return portN, nil
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
portN = int(math.Ceil(p))
|
|
|
|
case int:
|
|
|
|
portN = p
|
|
|
|
}
|
|
|
|
|
2023-02-07 08:29:54 +00: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 08:27:58 +00:00
|
|
|
if common.ScanPort(portN) {
|
2023-02-07 08:29:54 +00:00
|
|
|
return portN, buserr.WithDetail(constant.ErrPortInUsed, portN, nil)
|
2022-10-11 08:27:58 +00:00
|
|
|
} else {
|
|
|
|
return portN, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2023-09-28 07:28:21 +00:00
|
|
|
func checkPortExist(port int) error {
|
|
|
|
errMap := make(map[string]interface{})
|
|
|
|
errMap["port"] = port
|
|
|
|
appInstall, _ := appInstallRepo.GetFirst(appInstallRepo.WithPort(port))
|
|
|
|
if appInstall.ID > 0 {
|
|
|
|
errMap["type"] = i18n.GetMsgByKey("TYPE_APP")
|
|
|
|
errMap["name"] = appInstall.Name
|
|
|
|
return buserr.WithMap("ErrPortExist", errMap, nil)
|
|
|
|
}
|
|
|
|
runtime, _ := runtimeRepo.GetFirst(runtimeRepo.WithPort(port))
|
|
|
|
if runtime != nil {
|
|
|
|
errMap["type"] = i18n.GetMsgByKey("TYPE_RUNTIME")
|
|
|
|
errMap["name"] = runtime.Name
|
|
|
|
return buserr.WithMap("ErrPortExist", errMap, nil)
|
|
|
|
}
|
|
|
|
domain, _ := websiteDomainRepo.GetFirst(websiteDomainRepo.WithPort(port))
|
|
|
|
if domain.ID > 0 {
|
|
|
|
errMap["type"] = i18n.GetMsgByKey("TYPE_DOMAIN")
|
|
|
|
errMap["name"] = domain.Domain
|
|
|
|
return buserr.WithMap("ErrPortExist", errMap, nil)
|
|
|
|
}
|
|
|
|
if common.ScanPort(port) {
|
|
|
|
return buserr.WithDetail(constant.ErrPortInUsed, port, nil)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-12 09:12:11 +00:00
|
|
|
var DatabaseKeys = map[string]uint{
|
2024-01-03 05:26:29 +00:00
|
|
|
constant.AppMysql: 3306,
|
|
|
|
constant.AppMariaDB: 3306,
|
|
|
|
constant.AppPostgresql: 5432,
|
|
|
|
constant.AppPostgres: 5432,
|
|
|
|
constant.AppMongodb: 27017,
|
|
|
|
constant.AppRedis: 6379,
|
|
|
|
constant.AppMemcached: 11211,
|
2023-08-31 07:14:11 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 08:02:08 +00:00
|
|
|
var ToolKeys = map[string]uint{
|
|
|
|
"minio": 9001,
|
|
|
|
}
|
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall, params map[string]interface{}) error {
|
|
|
|
var dbConfig dto.AppDatabase
|
2023-12-08 08:02:08 +00:00
|
|
|
if DatabaseKeys[app.Key] > 0 {
|
2023-08-31 07:14:11 +00:00
|
|
|
database := &model.Database{
|
|
|
|
AppInstallID: appInstall.ID,
|
|
|
|
Name: appInstall.Name,
|
|
|
|
Type: app.Key,
|
|
|
|
Version: appInstall.Version,
|
|
|
|
From: "local",
|
|
|
|
Address: appInstall.ServiceName,
|
2023-09-12 09:12:11 +00:00
|
|
|
Port: DatabaseKeys[app.Key],
|
2023-08-31 07:14:11 +00:00
|
|
|
}
|
|
|
|
detail, err := appDetailRepo.GetFirst(commonRepo.WithByID(appInstall.AppDetailId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
formFields := &dto.AppForm{}
|
|
|
|
if err := json.Unmarshal([]byte(detail.Params), formFields); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, form := range formFields.FormFields {
|
|
|
|
if form.EnvKey == "PANEL_APP_PORT_HTTP" {
|
|
|
|
portFloat, ok := form.Default.(float64)
|
|
|
|
if ok {
|
|
|
|
database.Port = uint(int(portFloat))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 10:36:01 +00:00
|
|
|
switch app.Key {
|
2024-01-03 05:26:29 +00:00
|
|
|
case constant.AppMysql, constant.AppMariaDB, constant.AppPostgresql, constant.AppMongodb:
|
2023-08-03 10:36:01 +00:00
|
|
|
if password, ok := params["PANEL_DB_ROOT_PASSWORD"]; ok {
|
|
|
|
if password != "" {
|
2023-12-08 08:02:08 +00:00
|
|
|
database.Password = password.(string)
|
|
|
|
if app.Key == "mysql" || app.Key == "mariadb" {
|
|
|
|
database.Username = "root"
|
|
|
|
}
|
|
|
|
if rootUser, ok := params["PANEL_DB_ROOT_USER"]; ok {
|
|
|
|
database.Username = rootUser.(string)
|
|
|
|
}
|
2023-08-03 10:36:01 +00:00
|
|
|
authParam := dto.AuthParam{
|
|
|
|
RootPassword: password.(string),
|
2023-12-08 08:02:08 +00:00
|
|
|
RootUser: database.Username,
|
2023-08-03 10:36:01 +00:00
|
|
|
}
|
|
|
|
authByte, err := json.Marshal(authParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appInstall.Param = string(authByte)
|
2023-12-08 08:02:08 +00:00
|
|
|
|
2023-08-03 10:36:01 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-03 05:26:29 +00:00
|
|
|
case constant.AppRedis:
|
2023-08-03 10:36:01 +00:00
|
|
|
if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok {
|
|
|
|
if password != "" {
|
|
|
|
authParam := dto.RedisAuthParam{
|
|
|
|
RootPassword: password.(string),
|
|
|
|
}
|
|
|
|
authByte, err := json.Marshal(authParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appInstall.Param = string(authByte)
|
|
|
|
}
|
2023-08-31 07:14:11 +00:00
|
|
|
database.Password = password.(string)
|
2022-12-26 03:42:37 +00:00
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
2023-08-31 07:14:11 +00:00
|
|
|
if err := databaseRepo.Create(ctx, database); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
2023-12-08 08:02:08 +00:00
|
|
|
if ToolKeys[app.Key] > 0 {
|
|
|
|
if app.Key == "minio" {
|
|
|
|
authParam := dto.MinioAuthParam{}
|
|
|
|
if password, ok := params["PANEL_MINIO_ROOT_PASSWORD"]; ok {
|
|
|
|
authParam.RootPassword = password.(string)
|
|
|
|
}
|
|
|
|
if rootUser, ok := params["PANEL_MINIO_ROOT_USER"]; ok {
|
|
|
|
authParam.RootUser = rootUser.(string)
|
|
|
|
}
|
|
|
|
authByte, err := json.Marshal(authParam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appInstall.Param = string(authByte)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 08:21:17 +00:00
|
|
|
if app.Type == "website" || app.Type == "tool" {
|
2022-10-11 08:27:58 +00:00
|
|
|
paramByte, err := json.Marshal(params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-03 10:36:01 +00:00
|
|
|
if err = json.Unmarshal(paramByte, &dbConfig); err != nil {
|
2022-10-11 08:27:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 15:36:32 +00:00
|
|
|
if !reflect.DeepEqual(dbConfig, dto.AppDatabase{}) && dbConfig.ServiceName != "" {
|
2023-10-07 08:02:44 +00:00
|
|
|
hostName := params["PANEL_DB_HOST_NAME"]
|
|
|
|
if hostName == nil || hostName.(string) == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
database, _ := databaseRepo.Get(commonRepo.WithByName(hostName.(string)))
|
|
|
|
if database.ID == 0 {
|
|
|
|
return nil
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
2023-03-08 15:36:32 +00:00
|
|
|
var resourceId uint
|
|
|
|
if dbConfig.DbName != "" && dbConfig.DbUser != "" && dbConfig.Password != "" {
|
2023-12-28 08:29:18 +00:00
|
|
|
switch database.Type {
|
2024-01-03 05:26:29 +00:00
|
|
|
case constant.AppPostgresql, constant.AppPostgres:
|
2023-12-28 08:29:18 +00:00
|
|
|
iPostgresqlRepo := repo.NewIPostgresqlRepo()
|
|
|
|
oldPostgresqlDb, _ := iPostgresqlRepo.Get(commonRepo.WithByName(dbConfig.DbName), iPostgresqlRepo.WithByFrom(constant.ResourceLocal))
|
|
|
|
resourceId = oldPostgresqlDb.ID
|
|
|
|
if oldPostgresqlDb.ID > 0 {
|
|
|
|
if oldPostgresqlDb.Username != dbConfig.DbUser || oldPostgresqlDb.Password != dbConfig.Password {
|
|
|
|
return buserr.New(constant.ErrDbUserNotValid)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var createPostgresql dto.PostgresqlDBCreate
|
|
|
|
createPostgresql.Name = dbConfig.DbName
|
|
|
|
createPostgresql.Username = dbConfig.DbUser
|
|
|
|
createPostgresql.Database = database.Name
|
|
|
|
createPostgresql.Format = "UTF8"
|
|
|
|
createPostgresql.Password = dbConfig.Password
|
|
|
|
createPostgresql.From = database.From
|
2024-01-09 08:27:33 +00:00
|
|
|
createPostgresql.SuperUser = true
|
2023-12-28 08:29:18 +00:00
|
|
|
pgdb, err := NewIPostgresqlService().Create(ctx, createPostgresql)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resourceId = pgdb.ID
|
2023-03-08 15:36:32 +00:00
|
|
|
}
|
2024-01-03 05:26:29 +00:00
|
|
|
case constant.AppMysql, constant.AppMariaDB:
|
2023-12-28 08:29:18 +00:00
|
|
|
iMysqlRepo := repo.NewIMysqlRepo()
|
|
|
|
oldMysqlDb, _ := iMysqlRepo.Get(commonRepo.WithByName(dbConfig.DbName), iMysqlRepo.WithByFrom(constant.ResourceLocal))
|
|
|
|
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.Database = database.Name
|
|
|
|
createMysql.Format = "utf8mb4"
|
|
|
|
createMysql.Permission = "%"
|
|
|
|
createMysql.Password = dbConfig.Password
|
|
|
|
createMysql.From = database.From
|
|
|
|
mysqldb, err := NewIMysqlService().Create(ctx, createMysql)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resourceId = mysqldb.ID
|
2023-03-08 15:36:32 +00:00
|
|
|
}
|
2023-03-06 09:24:00 +00:00
|
|
|
}
|
2023-12-28 08:29:18 +00:00
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
|
|
|
var installResource model.AppInstallResource
|
2023-03-06 09:24:00 +00:00
|
|
|
installResource.ResourceId = resourceId
|
2022-10-11 08:27:58 +00:00
|
|
|
installResource.AppInstallId = appInstall.ID
|
2023-10-07 08:02:44 +00:00
|
|
|
if database.AppInstallID > 0 {
|
|
|
|
installResource.LinkId = database.AppInstallID
|
|
|
|
} else {
|
|
|
|
installResource.LinkId = database.ID
|
|
|
|
}
|
|
|
|
installResource.Key = database.Type
|
|
|
|
installResource.From = database.From
|
2022-10-11 08:27:58 +00:00
|
|
|
if err := appInstallResourceRepo.Create(ctx, &installResource); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 06:38:27 +00: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
|
|
|
|
}
|
|
|
|
}
|
2023-09-13 09:58:14 +00:00
|
|
|
if err := deleteLink(ctx, install, true, true, true); err != nil {
|
2023-04-11 06:38:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 06:22:29 +00:00
|
|
|
func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete bool, deleteDB bool) error {
|
2022-10-17 08:32:31 +00:00
|
|
|
op := files.NewFileOp()
|
|
|
|
appDir := install.GetPath()
|
|
|
|
dir, _ := os.Stat(appDir)
|
|
|
|
if dir != nil {
|
|
|
|
out, err := compose.Down(install.GetComposePath())
|
2023-02-07 08:29:54 +00:00
|
|
|
if err != nil && !forceDelete {
|
2022-10-17 08:32:31 +00:00
|
|
|
return handleErr(install, err, out)
|
|
|
|
}
|
2023-08-23 14:48:14 +00:00
|
|
|
if err = runScript(&install, "uninstall"); err != nil {
|
|
|
|
_, _ = compose.Up(install.GetComposePath())
|
|
|
|
return err
|
|
|
|
}
|
2022-10-17 08:32:31 +00:00
|
|
|
}
|
2023-04-12 06:22:29 +00:00
|
|
|
tx, ctx := helper.GetTxAndContext()
|
|
|
|
defer tx.Rollback()
|
2023-03-10 08:22:48 +00:00
|
|
|
if err := appInstallRepo.Delete(ctx, install); err != nil {
|
2022-10-17 08:32:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-13 09:58:14 +00:00
|
|
|
if err := deleteLink(ctx, &install, deleteDB, forceDelete, deleteBackup); err != nil && !forceDelete {
|
2022-10-17 08:32:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-09-16 05:16:15 +00:00
|
|
|
|
2023-09-15 08:46:13 +00:00
|
|
|
if DatabaseKeys[install.App.Key] > 0 {
|
|
|
|
_ = databaseRepo.Delete(ctx, databaseRepo.WithAppInstallID(install.ID))
|
|
|
|
}
|
2023-09-13 10:18:14 +00:00
|
|
|
|
|
|
|
switch install.App.Key {
|
|
|
|
case constant.AppOpenresty:
|
2023-10-10 11:26:32 +00:00
|
|
|
websites, _ := websiteRepo.List()
|
|
|
|
for _, website := range websites {
|
|
|
|
if website.AppInstallID > 0 {
|
|
|
|
websiteAppInstall, _ := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if websiteAppInstall.AppId > 0 {
|
|
|
|
websiteApp, _ := appRepo.GetFirst(commonRepo.WithByID(websiteAppInstall.AppId))
|
|
|
|
if websiteApp.Type == constant.RuntimePHP {
|
|
|
|
go func() {
|
2024-02-26 10:04:12 +00:00
|
|
|
_, _ = compose.Down(websiteAppInstall.GetComposePath())
|
|
|
|
_ = op.DeleteDir(websiteAppInstall.GetPath())
|
2023-10-10 11:26:32 +00:00
|
|
|
}()
|
|
|
|
_ = appInstallRepo.Delete(ctx, websiteAppInstall)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 02:42:13 +00:00
|
|
|
_ = websiteRepo.DeleteAll(ctx)
|
|
|
|
_ = websiteDomainRepo.DeleteAll(ctx)
|
2024-04-07 09:25:46 +00:00
|
|
|
xpack.RemoveTamper("")
|
2023-09-13 10:18:14 +00:00
|
|
|
case constant.AppMysql, constant.AppMariaDB:
|
2023-09-16 05:16:15 +00:00
|
|
|
_ = mysqlRepo.Delete(ctx, mysqlRepo.WithByMysqlName(install.Name))
|
2024-01-09 08:27:33 +00:00
|
|
|
case constant.AppPostgresql:
|
|
|
|
_ = postgresqlRepo.Delete(ctx, postgresqlRepo.WithByPostgresqlName(install.Name))
|
2023-04-12 06:22:29 +00:00
|
|
|
}
|
2023-09-13 10:18:14 +00:00
|
|
|
|
|
|
|
_ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("app"), commonRepo.WithByName(install.App.Key), backupRepo.WithByDetailName(install.Name))
|
2023-07-27 07:52:25 +00:00
|
|
|
uploadDir := path.Join(global.CONF.System.BaseDir, fmt.Sprintf("1panel/uploads/app/%s/%s", install.App.Key, install.Name))
|
2023-03-02 05:54:07 +00:00
|
|
|
if _, err := os.Stat(uploadDir); err == nil {
|
|
|
|
_ = os.RemoveAll(uploadDir)
|
|
|
|
}
|
2022-12-21 03:32:52 +00:00
|
|
|
if deleteBackup {
|
2023-04-12 06:22:29 +00:00
|
|
|
localDir, _ := loadLocalDir()
|
2023-07-27 07:52:25 +00:00
|
|
|
backupDir := path.Join(localDir, fmt.Sprintf("app/%s/%s", install.App.Key, install.Name))
|
2023-03-02 05:54:07 +00:00
|
|
|
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 08:32:31 +00:00
|
|
|
}
|
2023-04-12 06:22:29 +00:00
|
|
|
_ = op.DeleteDir(appDir)
|
|
|
|
tx.Commit()
|
2022-10-17 08:32:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 09:58:14 +00:00
|
|
|
func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, forceDelete bool, deleteBackup bool) error {
|
2022-10-11 08:27:58 +00:00
|
|
|
resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID))
|
|
|
|
if len(resources) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, re := range resources {
|
2023-12-28 08:29:18 +00:00
|
|
|
if deleteDB {
|
|
|
|
switch re.Key {
|
|
|
|
case constant.AppMysql, constant.AppMariaDB:
|
|
|
|
mysqlService := NewIMysqlService()
|
|
|
|
database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId))
|
|
|
|
if reflect.DeepEqual(database, model.DatabaseMysql{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{
|
|
|
|
ID: database.ID,
|
|
|
|
ForceDelete: forceDelete,
|
|
|
|
DeleteBackup: deleteBackup,
|
|
|
|
Type: re.Key,
|
|
|
|
Database: database.MysqlName,
|
|
|
|
}); err != nil && !forceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case constant.AppPostgresql:
|
|
|
|
pgsqlService := NewIPostgresqlService()
|
|
|
|
database, _ := postgresqlRepo.Get(commonRepo.WithByID(re.ResourceId))
|
|
|
|
if reflect.DeepEqual(database, model.DatabasePostgresql{}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := pgsqlService.Delete(ctx, dto.PostgresqlDBDelete{
|
|
|
|
ID: database.ID,
|
|
|
|
ForceDelete: forceDelete,
|
|
|
|
DeleteBackup: deleteBackup,
|
|
|
|
Type: re.Key,
|
|
|
|
Database: database.PostgresqlName,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-28 08:29:18 +00:00
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
|
|
|
return appInstallResourceRepo.DeleteBy(ctx, appInstallResourceRepo.WithAppInstallId(install.ID))
|
|
|
|
}
|
|
|
|
|
2024-01-30 08:11:41 +00:00
|
|
|
func upgradeInstall(installID uint, detailID uint, backup, pullImage bool) error {
|
|
|
|
install, err := appInstallRepo.GetFirst(commonRepo.WithByID(installID))
|
2022-10-13 10:56:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-30 08:11:41 +00:00
|
|
|
detail, err := appDetailRepo.GetFirst(commonRepo.WithByID(detailID))
|
2022-10-13 10:56:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if install.Version == detail.Version {
|
2022-10-17 08:32:31 +00:00
|
|
|
return errors.New("two version is same")
|
2022-10-13 10:56:53 +00:00
|
|
|
}
|
2023-05-18 08:48:19 +00:00
|
|
|
install.Status = constant.Upgrading
|
2023-04-27 07:30:15 +00:00
|
|
|
|
2023-05-18 08:48:19 +00:00
|
|
|
go func() {
|
2024-04-24 09:34:10 +00:00
|
|
|
var (
|
|
|
|
upErr error
|
|
|
|
backupFile string
|
|
|
|
)
|
2024-05-12 14:16:28 +00:00
|
|
|
global.LOG.Infof(i18n.GetMsgWithName("UpgradeAppStart", install.Name, nil))
|
2023-07-25 09:22:20 +00:00
|
|
|
if backup {
|
2024-04-24 09:34:10 +00:00
|
|
|
backupRecord, err := NewIBackupService().AppBackup(dto.CommonBackup{Name: install.App.Key, DetailName: install.Name})
|
|
|
|
if err == nil {
|
|
|
|
localDir, err := loadLocalDir()
|
|
|
|
if err == nil {
|
|
|
|
backupFile = path.Join(localDir, backupRecord.FileDir, backupRecord.FileName)
|
|
|
|
} else {
|
|
|
|
global.LOG.Errorf(i18n.GetMsgWithName("ErrAppBackup", install.Name, err))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
global.LOG.Errorf(i18n.GetMsgWithName("ErrAppBackup", install.Name, err))
|
2023-07-25 09:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-14 10:00:09 +00:00
|
|
|
|
2023-05-18 08:48:19 +00:00
|
|
|
defer func() {
|
|
|
|
if upErr != nil {
|
2024-04-24 09:34:10 +00:00
|
|
|
global.LOG.Infof(i18n.GetMsgWithName("ErrAppUpgrade", install.Name, upErr))
|
2023-11-14 10:00:09 +00:00
|
|
|
if backup {
|
2024-04-24 09:34:10 +00:00
|
|
|
global.LOG.Infof(i18n.GetMsgWithName("AppRecover", install.Name, nil))
|
|
|
|
if err := NewIBackupService().AppRecover(dto.CommonRecover{Name: install.App.Key, DetailName: install.Name, Type: "app", Source: constant.ResourceLocal, File: backupFile}); err != nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
global.LOG.Errorf("recover app [%s] [%s] failed %v", install.App.Key, install.Name, err)
|
|
|
|
}
|
|
|
|
}
|
2024-04-24 09:34:10 +00:00
|
|
|
existInstall, _ := appInstallRepo.GetFirst(commonRepo.WithByID(installID))
|
|
|
|
if existInstall.ID > 0 {
|
|
|
|
existInstall.Status = constant.UpgradeErr
|
|
|
|
existInstall.Message = upErr.Error()
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &existInstall)
|
|
|
|
}
|
2023-05-18 08:48:19 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-11-14 10:00:09 +00:00
|
|
|
fileOp := files.NewFileOp()
|
2023-05-18 08:48:19 +00:00
|
|
|
detailDir := path.Join(constant.ResourceDir, "apps", install.App.Resource, install.App.Key, detail.Version)
|
2023-06-05 05:45:26 +00:00
|
|
|
if install.App.Resource == constant.AppResourceRemote {
|
|
|
|
if upErr = downloadApp(install.App, detail, &install); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-11-14 10:00:09 +00:00
|
|
|
if detail.DockerCompose == "" {
|
|
|
|
composeDetail, err := fileOp.GetContent(path.Join(detailDir, "docker-compose.yml"))
|
|
|
|
if err != nil {
|
|
|
|
upErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
detail.DockerCompose = string(composeDetail)
|
|
|
|
_ = appDetailRepo.Update(context.Background(), detail)
|
|
|
|
}
|
2023-06-05 05:45:26 +00:00
|
|
|
go func() {
|
2024-05-29 03:05:09 +00:00
|
|
|
_, _, _ = httpUtil.HandleGet(detail.DownloadCallBackUrl, http.MethodGet)
|
2023-06-05 05:45:26 +00:00
|
|
|
}()
|
|
|
|
}
|
2023-11-14 10:00:09 +00:00
|
|
|
|
2023-05-18 08:48:19 +00:00
|
|
|
if install.App.Resource == constant.AppResourceLocal {
|
|
|
|
detailDir = path.Join(constant.ResourceDir, "apps", "local", strings.TrimPrefix(install.App.Key, "local"), detail.Version)
|
2023-04-03 09:47:23 +00:00
|
|
|
}
|
2022-10-17 08:32:31 +00:00
|
|
|
|
2023-08-23 14:48:14 +00:00
|
|
|
command := exec.Command("/bin/bash", "-c", fmt.Sprintf("cp -rn %s/* %s || true", detailDir, install.GetPath()))
|
|
|
|
stdout, _ := command.CombinedOutput()
|
2023-08-03 08:47:44 +00:00
|
|
|
if stdout != nil {
|
2023-08-23 14:48:14 +00:00
|
|
|
global.LOG.Infof("upgrade app [%s] [%s] cp file log : %s ", install.App.Key, install.Name, string(stdout))
|
|
|
|
}
|
|
|
|
sourceScripts := path.Join(detailDir, "scripts")
|
|
|
|
if fileOp.Stat(sourceScripts) {
|
|
|
|
dstScripts := path.Join(install.GetPath(), "scripts")
|
|
|
|
_ = fileOp.DeleteDir(dstScripts)
|
|
|
|
_ = fileOp.CreateDir(dstScripts, 0755)
|
|
|
|
scriptCmd := exec.Command("cp", "-rf", sourceScripts+"/.", dstScripts+"/")
|
|
|
|
_, _ = scriptCmd.CombinedOutput()
|
2023-04-03 09:47:23 +00:00
|
|
|
}
|
2023-05-18 08:48:19 +00:00
|
|
|
|
|
|
|
composeMap := make(map[string]interface{})
|
|
|
|
if upErr = yaml.Unmarshal([]byte(detail.DockerCompose), &composeMap); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
value, ok := composeMap["services"]
|
|
|
|
if !ok {
|
|
|
|
upErr = buserr.New(constant.ErrFileParse)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
servicesMap := value.(map[string]interface{})
|
2023-08-28 08:18:13 +00:00
|
|
|
if len(servicesMap) == 1 {
|
|
|
|
index := 0
|
|
|
|
oldServiceName := ""
|
|
|
|
for k := range servicesMap {
|
|
|
|
oldServiceName = k
|
|
|
|
index++
|
|
|
|
if index > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
servicesMap[install.ServiceName] = servicesMap[oldServiceName]
|
|
|
|
if install.ServiceName != oldServiceName {
|
|
|
|
delete(servicesMap, oldServiceName)
|
2023-05-18 08:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
envs := make(map[string]interface{})
|
|
|
|
if upErr = json.Unmarshal([]byte(install.Env), &envs); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config := getAppCommonConfig(envs)
|
2023-06-09 15:40:11 +00:00
|
|
|
if config.ContainerName == "" {
|
|
|
|
config.ContainerName = install.ContainerName
|
|
|
|
envs[constant.ContainerName] = install.ContainerName
|
|
|
|
}
|
2023-05-18 08:48:19 +00:00
|
|
|
config.Advanced = true
|
|
|
|
if upErr = addDockerComposeCommonParam(composeMap, install.ServiceName, config, envs); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-04-24 09:34:10 +00:00
|
|
|
paramByte, err := json.Marshal(envs)
|
|
|
|
if err != nil {
|
|
|
|
upErr = err
|
2023-05-18 08:48:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
install.Env = string(paramByte)
|
2024-04-24 09:34:10 +00:00
|
|
|
composeByte, err := yaml.Marshal(composeMap)
|
|
|
|
if err != nil {
|
|
|
|
upErr = err
|
2023-05-18 08:48:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
install.DockerCompose = string(composeByte)
|
|
|
|
install.Version = detail.Version
|
2024-01-30 08:11:41 +00:00
|
|
|
install.AppDetailId = detailID
|
2023-05-18 08:48:19 +00:00
|
|
|
|
2024-01-23 07:24:13 +00:00
|
|
|
content, err := fileOp.GetContent(install.GetEnvPath())
|
|
|
|
if err != nil {
|
|
|
|
upErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-30 08:11:41 +00:00
|
|
|
if pullImage {
|
2024-05-12 14:16:28 +00:00
|
|
|
projectName := strings.ToLower(install.Name)
|
|
|
|
images, err := composeV2.GetDockerComposeImages(projectName, content, []byte(detail.DockerCompose))
|
2024-01-30 08:11:41 +00:00
|
|
|
if err != nil {
|
|
|
|
upErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dockerCli, err := composeV2.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
upErr = err
|
2024-01-23 07:24:13 +00:00
|
|
|
return
|
|
|
|
}
|
2024-04-25 08:45:12 +00:00
|
|
|
defer dockerCli.Close()
|
2024-01-30 08:11:41 +00:00
|
|
|
for _, image := range images {
|
2024-04-24 09:34:10 +00:00
|
|
|
global.LOG.Infof(i18n.GetMsgWithName("PullImageStart", image, nil))
|
2024-01-30 08:11:41 +00:00
|
|
|
if err = dockerCli.PullImage(image, true); err != nil {
|
|
|
|
upErr = buserr.WithNameAndErr("ErrDockerPullImage", "", err)
|
|
|
|
return
|
2024-04-24 09:34:10 +00:00
|
|
|
} else {
|
|
|
|
global.LOG.Infof(i18n.GetMsgByKey("PullImageSuccess"))
|
2024-01-30 08:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-23 07:24:13 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 09:34:10 +00:00
|
|
|
if out, err := compose.Down(install.GetComposePath()); err != nil {
|
|
|
|
if out != "" {
|
|
|
|
upErr = errors.New(out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
upErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
envParams := make(map[string]string, len(envs))
|
|
|
|
handleMap(envs, envParams)
|
|
|
|
if upErr = env.Write(envParams, install.GetEnvPath()); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upErr = runScript(&install, "upgrade"); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-18 08:48:19 +00:00
|
|
|
if upErr = fileOp.WriteFile(install.GetComposePath(), strings.NewReader(install.DockerCompose), 0775); upErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if out, err := compose.Up(install.GetComposePath()); err != nil {
|
|
|
|
if out != "" {
|
|
|
|
upErr = errors.New(out)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
upErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
install.Status = constant.Running
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &install)
|
2024-04-24 09:34:10 +00:00
|
|
|
global.LOG.Infof(i18n.GetMsgWithName("UpgradeAppSuccess", install.Name, nil))
|
2023-05-18 08:48:19 +00:00
|
|
|
}()
|
|
|
|
|
2023-04-05 16:09:58 +00:00
|
|
|
return appInstallRepo.Save(context.Background(), &install)
|
2022-10-13 10:56:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 08:27:58 +00:00
|
|
|
func getContainerNames(install model.AppInstall) ([]string, error) {
|
2023-03-29 06:58:28 +00:00
|
|
|
envStr, err := coverEnvJsonToStr(install.Env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-09 14:32:12 +00:00
|
|
|
project, err := composeV2.GetComposeProject(install.Name, install.GetPath(), []byte(install.DockerCompose), []byte(envStr), true)
|
2022-10-11 08:27:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-07 08:46:11 +00:00
|
|
|
containerMap := make(map[string]struct{})
|
2022-10-11 08:27:58 +00:00
|
|
|
for _, service := range project.AllServices() {
|
2023-02-23 02:43:45 +00:00
|
|
|
if service.ContainerName == "${CONTAINER_NAME}" || service.ContainerName == "" {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-07 08:46:11 +00:00
|
|
|
containerMap[service.ContainerName] = struct{}{}
|
|
|
|
}
|
|
|
|
var containerNames []string
|
|
|
|
for k := range containerMap {
|
|
|
|
containerNames = append(containerNames, k)
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
2023-07-13 07:43:18 +00:00
|
|
|
if len(containerNames) == 0 {
|
|
|
|
containerNames = append(containerNames, install.ContainerName)
|
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
return containerNames, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 06:58:28 +00: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 08:45:00 +00:00
|
|
|
func checkLimit(app model.App) error {
|
2022-10-10 14:56:42 +00:00
|
|
|
if app.Limit > 0 {
|
2023-02-07 08:29:54 +00:00
|
|
|
installs, err := appInstallRepo.ListBy(appInstallRepo.WithAppId(app.ID))
|
2022-10-10 14:56:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(installs) >= app.Limit {
|
2022-12-04 04:47:19 +00:00
|
|
|
return buserr.New(constant.ErrAppLimit)
|
2022-10-10 14:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 08:45:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRequiredAndLimit(app model.App) error {
|
|
|
|
if err := checkLimit(app); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-10 14:56:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 08:46:38 +00: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)
|
2023-08-31 07:14:11 +00:00
|
|
|
case uint:
|
|
|
|
envParams[k] = strconv.Itoa(int(t))
|
2023-09-28 07:28:21 +00:00
|
|
|
case int:
|
|
|
|
envParams[k] = strconv.Itoa(t)
|
2024-02-20 06:58:58 +00:00
|
|
|
case []interface{}:
|
|
|
|
strArray := make([]string, len(t))
|
|
|
|
for i := range t {
|
|
|
|
strArray[i] = strings.ToLower(fmt.Sprintf("%v", t[i]))
|
|
|
|
}
|
|
|
|
envParams[k] = strings.Join(strArray, ",")
|
2024-04-08 09:54:07 +00:00
|
|
|
case map[string]interface{}:
|
|
|
|
handleMap(t, envParams)
|
2022-10-13 08:46:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-18 08:48:19 +00:00
|
|
|
func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall) (err error) {
|
2023-12-12 02:36:12 +00:00
|
|
|
if app.IsLocalApp() {
|
|
|
|
//本地应用,不去官网下载
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
appResourceDir := path.Join(constant.AppResourceDir, app.Resource)
|
2023-12-12 02:36:12 +00:00
|
|
|
appDownloadDir := app.GetAppResourcePath()
|
2023-05-23 02:31:45 +00:00
|
|
|
appVersionDir := path.Join(appDownloadDir, appDetail.Version)
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !appDetail.Update && fileOp.Stat(appVersionDir) {
|
2023-05-18 08:48:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !fileOp.Stat(appDownloadDir) {
|
|
|
|
_ = fileOp.CreateDir(appDownloadDir, 0755)
|
|
|
|
}
|
|
|
|
if !fileOp.Stat(appVersionDir) {
|
|
|
|
_ = fileOp.CreateDir(appVersionDir, 0755)
|
|
|
|
}
|
|
|
|
global.LOG.Infof("download app[%s] from %s", app.Name, appDetail.DownloadUrl)
|
2023-08-28 08:18:13 +00:00
|
|
|
filePath := path.Join(appVersionDir, app.Key+"-"+appDetail.Version+".tar.gz")
|
2023-05-15 14:40:05 +00:00
|
|
|
|
2023-05-18 08:48:19 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2023-05-18 11:08:20 +00:00
|
|
|
if appInstall != nil {
|
|
|
|
appInstall.Status = constant.DownloadErr
|
|
|
|
appInstall.Message = err.Error()
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
}
|
2023-05-18 08:48:19 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
if err = fileOp.DownloadFile(appDetail.DownloadUrl, filePath); err != nil {
|
|
|
|
global.LOG.Errorf("download app[%s] error %v", app.Name, err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-03 10:18:12 +00:00
|
|
|
if err = fileOp.Decompress(filePath, appResourceDir, files.SdkTarGz); err != nil {
|
2023-05-18 08:48:19 +00:00
|
|
|
global.LOG.Errorf("decompress app[%s] error %v", app.Name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = fileOp.DeleteFile(filePath)
|
2023-05-23 02:31:45 +00:00
|
|
|
appDetail.Update = false
|
|
|
|
_ = appDetailRepo.Update(context.Background(), appDetail)
|
2023-05-18 08:48:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyData(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, req request.AppInstallCreate) (err error) {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
appResourceDir := path.Join(constant.AppResourceDir, app.Resource)
|
|
|
|
|
|
|
|
if app.Resource == constant.AppResourceRemote {
|
|
|
|
err = downloadApp(app, appDetail, appInstall)
|
|
|
|
if err != nil {
|
2023-05-15 14:40:05 +00:00
|
|
|
return
|
|
|
|
}
|
2023-06-05 05:45:26 +00:00
|
|
|
go func() {
|
2024-05-29 03:05:09 +00:00
|
|
|
_, _, _ = httpUtil.HandleGet(appDetail.DownloadCallBackUrl, http.MethodGet)
|
2023-06-05 05:45:26 +00:00
|
|
|
}()
|
2023-05-15 14:40:05 +00:00
|
|
|
}
|
|
|
|
appKey := app.Key
|
|
|
|
installAppDir := path.Join(constant.AppInstallDir, app.Key)
|
|
|
|
if app.Resource == constant.AppResourceLocal {
|
2023-04-08 06:02:14 +00:00
|
|
|
appResourceDir = constant.LocalAppResourceDir
|
2023-05-16 09:31:47 +00:00
|
|
|
appKey = strings.TrimPrefix(app.Key, "local")
|
2023-04-08 06:02:14 +00:00
|
|
|
installAppDir = path.Join(constant.LocalAppInstallDir, appKey)
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
resourceDir := path.Join(appResourceDir, appKey, appDetail.Version)
|
2022-12-04 04:47:19 +00:00
|
|
|
|
2022-12-02 02:31:07 +00:00
|
|
|
if !fileOp.Stat(installAppDir) {
|
|
|
|
if err = fileOp.CreateDir(installAppDir, 0755); err != nil {
|
2022-11-20 14:49:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
appDir := path.Join(installAppDir, req.Name)
|
2022-12-02 02:31:07 +00:00
|
|
|
if fileOp.Stat(appDir) {
|
|
|
|
if err = fileOp.DeleteDir(appDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-10-10 07:10:53 +00:00
|
|
|
}
|
2022-12-04 04:47:19 +00:00
|
|
|
if err = fileOp.Copy(resourceDir, installAppDir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-05-15 14:40:05 +00:00
|
|
|
versionDir := path.Join(installAppDir, appDetail.Version)
|
2022-12-04 04:47:19 +00:00
|
|
|
if err = fileOp.Rename(versionDir, appDir); err != nil {
|
2022-10-10 07:10:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
envPath := path.Join(appDir, ".env")
|
|
|
|
|
2023-05-15 14:40:05 +00:00
|
|
|
envParams := make(map[string]string, len(req.Params))
|
|
|
|
handleMap(req.Params, envParams)
|
2023-03-09 06:24:59 +00:00
|
|
|
if err = env.Write(envParams, envPath); err != nil {
|
2022-10-10 07:10:53 +00:00
|
|
|
return
|
|
|
|
}
|
2023-05-17 05:46:28 +00:00
|
|
|
if err := fileOp.WriteFile(appInstall.GetComposePath(), strings.NewReader(appInstall.DockerCompose), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-10 07:10:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-23 14:48:14 +00:00
|
|
|
func runScript(appInstall *model.AppInstall, operate string) error {
|
|
|
|
workDir := appInstall.GetPath()
|
|
|
|
scriptPath := ""
|
|
|
|
switch operate {
|
|
|
|
case "init":
|
|
|
|
scriptPath = path.Join(workDir, "scripts", "init.sh")
|
|
|
|
case "upgrade":
|
|
|
|
scriptPath = path.Join(workDir, "scripts", "upgrade.sh")
|
|
|
|
case "uninstall":
|
|
|
|
scriptPath = path.Join(workDir, "scripts", "uninstall.sh")
|
|
|
|
}
|
|
|
|
if !files.NewFileOp().Stat(scriptPath) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out, err := cmd.ExecScript(scriptPath, workDir)
|
|
|
|
if err != nil {
|
|
|
|
if out != "" {
|
2023-08-24 10:20:15 +00:00
|
|
|
errMsg := fmt.Sprintf("run script %s error %s", scriptPath, out)
|
|
|
|
global.LOG.Error(errMsg)
|
|
|
|
return errors.New(errMsg)
|
2023-08-23 14:48:14 +00:00
|
|
|
}
|
|
|
|
return err
|
2023-03-31 04:16:14 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 06:59:26 +00:00
|
|
|
func checkContainerNameIsExist(containerName, appDir string) (bool, error) {
|
|
|
|
client, err := composeV2.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-04-25 08:45:12 +00:00
|
|
|
defer client.Close()
|
2024-04-08 09:54:07 +00:00
|
|
|
var options container.ListOptions
|
2023-06-05 06:59:26 +00:00
|
|
|
list, err := client.ContainerList(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, container := range list {
|
|
|
|
if containerName == container.Names[0][1:] {
|
|
|
|
if workDir, ok := container.Labels[composeWorkdirLabel]; ok {
|
|
|
|
if workDir != appDir {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2024-01-30 08:11:41 +00:00
|
|
|
func upApp(appInstall *model.AppInstall, pullImages bool) {
|
2023-04-11 06:38:27 +00:00
|
|
|
upProject := func(appInstall *model.AppInstall) (err error) {
|
2024-03-01 08:55:02 +00:00
|
|
|
var (
|
|
|
|
out string
|
|
|
|
errMsg string
|
|
|
|
)
|
|
|
|
if pullImages && appInstall.App.Type != "php" {
|
|
|
|
out, err = compose.Pull(appInstall.GetComposePath())
|
2023-04-06 10:32:16 +00:00
|
|
|
if err != nil {
|
2023-07-06 08:30:22 +00:00
|
|
|
if out != "" {
|
2024-03-01 08:55:02 +00:00
|
|
|
if strings.Contains(out, "no such host") {
|
|
|
|
errMsg = i18n.GetMsgByKey("ErrNoSuchHost") + ":"
|
|
|
|
}
|
|
|
|
if strings.Contains(out, "timeout") {
|
|
|
|
errMsg = i18n.GetMsgByKey("ErrImagePullTimeOut") + ":"
|
|
|
|
}
|
2023-07-06 08:30:22 +00:00
|
|
|
appInstall.Message = errMsg + out
|
|
|
|
}
|
2023-04-06 10:32:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-10 07:10:53 +00:00
|
|
|
}
|
2024-03-01 08:55:02 +00:00
|
|
|
|
|
|
|
out, err = compose.Up(appInstall.GetComposePath())
|
|
|
|
if err != nil {
|
|
|
|
if out != "" {
|
|
|
|
appInstall.Message = errMsg + out
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return
|
2023-04-06 10:32:16 +00:00
|
|
|
}
|
|
|
|
if err := upProject(appInstall); err != nil {
|
2023-07-06 08:30:22 +00:00
|
|
|
appInstall.Status = constant.Error
|
2022-10-10 07:10:53 +00:00
|
|
|
} else {
|
|
|
|
appInstall.Status = constant.Running
|
2023-04-06 08:38:16 +00:00
|
|
|
}
|
|
|
|
exist, _ := appInstallRepo.GetFirst(commonRepo.WithByID(appInstall.ID))
|
|
|
|
if exist.ID > 0 {
|
2023-04-11 06:38:27 +00:00
|
|
|
_ = appInstallRepo.Save(context.Background(), appInstall)
|
2022-10-10 07:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:04:22 +00:00
|
|
|
func rebuildApp(appInstall model.AppInstall) error {
|
2023-08-28 10:10:16 +00:00
|
|
|
appInstall.Status = constant.Rebuilding
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &appInstall)
|
|
|
|
go func() {
|
|
|
|
dockerComposePath := appInstall.GetComposePath()
|
|
|
|
out, err := compose.Down(dockerComposePath)
|
|
|
|
if err != nil {
|
|
|
|
_ = handleErr(appInstall, err, out)
|
|
|
|
return
|
|
|
|
}
|
2024-02-23 10:38:40 +00:00
|
|
|
out, err = compose.Up(appInstall.GetComposePath())
|
2023-08-28 10:10:16 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = handleErr(appInstall, err, out)
|
|
|
|
return
|
|
|
|
}
|
2024-02-19 05:55:27 +00:00
|
|
|
|
2023-08-28 10:10:16 +00:00
|
|
|
appInstall.Status = constant.Running
|
|
|
|
_ = appInstallRepo.Save(context.Background(), &appInstall)
|
|
|
|
}()
|
|
|
|
return nil
|
2023-03-08 03:04:22 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 14:40:05 +00:00
|
|
|
func getAppDetails(details []model.AppDetail, versions []dto.AppConfigVersion) map[string]model.AppDetail {
|
2022-10-10 07:10:53 +00:00
|
|
|
appDetails := make(map[string]model.AppDetail, len(details))
|
|
|
|
for _, old := range details {
|
|
|
|
old.Status = constant.AppTakeDown
|
|
|
|
appDetails[old.Version] = old
|
|
|
|
}
|
|
|
|
for _, v := range versions {
|
2023-05-15 14:40:05 +00:00
|
|
|
version := v.Name
|
|
|
|
detail, ok := appDetails[version]
|
2022-10-10 07:10:53 +00:00
|
|
|
if ok {
|
|
|
|
detail.Status = constant.AppNormal
|
2023-05-15 14:40:05 +00:00
|
|
|
appDetails[version] = detail
|
2022-10-10 07:10:53 +00:00
|
|
|
} else {
|
2023-05-15 14:40:05 +00:00
|
|
|
appDetails[version] = model.AppDetail{
|
|
|
|
Version: version,
|
2022-10-10 07:10:53 +00:00
|
|
|
Status: constant.AppNormal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return appDetails
|
|
|
|
}
|
|
|
|
|
2023-05-16 09:31:47 +00:00
|
|
|
func getApps(oldApps []model.App, items []dto.AppDefine) map[string]model.App {
|
2022-10-10 07:10:53 +00: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-05-15 14:40:05 +00:00
|
|
|
config := item.AppProperty
|
|
|
|
key := config.Key
|
2023-04-08 06:02:14 +00:00
|
|
|
app, ok := apps[key]
|
2022-10-10 07:10:53 +00:00
|
|
|
if !ok {
|
|
|
|
app = model.App{}
|
|
|
|
}
|
2023-05-16 09:31:47 +00:00
|
|
|
app.Resource = constant.AppResourceRemote
|
2022-10-10 07:10:53 +00:00
|
|
|
app.Name = item.Name
|
2023-05-15 14:40:05 +00:00
|
|
|
app.Limit = config.Limit
|
2023-04-08 06:02:14 +00:00
|
|
|
app.Key = key
|
2023-05-15 14:40:05 +00:00
|
|
|
app.ShortDescZh = config.ShortDescZh
|
|
|
|
app.ShortDescEn = config.ShortDescEn
|
|
|
|
app.Website = config.Website
|
|
|
|
app.Document = config.Document
|
|
|
|
app.Github = config.Github
|
|
|
|
app.Type = config.Type
|
|
|
|
app.CrossVersionUpdate = config.CrossVersionUpdate
|
2022-10-10 07:10:53 +00:00
|
|
|
app.Status = constant.AppNormal
|
2023-05-15 14:40:05 +00:00
|
|
|
app.LastModified = item.LastModified
|
|
|
|
app.ReadMe = item.ReadMe
|
2023-04-08 06:02:14 +00:00
|
|
|
apps[key] = app
|
2022-10-10 07:10:53 +00:00
|
|
|
}
|
|
|
|
return apps
|
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
|
2023-05-31 06:51:01 +00:00
|
|
|
func handleLocalAppDetail(versionDir string, appDetail *model.AppDetail) error {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
dockerComposePath := path.Join(versionDir, "docker-compose.yml")
|
|
|
|
if !fileOp.Stat(dockerComposePath) {
|
2023-11-14 10:00:09 +00:00
|
|
|
return buserr.WithName(constant.ErrFileNotFound, "docker-compose.yml")
|
2023-05-31 06:51:01 +00:00
|
|
|
}
|
|
|
|
dockerComposeByte, _ := fileOp.GetContent(dockerComposePath)
|
|
|
|
if dockerComposeByte == nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
return buserr.WithName(constant.ErrFileParseApp, "docker-compose.yml")
|
2023-05-31 06:51:01 +00:00
|
|
|
}
|
|
|
|
appDetail.DockerCompose = string(dockerComposeByte)
|
|
|
|
paramPath := path.Join(versionDir, "data.yml")
|
|
|
|
if !fileOp.Stat(paramPath) {
|
2023-11-14 10:00:09 +00:00
|
|
|
return buserr.WithName(constant.ErrFileNotFound, "data.yml")
|
2023-05-31 06:51:01 +00:00
|
|
|
}
|
|
|
|
paramByte, _ := fileOp.GetContent(paramPath)
|
|
|
|
if paramByte == nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
return buserr.WithName(constant.ErrFileNotFound, "data.yml")
|
2023-05-31 06:51:01 +00:00
|
|
|
}
|
|
|
|
appParamConfig := dto.LocalAppParam{}
|
|
|
|
if err := yaml.Unmarshal(paramByte, &appParamConfig); err != nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
return buserr.WithMap(constant.ErrFileParseApp, map[string]interface{}{"name": "data.yml", "err": err.Error()}, err)
|
2023-05-31 06:51:01 +00:00
|
|
|
}
|
|
|
|
dataJson, err := json.Marshal(appParamConfig.AppParams)
|
|
|
|
if err != nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
return buserr.WithMap(constant.ErrFileParseApp, map[string]interface{}{"name": "data.yml", "err": err.Error()}, err)
|
2023-05-31 06:51:01 +00:00
|
|
|
}
|
2024-03-04 03:53:03 +00:00
|
|
|
var appParam dto.AppForm
|
|
|
|
if err = json.Unmarshal(dataJson, &appParam); err != nil {
|
|
|
|
return buserr.WithMap(constant.ErrFileParseApp, map[string]interface{}{"name": "data.yml", "err": err.Error()}, err)
|
|
|
|
}
|
|
|
|
for _, formField := range appParam.FormFields {
|
|
|
|
if strings.Contains(formField.EnvKey, " ") {
|
|
|
|
return buserr.WithName(constant.ErrAppParamKey, formField.EnvKey)
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 10:10:08 +00:00
|
|
|
|
|
|
|
var dataMap map[string]interface{}
|
|
|
|
err = yaml.Unmarshal(paramByte, &dataMap)
|
|
|
|
if err != nil {
|
|
|
|
return buserr.WithMap(constant.ErrFileParseApp, map[string]interface{}{"name": "data.yml", "err": err.Error()}, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
additionalProperties, ok := dataMap["additionalProperties"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return buserr.WithName(constant.ErrAppParamKey, "additionalProperties")
|
|
|
|
}
|
|
|
|
|
|
|
|
formFieldsInterface, ok := additionalProperties["formFields"]
|
|
|
|
if ok {
|
|
|
|
formFields, ok := formFieldsInterface.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return buserr.WithName(constant.ErrAppParamKey, "formFields")
|
|
|
|
}
|
|
|
|
for _, item := range formFields {
|
|
|
|
field := item.(map[string]interface{})
|
|
|
|
for key, value := range field {
|
|
|
|
if value == nil {
|
|
|
|
return buserr.WithName(constant.ErrAppParamKey, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 06:51:01 +00:00
|
|
|
appDetail.Params = string(dataJson)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleLocalApp(appDir string) (app *model.App, err error) {
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
configYamlPath := path.Join(appDir, "data.yml")
|
|
|
|
if !fileOp.Stat(configYamlPath) {
|
2023-11-14 10:00:09 +00:00
|
|
|
err = buserr.WithName(constant.ErrFileNotFound, "data.yml")
|
2023-05-31 06:51:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
iconPath := path.Join(appDir, "logo.png")
|
|
|
|
if !fileOp.Stat(iconPath) {
|
2023-11-14 10:00:09 +00:00
|
|
|
err = buserr.WithName(constant.ErrFileNotFound, "logo.png")
|
2023-05-31 06:51:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
configYamlByte, err := fileOp.GetContent(configYamlPath)
|
|
|
|
if err != nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
err = buserr.WithMap(constant.ErrFileParseApp, map[string]interface{}{"name": "data.yml", "err": err.Error()}, err)
|
2023-05-31 06:51:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
localAppDefine := dto.LocalAppAppDefine{}
|
|
|
|
if err = yaml.Unmarshal(configYamlByte, &localAppDefine); err != nil {
|
2023-11-14 10:00:09 +00:00
|
|
|
err = buserr.WithMap(constant.ErrFileParseApp, map[string]interface{}{"name": "data.yml", "err": err.Error()}, err)
|
2023-05-31 06:51:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
app = &localAppDefine.AppProperty
|
|
|
|
app.Resource = constant.AppResourceLocal
|
|
|
|
app.Status = constant.AppNormal
|
|
|
|
app.Recommend = 9999
|
|
|
|
app.TagsKey = append(app.TagsKey, "Local")
|
|
|
|
app.Key = "local" + app.Key
|
|
|
|
readMePath := path.Join(appDir, "README.md")
|
|
|
|
readMeByte, err := fileOp.GetContent(readMePath)
|
|
|
|
if err == nil {
|
|
|
|
app.ReadMe = string(readMeByte)
|
|
|
|
}
|
|
|
|
iconByte, _ := fileOp.GetContent(iconPath)
|
|
|
|
if iconByte != nil {
|
|
|
|
iconStr := base64.StdEncoding.EncodeToString(iconByte)
|
|
|
|
app.Icon = iconStr
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 08:27:58 +00: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)
|
|
|
|
}
|
2024-02-19 05:55:27 +00:00
|
|
|
install.Status = constant.Error
|
2023-04-05 16:09:58 +00:00
|
|
|
_ = appInstallRepo.Save(context.Background(), &install)
|
2022-10-11 08:27:58 +00:00
|
|
|
return reErr
|
|
|
|
}
|
2022-10-14 06:48:55 +00:00
|
|
|
|
2024-05-29 10:41:13 +00:00
|
|
|
func handleInstalled(appInstallList []model.AppInstall, updated bool, sync bool) ([]response.AppInstalledDTO, error) {
|
2022-12-14 07:08:21 +00:00
|
|
|
var res []response.AppInstalledDTO
|
2023-01-16 07:30:24 +00:00
|
|
|
for _, installed := range appInstallList {
|
2023-07-12 06:41:16 +00:00
|
|
|
if updated && (installed.App.Type == "php" || installed.Status == constant.Installing || (installed.App.Key == constant.AppMysql && installed.Version == "5.6.51")) {
|
2023-05-10 06:32:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-05-29 10:41:13 +00:00
|
|
|
if sync {
|
|
|
|
if err := syncAppInstallStatus(&installed); err != nil {
|
|
|
|
global.LOG.Error("sync app install status error : ", err)
|
|
|
|
}
|
2024-02-09 01:50:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 07:08:21 +00:00
|
|
|
installDTO := response.AppInstalledDTO{
|
2022-11-22 06:22:25 +00:00
|
|
|
AppInstall: installed,
|
2023-05-22 09:10:51 +00:00
|
|
|
Path: installed.GetPath(),
|
2022-11-22 06:22:25 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-02-04 10:15:41 +00:00
|
|
|
if detail.IgnoreUpgrade || installed.Version == "latest" {
|
2023-07-03 08:36:18 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-06-07 11:09:23 +00:00
|
|
|
if common.IsCrossVersion(installed.Version, detail.Version) && !app.CrossVersionUpdate {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-22 06:22:25 +00:00
|
|
|
versions = append(versions, detail.Version)
|
|
|
|
}
|
|
|
|
versions = common.GetSortedVersions(versions)
|
2023-07-04 03:36:10 +00:00
|
|
|
if len(versions) == 0 {
|
2023-08-22 07:38:11 +00:00
|
|
|
if !updated {
|
|
|
|
installDTO.CanUpdate = false
|
|
|
|
res = append(res, installDTO)
|
|
|
|
}
|
2023-07-04 03:36:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-11-22 06:22:25 +00:00
|
|
|
lastVersion := versions[0]
|
|
|
|
if common.IsCrossVersion(installed.Version, lastVersion) {
|
|
|
|
installDTO.CanUpdate = app.CrossVersionUpdate
|
|
|
|
} else {
|
|
|
|
installDTO.CanUpdate = common.CompareVersion(lastVersion, installed.Version)
|
|
|
|
}
|
2023-01-16 07:30:24 +00:00
|
|
|
if updated {
|
|
|
|
if installDTO.CanUpdate {
|
|
|
|
res = append(res, installDTO)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = append(res, installDTO)
|
|
|
|
}
|
2022-11-22 06:22:25 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2022-11-24 02:28:39 +00: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 08:03:00 +00:00
|
|
|
|
2023-08-24 10:20:15 +00:00
|
|
|
func getAppInstallPort(key string) (httpPort, httpsPort int, err error) {
|
|
|
|
install, err := getAppInstallByKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
httpPort = install.HttpPort
|
|
|
|
httpsPort = install.HttpsPort
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-11 06:38:27 +00:00
|
|
|
func updateToolApp(installed *model.AppInstall) {
|
2022-12-09 08:03:00 +00: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 06:24:59 +00:00
|
|
|
if err = env.Write(envFileMap, envPath); err != nil {
|
2022-12-09 08:03:00 +00:00
|
|
|
global.LOG.Errorf("update tool app [%s] error : %s", toolInstall.Name, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
toolInstall.Env = string(contentByte)
|
2023-04-05 16:09:58 +00:00
|
|
|
if err := appInstallRepo.Save(context.Background(), &toolInstall); err != nil {
|
2022-12-09 08:03:00 +00: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
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 08:48:19 +00:00
|
|
|
|
|
|
|
func addDockerComposeCommonParam(composeMap map[string]interface{}, serviceName string, req request.AppContainerConfig, params map[string]interface{}) error {
|
|
|
|
services, serviceValid := composeMap["services"].(map[string]interface{})
|
|
|
|
if !serviceValid {
|
|
|
|
return buserr.New(constant.ErrFileParse)
|
|
|
|
}
|
|
|
|
service, serviceExist := services[serviceName]
|
|
|
|
if !serviceExist {
|
|
|
|
return buserr.New(constant.ErrFileParse)
|
|
|
|
}
|
|
|
|
serviceValue := service.(map[string]interface{})
|
2024-04-11 10:28:11 +00:00
|
|
|
|
|
|
|
deploy := map[string]interface{}{}
|
|
|
|
if de, ok := serviceValue["deploy"]; ok {
|
|
|
|
deploy = de.(map[string]interface{})
|
|
|
|
}
|
2024-04-15 03:54:09 +00:00
|
|
|
resource := map[string]interface{}{}
|
|
|
|
if res, ok := deploy["resources"]; ok {
|
|
|
|
resource = res.(map[string]interface{})
|
|
|
|
}
|
|
|
|
resource["limits"] = map[string]interface{}{
|
|
|
|
"cpus": "${CPUS}",
|
|
|
|
"memory": "${MEMORY_LIMIT}",
|
2023-05-18 08:48:19 +00:00
|
|
|
}
|
2024-04-15 03:54:09 +00:00
|
|
|
deploy["resources"] = resource
|
2023-05-18 08:48:19 +00:00
|
|
|
serviceValue["deploy"] = deploy
|
|
|
|
|
|
|
|
ports, ok := serviceValue["ports"].([]interface{})
|
|
|
|
if ok {
|
|
|
|
for i, port := range ports {
|
|
|
|
portStr, portOK := port.(string)
|
|
|
|
if !portOK {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
portArray := strings.Split(portStr, ":")
|
|
|
|
if len(portArray) == 2 {
|
|
|
|
portArray = append([]string{"${HOST_IP}"}, portArray...)
|
|
|
|
}
|
|
|
|
ports[i] = strings.Join(portArray, ":")
|
|
|
|
}
|
|
|
|
serviceValue["ports"] = ports
|
|
|
|
}
|
|
|
|
|
|
|
|
params[constant.CPUS] = "0"
|
|
|
|
params[constant.MemoryLimit] = "0"
|
|
|
|
if req.Advanced {
|
|
|
|
if req.CpuQuota > 0 {
|
|
|
|
params[constant.CPUS] = req.CpuQuota
|
|
|
|
}
|
|
|
|
if req.MemoryLimit > 0 {
|
|
|
|
params[constant.MemoryLimit] = strconv.FormatFloat(req.MemoryLimit, 'f', -1, 32) + req.MemoryUnit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, portExist := serviceValue["ports"].([]interface{})
|
|
|
|
if portExist {
|
|
|
|
allowHost := "127.0.0.1"
|
|
|
|
if req.Advanced && req.AllowPort {
|
2023-12-01 08:58:10 +00:00
|
|
|
allowHost = ""
|
2023-05-18 08:48:19 +00:00
|
|
|
}
|
|
|
|
params[constant.HostIP] = allowHost
|
|
|
|
}
|
|
|
|
services[serviceName] = serviceValue
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAppCommonConfig(envs map[string]interface{}) request.AppContainerConfig {
|
|
|
|
config := request.AppContainerConfig{}
|
|
|
|
|
|
|
|
if hostIp, ok := envs[constant.HostIP]; ok {
|
2023-12-01 08:58:10 +00:00
|
|
|
config.AllowPort = hostIp.(string) != "127.0.0.1"
|
2023-05-18 08:48:19 +00:00
|
|
|
} else {
|
|
|
|
config.AllowPort = true
|
|
|
|
}
|
|
|
|
if cpuCore, ok := envs[constant.CPUS]; ok {
|
|
|
|
numStr, ok := cpuCore.(string)
|
|
|
|
if ok {
|
|
|
|
num, err := strconv.ParseFloat(numStr, 64)
|
|
|
|
if err == nil {
|
|
|
|
config.CpuQuota = num
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
num64, flOk := cpuCore.(float64)
|
|
|
|
if flOk {
|
|
|
|
config.CpuQuota = num64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config.CpuQuota = 0
|
|
|
|
}
|
|
|
|
if memLimit, ok := envs[constant.MemoryLimit]; ok {
|
2023-06-02 09:45:24 +00:00
|
|
|
re := regexp.MustCompile(`(\d+)([A-Za-z]+)`)
|
2023-05-18 08:48:19 +00:00
|
|
|
matches := re.FindStringSubmatch(memLimit.(string))
|
|
|
|
if len(matches) == 3 {
|
|
|
|
num, err := strconv.ParseFloat(matches[1], 64)
|
|
|
|
if err == nil {
|
|
|
|
unit := matches[2]
|
|
|
|
config.MemoryLimit = num
|
|
|
|
config.MemoryUnit = unit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
config.MemoryLimit = 0
|
|
|
|
config.MemoryUnit = "M"
|
|
|
|
}
|
|
|
|
|
|
|
|
if containerName, ok := envs[constant.ContainerName]; ok {
|
|
|
|
config.ContainerName = containerName.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
2023-11-03 07:39:38 +00:00
|
|
|
|
|
|
|
func isHostModel(dockerCompose string) bool {
|
|
|
|
composeMap := make(map[string]interface{})
|
|
|
|
_ = yaml.Unmarshal([]byte(dockerCompose), &composeMap)
|
|
|
|
services, serviceValid := composeMap["services"].(map[string]interface{})
|
|
|
|
if !serviceValid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, service := range services {
|
|
|
|
serviceValue := service.(map[string]interface{})
|
|
|
|
if value, ok := serviceValue["network_mode"]; ok && value == "host" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|