Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added respective .go files for setup.sh and init-shard.sh #727

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docker/scripts/init-shard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"net"
"os"
"os/exec"
"time"
)

func resolveHost(hostname string) (string, error) {
addrs, err := net.LookupHost(hostname)
if err != nil {
return "", err
}
return addrs[0], nil
}

func main() {

Check failure on line 19 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

other declaration of main

Check failure on line 19 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

other declaration of main

Check failure on line 19 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

other declaration of main

Check failure on line 19 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

other declaration of main

Check failure on line 19 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

other declaration of main

Check failure on line 19 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

other declaration of main
mongos := os.Getenv("MONGOS")
mongo11 := os.Getenv("MONGO11")
mongo12 := os.Getenv("MONGO12")
mongo13 := os.Getenv("MONGO13")

mongo21 := os.Getenv("MONGO21")
mongo22 := os.Getenv("MONGO22")
mongo23 := os.Getenv("MONGO23")

mongo31 := os.Getenv("MONGO31")
mongo32 := os.Getenv("MONGO32")
mongo33 := os.Getenv("MONGO33")

port := os.Getenv("PORT")
if port == "" {
port = "27017"
}

fmt.Println("Waiting for startup..")
for {
_, err := net.DialTimeout("tcp", net.JoinHostPort(mongos, port), 1*time.Second)
if err == nil {
break
}
fmt.Print(".")
time.Sleep(1 * time.Second)
}
fmt.Println("Started..")

fmt.Printf("init-shard.sh time now: %s\n", time.Now().Format("15:04:05"))

rs1 := os.Getenv("RS1")
rs2 := os.Getenv("RS2")

mongodb11Resolved, _ := resolveHost(mongo11)
RISHIKESHk07 marked this conversation as resolved.
Show resolved Hide resolved
mongodb12Resolved, _ := resolveHost(mongo12)
mongodb13Resolved, _ := resolveHost(mongo13)

mongodb21Resolved, _ := resolveHost(mongo21)
mongodb22Resolved, _ := resolveHost(mongo22)
mongodb23Resolved, _ := resolveHost(mongo23)

mongodb31Resolved, _ := resolveHost(mongo31)

Check failure on line 62 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

mongodb31Resolved declared but not used

Check failure on line 62 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

mongodb31Resolved declared but not used

Check failure on line 62 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

mongodb31Resolved declared but not used

Check failure on line 62 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

mongodb31Resolved declared but not used

Check failure on line 62 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

mongodb31Resolved declared but not used

Check failure on line 62 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

mongodb31Resolved declared but not used
mongodb32Resolved, _ := resolveHost(mongo32)

Check failure on line 63 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

mongodb32Resolved declared but not used

Check failure on line 63 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

mongodb32Resolved declared but not used

Check failure on line 63 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

mongodb32Resolved declared but not used

Check failure on line 63 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

mongodb32Resolved declared but not used

Check failure on line 63 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

mongodb32Resolved declared but not used

Check failure on line 63 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

mongodb32Resolved declared but not used
mongodb33Resolved, _ := resolveHost(mongo33)

Check failure on line 64 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

mongodb33Resolved declared but not used

Check failure on line 64 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

mongodb33Resolved declared but not used

Check failure on line 64 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

mongodb33Resolved declared but not used

Check failure on line 64 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

mongodb33Resolved declared but not used

Check failure on line 64 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

mongodb33Resolved declared but not used

Check failure on line 64 in docker/scripts/init-shard.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

mongodb33Resolved declared but not used

cmd := exec.Command("mongo", "--host", fmt.Sprintf("%s:%s", mongos, port))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

input := fmt.Sprintf(`
sh.addShard("%s/%s:%s,%s:%s,%s:%s");
sh.addShard("%s/%s:%s,%s:%s,%s:%s");
sh.status();
quit();
`, rs1, mongodb11Resolved, port, mongodb12Resolved, port, mongodb13Resolved, port, rs2, mongodb21Resolved, port, mongodb22Resolved, port, mongodb23Resolved, port)

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Start()
if err != nil {
fmt.Println("Error:", err)
return
}
cmd.Stdin.Write([]byte(input))
cmd.Wait()
}

101 changes: 101 additions & 0 deletions docker/scripts/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"context"
"fmt"
"os"
"time"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

func waitForStartup(client *mongo.Client, ctx context.Context) error {
for {
err := client.Ping(ctx, nil)
if err == nil {
break
}
fmt.Print(".")
time.Sleep(1 * time.Second)
}
fmt.Println("Started..")
return nil
}

func cnfServers(client *mongo.Client, ctx context.Context, configServers []string) error {
fmt.Println("Setting up config servers")
rsConfig := mongo.Cmd{

Check failure on line 28 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

undefined: mongo.Cmd

Check failure on line 28 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

undefined: mongo.Cmd

Check failure on line 28 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

undefined: mongo.Cmd

Check failure on line 28 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

undefined: mongo.Cmd

Check failure on line 28 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

undefined: mongo.Cmd

Check failure on line 28 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

undefined: mongo.Cmd
{"replSetInitiate", map[string]interface{}{

Check failure on line 29 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

missing type in composite literal

Check failure on line 29 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

missing type in composite literal

Check failure on line 29 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

missing type in composite literal

Check failure on line 29 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

missing type in composite literal

Check failure on line 29 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

missing type in composite literal

Check failure on line 29 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

missing type in composite literal
"_id": "configReplSet",
"configsvr": true,
"members": configServers,
"protocolVersion": 1,
}},
}

_, err := client.Database("admin").RunCommand(ctx, rsConfig).DecodeBytes()
return err
}

func generalServers(client *mongo.Client, ctx context.Context, replicaSet []string, arbiter string) error {
fmt.Println("Setting up general servers")
rsConfig := mongo.Cmd{

Check failure on line 43 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

undefined: mongo.Cmd

Check failure on line 43 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

undefined: mongo.Cmd

Check failure on line 43 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

undefined: mongo.Cmd

Check failure on line 43 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

undefined: mongo.Cmd

Check failure on line 43 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

undefined: mongo.Cmd

Check failure on line 43 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

undefined: mongo.Cmd
{"replSetInitiate", map[string]interface{}{

Check failure on line 44 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

missing type in composite literal

Check failure on line 44 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

missing type in composite literal

Check failure on line 44 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

missing type in composite literal

Check failure on line 44 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

missing type in composite literal

Check failure on line 44 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

missing type in composite literal

Check failure on line 44 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

missing type in composite literal
"_id": "rs0",
"members": replicaSet,
"protocolVersion": 1,
}},
}

_, err := client.Database("admin").RunCommand(ctx, rsConfig).DecodeBytes()
if err != nil {
return err
}

arbCmd := fmt.Sprintf("rs.addArb(\"%s\")", arbiter)
_, err = client.Database("admin").RunCommand(ctx, mongo.Cmd{"eval", arbCmd}).DecodeBytes()

Check failure on line 57 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

undefined: mongo.Cmd

Check failure on line 57 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

undefined: mongo.Cmd

Check failure on line 57 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

undefined: mongo.Cmd

Check failure on line 57 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

undefined: mongo.Cmd

Check failure on line 57 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

undefined: mongo.Cmd

Check failure on line 57 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

undefined: mongo.Cmd

return err
}

func main() {

Check failure on line 62 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

main redeclared in this block

Check failure on line 62 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

main redeclared in this block

Check failure on line 62 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.2)

main redeclared in this block

Check failure on line 62 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

main redeclared in this block

Check failure on line 62 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

main redeclared in this block

Check failure on line 62 in docker/scripts/setup.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.2)

main redeclared in this block
ctx := context.TODO()

clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
fmt.Println("Error connecting to MongoDB:", err)
os.Exit(1)
}
defer client.Disconnect(ctx)

mongo1 := os.Getenv("MONGO1")
mongo2 := os.Getenv("MONGO2")
mongo3 := os.Getenv("MONGO3")
arbiter := os.Getenv("ARBITER")

configServers := []string{mongo1, mongo2, mongo3}
replicaSet := []string{mongo1, mongo2, mongo3}

err = waitForStartup(client, ctx)
if err != nil {
fmt.Println("Error waiting for startup:", err)
os.Exit(1)
}

switch os.Args[1] {
RISHIKESHk07 marked this conversation as resolved.
Show resolved Hide resolved
case "cnf_servers":
err = cnfServers(client, ctx, configServers)
case "general_servers":
err = generalServers(client, ctx, replicaSet, arbiter)
default:
err = generalServers(client, ctx, replicaSet, arbiter)
}

if err != nil {
fmt.Println("Error setting up MongoDB replica set:", err)
os.Exit(1)
}
}

Loading