fix: redis 配置更新方式修改

pull/81/head
ssongliu 2022-12-21 16:21:28 +08:00 committed by ssongliu
parent e13bb924d6
commit 94ebe4952a
6 changed files with 137 additions and 91 deletions

View File

@ -1,3 +1,12 @@
# Redis configuration rewrite by 1Panel
timeout 0
# maxclients 10000
# maxmemory <bytes>
save 3600 1 300 100 60 10000
appendonly no
appendfsync everysec
# End Redis configuration rewrite by 1Panel
# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
@ -156,7 +165,7 @@ tcp-backlog 511
# unixsocketperm 700
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# timeout 0
# TCP keepalive.
#
@ -1375,7 +1384,7 @@ disable-thp yes
#
# Please check https://redis.io/topics/persistence for more information.
appendonly no
# appendonly no
# The base name of the append only file.
#
@ -1434,7 +1443,7 @@ appenddirname "appendonlydir"
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec
# appendfsync everysec
# appendfsync no
# When the AOF fsync policy is set to always or everysec, and a background

View File

@ -1,3 +1,12 @@
# Redis configuration rewrite by 1Panel
timeout 0
# maxclients 10000
# maxmemory <bytes>
save 3600 1 300 100 60 10000
appendonly no
appendfsync everysec
# End Redis configuration rewrite by 1Panel
# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
@ -156,7 +165,7 @@ tcp-backlog 511
# unixsocketperm 700
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# timeout 0
# TCP keepalive.
#
@ -430,7 +439,7 @@ proc-title-template "{title} {listen-addr} {server-mode}"
#
# You can set these explicitly by uncommenting the following line.
#
save 3600 1 300 100 60 10000
# save 3600 1 300 100 60 10000
# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
@ -1376,7 +1385,7 @@ disable-thp yes
#
# Please check https://redis.io/topics/persistence for more information.
appendonly no
# appendonly no
# The base name of the append only file.
#
@ -1435,7 +1444,7 @@ appenddirname "appendonlydir"
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec
# appendfsync everysec
# appendfsync no
# When the AOF fsync policy is set to always or everysec, and a background

View File

@ -436,9 +436,9 @@ func (u *MysqlService) UpdateVariables(updatas []dto.MysqlVariablesUpdate) error
lineBytes, err := ioutil.ReadFile(path)
if err != nil {
return err
} else {
files = strings.Split(string(lineBytes), "\n")
}
files = strings.Split(string(lineBytes), "\n")
group := "[mysqld]"
for _, info := range updatas {
if app.Version != "5.7.39" {

View File

@ -42,23 +42,18 @@ func (u *RedisService) UpdateConf(req dto.RedisConfUpdate) error {
if err != nil {
return err
}
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "timeout", req.Timeout); err != nil {
var confs []redisConfig
confs = append(confs, redisConfig{key: "timeout", value: req.Timeout})
confs = append(confs, redisConfig{key: "maxclients", value: req.Maxclients})
confs = append(confs, redisConfig{key: "maxmemory", value: req.Maxmemory})
if err := confSet(redisInfo.Name, confs); err != nil {
return err
}
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "maxclients", req.Maxclients); err != nil {
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
return err
}
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "maxmemory", req.Maxmemory); err != nil {
return err
}
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), []string{"config", "rewrite"}...)
cmd := exec.Command("docker", commands...)
stdout, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(stdout))
}
return nil
}
@ -79,24 +74,20 @@ func (u *RedisService) UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate)
return err
}
var confs []redisConfig
if req.Type == "rbd" {
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "save", req.Save); err != nil {
return err
}
confs = append(confs, redisConfig{key: "save", value: req.Save})
} else {
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "appendonly", req.Appendonly); err != nil {
return err
}
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "appendfsync", req.Appendfsync); err != nil {
return err
}
confs = append(confs, redisConfig{key: "appendonly", value: req.Appendonly})
confs = append(confs, redisConfig{key: "appendfsync", value: req.Appendfsync})
}
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), []string{"config", "rewrite"}...)
cmd := exec.Command("docker", commands...)
stdout, err := cmd.CombinedOutput()
if err != nil {
return errors.New(string(stdout))
if err := confSet(redisInfo.Name, confs); err != nil {
return err
}
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
return err
}
return nil
}
@ -150,7 +141,6 @@ func (u *RedisService) LoadPersistenceConf() (*dto.RedisPersistence, error) {
if err != nil {
return nil, err
}
var item dto.RedisPersistence
if item.Appendonly, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "appendonly"); err != nil {
return nil, err
@ -301,12 +291,63 @@ func configGetStr(containerName, password, param string) (string, error) {
}
return "", nil
}
func configSetStr(containerName, password, param, value string) error {
commands := append(redisExec(containerName, password), []string{"config", "set", param, value}...)
cmd := exec.Command("docker", commands...)
stdout, err := cmd.CombinedOutput()
type redisConfig struct {
key string
value string
}
func confSet(redisName string, changeConf []redisConfig) error {
path := fmt.Sprintf("%s/redis/%s/conf/redis.conf", constant.AppInstallDir, redisName)
lineBytes, err := ioutil.ReadFile(path)
if err != nil {
return errors.New(string(stdout))
return err
}
files := strings.Split(string(lineBytes), "\n")
isStartRange := false
isEndRange := false
var newFiles []string
for _, line := range files {
if !isStartRange {
if line == "# Redis configuration rewrite by 1Panel" {
isStartRange = true
}
newFiles = append(newFiles, line)
continue
}
if !isEndRange {
isExist := false
for _, item := range changeConf {
if strings.HasPrefix(line, item.key) || strings.HasPrefix(line, "# "+item.key) {
if item.value == "0" || len(item.value) == 0 {
newFiles = append(newFiles, fmt.Sprintf("# %s %s", item.key, item.value))
} else {
newFiles = append(newFiles, fmt.Sprintf("%s %s", item.key, item.value))
}
isExist = true
break
}
}
if isExist {
continue
}
newFiles = append(newFiles, line)
if line == "# End Redis configuration rewrite by 1Panel" {
isEndRange = true
}
continue
}
newFiles = append(newFiles, line)
}
file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(strings.Join(newFiles, "\n"))
if err != nil {
return err
}
return nil
}

View File

@ -37,11 +37,11 @@ type daemonJsonItem struct {
func (u *DockerService) LoadDockerStatus() string {
status := constant.StatusRunning
// cmd := exec.Command("systemctl", "is-active", "docker")
// stdout, err := cmd.CombinedOutput()
// if string(stdout) != "active\n" || err != nil {
// status = constant.Stopped
// }
cmd := exec.Command("systemctl", "is-active", "docker")
stdout, err := cmd.CombinedOutput()
if string(stdout) != "active\n" || err != nil {
status = constant.Stopped
}
return status
}

View File

@ -46,7 +46,7 @@
<span class="input-help">{{ $t('database.maxmemoryHelper') }}</span>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submtiForm(formRef)">
<el-button type="primary" @click="onSubmtiForm(formRef)">
{{ $t('commons.button.save') }}
</el-button>
</el-form-item>
@ -150,6 +150,7 @@ const acceptParams = (prop: DialogProps): void => {
if (redisStatus.value === 'Running') {
statusRef.value!.acceptParams({ status: prop.status });
persistenceRef.value!.acceptParams({ status: prop.status });
loadform();
}
};
const onClose = (): void => {
@ -201,43 +202,36 @@ const onChangePort = async (formEl: FormInstance | undefined) => {
});
};
// const onChangePassword = async () => {
// loading.value = true;
// let param = {
// id: 0,
// value: form.requirepass,
// };
// changeRedisPassword(param)
// .then(() => {
// loading.value = false;
// ElMessage.success(i18n.global.t('commons.msg.operationSuccess'));
// })
// .finally(() => {
// loading.value = false;
// });
// };
const submtiForm = async (formEl: FormInstance | undefined) => {
const confirmFormRef = ref();
const onSubmtiForm = async (formEl: FormInstance | undefined) => {
if (!formEl) return;
formEl.validate(async (valid) => {
if (!valid) return;
let param = {
timeout: form.timeout + '',
maxclients: form.maxclients + '',
maxmemory: form.maxmemory + '',
let params = {
header: i18n.global.t('database.confChange'),
operationInfo: i18n.global.t('database.restartNowHelper'),
submitInputInfo: i18n.global.t('database.restartNow'),
};
loading.value = true;
await updateRedisConf(param)
.then(() => {
loadform();
loading.value = false;
ElMessage.success(i18n.global.t('commons.msg.operationSuccess'));
})
.finally(() => {
loading.value = false;
});
confirmFormRef.value!.acceptParams(params);
});
};
const submtiForm = async () => {
let param = {
timeout: form.timeout + '',
maxclients: form.maxclients + '',
maxmemory: form.maxmemory + '',
};
loading.value = true;
await updateRedisConf(param)
.then(() => {
loadform();
loading.value = false;
ElMessage.success(i18n.global.t('commons.msg.operationSuccess'));
})
.finally(() => {
loading.value = false;
});
};
const onSaveFile = async () => {
let params = {
@ -264,19 +258,12 @@ const submtiFile = async () => {
};
const loadform = async () => {
loading.value = true;
await loadRedisConf()
.then((res) => {
loading.value = false;
form.name = res.data?.name;
form.timeout = Number(res.data?.timeout);
form.maxclients = Number(res.data?.maxclients);
form.maxmemory = Number(res.data?.maxmemory);
form.port = Number(res.data?.port);
})
.catch(() => {
loading.value = false;
});
const res = await loadRedisConf();
form.name = res.data?.name;
form.timeout = Number(res.data?.timeout);
form.maxclients = Number(res.data?.maxclients);
form.maxmemory = Number(res.data?.maxmemory);
form.port = Number(res.data?.port);
};
const loadConfFile = async () => {