1Panel/backend/app/service/database_redis.go

277 lines
7.7 KiB
Go
Raw Normal View History

2022-10-31 09:26:15 +00:00
package service
import (
"encoding/json"
2022-11-03 15:42:42 +00:00
"errors"
2022-10-31 09:26:15 +00:00
"fmt"
"os"
2022-11-03 15:42:42 +00:00
"os/exec"
"path/filepath"
"strings"
2022-10-31 09:26:15 +00:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
2022-11-03 15:42:42 +00:00
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/compose"
2022-10-31 09:26:15 +00:00
_ "github.com/go-sql-driver/mysql"
)
type RedisService struct{}
type IRedisService interface {
UpdateConf(req dto.RedisConfUpdate) error
2022-11-03 15:42:42 +00:00
UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate) error
2022-12-12 06:06:39 +00:00
ChangePassword(info dto.ChangeDBInfo) error
LoadStatus() (*dto.RedisStatus, error)
LoadConf() (*dto.RedisConf, error)
LoadPersistenceConf() (*dto.RedisPersistence, error)
2022-10-31 09:26:15 +00:00
2022-11-03 15:42:42 +00:00
SearchBackupListWithPage(req dto.PageInfo) (int64, interface{}, error)
2022-10-31 09:26:15 +00:00
}
func NewIRedisService() IRedisService {
return &RedisService{}
}
2022-11-03 15:42:42 +00:00
func (u *RedisService) UpdateConf(req dto.RedisConfUpdate) error {
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", "")
2022-10-31 09:26:15 +00:00
if err != nil {
2022-11-03 15:42:42 +00:00
return err
}
2022-12-21 08:21:28 +00:00
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 {
2022-11-03 15:42:42 +00:00
return err
}
2022-12-21 08:21:28 +00:00
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
2022-11-03 15:42:42 +00:00
return err
2022-10-31 09:26:15 +00:00
}
2022-11-03 15:42:42 +00:00
return nil
}
2022-12-12 06:06:39 +00:00
func (u *RedisService) ChangePassword(req dto.ChangeDBInfo) error {
if err := updateInstallInfoInDB("redis", "", "password", true, req.Value); err != nil {
2022-12-12 06:06:39 +00:00
return err
}
if err := updateInstallInfoInDB("redis-commander", "", "password", true, req.Value); err != nil {
return err
2022-12-12 06:06:39 +00:00
}
return nil
}
2022-11-03 15:42:42 +00:00
func (u *RedisService) UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate) error {
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", "")
if err != nil {
return err
}
2022-11-03 15:42:42 +00:00
2022-12-21 08:21:28 +00:00
var confs []redisConfig
2022-11-03 15:42:42 +00:00
if req.Type == "rbd" {
2022-12-21 08:21:28 +00:00
confs = append(confs, redisConfig{key: "save", value: req.Save})
2022-11-03 15:42:42 +00:00
} else {
2022-12-21 08:21:28 +00:00
confs = append(confs, redisConfig{key: "appendonly", value: req.Appendonly})
confs = append(confs, redisConfig{key: "appendfsync", value: req.Appendfsync})
2022-11-03 15:42:42 +00:00
}
2022-12-21 08:21:28 +00:00
if err := confSet(redisInfo.Name, confs); err != nil {
return err
}
2022-12-21 08:21:28 +00:00
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
return err
}
return nil
}
func (u *RedisService) LoadStatus() (*dto.RedisStatus, error) {
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", "")
if err != nil {
return nil, err
}
2022-11-03 15:42:42 +00:00
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), "info")
cmd := exec.Command("docker", commands...)
stdout, err := cmd.CombinedOutput()
if err != nil {
2022-11-03 15:42:42 +00:00
return nil, errors.New(string(stdout))
}
2022-11-03 15:42:42 +00:00
rows := strings.Split(string(stdout), "\r\n")
rowMap := make(map[string]string)
for _, v := range rows {
itemRow := strings.Split(v, ":")
if len(itemRow) == 2 {
rowMap[itemRow[0]] = itemRow[1]
}
}
var info dto.RedisStatus
arr, err := json.Marshal(rowMap)
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
return &info, nil
}
func (u *RedisService) LoadConf() (*dto.RedisConf, error) {
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", "")
if err != nil {
return nil, err
}
2022-11-03 15:42:42 +00:00
var item dto.RedisConf
item.ContainerName = redisInfo.ContainerName
item.Name = redisInfo.Name
item.Port = redisInfo.Port
2022-12-12 06:06:39 +00:00
item.Requirepass = redisInfo.Password
item.Timeout, _ = configGetStr(redisInfo.ContainerName, redisInfo.Password, "timeout")
item.Maxclients, _ = configGetStr(redisInfo.ContainerName, redisInfo.Password, "maxclients")
item.Maxmemory, _ = configGetStr(redisInfo.ContainerName, redisInfo.Password, "maxmemory")
return &item, nil
}
func (u *RedisService) LoadPersistenceConf() (*dto.RedisPersistence, error) {
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", "")
if err != nil {
return nil, err
}
var item dto.RedisPersistence
2022-11-03 15:42:42 +00:00
if item.Appendonly, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "appendonly"); err != nil {
return nil, err
}
if item.Appendfsync, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "appendfsync"); err != nil {
return nil, err
}
if item.Save, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "save"); err != nil {
return nil, err
}
return &item, nil
}
2022-11-03 15:42:42 +00:00
func (u *RedisService) SearchBackupListWithPage(req dto.PageInfo) (int64, interface{}, error) {
var (
list []dto.DatabaseFileRecords
backDatas []dto.DatabaseFileRecords
2022-11-03 15:42:42 +00:00
)
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", "")
2022-11-03 15:42:42 +00:00
if err != nil {
return 0, nil, err
}
localDir, err := loadLocalDir()
2022-11-03 15:42:42 +00:00
if err != nil {
return 0, nil, err
}
backupDir := fmt.Sprintf("%s/database/redis/%s", localDir, redisInfo.Name)
_ = filepath.Walk(backupDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
2022-11-03 15:42:42 +00:00
if !info.IsDir() {
list = append(list, dto.DatabaseFileRecords{
2022-11-03 15:42:42 +00:00
CreatedAt: info.ModTime().Format("2006-01-02 15:04:05"),
Size: int(info.Size()),
FileDir: backupDir,
FileName: info.Name(),
})
}
return nil
})
total, start, end := len(list), (req.Page-1)*req.PageSize, req.Page*req.PageSize
if start > total {
backDatas = make([]dto.DatabaseFileRecords, 0)
2022-11-03 15:42:42 +00:00
} else {
if end >= total {
end = total
}
2022-11-03 15:42:42 +00:00
backDatas = list[start:end]
}
2022-11-03 15:42:42 +00:00
return int64(total), backDatas, nil
}
2022-11-03 15:42:42 +00:00
func configGetStr(containerName, password, param string) (string, error) {
commands := append(redisExec(containerName, password), []string{"config", "get", param}...)
cmd := exec.Command("docker", commands...)
stdout, err := cmd.CombinedOutput()
if err != nil {
return "", errors.New(string(stdout))
}
rows := strings.Split(string(stdout), "\r\n")
for _, v := range rows {
itemRow := strings.Split(v, "\n")
if len(itemRow) == 3 {
return itemRow[1], nil
}
}
2022-11-03 15:42:42 +00:00
return "", nil
}
2022-12-21 08:21:28 +00:00
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 := os.ReadFile(path)
2022-11-03 15:42:42 +00:00
if err != nil {
2022-12-21 08:21:28 +00:00
return err
}
files := strings.Split(string(lineBytes), "\n")
startIndex, endIndex := 0, 0
2022-12-21 08:21:28 +00:00
var newFiles []string
for i := 0; i < len(files); i++ {
if files[i] == "# Redis configuration rewrite by 1Panel" {
startIndex = i
}
if files[i] == "# End Redis configuration rewrite by 1Panel" {
endIndex = i
break
2022-12-21 08:21:28 +00:00
}
newFiles = append(newFiles, files[i])
}
for _, item := range changeConf {
isExist := false
for i := startIndex; i < endIndex; i++ {
if strings.HasPrefix(newFiles[i], item.key) || strings.HasPrefix(newFiles[i], "# "+item.key) {
if item.value == "0" || len(item.value) == 0 {
newFiles[i] = fmt.Sprintf("# %s %s", item.key, item.value)
} else {
newFiles[i] = fmt.Sprintf("%s %s", item.key, item.value)
2022-12-21 08:21:28 +00:00
}
isExist = true
break
2022-12-21 08:21:28 +00:00
}
}
if !isExist {
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))
2022-12-21 08:21:28 +00:00
}
}
}
newFiles = append(newFiles, "# End Redis configuration rewrite by 1Panel")
2022-12-21 08:21:28 +00:00
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
2022-11-03 15:42:42 +00:00
}
return nil
}
func redisExec(containerName, password string) []string {
cmds := []string{"exec", containerName, "redis-cli", "-a", password, "--no-auth-warning"}
if len(password) == 0 {
cmds = []string{"exec", containerName, "redis-cli"}
}
return cmds
}