Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Added upload jar command. (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerg Schad authored Aug 10, 2017
1 parent 5eed8ae commit 4f97992
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 3 deletions.
Binary file not shown.
Binary file added cli/dcos-flink/artifacts/dcos-flink-service-linux
Binary file not shown.
Binary file added cli/dcos-flink/artifacts/dcos-flink-service.exe
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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"
)


Expand All @@ -19,6 +30,7 @@ func main() {
handleRunSection(app)
handleCancelSection(app)
handleJarsSection(app)
handleUploadSection(app)

kingpin.MustParse(app.Parse(cli.GetArguments()))
}
Expand Down Expand Up @@ -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)
}
20 changes: 20 additions & 0 deletions cli/dcos-flink/tools/build_cli.sh
Original file line number Diff line number Diff line change
@@ -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"
Binary file not shown.
Binary file not shown.

0 comments on commit 4f97992

Please sign in to comment.