Compare commits
2 commits
77d9030e80
...
5982888cb1
Author | SHA1 | Date | |
---|---|---|---|
5982888cb1 | |||
b3751856af |
3 changed files with 101 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,17 +4,64 @@ 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() {
|
||||||
|
|
||||||
|
if *localIpPtr{
|
||||||
|
|
||||||
|
ips, err := GetLocalIPs()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
// Create a new replica set client
|
// Create a new replica set client
|
||||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://sasha:12345@localhost:30000/?directConnection=true"))
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -34,6 +81,10 @@ func initReplMongo() {
|
||||||
"members": members,
|
"members": members,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *verbosePtr{
|
||||||
|
fmt.Println(config)
|
||||||
|
}
|
||||||
|
|
||||||
command := bson.D{{"replSetInitiate", config}}
|
command := bson.D{{"replSetInitiate", config}}
|
||||||
var result bson.M
|
var result bson.M
|
||||||
err = db.RunCommand(context.TODO(), command).Decode(&result)
|
err = db.RunCommand(context.TODO(), command).Decode(&result)
|
||||||
|
@ -41,4 +92,5 @@ func initReplMongo() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
client.Disconnect(ctx)
|
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