Compare commits

..

No commits in common. "5982888cb1b35c2ea6d3db98e12e7bc47e9da69e" and "77d9030e80a1508227748fe1ae909f999946e514" have entirely different histories.

3 changed files with 27 additions and 102 deletions

View file

@ -15,7 +15,6 @@ var maxMemAllocPercent int = 80
var mongoInitdbRootUsername = "sasha"
var mongoInitdbRootPassword = "12345"
var verbosePtr = flag.Bool("v", false, "boolean")
var localIpPtr = flag.Bool("localip", false, "boolean")
func ExecSystem(command string) {

View file

@ -4,93 +4,41 @@ import (
"context"
"time"
"fmt"
"log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/bson"
)
func initReplMongo() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if *localIpPtr{
// Create a new replica set client
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://sasha:12345@localhost:30000/?directConnection=true"))
if err != nil {
panic(err)
}
ips, err := GetLocalIPs()
if err != nil {
log.Fatal(err)
db := client.Database("admin")
members := make([]bson.M, numberOfContainers)
for i := 0; i < numberOfContainers; i++ {
members[i] = bson.M{
"_id": i,
"host": fmt.Sprintf("mongo%d", i),
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create a new replica set client
uri := fmt.Sprintf("mongodb://%s:%s@localhost:30000/?directConnection=true", mongoInitdbRootUsername, mongoInitdbRootPassword)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
db := client.Database("admin")
members := make([]bson.M, numberOfContainers)
for i := 0; i < numberOfContainers; i++ {
members[i] = bson.M{
"_id": i,
"host": fmt.Sprintf("%s:%d", ips[0], 30000+i),
}
}
config := bson.M{
"_id": replicasetName,
"members": members,
}
if *verbosePtr{
fmt.Println(config)
}
command := bson.D{{"replSetInitiate", config}}
var result bson.M
err = db.RunCommand(context.TODO(), command).Decode(&result)
if err != nil {
panic(err)
}
client.Disconnect(ctx)
} else {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Create a new replica set client
uri := fmt.Sprintf("mongodb://%s:%s@localhost:30000/?directConnection=true", mongoInitdbRootUsername, mongoInitdbRootPassword)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
db := client.Database("admin")
members := make([]bson.M, numberOfContainers)
for i := 0; i < numberOfContainers; i++ {
members[i] = bson.M{
"_id": i,
"host": fmt.Sprintf("mongo%d", i),
}
}
config := bson.M{
"_id": replicasetName,
"members": members,
}
if *verbosePtr{
fmt.Println(config)
}
command := bson.D{{"replSetInitiate", config}}
var result bson.M
err = db.RunCommand(context.TODO(), command).Decode(&result)
if err != nil {
panic(err)
}
client.Disconnect(ctx)
}
config := bson.M{
"_id": replicasetName,
"members": members,
}
command := bson.D{{"replSetInitiate", config}}
var result bson.M
err = db.RunCommand(context.TODO(), command).Decode(&result)
if err != nil {
panic(err)
}
client.Disconnect(ctx)
}

View file

@ -1,22 +0,0 @@
package main
import (
"net"
)
func GetLocalIPs() ([]net.IP, error) {
var ips []net.IP
addresses, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
for _, addr := range addresses {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP)
}
}
}
return ips, nil
}