diff --git a/main.go b/main.go index c08616b..390cc6d 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ 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) { diff --git a/mongo_replica_init.go b/mongo_replica_init.go index 7b41778..9ef1eb2 100644 --- a/mongo_replica_init.go +++ b/mongo_replica_init.go @@ -4,41 +4,91 @@ 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() - // 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) - } + if *localIpPtr{ - 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), + ips, err := GetLocalIPs() + if err != nil { + log.Fatal(err) } - } - config := bson.M{ - "_id": replicasetName, - "members": members, + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + // 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) + } + + 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", string(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 + client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://sasha:12345@localhost:30000/?directConnection=true")) + 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) } - - 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) } \ No newline at end of file diff --git a/network.go b/network.go new file mode 100644 index 0000000..dca9520 --- /dev/null +++ b/network.go @@ -0,0 +1,22 @@ +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 +} \ No newline at end of file