feat: 数据库增加默认配置功能

This commit is contained in:
ssongliu 2022-12-23 18:00:19 +08:00 committed by ssongliu
parent 2737155a7f
commit e1c2348e44
7 changed files with 75 additions and 22 deletions

View file

@ -446,7 +446,7 @@ func syncById(installId uint) error {
} }
func updateInstallInfoInDB(appKey, appName, param string, isRestart bool, value interface{}) error { func updateInstallInfoInDB(appKey, appName, param string, isRestart bool, value interface{}) error {
if param != "password" && param != "port" { if param != "password" && param != "port" && param != "user-password" {
return nil return nil
} }
appInstall, err := appInstallRepo.LoadBaseInfo(appKey, appName) appInstall, err := appInstallRepo.LoadBaseInfo(appKey, appName)
@ -458,9 +458,15 @@ func updateInstallInfoInDB(appKey, appName, param string, isRestart bool, value
if err != nil { if err != nil {
return err return err
} }
envKey := "PANEL_DB_ROOT_PASSWORD="
if param == "port" { envKey := ""
switch param {
case "password":
envKey = "PANEL_DB_ROOT_PASSWORD="
case "port":
envKey = "PANEL_APP_PORT_HTTP=" envKey = "PANEL_APP_PORT_HTTP="
case "user-password":
envKey = "PANEL_DB_USER_PASSWORD="
} }
files := strings.Split(string(lineBytes), "\n") files := strings.Split(string(lineBytes), "\n")
var newFiles []string var newFiles []string

View file

@ -19,6 +19,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/compose" "github.com/1Panel-dev/1Panel/backend/utils/compose"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -264,7 +265,7 @@ func (u *MysqlService) Delete(id uint) error {
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists '%s'@'%s'", db.Name, db.Permission)); err != nil { if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop user if exists '%s'@'%s'", db.Name, db.Permission)); err != nil {
return err return err
} }
if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop database if exists %s", db.Name)); err != nil { if err := excuteSql(app.ContainerName, app.Password, fmt.Sprintf("drop database if exists `%s`", db.Name)); err != nil {
return err return err
} }
@ -318,7 +319,7 @@ func (u *MysqlService) ChangePassword(info dto.ChangeDBInfo) error {
if err != nil { if err != nil {
return err return err
} }
if err := updateInstallInfoInDB(appModel.Key, appInstall.Name, "password", true, info.Value); err != nil { if err := updateInstallInfoInDB(appModel.Key, appInstall.Name, "user-password", true, info.Value); err != nil {
return err return err
} }
} }
@ -447,8 +448,8 @@ func (u *MysqlService) UpdateVariables(updatas []dto.MysqlVariablesUpdate) error
} }
} }
if _, ok := info.Value.(int64); ok { if _, ok := info.Value.(float64); ok {
files = updateMyCnf(files, group, info.Param, loadSizeUnit(info.Value.(int64))) files = updateMyCnf(files, group, info.Param, common.LoadSizeUnit(info.Value.(float64)))
} else { } else {
files = updateMyCnf(files, group, info.Param, info.Value) files = updateMyCnf(files, group, info.Param, info.Value)
} }
@ -685,13 +686,3 @@ func updateMyCnf(oldFiles []string, group string, param string, value interface{
} }
return newFiles return newFiles
} }
func loadSizeUnit(value int64) string {
if value > 1048576 {
return fmt.Sprintf("%dM", value/1048576)
}
if value > 1024 {
return fmt.Sprintf("%dK", value/1024)
}
return fmt.Sprintf("%d", value)
}

View file

@ -116,3 +116,13 @@ func RemoveRepeatElement(a interface{}) (ret []interface{}) {
} }
return ret return ret
} }
func LoadSizeUnit(value float64) string {
if value > 1048576 {
return fmt.Sprintf("%vM", value/1048576)
}
if value > 1024 {
return fmt.Sprintf("%vK", value/1024)
}
return fmt.Sprintf("%v", value)
}

View file

@ -72,7 +72,6 @@ func (sws *ExecWsSession) receiveWsMsg(ctx context.Context, exitCh chan bool) {
for { for {
_, wsData, err := wsConn.ReadMessage() _, wsData, err := wsConn.ReadMessage()
if err != nil { if err != nil {
global.LOG.Errorf("reading webSocket message failed, err: %v", err)
return return
} }
msgObj := wsMsg{} msgObj := wsMsg{}

View file

@ -124,7 +124,6 @@ func (sws *LogicSshWsSession) receiveWsMsg(exitCh chan bool) {
default: default:
_, wsData, err := wsConn.ReadMessage() _, wsData, err := wsConn.ReadMessage()
if err != nil { if err != nil {
global.LOG.Errorf("reading webSocket message failed, err: %v", err)
return return
} }
msgObj := wsMsg{} msgObj := wsMsg{}

View file

@ -18,9 +18,23 @@
v-model="mysqlConf" v-model="mysqlConf"
:readOnly="true" :readOnly="true"
/> />
<el-button style="margin-top: 10px" @click="getDefaultConfig()">
{{ $t('app.defaultConfig') }}
</el-button>
<el-button type="primary" style="margin-top: 10px" @click="onSaveConf"> <el-button type="primary" style="margin-top: 10px" @click="onSaveConf">
{{ $t('commons.button.save') }} {{ $t('commons.button.save') }}
</el-button> </el-button>
<el-row>
<el-col :span="8">
<el-alert
v-if="useOld"
style="margin-top: 10px"
:title="$t('app.defaultConfigHelper')"
type="info"
:closable="false"
></el-alert>
</el-col>
</el-row>
</el-collapse-item> </el-collapse-item>
<el-collapse-item <el-collapse-item
:disabled="mysqlStatus !== 'Running'" :disabled="mysqlStatus !== 'Running'"
@ -84,7 +98,7 @@ import { javascript } from '@codemirror/lang-javascript';
import { oneDark } from '@codemirror/theme-one-dark'; import { oneDark } from '@codemirror/theme-one-dark';
import { LoadFile } from '@/api/modules/files'; import { LoadFile } from '@/api/modules/files';
import { loadMysqlBaseInfo, loadMysqlVariables, updateMysqlConfByFile } from '@/api/modules/database'; import { loadMysqlBaseInfo, loadMysqlVariables, updateMysqlConfByFile } from '@/api/modules/database';
import { ChangePort } from '@/api/modules/app'; import { ChangePort, GetAppDefaultConfig } from '@/api/modules/app';
import { Rules } from '@/global/form-rules'; import { Rules } from '@/global/form-rules';
import i18n from '@/lang'; import i18n from '@/lang';
@ -103,6 +117,8 @@ const baseInfo = reactive({
const panelFormRef = ref<FormInstance>(); const panelFormRef = ref<FormInstance>();
const mysqlConf = ref(); const mysqlConf = ref();
const useOld = ref(false);
const statusRef = ref(); const statusRef = ref();
const variablesRef = ref(); const variablesRef = ref();
const slowLogRef = ref(); const slowLogRef = ref();
@ -182,6 +198,14 @@ function callback(error: any) {
} }
} }
const getDefaultConfig = async () => {
loading.value = true;
const res = await GetAppDefaultConfig('mysql');
mysqlConf.value = res.data;
useOld.value = true;
loading.value = false;
};
const onSubmitChangeConf = async () => { const onSubmitChangeConf = async () => {
let param = { let param = {
mysqlName: mysqlName.value, mysqlName: mysqlName.value,

View file

@ -18,9 +18,23 @@
v-model="redisConf" v-model="redisConf"
:readOnly="true" :readOnly="true"
/> />
<el-button style="margin-top: 10px" @click="getDefaultConfig()">
{{ $t('app.defaultConfig') }}
</el-button>
<el-button type="primary" @click="onSaveFile" style="margin-top: 5px"> <el-button type="primary" @click="onSaveFile" style="margin-top: 5px">
{{ $t('commons.button.save') }} {{ $t('commons.button.save') }}
</el-button> </el-button>
<el-row>
<el-col :span="8">
<el-alert
v-if="useOld"
style="margin-top: 10px"
:title="$t('app.defaultConfigHelper')"
type="info"
:closable="false"
></el-alert>
</el-col>
</el-row>
</el-collapse-item> </el-collapse-item>
<el-collapse-item :disabled="redisStatus !== 'Running'" :title="$t('database.status')" name="2"> <el-collapse-item :disabled="redisStatus !== 'Running'" :title="$t('database.status')" name="2">
<Status ref="statusRef" /> <Status ref="statusRef" />
@ -104,7 +118,7 @@ import Persistence from '@/views/database/redis/setting/persistence/index.vue';
import { loadRedisConf, updateRedisConf, updateRedisConfByFile } from '@/api/modules/database'; import { loadRedisConf, updateRedisConf, updateRedisConfByFile } from '@/api/modules/database';
import i18n from '@/lang'; import i18n from '@/lang';
import { Rules } from '@/global/form-rules'; import { Rules } from '@/global/form-rules';
import { ChangePort } from '@/api/modules/app'; import { ChangePort, GetAppDefaultConfig } from '@/api/modules/app';
const extensions = [javascript(), oneDark]; const extensions = [javascript(), oneDark];
@ -128,6 +142,8 @@ const activeName = ref('1');
const statusRef = ref(); const statusRef = ref();
const persistenceRef = ref(); const persistenceRef = ref();
const useOld = ref(false);
const redisStatus = ref(); const redisStatus = ref();
const redisName = ref(); const redisName = ref();
@ -233,6 +249,14 @@ const submtiForm = async () => {
}); });
}; };
const getDefaultConfig = async () => {
loading.value = true;
const res = await GetAppDefaultConfig('redis');
redisConf.value = res.data;
useOld.value = true;
loading.value = false;
};
const onSaveFile = async () => { const onSaveFile = async () => {
let params = { let params = {
header: i18n.global.t('database.confChange'), header: i18n.global.t('database.confChange'),