github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/redis/commands.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package redis 21 22 import ( 23 "fmt" 24 "strings" 25 26 corev1 "k8s.io/api/core/v1" 27 28 "github.com/1aal/kubeblocks/pkg/lorry/engines" 29 "github.com/1aal/kubeblocks/pkg/lorry/engines/models" 30 ) 31 32 var _ engines.ClusterCommands = &Commands{} 33 34 type Commands struct { 35 info engines.EngineInfo 36 examples map[models.ClientType]engines.BuildConnectExample 37 } 38 39 func NewCommands() engines.ClusterCommands { 40 return &Commands{ 41 info: engines.EngineInfo{ 42 Client: "redis-cli", 43 Container: "redis", 44 }, 45 examples: map[models.ClientType]engines.BuildConnectExample{ 46 models.CLI: func(info *engines.ConnectionInfo) string { 47 return fmt.Sprintf(`# redis client connection example 48 redis-cli -h %s -p %s 49 `, info.Host, info.Port) 50 }, 51 }, 52 } 53 } 54 55 func (r Commands) ConnectCommand(connectInfo *engines.AuthInfo) []string { 56 redisCmd := []string{ 57 "redis-cli", 58 } 59 60 if connectInfo != nil { 61 redisCmd = append(redisCmd, "--user", connectInfo.UserName) 62 redisCmd = append(redisCmd, "--pass", connectInfo.UserPasswd) 63 } 64 return []string{"sh", "-c", strings.Join(redisCmd, " ")} 65 } 66 67 func (r Commands) Container() string { 68 return r.info.Container 69 } 70 71 func (r Commands) ConnectExample(info *engines.ConnectionInfo, client string) string { 72 return engines.BuildExample(info, client, r.examples) 73 } 74 75 func (r Commands) ExecuteCommand(scripts []string) ([]string, []corev1.EnvVar, error) { 76 cmd := []string{} 77 args := []string{} 78 cmd = append(cmd, "/bin/sh", "-c") 79 for _, script := range scripts { 80 args = append(args, fmt.Sprintf("%s -h %s -p 6379 --user %s --pass %s %s", r.info.Client, 81 fmt.Sprintf("$%s", engines.EnvVarMap[engines.HOST]), 82 fmt.Sprintf("$%s", engines.EnvVarMap[engines.USER]), 83 fmt.Sprintf("$%s", engines.EnvVarMap[engines.PASSWORD]), script)) 84 } 85 cmd = append(cmd, strings.Join(args, " && ")) 86 return cmd, nil, nil 87 }