github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mongodb/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 mongodb 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: "mongosh", 43 Container: "mongodb", 44 UserEnv: "$MONGODB_ROOT_USER", 45 PasswordEnv: "$MONGODB_ROOT_PASSWORD", 46 Database: "admin", 47 }, 48 examples: map[models.ClientType]engines.BuildConnectExample{ 49 models.CLI: func(info *engines.ConnectionInfo) string { 50 return fmt.Sprintf(`# mongodb client connection example 51 mongosh mongodb://%s:%s@%s/%s 52 `, info.User, info.Password, info.Host, info.Database) 53 }, 54 }, 55 } 56 } 57 58 func (r Commands) ConnectCommand(connectInfo *engines.AuthInfo) []string { 59 userName := r.info.UserEnv 60 userPass := r.info.PasswordEnv 61 62 if connectInfo != nil { 63 userName = connectInfo.UserName 64 userPass = connectInfo.UserPasswd 65 } 66 67 mongodbCmd := []string{fmt.Sprintf("export CLIENT=`which mongosh>/dev/null&&echo %s||echo mongo`; $CLIENT mongodb://%s:%s@$KB_POD_FQDN:27017/admin?replicaSet=$KB_CLUSTER_COMP_NAME", r.info.Client, userName, userPass)} 68 69 return []string{"sh", "-c", strings.Join(mongodbCmd, " ")} 70 } 71 72 func (r Commands) Container() string { 73 return r.info.Container 74 } 75 76 func (r Commands) ConnectExample(info *engines.ConnectionInfo, client string) string { 77 if len(info.Database) == 0 { 78 info.Database = r.info.Database 79 } 80 return engines.BuildExample(info, client, r.examples) 81 } 82 83 func (r Commands) ExecuteCommand([]string) ([]string, []corev1.EnvVar, error) { 84 return nil, nil, fmt.Errorf("%s not implemented", r.info.Client) 85 }