forked from liamjbennett/sous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.51 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
package main
import (
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
func main() {
// Setting up logging
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
log.SetPrefix("sous-server:")
// Getting config from Mesos ENV
port0 := os.Getenv("PORT0")
gdmRepo := os.Getenv("GDM_REPO")
// Building HTTP config
if port0 == "" {
port0 = "80"
}
listen := ":" + port0
log.Printf("Configured:\nListening on %q\nState from %q", listen, gdmRepo)
// Determining location for GDM git repo
slConfig := exec.Command("/go/bin/sous", "config", "StateLocation")
res, err := slConfig.Output()
if err != nil {
log.Panic(err)
}
stateLocation := strings.TrimSpace(string(res))
// Checking out GDM
cloneGDM := exec.Command("git", "clone",
"--config", "user.email=sous-server@example.com",
"--config", "user.name=Sous Server",
gdmRepo, stateLocation)
log.Printf("Cloning GDM: %#v", cloneGDM)
err = cloneGDM.Run()
if err != nil {
log.Panic(err)
}
// Running the `sous server` command proper
args := []string{"server", "-d", "-v", "-listen", listen}
sous := exec.Command("/go/bin/sous", args...)
sous.Stdout = os.Stdout
sous.Stderr = os.Stderr
err = sous.Start()
if err != nil {
log.Panic(err)
}
log.Println("Waiting for sous server to start on " + listen)
for i := 0; i < 10; i++ {
r, err := http.Get("http://" + listen)
if err == nil {
r.Body.Close()
// Block until server dies
sous.Process.Wait()
return
}
time.Sleep(1 * time.Second)
}
log.Fatalf("Server did not start within 10s")
}