-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.sc
executable file
·145 lines (124 loc) · 4.29 KB
/
start.sc
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
#!/usr/bin/env scala
import java.io._
import scala.io.Source
import scala.sys.process._
val EnvDir = "target/env"
def isCassandraRunning: Boolean = {
val lsofPattern = raw"""p(\d+)""".r
val lsof = Seq("lsof", "-Fp", "-i", ":9042")
lsof.lazyLines_!.map {
case lsofPattern(_) => true
case _ => false
}.exists(x => x)
}
def runCmdAsync(cmd: String, dryRun: Boolean): Unit = {
println("$ " + cmd)
if (!dryRun) {
Runtime.getRuntime.exec(cmd)
}
}
def runSbt(cmd: String, dryRun: Boolean): Unit = {
val isWindows = sys.props("os.name").startsWith("Windows")
val shellPrefix: Array[String] = if (isWindows) Array("cmd", "/C") else Array()
val runSeq = shellPrefix ++ Array("sbt", cmd)
println("\n$ " + runSeq.mkString(" "))
if (!dryRun) {
Process(runSeq).!
}
}
def createAkkaConfig(serverHost: String,
clientHost: String,
clientPort: Int,
isClient: Boolean): File = {
val src = Source.fromFile("resources/akka/cluster.conf").mkString
val role = if (isClient) "" else "frontend"
val dst = src.replaceAll("<server-host>", serverHost)
.replaceAll("<client-host>", clientHost)
.replaceAll("<client-port>", clientPort.toString)
.replaceAll("<role>", role)
val akkaConf = new File(s"$EnvDir/cluster.conf")
new PrintWriter(akkaConf) { write(dst); close() }
akkaConf
}
def setupServers(serverHost: String,
noLocalAnalyzer: Boolean,
dryRun: Boolean): Unit = {
// Run Cassandra
val src = Source.fromFile("resources/cassandra/cassandra.yaml").mkString
val dst = src.replaceAll("<host>", serverHost)
val cassandraYml = new File(s"$EnvDir/cassandra.yaml")
new PrintWriter(cassandraYml) { write(dst); close() }
val url = cassandraYml.toURI.toURL
runCmdAsync(s"cassandra -f -Dcassandra.config=$url", dryRun)
// Run Redis
runCmdAsync(s"redis-server --bind $serverHost", dryRun)
// Run Mosquitto
runCmdAsync("mosquitto", dryRun)
if (!dryRun) {
// Waiting for Cassandra
print("Waiting for Cassandra")
while (!isCassandraRunning) {
print(".")
Thread.sleep(1000)
}
}
// Run the cluster
val conf = createAkkaConfig(serverHost, serverHost, 2551, isClient = false)
val opts = Seq("run",
"-c", serverHost,
"-r", serverHost,
"--config", conf,
if (noLocalAnalyzer) "--no-local-analyzer" else ""
)
runSbt(opts.mkString(" "), dryRun)
}
def setupClient(serverHost: String,
clientHost: String,
clientPort: Int,
dryRun: Boolean): Unit = {
// Run the client
val conf = createAkkaConfig(serverHost, clientHost, clientPort, isClient = true)
runSbt(s"run --client -c $serverHost -r $serverHost --config $conf", dryRun)
}
def usage(): Unit = println("""
scala start.sc [server|client] [options]
Cluster helper: runs either servers or client with the provided server host.
Options:
--server-host=<host> Server IP address (used for both modes)
--client-host=<port> Client IP address (used for client mode only)
--client-port=<port> Client TCP port (used for client mode only)
--no-local-analyzer Don't use the local analyzer (used for server mode only)
--dry-run Only update the config files
""")
def entrypoint(args: Array[String]): Unit = {
val serverHostPattern = raw"""--server-host=(\d+\.\d+\.\d+\.\d+)""".r
val clientHostPattern = raw"""--client-host=(\d+\.\d+\.\d+\.\d+)""".r
val clientPortPattern = raw"""--client-port=(\d+)""".r
var isClientOpt: Option[Boolean] = None
var serverHost = "127.0.0.1"
var clientHost = "127.0.0.1"
var clientPort = 2552
var dryRun = false
var noLocalAnalyzer = false
args foreach {
case "server" => isClientOpt = Some(false)
case "client" => isClientOpt = Some(true)
case "--dry-run" => dryRun = true
case "--no-local-analyzer" => noLocalAnalyzer = true
case serverHostPattern(h) => serverHost = h
case clientHostPattern(h) => clientHost = h
case clientPortPattern(p) => clientPort = p.toInt
case _ => // pass
}
isClientOpt match {
case Some(isClient) =>
new File(EnvDir).mkdir()
if (isClient) {
setupClient(serverHost, clientHost, clientPort, dryRun)
} else {
setupServers(serverHost, noLocalAnalyzer, dryRun)
}
case None => usage()
}
}
entrypoint(args)