diff --git a/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service-darwin b/cli/dcos-flink/artifacts/dcos-flink-service-darwin similarity index 50% rename from container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service-darwin rename to cli/dcos-flink/artifacts/dcos-flink-service-darwin index 64ce466..c22b142 100755 Binary files a/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service-darwin and b/cli/dcos-flink/artifacts/dcos-flink-service-darwin differ diff --git a/cli/dcos-flink/artifacts/dcos-flink-service-linux b/cli/dcos-flink/artifacts/dcos-flink-service-linux new file mode 100755 index 0000000..ad443ac Binary files /dev/null and b/cli/dcos-flink/artifacts/dcos-flink-service-linux differ diff --git a/cli/dcos-flink/artifacts/dcos-flink-service.exe b/cli/dcos-flink/artifacts/dcos-flink-service.exe new file mode 100755 index 0000000..ebe2ebd Binary files /dev/null and b/cli/dcos-flink/artifacts/dcos-flink-service.exe differ diff --git a/container/appmaster/cli/dcos-flink/main.go b/cli/dcos-flink/main.go similarity index 56% rename from container/appmaster/cli/dcos-flink/main.go rename to cli/dcos-flink/main.go index 67a1e0c..7c4e3d7 100644 --- a/container/appmaster/cli/dcos-flink/main.go +++ b/cli/dcos-flink/main.go @@ -1,13 +1,24 @@ -// Created by @OhRobin +// Created by @OhRobin and @joerg84 package main import ( + "bytes" + "errors" + "fmt" + "log" + "strings" + "mime/multipart" + "net/http" + "io" + "io/ioutil" + "os" + "path/filepath" + "github.com/mesosphere/dcos-commons/cli" "github.com/mesosphere/dcos-commons/cli/client" + "github.com/mesosphere/dcos-commons/cli/config" "gopkg.in/alecthomas/kingpin.v2" - "fmt" - "log" ) @@ -19,6 +30,7 @@ func main() { handleRunSection(app) handleCancelSection(app) handleJarsSection(app) + handleUploadSection(app) kingpin.MustParse(app.Parse(cli.GetArguments())) } @@ -127,3 +139,76 @@ func handleCancelSection(app *kingpin.Application) { cancel := app.Command("cancel", "Cancel flink job").Action(cmd.runCancel) cancel.Arg("job id", "job id of flink").Required().StringVar(&cmd.cancel) } + +//upload + type UploadHandler struct { + filename string + } + + func (cmd *UploadHandler) runUpload(c *kingpin.ParseContext) error { + + //TODO: x509 auth instead of https to http change + url := client.OptionalCLIConfigValue("core.dcos_url") //TODO this should be a RequiredCLIConfigValue + url = strings.Replace(url,"https://", "http://", 1) + serviceName := config.ServiceName + url = fmt.Sprintf("%s/service/%s/jars/upload", url, serviceName) + + fmt.Println(url) + + + //create multipart payload + payload := &bytes.Buffer{} + bodyWriter := multipart.NewWriter(payload) + + fileWriter, err := bodyWriter.CreateFormFile("jarfile", filepath.Base(cmd.filename)) + if err != nil { + fmt.Println("error writing to buffer") + return err + } + + // open file handle + fh, err := os.Open(cmd.filename) + if err != nil { + fmt.Println("error opening file") + return err + } + + //iocopy + _, err = io.Copy(fileWriter, fh) + if err != nil { + return err + } + + // create request + contentType := bodyWriter.FormDataContentType() + bodyWriter.Close() + + req, err := http.NewRequest("POST", url, payload) + if err != nil { + return err + } + req.Header.Set("Content-Type", contentType) + req.Header.Add("authorization", fmt.Sprintf("token=%s", client.OptionalCLIConfigValue("core.dcos_acs_token"))) + + res, err := http.DefaultClient.Do(req) + + defer res.Body.Close() + + // handle response + resp_body, err := ioutil.ReadAll(res.Body) + if err != nil { + return err + } + if res.StatusCode != 200 { + fmt.Println(res.Status) + return errors.New("Upload did not succeed.") + } + fmt.Println(string(resp_body)) + return nil + } + + func handleUploadSection(app *kingpin.Application) { + cmd := &UploadHandler{} + upload := app.Command("upload", "Upload flink jar to run").Action(cmd.runUpload) + upload.Arg("jar file", "jar file to upload").Required().StringVar(&cmd.filename) + } diff --git a/cli/dcos-flink/tools/build_cli.sh b/cli/dcos-flink/tools/build_cli.sh new file mode 100755 index 0000000..52506fa --- /dev/null +++ b/cli/dcos-flink/tools/build_cli.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# exit immediately on failure +set -e + +BASEDIR=$(pwd)/$(dirname "$0") +cd "$BASEDIR" + +CLI_EXE_NAME=dcos-flink-service + +# --- + +# go +cd .. + +go get + +CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="-s -w" -o $CLI_EXE_NAME".exe" +CGO_ENABLED=0 GOOS=darwin GOARCH=386 go build -ldflags="-s -w" -o $CLI_EXE_NAME"-darwin" +CGO_ENABLED=0 GOOS=linux GOARCH=386 go build -ldflags="-s -w" -o $CLI_EXE_NAME"-linux" diff --git a/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service-linux b/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service-linux deleted file mode 100755 index 8d8913b..0000000 Binary files a/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service-linux and /dev/null differ diff --git a/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service.exe b/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service.exe deleted file mode 100755 index 7d69bbe..0000000 Binary files a/container/appmaster/cli/dcos-flink/artifacts/dcos-flink-service.exe and /dev/null differ