From d9fc5aa3b3ca8a3e92e635783f8a30f6c22c0611 Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Wed, 21 Aug 2019 13:50:16 +0400 Subject: [PATCH] Add exec command to API --- pkg/hypervisor/hypervisor.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/hypervisor/hypervisor.go b/pkg/hypervisor/hypervisor.go index d7203b5b69..b88ebdf25e 100644 --- a/pkg/hypervisor/hypervisor.go +++ b/pkg/hypervisor/hypervisor.go @@ -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()) @@ -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