Compare commits
No commits in common. "5982888cb1b35c2ea6d3db98e12e7bc47e9da69e" and "77d9030e80a1508227748fe1ae909f999946e514" have entirely different histories.
5982888cb1
...
77d9030e80
3 changed files with 27 additions and 102 deletions
1
main.go
1
main.go
|
@ -15,7 +15,6 @@ 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,64 +4,17 @@ 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
|
||||||
uri := fmt.Sprintf("mongodb://%s:%s@localhost:30000/?directConnection=true", mongoInitdbRootUsername, mongoInitdbRootPassword)
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://sasha:12345@localhost:30000/?directConnection=true"))
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
@ -81,10 +34,6 @@ 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)
|
||||||
|
@ -92,5 +41,4 @@ func initReplMongo() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
client.Disconnect(ctx)
|
client.Disconnect(ctx)
|
||||||
}
|
|
||||||
}
|
}
|
22
network.go
22
network.go
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in a new issue