-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (77 loc) · 1.64 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
// version is definded during the build
var version string
var debug bool
func logDebug(msg interface{}) {
if debug {
log.Print(msg)
}
}
func getServiceAddress() string {
if env := os.Getenv("VIRTUAL_PORT"); env != "" {
return ":" + env
}
if env := os.Getenv("HTTP_PORT"); env != "" {
return ":" + env
}
return ":3000"
}
func handleSubmit(w http.ResponseWriter, r *http.Request, defaults []string) {
var d struct{ Args []string }
if r.Body == nil {
http.Error(w, "Please send a request body", 400)
return
}
err := json.NewDecoder(r.Body).Decode(&d)
logDebug(d)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
if len(d.Args) == 0 {
http.Error(w, "Missing submit args", 400)
return
}
args := append(defaults, d.Args...)
out, err := exec.Command("spark-submit", args...).CombinedOutput()
logDebug(string(out))
if err != nil {
msg := err.Error()
if out != nil {
msg = string(out)
}
http.Error(w, msg, 400)
return
}
w.WriteHeader(200)
w.Write(out)
}
func main() {
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println("server-spark-submit version " + version)
return
}
if len(os.Args) > 1 && os.Args[1] == "debug" {
debug = true
}
defaults := strings.Split(os.Getenv("SPARK_SUBMIT_DEFAULT_ARGS"), " ")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
handleSubmit(w, r, defaults)
return
}
http.NotFound(w, r)
})
addrs := getServiceAddress()
log.Printf("Starting server at %s\n", addrs)
log.Fatal(http.ListenAndServe(addrs, nil))
}