8 "github.com/gin-gonic/gin"
9 common "github.com/iotbzh/xds-common/golib"
12 // Only define useful fields
13 type ExecArgs struct {
14 ID string `json:"id" binding:"required"`
17 // ExecCmd executes remotely a command
18 func (s *APIService) execCmd(c *gin.Context) {
19 s._execRequest("/exec", c)
22 // execSignalCmd executes remotely a command
23 func (s *APIService) execSignalCmd(c *gin.Context) {
24 s._execRequest("/signal", c)
27 func (s *APIService) _execRequest(url string, c *gin.Context) {
28 data, err := c.GetRawData()
30 common.APIError(c, err.Error())
33 // First get Project ID to retrieve Server ID and send command to right server
37 // XXX - we cannot use c.BindJSON, so directly unmarshall it
38 // (see https://github.com/gin-gonic/gin/issues/1078)
39 if err := json.Unmarshal(data, &args); err != nil {
40 common.APIError(c, "Invalid arguments")
45 prj := s.projects.Get(id)
47 common.APIError(c, "Unknown id")
51 svr := (*prj).GetServer()
53 common.APIError(c, "Cannot identify XDS Server")
57 // Retrieve session info
58 sess := s.sessions.Get(c)
60 common.APIError(c, "Unknown sessions")
65 common.APIError(c, "Websocket not established")
69 // Forward XDSServer WS events to client WS
70 // TODO removed static event name list and get it from XDSServer
71 for _, evName := range []string{
75 "exec:inferior-input",
76 "exec:inferior-output",
79 svr.EventOn(evN, func(evData interface{}) {
80 (*sock).Emit(evN, evData)
84 // Forward back command to right server
85 response, err := svr.HTTPPostBody(url, string(data))
87 common.APIError(c, err.Error())
92 body, err := ioutil.ReadAll(response.Body)
94 common.APIError(c, "Cannot read response body")
97 c.JSON(http.StatusOK, string(body))