2022-10-31 17:26:15 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-11-02 16:28:54 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2022-10-31 17:26:15 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-11-02 16:28:54 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/terminal"
|
|
|
|
"github.com/docker/docker/api/types"
|
2022-10-31 17:26:15 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-11-02 16:28:54 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-10-31 17:26:15 +08:00
|
|
|
)
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (b *BaseApi) LoadRedisStatus(c *gin.Context) {
|
|
|
|
data, err := redisService.LoadStatus()
|
|
|
|
if err != nil {
|
2022-10-31 17:26:15 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
|
|
|
|
helper.SuccessWithData(c, data)
|
2022-10-31 17:26:15 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (b *BaseApi) LoadRedisConf(c *gin.Context) {
|
|
|
|
data, err := redisService.LoadConf()
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
2022-10-31 17:26:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
helper.SuccessWithData(c, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) LoadPersistenceConf(c *gin.Context) {
|
|
|
|
data, err := redisService.LoadPersistenceConf()
|
2022-10-31 17:26:15 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
helper.SuccessWithData(c, data)
|
2022-10-31 17:26:15 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (b *BaseApi) UpdateRedisConf(c *gin.Context) {
|
|
|
|
var req dto.RedisConfUpdate
|
2022-10-31 17:26:15 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
if err := redisService.UpdateConf(req); err != nil {
|
2022-10-31 17:26:15 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
func (b *BaseApi) RedisExec(c *gin.Context) {
|
|
|
|
redisConf, err := redisService.LoadConf()
|
2022-10-31 23:52:39 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
cols, err := strconv.Atoi(c.DefaultQuery("cols", "80"))
|
|
|
|
if err != nil {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
rows, err := strconv.Atoi(c.DefaultQuery("rows", "40"))
|
|
|
|
if err != nil {
|
2022-10-31 23:52:39 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
wsConn, err := upGrader.Upgrade(c.Writer, c.Request, nil)
|
2022-10-31 23:52:39 +08:00
|
|
|
if err != nil {
|
2022-11-02 16:28:54 +08:00
|
|
|
global.LOG.Errorf("gin context http handler failed, err: %v", err)
|
2022-10-31 23:52:39 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
defer wsConn.Close()
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if wshandleError(wsConn, errors.WithMessage(err, "New docker client failed.")) {
|
2022-10-31 17:26:15 +08:00
|
|
|
return
|
|
|
|
}
|
2022-10-31 23:52:39 +08:00
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
auth := "redis-cli"
|
|
|
|
if len(redisConf.Requirepass) != 0 {
|
|
|
|
auth = fmt.Sprintf("redis-cli -a %s --no-auth-warning", redisConf.Requirepass)
|
2022-10-31 23:52:39 +08:00
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
conf := types.ExecConfig{Tty: true, Cmd: []string{"bash"}, AttachStderr: true, AttachStdin: true, AttachStdout: true, User: "root"}
|
|
|
|
ir, err := client.ContainerExecCreate(context.TODO(), redisConf.ContainerName, conf)
|
|
|
|
if wshandleError(wsConn, errors.WithMessage(err, "failed to set exec conf.")) {
|
2022-10-31 23:52:39 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
hr, err := client.ContainerExecAttach(c, ir.ID, types.ExecStartCheck{Detach: false, Tty: true})
|
|
|
|
if wshandleError(wsConn, errors.WithMessage(err, "failed to set up the connection.")) {
|
2022-10-31 17:26:15 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-02 16:28:54 +08:00
|
|
|
defer hr.Close()
|
2022-10-31 17:26:15 +08:00
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
sws, err := terminal.NewExecConn(cols, rows, wsConn, hr.Conn, auth)
|
|
|
|
if wshandleError(wsConn, err) {
|
2022-10-31 23:52:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-02 16:28:54 +08:00
|
|
|
quitChan := make(chan bool, 3)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
sws.Start(ctx, quitChan)
|
|
|
|
<-quitChan
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if wshandleError(wsConn, err) {
|
2022-10-31 17:26:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|