2022-10-31 17:26:15 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
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-03 23:42:42 +08:00
|
|
|
"io/ioutil"
|
2022-11-02 18:30:22 +08:00
|
|
|
"os"
|
2022-11-03 23:42:42 +08:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2022-10-31 23:52:39 +08:00
|
|
|
"strings"
|
2022-11-03 23:42:42 +08:00
|
|
|
"time"
|
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"
|
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
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
LoadStatus() (*dto.RedisStatus, error)
|
|
|
|
LoadConf() (*dto.RedisConf, error)
|
|
|
|
LoadPersistenceConf() (*dto.RedisPersistence, error)
|
2022-10-31 17:26:15 +08:00
|
|
|
|
2022-11-03 23:42:42 +08:00
|
|
|
Backup() error
|
|
|
|
SearchBackupListWithPage(req dto.PageInfo) (int64, interface{}, error)
|
|
|
|
Recover(req dto.RedisBackupRecover) 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 {
|
2022-11-02 16:28:54 +08:00
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
2022-10-31 17:26:15 +08:00
|
|
|
if err != nil {
|
2022-11-03 23:42:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "timeout", req.Timeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "maxclients", req.Maxclients); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mysqlRepo.UpdateDatabasePassword(redisInfo.ID, map[string]interface{}{
|
|
|
|
"param": strings.ReplaceAll(redisInfo.Param, redisInfo.Password, req.Requirepass),
|
|
|
|
"env": strings.ReplaceAll(redisInfo.Env, redisInfo.Password, req.Requirepass),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "requirepass", req.Requirepass); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "maxmemory", req.Maxmemory); err != nil {
|
|
|
|
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
|
|
|
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), []string{"config", "rewrite"}...)
|
|
|
|
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 errors.New(string(stdout))
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *RedisService) UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate) error {
|
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
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
|
|
|
|
|
|
|
if req.Type == "rbd" {
|
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "save", req.Save); err != nil {
|
|
|
|
return err
|
2022-11-02 18:30:22 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
} else {
|
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "appendonly", req.Appendonly); err != nil {
|
|
|
|
return err
|
2022-11-02 18:30:22 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
if err := configSetStr(redisInfo.ContainerName, redisInfo.Password, "appendfsync", req.Appendfsync); err != nil {
|
|
|
|
return err
|
2022-11-02 18:30:22 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
}
|
|
|
|
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))
|
2022-11-02 18:30:22 +08:00
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (u *RedisService) LoadStatus() (*dto.RedisStatus, error) {
|
2022-11-03 23:42:42 +08:00
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (u *RedisService) LoadConf() (*dto.RedisConf, error) {
|
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
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-03 23:42:42 +08:00
|
|
|
if item.Timeout, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "timeout"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if item.Maxclients, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "maxclients"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if item.Requirepass, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "requirepass"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if item.Maxmemory, err = configGetStr(redisInfo.ContainerName, redisInfo.Password, "maxmemory"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
return &item, nil
|
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (u *RedisService) LoadPersistenceConf() (*dto.RedisPersistence, error) {
|
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
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 (u *RedisService) Backup() error {
|
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
commands := append(redisExec(redisInfo.ContainerName, redisInfo.Password), "save")
|
|
|
|
cmd := exec.Command("docker", commands...)
|
|
|
|
if stdout, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
name := fmt.Sprintf("%s.rdb", time.Now().Format("20060102150405"))
|
|
|
|
|
|
|
|
backupLocal, err := backupRepo.Get(commonRepo.WithByType("LOCAL"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
localDir, err := loadLocalDir(backupLocal)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
backupDir := fmt.Sprintf("database/redis/%s/", redisInfo.Name)
|
|
|
|
fullDir := fmt.Sprintf("%s/%s", localDir, backupDir)
|
|
|
|
if _, err := os.Stat(fullDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(fullDir, os.ModePerm); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("mkdir %s failed, err: %v", fullDir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd2 := exec.Command("docker", "cp", fmt.Sprintf("%s:/data/dump.rdb", redisInfo.ContainerName), fmt.Sprintf("%s/%s", fullDir, name))
|
|
|
|
if stdout, err := cmd2.CombinedOutput(); err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *RedisService) Recover(req dto.RedisBackupRecover) error {
|
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
composeDir := fmt.Sprintf("%s/redis/%s", constant.AppInstallDir, redisInfo.Name)
|
|
|
|
if _, err := compose.Down(composeDir + "/docker-compose.yml"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fullName := fmt.Sprintf("%s/%s", req.FileDir, req.FileName)
|
|
|
|
input, err := ioutil.ReadFile(fullName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(composeDir+"/data/dump.rdb", input, 0640); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := compose.Up(composeDir + "/docker-compose.yml"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *RedisService) SearchBackupListWithPage(req dto.PageInfo) (int64, interface{}, error) {
|
|
|
|
var (
|
|
|
|
list []dto.RedisBackupRecords
|
|
|
|
backDatas []dto.RedisBackupRecords
|
|
|
|
)
|
|
|
|
redisInfo, err := mysqlRepo.LoadRedisBaseInfo()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
backupLocal, err := backupRepo.Get(commonRepo.WithByType("LOCAL"))
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
localDir, err := loadLocalDir(backupLocal)
|
|
|
|
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 !info.IsDir() {
|
|
|
|
list = append(list, dto.RedisBackupRecords{
|
|
|
|
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.RedisBackupRecords, 0)
|
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
backDatas = list[start:end]
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-03 23:42:42 +08:00
|
|
|
return int64(total), backDatas, nil
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-02 18:30:22 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|