implemented most functions, except replset init and distro-dependent commands

This commit is contained in:
Late Night Defender 2024-05-15 12:56:21 +07:00
parent 31bca6e058
commit b8c538d550
8 changed files with 220 additions and 18 deletions

27
docker.go Normal file
View file

@ -0,0 +1,27 @@
package main
import (
"fmt"
"os/exec"
"time"
)
func dockerNetworkCreate() {
fmt.Printf("\n\033[1;32mCreating Docker network %s for MongoDB replica set %s\033[0m\n", networkName, replicasetName)
exec.Command("/usr/bin/docker network create", networkName)
}
func dockerPullMongo() {
fmt.Printf("\n\033[1;32mPulling MongoDB Image\033[0m\n")
exec.Command("/usr/bin/docker pull mongo")
}
func dockerRunMongo() {
for containerNumber := 0; containerNumber < numberOfContainers; containerNumber++ {
fmt.Printf("\n\033[1;32mStarting MongoDB Container %d with %s GB of memory\033[0m\n", containerNumber, get_memory())
dockerRunCmd := fmt.Sprintf("docker run -d -p 3000%d:27017 --name mongo%d --net %s -e MONGO_INITDB_ROOT_USERNAME=%s -e MONGO_INITDB_ROOT_PASSWORD=%s -v %s/mongo%d:/data/db -v %s:/opt/keyfile --restart unless-stopped mongo mongod --replSet %s --wiredTigerCacheSizeGB %s --keyFile /opt/keyfile/mongodb-keyfile", containerNumber, containerNumber, networkName, mongoInitdbRootUsername, mongoInitdbRootPassword, mongoBasePath, containerNumber, mongoBasePath, replicasetName, get_memory())
exec.Command(dockerRunCmd)
}
fmt.Printf("\n\033[1;32mWaiting for all containers to start\033[0m\n")
time.Sleep(10 * time.Second)
}

18
go.mod
View file

@ -1,5 +1,17 @@
module example/server_setup
module main
go 1.22.3
require github.com/go-ini/ini v1.67.0
require (
github.com/go-ini/ini v1.67.0
github.com/shirou/gopsutil/v3 v3.24.4
)
require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.19.0 // indirect
)

40
go.sum
View file

@ -1,2 +1,42 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v3 v3.24.4 h1:dEHgzZXt4LMNm+oYELpzl9YCqV65Yr/6SfrvgRBtXeU=
github.com/shirou/gopsutil/v3 v3.24.4/go.mod h1:lTd2mdiOspcqLgAnr9/nGi71NkeMpWKdmhuxm9GusH8=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

45
inst_deps.go Normal file
View file

@ -0,0 +1,45 @@
package main
import(
"fmt"
"os/exec"
"os"
)
func instDepsUbuntu() {
fmt.Printf("\n\033[1;32mInstalling Python dependencies and OpenSSL from Ubuntu repo\033[0m\n")
exec.Command("/usr/bin/apt install -y python3-pymongo python3-psutil openssl")
}
func instDepsDebian() {
fmt.Println("Installing dependencies on Debian")
}
func instDepsFedora() {
fmt.Println("Installing dependencies on Fedora")
}
func instDepsRedHat() {
fmt.Println("Installing dependencies on Red Hat")
}
func instDepsOpenSUSE() {
fmt.Println("Installing dependencies on openSUSE")
}
func instDeps() {
if getDistro() == "ubuntu" {
instDepsUbuntu()
} else if getDistro() == "debian" {
instDepsDebian()
} else if getDistro() == "fedora" {
instDepsFedora()
} else if getDistro() == "redhat" {
instDepsRedHat()
} else if getDistro() == "opensuse" {
instDepsOpenSUSE()
} else {
fmt.Println("OS not supported")
os.Exit(3)
}
}

41
inst_docker.go Normal file
View file

@ -0,0 +1,41 @@
package main
import(
"fmt"
"os/exec"
)
func Inst_docker_ubuntu() {
fmt.Printf("\n\033[1;32mInstalling Docker CE from official repo\033[0m\n")
docker_install := `
apt update
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt update
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin`
exec.Command(docker_install)
}
func Inst_docker_debian() {
fmt.Println()
}
func Inst_docker_fedora() {
fmt.Println()
}
func Inst_docker_redhat() {
fmt.Println()
}
func Inst_docker_opensuse() {
fmt.Println()
}

36
main.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"fmt"
"os/exec"
)
var mongoBasePath = "/opt/my-mongo"
var replicasetName = "techtransthai-db"
var networkName = "mongonet"
var numberOfContainers int = 5
var maxMemAllocPercent int = 95
var mongoInitdbRootUsername = "sasha"
var mongoInitdbRootPassword = "12345"
func genKeys() {
fmt.Printf("\n\033[1;32mGenerating keys\033[0m\n")
keyfileLocation := mongoBasePath + "/mongodb-keyfile"
exec.Command("/usr/bin/openssl rand -base64 741 > ", keyfileLocation)
exec.Command("/usr/bin/chmod 600 ", keyfileLocation)
exec.Command("/usr/bin/chown 999 ", keyfileLocation)
}
func main() {
check_root()
check_settings()
instDeps()
genKeys()
dockerNetworkCreate()
dockerPullMongo()
dockerRunMongo()
initReplMongo()
}

9
mongo_replica_init.go Normal file
View file

@ -0,0 +1,9 @@
package main
import (
"fmt"
)
func initReplMongo() {
fmt.Println("Mongo replica set code coming soon!")
}

View file

@ -5,16 +5,9 @@ import (
"os"
"os/user"
"github.com/go-ini/ini"
"github.com/shirou/gopsutil/v3/mem"
)
var mongo_base_path = "/opt/my-mongo"
var replicaset_name = "techtransthai-db"
var network_name = "mongonet"
var number_of_containers int = 5
var max_mem_alloc_percent int = 95
var mongo_initdb_root_username = "sasha"
var mongo_initdb_root_password = "12345"
func check_root() {
currentUser, err := user.Current()
if err != nil {
@ -28,7 +21,7 @@ func check_root() {
}
func check_settings() {
if number_of_containers < 1 && number_of_containers > 7 {
if numberOfContainers < 1 && numberOfContainers > 7 {
fmt.Println("Number of containers must be between 1 and 7")
os.Exit(2)
}
@ -47,15 +40,14 @@ func ReadOSRelease(configfile string) map[string]string {
return ConfigParams
}
func get_distro() string {
func getDistro() string {
OSInfo := ReadOSRelease("/etc/os-release")
OSRelease := OSInfo["ID"]
return OSRelease
}
func main() {
check_root()
check_settings()
fmt.Println(get_distro())
func get_memory() string {
system_mem, _ := mem.VirtualMemory()
mem_gb := fmt.Sprintf("%.2f", (float64(system_mem.Total)/1073741824)*float64(maxMemAllocPercent/(numberOfContainers*100)))
return mem_gb
}