-
Notifications
You must be signed in to change notification settings - Fork 3
/
msct.go
289 lines (256 loc) · 7.62 KB
/
msct.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/golang/glog"
"github.com/olebedev/config"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
var cfg = loadConfig()
func main() {
app := cli.NewApp()
app.Name = "msct"
app.Version = "1.1.0"
app.Usage = "Minecraft Server Control Tool"
app.Author = "Nathan Young (http://github.com/nathanpaulyoung)"
app.Commands = []cli.Command{
startCommand(),
resumeCommand(),
haltCommand(),
keepAliveCommand(),
commandCommand(),
}
app.Run(os.Args)
}
func startCommand() cli.Command {
command := cli.Command{
Name: "start",
Aliases: []string{"s"},
Usage: "start a server",
Action: func(c *cli.Context) {
servername := c.Args()[0]
startServer(servername)
},
}
return command
}
func haltCommand() cli.Command {
command := cli.Command{
Name: "halt",
Aliases: []string{"h", "stop"},
Usage: "halt a server",
Action: func(c *cli.Context) {
servername := c.Args()[0]
tmuxSendKeys(servername, 0, 0, "stop")
},
}
return command
}
func resumeCommand() cli.Command {
command := cli.Command{
Name: "resume",
Aliases: []string{"r"},
Usage: "resume a server's tmux session",
Action: func(c *cli.Context) {
servername := c.Args()[0]
args := []string{"a", "-t", buildTmuxName(servername)}
cmd := exec.Command("tmux", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if serverIsRunning(servername) {
if err := cmd.Run(); err != nil {
glog.Fatalf("Error in function resumeCommand(); please inform the developer.\n%s", err)
}
} else {
glog.Errorf("Server '%s' is not running.", servername)
}
},
}
return command
}
func keepAliveCommand() cli.Command {
command := cli.Command{
Name: "keepalive",
Aliases: []string{"ka"},
Usage: "restart a server's tmux session if server detected as stopped",
Action: func(c *cli.Context) {
servername := c.Args()[0]
keepAliveFreq, _ := cfg.Int("keepAliveFreq")
for {
if !serverIsRunning(servername) {
startServer(servername)
time.Sleep(time.Second * time.Duration(keepAliveFreq))
}
}
},
}
return command
}
func commandCommand() cli.Command {
command := cli.Command{
Name: "command",
Aliases: []string{"cmd", "c"},
Usage: "send a command to the tmux session",
Action: func(c *cli.Context) {
servername := c.Args()[0]
var args []string
for i := 1; i < len(c.Args()); i++ {
args = append(args, c.Args()[i])
}
command := strings.Join(args, " ")
tmuxSendKeys(servername, 0, 0, command)
},
}
return command
}
func startServer(servername string) {
args := buildInvocation(servername)
cmd := exec.Command("tmux", args...)
cmd.Dir = buildServerDir(servername)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if serverExists(servername) && !serverIsRunning(servername) {
if err := cmd.Run(); err != nil {
glog.Fatalf("Error in function startServer(); please inform the developer.\n%s", err)
}
} else {
glog.Errorf("Server '%s' does not exist or is already running.", servername)
}
}
func tmuxSendKeys(servername string, window int, pane int, command string) {
cmd := exec.Command("tmux", "send-keys", "-t", buildTmuxName(servername)+":"+strconv.Itoa(window)+"."+strconv.Itoa(pane), command, "Enter")
if serverIsRunning(servername) {
if err := cmd.Run(); err != nil {
glog.Fatalf("Error in function tmuxSendKeys(); please inform the developer.\n%s", err)
}
} else {
glog.Errorf("Server '%s' is not running.", servername)
}
}
func serverExists(servername string) bool {
if _, err := os.Stat(buildServerDir(servername) + getJarFile()); err != nil {
return false
}
return true
}
func serverIsRunning(servername string) bool {
cmd := exec.Command("tmux", "has-session", "-t", buildTmuxName(servername))
if err := cmd.Run(); err != nil {
return false
}
return true
}
func loadConfig() *config.Config {
//Load msct.conf, prefer local over /etc/, and parse yaml
var yaml *config.Config
if _, err := os.Stat("./msct.conf"); err == nil {
yaml, _ = config.ParseYamlFile("./msct.conf")
} else if _, err := os.Stat("/etc/msct.conf"); err == nil {
yaml, _ = config.ParseYamlFile("/etc/msct.conf")
} else {
err := generateConfig()
if err != nil {
glog.Fatalf("Could not generate config file; please inform the developer.\n%s", err)
}
glog.Warningf("Could not locate msct.conf, so I generated the default file for you at /etc/msct.conf")
yaml, _ = config.ParseYamlFile("/etc/msct.conf")
}
return yaml
}
func generateConfig() error {
defaultConfig := map[string]interface{}{
"user": "minecraft",
"screenBaseName": "msct-",
"rammin": "2048",
"rammax": "4096",
"paths": map[string]interface{}{
"root": "/opt/minecraft/",
"jarFile": "server.jar",
},
"startTmuxAttached": "false",
"javaParams": "-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:ParallelGCThreads=2 -XX:+DisableExplicitGC -XX:MaxGCPauseMillis=500 -XX:SurvivorRatio=16 -XX:TargetSurvivorRatio=90",
"debug": "false",
"keepAliveFreq": "30",
}
yaml, err := config.RenderYaml(defaultConfig)
if err != nil {
return err
}
err = ioutil.WriteFile("/etc/msct.conf", []byte(yaml), 0644)
if err != nil {
return err
}
return nil
}
func buildTmuxName(servername string) string {
//Load from config and set base tmux prefix, if not set in config, default to "msct-"
tmuxbasename, err := cfg.String("tmuxbasename")
if err != nil {
tmuxbasename = "msct-"
}
return tmuxbasename + servername
}
func buildInvocation(servername string) []string {
//Load from config and set whether to start tmux attached or not, if not set in config, default to attached
startTmuxAttached, err := cfg.Bool("startTmuxAttached")
if err != nil {
startTmuxAttached = true
}
var tmuxParams []string
if startTmuxAttached == true {
tmuxParams = append(tmuxParams, "new", "-s", buildTmuxName(servername))
} else {
tmuxParams = append(tmuxParams, "new", "-d", "-s", buildTmuxName(servername))
}
//Load from config and set java parameters, if not set in config, set reasonable defaults
rammin, err := cfg.String("rammin")
if err != nil {
rammin = "2048M"
}
rammax, err := cfg.String("rammax")
if err != nil {
rammax = "4096M"
}
//Load from config and set java parameters, if not set in config, set reasonable defaults
javaParams, err := cfg.String("javaParams")
if err != nil {
javaParams = "-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:ParallelGCThreads=3 -XX:+DisableExplicitGC -XX:MaxGCPauseMillis=500 -XX:SurvivorRatio=16 -XX:TargetSurvivorRatio=90"
}
javaParamsArray := strings.Fields(javaParams)
//Create full server path of the form /opt/minecraft/<servername>/server.jar
fullpath := buildServerDir(servername) + getJarFile()
var args []string
args = append(args, tmuxParams...)
args = append(args, fmt.Sprintf("java -server -Xms%sM -Xmx%sM %s -jar %s", rammin, rammax, strings.Join(javaParamsArray, " "), fullpath))
if debugIsEnabled() {
println(strings.Join(args, " "))
}
return args
}
func buildServerDir(servername string) string {
//Load from config and set msct root directory, if not set in config, default to /opt/minecraft/
rootdir, err := cfg.String("paths.root")
if err != nil {
rootdir = "/opt/minecraft/"
}
return rootdir + servername + "/"
}
func getJarFile() string {
//Load from config and set server jar filename, if not set in config, default to server.jar
jarFile, err := cfg.String("paths.jarFile")
if err != nil {
jarFile = "server.jar"
}
return jarFile
}
func debugIsEnabled() bool {
debug, _ := cfg.Bool("debug")
return debug
}