Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcnunes committed Nov 11, 2016
0 parents commit 331df14
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; Unix-style newlines with a newline ending every file
[*]
charset = utf-8
indent_size = 2
end_of_line = lf
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
insert_final_newline = true

; Golang
[*.go]
indent_style = tab
indent_size = 4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server-spark-submit
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
server-spark-submit
================

This server expose an API to create spark jobs with `spark-submit` command. Due that this server must run in a environment where that command is available.

Checkout the `example-submit.sh` to understand how to call this API.

### Running the server with default args

```
SPARK_SUBMIT_DEFAULT_ARGS="--deploy-mode client --packages com.databricks:spark-csv_2.11:1.4.0,com.datastax.spark:spark-cassandra-connector_2.10:1.6.0 --conf spark.cassandra.connection.host=cassandra.production-cassandra.svc.cluster.local --master spark://10.2.93.2:7077 --verbose" server-spark-submit
```

### Building

```
go build -ldflags "-X main.version=1"
```
11 changes: 11 additions & 0 deletions example-submit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
curl -H "Content-Type: application/json" \
-X POST \
-d '{
"args" : [
"arg1",
"arg2",
"arg3",
"arg4"
]
}' \
http://localhost:8080
67 changes: 67 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
)

// version is definded during the build
var version string

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)
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()
if err != nil {
http.Error(w, err.Error(), 400)
return
}

log.Println(out)

w.WriteHeader(200)
}

func main() {
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println("server-spark-submit version " + version)
return
}

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)
})

log.Println("Starting server")
log.Fatal(http.ListenAndServe(":8080", nil))
}

0 comments on commit 331df14

Please sign in to comment.