Skip to content

Commit

Permalink
Add exec command to API
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Aug 21, 2019
1 parent b80011f commit d9fc5aa
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/hypervisor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (m *Node) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
r.Get("/user", m.users.UserInfo())
r.Post("/change-password", m.users.ChangePassword())
r.Post("/exec", m.exec())
r.Get("/nodes", m.getNodes())
r.Get("/nodes/{pk}", m.getNode())
r.Get("/nodes/{pk}/apps", m.getApps())
Expand All @@ -150,6 +151,30 @@ func (m *Node) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.ServeHTTP(w, req)
}

// executes a command and returns its output
func (m *Node) exec() http.HandlerFunc {
return m.withCtx(m.nodeCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) {
var reqBody struct {
Command string `json:"command"`
}
if err := httputil.ReadJSON(r, &reqBody); err != nil {
httputil.WriteJSON(w, r, http.StatusBadRequest, err)
return
}

out, err := ctx.RPC.Exec(reqBody.Command)
if err != nil {
httputil.WriteJSON(w, r, http.StatusInternalServerError, err)
return
}

output := struct {
Output string `json:"output"`
}{string(out)}
httputil.WriteJSON(w, r, http.StatusOK, output)
})
}

type summaryResp struct {
TCPAddr string `json:"tcp_addr"`
*visor.Summary
Expand Down

0 comments on commit d9fc5aa

Please sign in to comment.