Implement adding members based on local ip
This commit is contained in:
parent
77d9030e80
commit
b3751856af
3 changed files with 99 additions and 26 deletions
1
main.go
1
main.go
|
@ -15,6 +15,7 @@ var maxMemAllocPercent int = 80
|
||||||
var mongoInitdbRootUsername = "sasha"
|
var mongoInitdbRootUsername = "sasha"
|
||||||
var mongoInitdbRootPassword = "12345"
|
var mongoInitdbRootPassword = "12345"
|
||||||
var verbosePtr = flag.Bool("v", false, "boolean")
|
var verbosePtr = flag.Bool("v", false, "boolean")
|
||||||
|
var localIpPtr = flag.Bool("localip", false, "boolean")
|
||||||
|
|
||||||
func ExecSystem(command string) {
|
func ExecSystem(command string) {
|
||||||
|
|
||||||
|
|
|
@ -4,41 +4,91 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initReplMongo() {
|
func initReplMongo() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
// Create a new replica set client
|
if *localIpPtr{
|
||||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://sasha:12345@localhost:30000/?directConnection=true"))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
db := client.Database("admin")
|
ips, err := GetLocalIPs()
|
||||||
|
if err != nil {
|
||||||
members := make([]bson.M, numberOfContainers)
|
log.Fatal(err)
|
||||||
for i := 0; i < numberOfContainers; i++ {
|
|
||||||
members[i] = bson.M{
|
|
||||||
"_id": i,
|
|
||||||
"host": fmt.Sprintf("mongo%d", i),
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
config := bson.M{
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
"_id": replicasetName,
|
defer cancel()
|
||||||
"members": members,
|
|
||||||
|
// 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)
|
|
||||||
}
|
}
|
22
network.go
Normal file
22
network.go
Normal file
|
@ -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
|
||||||
|
}
|
Loading…
Reference in a new issue