2022-10-31 17:26:15 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2024-05-15 11:56:40 +08:00
|
|
|
"context"
|
2022-10-31 23:52:39 +08:00
|
|
|
"encoding/json"
|
2022-11-03 23:42:42 +08:00
|
|
|
"errors"
|
2022-10-31 17:26:15 +08:00
|
|
|
"fmt"
|
2022-11-02 18:30:22 +08:00
|
|
|
"os"
|
2022-11-03 23:42:42 +08:00
|
|
|
"os/exec"
|
2022-10-31 23:52:39 +08:00
|
|
|
"strings"
|
2022-10-31 17:26:15 +08:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-11-03 23:42:42 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/compose"
|
2024-05-15 11:56:40 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2024-05-30 15:13:21 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
2024-05-15 11:56:40 +08:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2022-10-31 17:26:15 +08:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RedisService struct{}
|
|
|
|
|
|
|
|
type IRedisService interface {
|
2022-10-31 23:52:39 +08:00
|
|
|
UpdateConf(req dto.RedisConfUpdate) error
|
2022-11-03 23:42:42 +08:00
|
|
|
UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate) error
|
2023-09-13 14:10:11 +08:00
|
|
|
ChangePassword(info dto.ChangeRedisPass) error
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2024-05-13 22:40:31 +08:00
|
|
|
LoadStatus(req dto.OperationWithName) (*dto.RedisStatus, error)
|
|
|
|
LoadConf(req dto.OperationWithName) (*dto.RedisConf, error)
|
|
|
|
LoadPersistenceConf(req dto.OperationWithName) (*dto.RedisPersistence, error)
|
2024-05-15 11:56:40 +08:00
|
|
|
|
|
|
|
CheckHasCli() bool
|
|
|
|
InstallCli() error
|
2022-10-31 17:26:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIRedisService() IRedisService {
|
|
|
|
return &RedisService{}
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:42:42 +08:00
|
|
|
func (u *RedisService) UpdateConf(req dto.RedisConfUpdate) error {
|
2024-05-13 22:40:31 +08:00
|
|
|
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", req.Database)
|
2022-10-31 17:26:15 +08:00
|
|
|
if err != nil {
|
2022-11-03 23:42:42 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-12-21 16:21:28 +08: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})
|
2024-02-01 18:04:02 +08:00
|
|
|
if err := confSet(redisInfo.Name, "", confs); err != nil {
|
2022-11-03 23:42:42 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-12-21 16:21:28 +08:00
|
|
|
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
|
2022-11-03 23:42:42 +08:00
|
|
|
return err
|
2022-10-31 17:26:15 +08:00
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2022-11-03 23:42:42 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 11:56:40 +08:00
|
|
|
func (u *RedisService) CheckHasCli() bool {
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
containerLists, err := client.ContainerList(context.Background(), container.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, item := range containerLists {
|
|
|
|
if strings.ReplaceAll(item.Names[0], "/", "") == "1Panel-redis-cli-tools" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *RedisService) InstallCli() error {
|
|
|
|
item := dto.ContainerOperate{
|
|
|
|
Name: "1Panel-redis-cli-tools",
|
|
|
|
Image: "redis:7.2.4",
|
|
|
|
Network: "1panel-network",
|
|
|
|
}
|
|
|
|
return NewIContainerService().ContainerCreate(item)
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:10:11 +08:00
|
|
|
func (u *RedisService) ChangePassword(req dto.ChangeRedisPass) error {
|
2024-05-13 22:40:31 +08:00
|
|
|
if err := updateInstallInfoInDB("redis", req.Database, "password", req.Value); err != nil {
|
2022-12-12 14:06:39 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-05-30 15:13:21 +08:00
|
|
|
remote, err := databaseRepo.Get(commonRepo.WithByName(req.Database))
|
|
|
|
if err != nil {
|
2022-12-12 19:04:45 +08:00
|
|
|
return err
|
2022-12-12 14:06:39 +08:00
|
|
|
}
|
2024-05-30 15:13:21 +08:00
|
|
|
if remote.From == "local" {
|
|
|
|
pass, err := encrypt.StringEncrypt(req.Value)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decrypt database password failed, err: %v", err)
|
|
|
|
}
|
|
|
|
_ = databaseRepo.Update(remote.ID, map[string]interface{}{"password": pass})
|
|
|
|
}
|
2022-12-12 14:06:39 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:42:42 +08:00
|
|
|
func (u *RedisService) UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate) error {
|
2024-05-13 22:40:31 +08:00
|
|
|
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", req.Database)
|
2022-11-02 18:30:22 +08:00
|
|
|
if err != nil {
|
2022-10-31 23:52:39 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
|
2022-12-21 16:21:28 +08:00
|
|
|
var confs []redisConfig
|
2022-11-03 23:42:42 +08:00
|
|
|
if req.Type == "rbd" {
|
2022-12-21 16:21:28 +08:00
|
|
|
confs = append(confs, redisConfig{key: "save", value: req.Save})
|
2022-11-03 23:42:42 +08:00
|
|
|
} else {
|
2022-12-21 16:21:28 +08:00
|
|
|
confs = append(confs, redisConfig{key: "appendonly", value: req.Appendonly})
|
|
|
|
confs = append(confs, redisConfig{key: "appendfsync", value: req.Appendfsync})
|
2022-11-03 23:42:42 +08:00
|
|
|
}
|
2024-02-01 18:04:02 +08:00
|
|
|
if err := confSet(redisInfo.Name, req.Type, confs); err != nil {
|
2022-12-21 16:21:28 +08:00
|
|
|
return err
|
2022-11-02 18:30:22 +08:00
|
|
|
}
|
2022-12-21 16:21:28 +08:00
|
|
|
if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:40:31 +08:00
|
|
|
func (u *RedisService) LoadStatus(req dto.OperationWithName) (*dto.RedisStatus, error) {
|
|
|
|
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", req.Name)
|
2022-10-31 23:52:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), "info")
|
|
|
|
cmd := exec.Command("docker", commands...)
|
|
|
|
stdout, err := cmd.CombinedOutput()
|
2022-10-31 23:52:39 +08:00
|
|
|
if err != nil {
|
2022-11-03 23:42:42 +08:00
|
|
|
return nil, errors.New(string(stdout))
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
rows := strings.Split(string(stdout), "\r\n")
|
2022-10-31 23:52:39 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-13 22:40:31 +08:00
|
|
|
func (u *RedisService) LoadConf(req dto.OperationWithName) (*dto.RedisConf, error) {
|
|
|
|
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", req.Name)
|
2022-10-31 23:52:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
|
2022-10-31 23:52:39 +08:00
|
|
|
var item dto.RedisConf
|
2022-11-02 16:28:54 +08:00
|
|
|
item.ContainerName = redisInfo.ContainerName
|
|
|
|
item.Name = redisInfo.Name
|
2022-11-08 14:34:41 +08:00
|
|
|
item.Port = redisInfo.Port
|
2022-12-12 14:06:39 +08: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")
|
2022-11-02 16:28:54 +08:00
|
|
|
return &item, nil
|
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2024-05-13 22:40:31 +08:00
|
|
|
func (u *RedisService) LoadPersistenceConf(req dto.OperationWithName) (*dto.RedisPersistence, error) {
|
|
|
|
redisInfo, err := appInstallRepo.LoadBaseInfo("redis", req.Name)
|
2022-11-02 16:28:54 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var item dto.RedisPersistence
|
2022-11-03 23:42:42 +08: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
|
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
return &item, nil
|
|
|
|
}
|
|
|
|
|
2022-11-03 23:42:42 +08: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-02 18:30:22 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
return "", nil
|
|
|
|
}
|
2022-12-21 16:21:28 +08:00
|
|
|
|
|
|
|
type redisConfig struct {
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
2024-02-01 18:04:02 +08:00
|
|
|
func confSet(redisName string, updateType string, changeConf []redisConfig) error {
|
2022-12-21 16:21:28 +08:00
|
|
|
path := fmt.Sprintf("%s/redis/%s/conf/redis.conf", constant.AppInstallDir, redisName)
|
2023-04-07 11:30:10 +08:00
|
|
|
lineBytes, err := os.ReadFile(path)
|
2022-11-03 23:42:42 +08:00
|
|
|
if err != nil {
|
2022-12-21 16:21:28 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
files := strings.Split(string(lineBytes), "\n")
|
|
|
|
|
2024-02-01 18:04:02 +08:00
|
|
|
startIndex, endIndex, emptyLine := 0, 0, 0
|
2022-12-21 16:21:28 +08:00
|
|
|
var newFiles []string
|
2023-03-07 11:18:06 +08:00
|
|
|
for i := 0; i < len(files); i++ {
|
|
|
|
if files[i] == "# Redis configuration rewrite by 1Panel" {
|
|
|
|
startIndex = i
|
2024-02-01 18:04:02 +08:00
|
|
|
newFiles = append(newFiles, files[i])
|
|
|
|
continue
|
2023-03-07 11:18:06 +08:00
|
|
|
}
|
|
|
|
if files[i] == "# End Redis configuration rewrite by 1Panel" {
|
|
|
|
endIndex = i
|
|
|
|
break
|
2022-12-21 16:21:28 +08:00
|
|
|
}
|
2024-02-01 18:04:02 +08:00
|
|
|
if startIndex == 0 && strings.HasPrefix(files[i], "save ") {
|
|
|
|
newFiles = append(newFiles, "# "+files[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if startIndex != 0 && endIndex == 0 && (len(files[i]) == 0 || (updateType == "rbd" && strings.HasPrefix(files[i], "save "))) {
|
|
|
|
emptyLine++
|
|
|
|
continue
|
|
|
|
}
|
2023-03-07 11:18:06 +08:00
|
|
|
newFiles = append(newFiles, files[i])
|
|
|
|
}
|
2024-02-01 18:04:02 +08:00
|
|
|
endIndex = endIndex - emptyLine
|
2023-03-07 11:18:06 +08:00
|
|
|
for _, item := range changeConf {
|
2024-02-01 18:04:02 +08:00
|
|
|
if item.key == "save" {
|
|
|
|
saveVal := strings.Split(item.value, ",")
|
|
|
|
for i := 0; i < len(saveVal); i++ {
|
|
|
|
newFiles = append(newFiles, "save "+saveVal[i])
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-07 11:18:06 +08:00
|
|
|
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 16:21:28 +08:00
|
|
|
}
|
2023-03-07 11:18:06 +08:00
|
|
|
isExist = true
|
|
|
|
break
|
2022-12-21 16:21:28 +08:00
|
|
|
}
|
2023-03-07 11:18:06 +08: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 16:21:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-07 11:18:06 +08:00
|
|
|
newFiles = append(newFiles, "# End Redis configuration rewrite by 1Panel")
|
|
|
|
|
2022-12-21 16:21:28 +08: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 23:42:42 +08: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
|
2022-11-02 18:30:22 +08:00
|
|
|
}
|