Add endpoint for Mesos agent CLI
This commit is contained in:
parent
d818ad9cda
commit
7f51ab3f69
|
@ -16,6 +16,7 @@ type restapi interface {
|
|||
gpuStatus(http.ResponseWriter, *http.Request)
|
||||
gpuStatusJSON(http.ResponseWriter, *http.Request)
|
||||
dockerCLI(http.ResponseWriter, *http.Request)
|
||||
mesosCLI(http.ResponseWriter, *http.Request)
|
||||
}
|
||||
|
||||
type RemoteAPI struct {
|
||||
|
@ -44,6 +45,7 @@ func (a *RemoteAPI) register(apis ...restapi) {
|
|||
a.Handle("GET", prefix+"/gpu/status", api.gpuStatus)
|
||||
a.Handle("GET", prefix+"/gpu/status/json", api.gpuStatusJSON)
|
||||
a.Handle("GET", prefix+"/docker/cli", api.dockerCLI)
|
||||
a.Handle("GET", prefix+"/mesos/cli", api.mesosCLI)
|
||||
|
||||
if i == len(apis)-1 && prefix != "" {
|
||||
prefix = ""
|
||||
|
|
|
@ -4,6 +4,8 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -226,3 +228,45 @@ func dockerCLIVolume(wr io.Writer, names []string) error {
|
|||
assert(t.Execute(wr, vols))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Zlib compress
|
||||
func compress(buf []byte) []byte {
|
||||
b := bytes.NewBuffer(make([]byte, 0, len(buf)))
|
||||
w := zlib.NewWriter(b)
|
||||
_, err := w.Write(buf)
|
||||
assert(err)
|
||||
err = w.Close()
|
||||
assert(err)
|
||||
return b.Bytes()
|
||||
}
|
||||
|
||||
// Base 64 (RFC 6920)
|
||||
func encode(buf []byte) string {
|
||||
s := base64.URLEncoding.EncodeToString(buf)
|
||||
if n := len(buf) % 3; n > 0 {
|
||||
s = s[:len(s)-(3-n)] // remove padding
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (r *remoteV10) mesosCLI(resp http.ResponseWriter, req *http.Request) {
|
||||
const format = "--attributes=gpus:%s --resources=gpus:{%s}"
|
||||
|
||||
// Generate Mesos attributes
|
||||
var b bytes.Buffer
|
||||
if err := writeInfoJSON(&b); err != nil {
|
||||
http.Error(resp, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
attr := encode(compress(b.Bytes()))
|
||||
|
||||
// Generate Mesos custom resources
|
||||
uuids := make([]string, 0, len(Devices))
|
||||
for i := range Devices {
|
||||
uuids = append(uuids, Devices[i].UUID)
|
||||
}
|
||||
res := strings.Join(uuids, ",")
|
||||
|
||||
_, err := fmt.Fprintf(resp, format, attr, res)
|
||||
assert(err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue