github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/oceanbase/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 oceanbase 21 22 import ( 23 "fmt" 24 "strconv" 25 "strings" 26 27 corev1 "k8s.io/api/core/v1" 28 29 "github.com/1aal/kubeblocks/pkg/lorry/engines" 30 "github.com/1aal/kubeblocks/pkg/lorry/engines/models" 31 ) 32 33 var _ engines.ClusterCommands = &Commands{} 34 35 type Commands struct { 36 info engines.EngineInfo 37 examples map[models.ClientType]engines.BuildConnectExample 38 } 39 40 func NewCommands() engines.ClusterCommands { 41 return &Commands{ 42 info: engines.EngineInfo{ 43 Client: "mysql", 44 }, 45 examples: map[models.ClientType]engines.BuildConnectExample{ 46 models.CLI: func(info *engines.ConnectionInfo) string { 47 return fmt.Sprintf(`# oceanbase client connection example 48 mysql -h %s -P %s -u %s 49 `, info.Host, info.Port, info.User) 50 }, 51 }, 52 } 53 } 54 55 func (r *Commands) ConnectCommand(connectInfo *engines.AuthInfo) []string { 56 userName := "root" 57 userPass := "" 58 59 if connectInfo != nil { 60 userName = connectInfo.UserName 61 userPass = connectInfo.UserPasswd 62 } 63 64 var obCmd []string 65 66 if userPass != "" { 67 obCmd = []string{fmt.Sprintf("%s -h127.0.0.1 -P2881 -u%s -A -p%s", r.info.Client, userName, userPass)} 68 } else { 69 obCmd = []string{fmt.Sprintf("%s -h127.0.0.1 -P2881 -u%s -A", r.info.Client, userName)} 70 } 71 72 return []string{"bash", "-c", strings.Join(obCmd, " ")} 73 } 74 75 func (r *Commands) Container() string { 76 return r.info.Container 77 } 78 79 func (r *Commands) ConnectExample(info *engines.ConnectionInfo, client string) string { 80 return engines.BuildExample(info, client, r.examples) 81 } 82 83 func (r *Commands) ExecuteCommand(scripts []string) ([]string, []corev1.EnvVar, error) { 84 cmd := []string{} 85 cmd = append(cmd, "/bin/bash", "-c", "-ex") 86 if engines.EnvVarMap[engines.PASSWORD] == "" { 87 cmd = append(cmd, fmt.Sprintf("%s -h127.0.0.1 -P2881 -u%s -e %s", r.info.Client, engines.EnvVarMap[engines.USER], strconv.Quote(strings.Join(scripts, " ")))) 88 } else { 89 cmd = append(cmd, fmt.Sprintf("%s -h127.0.0.1 -P2881 -u%s -p%s -e %s", r.info.Client, 90 fmt.Sprintf("$%s", engines.EnvVarMap[engines.USER]), 91 fmt.Sprintf("$%s", engines.EnvVarMap[engines.PASSWORD]), 92 strconv.Quote(strings.Join(scripts, " ")))) 93 } 94 95 return cmd, nil, nil 96 }