fix: 解决安装redis-commander没有同步密码的BUG,安装APP增加名称唯一性校验

This commit is contained in:
zhengkunwang223 2022-12-02 10:31:07 +08:00 committed by zhengkunwang223
parent 1090047e55
commit 7de0284e81
11 changed files with 67 additions and 28 deletions

View File

@ -104,8 +104,9 @@ type PortUpdate struct {
}
type AppService struct {
Label string `json:"label"`
Value string `json:"value"`
Label string `json:"label"`
Value string `json:"value"`
Config interface{} `json:"config"`
}
type AppDatabase struct {

View File

@ -7,17 +7,16 @@ import (
type AppInstall struct {
BaseModel
Name string `json:"name" gorm:"type:varchar(64);not null"`
AppId uint `json:"appId" gorm:"type:integer;not null"`
AppDetailId uint `json:"appDetailId" gorm:"type:integer;not null"`
Version string `json:"version" gorm:"type:varchar(64);not null"`
Param string `json:"param" gorm:"type:longtext;"`
Env string `json:"env" gorm:"type:longtext;"`
DockerCompose string `json:"dockerCompose" gorm:"type:longtext;"`
Status string `json:"status" gorm:"type:varchar(256);not null"`
Description string `json:"description" gorm:"type:varchar(256);"`
Message string `json:"message" gorm:"type:longtext;"`
//CanUpdate bool `json:"canUpdate"`
Name string `json:"name" gorm:"type:varchar(64);not null;UNIQUE"`
AppId uint `json:"appId" gorm:"type:integer;not null"`
AppDetailId uint `json:"appDetailId" gorm:"type:integer;not null"`
Version string `json:"version" gorm:"type:varchar(64);not null"`
Param string `json:"param" gorm:"type:longtext;"`
Env string `json:"env" gorm:"type:longtext;"`
DockerCompose string `json:"dockerCompose" gorm:"type:longtext;"`
Status string `json:"status" gorm:"type:varchar(256);not null"`
Description string `json:"description" gorm:"type:varchar(256);"`
Message string `json:"message" gorm:"type:longtext;"`
ContainerName string `json:"containerName" gorm:"type:varchar(256);not null"`
ServiceName string `json:"serviceName" gorm:"type:varchar(256);not null"`
HttpPort int `json:"httpPort" gorm:"type:integer;not null"`

View File

@ -3,6 +3,7 @@ package service
import (
"encoding/base64"
"encoding/json"
"github.com/1Panel-dev/1Panel/backend/buserr"
"os"
"path"
"strings"
@ -139,6 +140,11 @@ func (a AppService) GetAppDetail(appId uint, version string) (dto.AppDetailDTO,
func (a AppService) Install(name string, appDetailId uint, params map[string]interface{}) (*model.AppInstall, error) {
list, _ := appInstallRepo.GetBy(commonRepo.WithByName(name))
if len(list) > 0 {
return nil, buserr.New(constant.ErrNameIsExist, "", nil)
}
httpPort, err := checkPort("PANEL_APP_PORT_HTTP", params)
if err != nil {
return nil, err

View File

@ -2,6 +2,7 @@ package service
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
@ -210,9 +211,14 @@ func (a AppInstallService) GetServices(key string) ([]dto.AppService, error) {
}
var res []dto.AppService
for _, install := range installs {
paramMap := make(map[string]string)
if install.Param != "" {
_ = json.Unmarshal([]byte(install.Param), &paramMap)
}
res = append(res, dto.AppService{
Label: install.Name,
Value: install.ServiceName,
Label: install.Name,
Value: install.ServiceName,
Config: paramMap,
})
}
return res, nil

View File

@ -436,20 +436,21 @@ func handleMap(params map[string]interface{}, envParams map[string]string) {
}
func copyAppData(key, version, installName string, params map[string]interface{}) (err error) {
resourceDir := path.Join(constant.AppResourceDir, key, "versions", version)
installDir := path.Join(constant.AppInstallDir, key)
installVersionDir := path.Join(installDir, version)
fileOp := files.NewFileOp()
if fileOp.Stat(installVersionDir) {
if err = fileOp.DeleteDir(installVersionDir); err != nil {
resourceDir := path.Join(constant.AppResourceDir, key, "versions", version)
installAppDir := path.Join(constant.AppInstallDir, key)
if !fileOp.Stat(installAppDir) {
if err = fileOp.CreateDir(installAppDir, 0755); err != nil {
return
}
}
if err = fileOp.Copy(resourceDir, installVersionDir); err != nil {
return
appDir := path.Join(installAppDir, installName)
if fileOp.Stat(appDir) {
if err = fileOp.DeleteDir(appDir); err != nil {
return
}
}
appDir := path.Join(installDir, installName)
if err = fileOp.Rename(installVersionDir, appDir); err != nil {
if err = fileOp.Copy(resourceDir, appDir); err != nil {
return
}
envPath := path.Join(appDir, ".env")

View File

@ -40,6 +40,7 @@ var (
ErrTypeNotLogin = "ErrNotLogin"
ErrTypePasswordExpired = "ErrPasswordExpired"
ErrTypeNotSafety = "ErrNotSafety"
ErrNameIsExist = "ErrNameIsExist"
)
// app

View File

@ -14,6 +14,9 @@ ErrPasswordExpired: "The current password has expired: {{ .detail }}"
ErrNotSupportType: "The system does not support the current type: {{ .detail }}"
#common
ErrNameIsExist: "Name is already exist"
#app
ErrPortInUsed: "{{ .detail }} port already in use"
ErrAppLimit: "App exceeds install limit"

View File

@ -14,6 +14,9 @@ ErrPasswordExpired: "当前密码已过期: {{ .detail }}"
ErrNotSupportType: "系统暂不支持当前类型: {{ .detail }}"
#common
ErrNameIsExist: "名称已存在"
#app
ErrPortInUsed: "{{ .detail }} 已被占用!"
ErrAppLimit: "应用超出安装数量限制"

View File

@ -117,6 +117,7 @@ export namespace App {
export interface AppService {
label: string;
value: string;
config?: Object;
}
export interface AppBackupReq extends ReqPage {

View File

@ -51,7 +51,7 @@ export const SyncInstalledApp = () => {
};
export const GetAppService = (key: string | undefined) => {
return http.get<any>(`apps/services/${key}`);
return http.get<App.AppService[]>(`apps/services/${key}`);
};
export const GetAppBackups = (info: App.AppBackupReq) => {

View File

@ -15,7 +15,11 @@
show-password
@change="updateParam"
></el-input>
<el-select v-model="form[p.envKey]" v-if="p.type == 'service'" @change="updateParam">
<el-select
v-model="form[p.envKey]"
v-if="p.type == 'service'"
@change="changeService(form[p.envKey], p.services)"
>
<el-option
v-for="service in p.services"
:key="service.label"
@ -112,8 +116,6 @@ const handleParams = () => {
emit('update:rules', rules);
updateParam();
}
console.log(rules);
console.log(paramObjs);
}
};
@ -122,11 +124,27 @@ const getServices = async (envKey: string, key: string | undefined, pObj: ParamO
pObj.services = res.data;
if (res.data.length > 0) {
form[envKey] = res.data[0].value;
if (res.data[0].config) {
Object.entries(res.data[0].config).forEach(([k, v]) => {
form[k] = v;
});
}
updateParam();
}
});
};
const changeService = (value: string, services: App.AppService[]) => {
services.forEach((item) => {
if (item.value === value) {
Object.entries(item.config).forEach(([k, v]) => {
form[k] = v;
});
}
});
updateParam();
};
onMounted(() => {
handleParams();
});