forked from googleforgames/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (96 loc) · 2.82 KB
/
main.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"time"
"agones.dev/agones/sdks/go"
)
type interceptor struct {
forward io.Writer
intercept func(p []byte)
}
// Write will intercept the incoming stream, and forward
// the contents to its `forward` Writer.
func (i *interceptor) Write(p []byte) (n int, err error) {
if i.intercept != nil {
i.intercept(p)
}
return i.forward.Write(p)
}
// main intercepts the stdout of the Xonotic gameserver and uses it
// to determine if the game server is ready or not.
func main() {
input := flag.String("i", "", "path to server_linux.sh")
flag.Parse()
fmt.Println(">>> Connecting to Agones with the SDK")
s, err := sdk.NewSDK()
if err != nil {
log.Fatalf(">>> Could not connect to sdk: %v", err)
}
fmt.Println(">>> Starting health checking")
go doHealth(s)
fmt.Println(">>> Starting wrapper for Xonotic!")
fmt.Printf(">>> Path to Xonotic server script: %s \n", *input)
// track references to listening count
listeningCount := 0
cmd := exec.Command(*input) // #nosec
cmd.Stderr = &interceptor{forward: os.Stderr}
cmd.Stdout = &interceptor{
forward: os.Stdout,
intercept: func(p []byte) {
if listeningCount >= 4 {
return
}
str := strings.TrimSpace(string(p))
// Xonotic will say "Server listening" 4 times before being ready,
// once for ipv4 and once for ipv6.
// but it does it each twice because it loads the maps between
// each one, and resets state as it does so
if count := strings.Count(str, "Server listening on address"); count > 0 {
listeningCount += count
fmt.Printf(">>> Found 'listening' statement: %d \n", listeningCount)
if listeningCount == 4 {
fmt.Printf(">>> Moving to READY: %s \n", str)
err = s.Ready()
if err != nil {
log.Fatalf("Could not send ready message")
}
}
}
}}
err = cmd.Start()
if err != nil {
log.Fatalf(">>> Error Starting Cmd %v", err)
}
err = cmd.Wait()
log.Fatal(">>> Xonotic shutdown unexpectantly", err)
}
// doHealth sends the regular Health Pings
func doHealth(sdk *sdk.SDK) {
tick := time.Tick(2 * time.Second)
for {
err := sdk.Health()
if err != nil {
log.Fatalf("[wrapper] Could not send health ping, %v", err)
}
<-tick
}
}