-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlgo.go
151 lines (138 loc) · 5.32 KB
/
mlgo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"flag"
"fmt"
"os"
)
const Datadir = "mlgo-data"
var (
version string
date string
)
func main() {
// Subcommands
standaloneCommand := flag.NewFlagSet("standalone", flag.ExitOnError)
replsetCommand := flag.NewFlagSet("replset", flag.ExitOnError)
shardedCommand := flag.NewFlagSet("sharded", flag.ExitOnError)
// Standalone
standaloneAuthPtr := standaloneCommand.Bool("auth", false, "use auth")
standalonePortPtr := standaloneCommand.Int("port", 27017, "start on this port")
standaloneScriptPtr := standaloneCommand.Bool("script", false, "print deployment script")
// Replica set
replsetAuthPtr := replsetCommand.Bool("auth", false, "use auth")
replsetPortPtr := replsetCommand.Int("port", 27017, "start on this port")
replsetNumPtr := replsetCommand.Int("num", 1, "run this many nodes")
replsetConfigPtr := replsetCommand.String("cfg", "", "configuration of the set")
replsetNamePtr := replsetCommand.String("name", "replset", "name of the set")
replsetScriptPtr := replsetCommand.Bool("script", false, "print deployment script")
replsetInitPtr := replsetCommand.Bool("noinit", false, "don't initiate the replica set")
replset1Ptr := replsetCommand.Bool("1", false, "initiate a single-node replica set")
replset3Ptr := replsetCommand.Bool("3", false, "initiate a three-node replica set")
replset5Ptr := replsetCommand.Bool("5", false, "initiate a five-node replica set")
replset7Ptr := replsetCommand.Bool("7", false, "initiate a seven-node replica set")
// Sharded cluster
shardedAuthPtr := shardedCommand.Bool("auth", false, "use auth")
shardedPortPtr := shardedCommand.Int("port", 27017, "start on this port")
shardedNumPtr := shardedCommand.Int("num", 2, "run this many shards")
shardedShardsvrPtr := shardedCommand.Int("shardsvr", 1, "run this many nodes per shard")
shardedConfigSvrPtr := shardedCommand.Int("configsvr", 1, "run this many config servers")
shardedShardsvrConfigPtr := shardedCommand.String("shardcfg", "", "configuration of the shard replica set")
shardedScriptPtr := shardedCommand.Bool("script", false, "print deployment script")
// Verify that a subcommand has been provided
// os.Arg[0] is the main command
// os.Arg[1] will be the subcommand
if len(os.Args) < 2 {
var helptext string
helptext = fmt.Sprintf("%s %s %s\n\n", os.Args[0], version, date)
helptext += "Usage:\n"
helptext += " standalone (st) -- run a standalone node\n"
helptext += " replset (rs) -- run a replica set\n"
helptext += " sharded (sh) -- run a sharded cluster\n"
helptext += "\n"
helptext += " start [criteria] -- start some mongod/mongos using the start.sh script\n"
helptext += " kill/stop [all|criteria] -- kill running mongod/mongos under the current directory\n"
helptext += " rm -- remove the data directory\n"
helptext += "\n"
helptext += " [criteria] for ps, start, and kill is an expression that will restrict the output or operations of the command\n"
helptext += "\n"
helptext += "Examples:\n"
helptext += " mlgo rs # Start a basic single-node replica set\n"
helptext += " mlgo rs -cfg PSA # Start a 3-node replica set with Primary-Secondary-Arbiter configuration\n"
helptext += " mlgo rs -cfg PSH # Start a 3-node replica set with Primary-Secondary-Hidden configuration\n"
helptext += " mlgo rs -3 # Start a 3-node replica set\n"
helptext += " mlgo sh # Start a basic 2 shards, 1 node per shard, 1 config server\n"
helptext += " mlgo sh -shardcfg PSA # Start a 2 shards, PSA configuration on shards, 1 config server\n"
fmt.Println(helptext)
if ps := Util_ps(""); ps != "" {
fmt.Println(Util_ps_pretty())
} else {
fmt.Println("No running processes")
}
os.Exit(0)
}
// Switch on the subcommand
// Parse the flags for appropriate FlagSet
// FlagSet.Parse() requires a set of arguments to parse as input
// os.Args[2:] will be all arguments starting after the subcommand at os.Args[1]
switch os.Args[1] {
case "install-m":
if len(os.Args) == 3 {
fmt.Println(Util_install_m(os.Args[2]))
} else {
fmt.Println(Util_install_m(""))
}
case "start":
if len(os.Args) == 3 {
Util_start_process(os.Args[2])
} else {
Util_start_process("")
}
case "kill", "stop":
if len(os.Args) == 3 {
Util_kill(os.Args[2])
} else {
Util_kill("")
}
case "rm":
Util_rm()
case "standalone", "st":
standaloneCommand.Parse(os.Args[2:])
case "replset", "rs":
replsetCommand.Parse(os.Args[2:])
case "sharded", "sh":
shardedCommand.Parse(os.Args[2:])
default:
flag.PrintDefaults()
os.Exit(1)
}
// Standalone
if standaloneCommand.Parsed() {
m := new(Mongod)
m.Init(*standalonePortPtr, *standaloneAuthPtr, "", *standaloneScriptPtr)
m.Deploy()
}
// Replica set
if replsetCommand.Parsed() {
var rs_num int
rs := new(ReplSet)
if *replset1Ptr {
rs_num = 1
} else if *replset3Ptr {
rs_num = 3
} else if *replset5Ptr {
rs_num = 5
} else if *replset7Ptr {
rs_num = 7
} else {
rs_num = *replsetNumPtr
}
rs.Init(rs_num, *replsetPortPtr, *replsetConfigPtr, *replsetNamePtr, *replsetAuthPtr, *replsetInitPtr, *replsetScriptPtr)
rs.Deploy()
}
// Sharded cluster
if shardedCommand.Parsed() {
sh := new(Sharded)
sh.Init(*shardedPortPtr, *shardedNumPtr, *shardedShardsvrPtr, *shardedShardsvrConfigPtr, *shardedConfigSvrPtr, *shardedAuthPtr, *shardedScriptPtr)
sh.Deploy()
}
}