2024-05-15 05:56:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2024-05-16 05:06:38 +00:00
|
|
|
"bufio"
|
2024-05-16 07:43:17 +00:00
|
|
|
"flag"
|
2024-05-15 05:56:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var mongoBasePath = "/opt/my-mongo"
|
|
|
|
var replicasetName = "techtransthai-db"
|
|
|
|
var networkName = "mongonet"
|
2024-05-17 04:26:27 +00:00
|
|
|
var numberOfContainers int = 2
|
|
|
|
var maxMemAllocPercent int = 80
|
2024-05-15 05:56:21 +00:00
|
|
|
var mongoInitdbRootUsername = "sasha"
|
|
|
|
var mongoInitdbRootPassword = "12345"
|
2024-05-16 07:43:17 +00:00
|
|
|
var verbosePtr = flag.Bool("v", false, "boolean")
|
2024-05-27 09:04:43 +00:00
|
|
|
var localIpPtr = flag.Bool("localip", false, "boolean")
|
2024-05-15 05:56:21 +00:00
|
|
|
|
2024-05-16 05:06:38 +00:00
|
|
|
func ExecSystem(command string) {
|
2024-05-15 05:56:21 +00:00
|
|
|
|
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 05:06:38 +00:00
|
|
|
|
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 05:06:38 +00:00
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2024-05-16 05:06:38 +00:00
|
|
|
}
|
2024-05-15 05:56:21 +00:00
|
|
|
|
|
|
|
func genKeys() {
|
2024-05-16 05:06:38 +00:00
|
|
|
fmt.Printf("\033[1;32mGenerating keys\033[0m\n")
|
2024-05-15 05:56:21 +00:00
|
|
|
|
|
|
|
keyfileLocation := mongoBasePath + "/mongodb-keyfile"
|
2024-05-16 05:06:38 +00:00
|
|
|
ExecSystem("openssl rand -base64 741 > " + keyfileLocation)
|
|
|
|
ExecSystem("chmod 600 " + keyfileLocation)
|
|
|
|
ExecSystem("chown 999 " + keyfileLocation)
|
2024-05-15 05:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-05-16 07:43:17 +00:00
|
|
|
flag.Parse()
|
2024-05-16 05:06:38 +00:00
|
|
|
checkRoot()
|
|
|
|
checkSettings()
|
2024-05-15 05:56:21 +00:00
|
|
|
instDeps()
|
|
|
|
genKeys()
|
2024-05-16 10:23:43 +00:00
|
|
|
checkDockerExistence()
|
2024-05-15 05:56:21 +00:00
|
|
|
dockerNetworkCreate()
|
|
|
|
dockerPullMongo()
|
|
|
|
dockerRunMongo()
|
|
|
|
initReplMongo()
|
|
|
|
}
|