101 lines
3.7 KiB
Go
101 lines
3.7 KiB
Go
package internal
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"oc-k8s/utils"
|
|
"os"
|
|
"strings"
|
|
"bytes"
|
|
"os/exec"
|
|
)
|
|
|
|
func Help_DB() {
|
|
fmt.Println(`
|
|
DB commands: oc-k8s <action> db [options]
|
|
create - Add datas in db
|
|
replace - Replace datas in db
|
|
delete - Delete datas in db
|
|
help - Show this help message
|
|
|
|
Usage:
|
|
oc-k8s create db --d [db_name] -r [release] -n [namespace] -f [folder]
|
|
folder - Datas folder files path (required)
|
|
release - Release values name (default: dev)
|
|
namespace - Namespace values name (default: dev)
|
|
db_name - db name (default: opencloud)
|
|
folder - Helm config folder (required, default: .)
|
|
oc-k8s upgrade db -d [db_name] -r [release] -n [namespace] -f [folder]
|
|
folder - Datas folder files path (required)
|
|
release - Release values name (default: dev)
|
|
namespace - Namespace values name (default: dev)
|
|
db_name - db name (default: opencloud)
|
|
oc-k8s delete db -d [db_name] -r [release] -n [namespace] -f [folder]
|
|
release - Release values name (default: dev)
|
|
namespace - Namespace values name (default: dev)
|
|
db_name - db name (default: opencloud)
|
|
folder - Helm config folder (required, default: .)
|
|
oc-k8s help db `)
|
|
}
|
|
|
|
func Delete_DB(release string, namespace string, dbName string, adminUsr string, adminPsw string) error {
|
|
podName, err := getMongoPod(namespace)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return utils.Exec("kubectl exec " + podName + " -- mongosh --eval \"db.getSiblingDB('" + dbName + "').dropDatabase()\" -u " + adminUsr + " -p " + adminPsw +" --authenticationDatabase admin")
|
|
}
|
|
|
|
func getFileNames(folderPath string) []string {
|
|
fileNames := []string{}
|
|
entries, err := os.ReadDir(folderPath)
|
|
if err != nil {
|
|
return fileNames
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
fileNames = append(fileNames, entry.Name())
|
|
}
|
|
return fileNames
|
|
}
|
|
|
|
func Create_DB(filePath *string, release string, namespace string, dbName string, adminUsr string, adminPsw string) error {
|
|
if filePath == nil {
|
|
return errors.New("missing db datas file path")
|
|
}
|
|
podName, err := getMongoPod(namespace)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, fileName := range getFileNames(*filePath) {
|
|
fmt.Println("ADD file " + fileName + " in collection")
|
|
fmt.Println("kubectl cp -n " + namespace + " " + *filePath + "/" + fileName + " \"" + podName + ":/tmp/" + fileName + "\"")
|
|
if err := utils.Exec("kubectl cp -n " + namespace + " " + *filePath + "/" + fileName + " \"" + podName + ":/tmp/" + fileName + "\""); err != nil {
|
|
return err
|
|
}
|
|
fmt.Println("kubectl exec -n " + namespace + " " + podName + " -- mongoimport --db " + dbName + " --collection " + strings.ReplaceAll(fileName, ".json", "") + " --file /tmp/" + fileName + " --jsonArray -u " + adminUsr + " -p " + adminPsw +" --authenticationDatabase admin")
|
|
if err := utils.Exec("kubectl exec -n " + namespace + " " + podName + " -- mongoimport --db " + dbName + " --collection " + strings.ReplaceAll(fileName, ".json", "") + " --file /tmp/" + fileName + " --jsonArray -u " + adminUsr + " -p " + adminPsw +" --authenticationDatabase admin"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getMongoPod(namespace string) (string, error) {
|
|
cmd := exec.Command("bash", "-c", "kubectl get pods -n "+namespace+" --no-headers -o custom-columns=\":metadata.name\" | grep mongodb | head -n 1")
|
|
var out bytes.Buffer
|
|
cmd.Stdout = &out
|
|
if err := cmd.Run(); err != nil {
|
|
return "", err
|
|
}
|
|
podName := strings.TrimSpace(out.String()) // remove newline
|
|
return podName, nil
|
|
}
|
|
|
|
func Upgrade_DB(filePath *string, release string, namespace string, dbName string, adminUsr string, adminPsw string) error {
|
|
Delete_DB(release, namespace, dbName, adminUsr, adminPsw)
|
|
return Create_DB(filePath, release, namespace, dbName, adminUsr, adminPsw)
|
|
}
|