mongo-replica-initializer/main.go

96 lines
2 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os/exec"
"bufio"
2024-05-16 07:43:17 +00:00
"flag"
)
var mongoBasePath = "/opt/my-mongo"
var replicasetName = "techtransthai-db"
var networkName = "mongonet"
var numberOfContainers int = 2
var maxMemAllocPercent int = 80
var mongoInitdbRootUsername = "sasha"
var mongoInitdbRootPassword = "12345"
2024-05-16 07:43:17 +00:00
var verbosePtr = flag.Bool("v", false, "boolean")
var localIpPtr = flag.Bool("localip", false, "boolean")
func ExecSystem(command string) {
2024-05-16 07:43:17 +00:00
if *verbosePtr{
cmd := exec.Command("/usr/bin/bash", "-c", command)
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("Error obtaining stdout pipe:", err)
return
}
2024-05-16 07:43:17 +00:00
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("Error obtaining stderr pipe:", err)
return
}
2024-05-16 07:43:17 +00:00
// Start the command
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
// Read the output line by line
stdoutScanner := bufio.NewScanner(stdout)
stderrScanner := bufio.NewScanner(stderr)
for stdoutScanner.Scan() {
fmt.Println(stdoutScanner.Text())
}
for stderrScanner.Scan() {
fmt.Println(stderrScanner.Text())
}
} else {
cmd := exec.Command("/usr/bin/bash", "-c", command)
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("Error obtaining stderr pipe:", err)
return
}
// Start the command
if err := cmd.Start(); err != nil {
fmt.Println("Error starting command:", err)
return
}
// Read the output line by line
stderrScanner := bufio.NewScanner(stderr)
for stderrScanner.Scan() {
fmt.Println(stderrScanner.Text())
}
}
}
func genKeys() {
fmt.Printf("\033[1;32mGenerating keys\033[0m\n")
keyfileLocation := mongoBasePath + "/mongodb-keyfile"
ExecSystem("openssl rand -base64 741 > " + keyfileLocation)
ExecSystem("chmod 600 " + keyfileLocation)
ExecSystem("chown 999 " + keyfileLocation)
}
func main() {
2024-05-16 07:43:17 +00:00
flag.Parse()
checkRoot()
checkSettings()
instDeps()
genKeys()
checkDockerExistence()
dockerNetworkCreate()
dockerPullMongo()
dockerRunMongo()
initReplMongo()
}