9 "github.com/gin-gonic/gin"
10 "github.com/iotbzh/xds-server/lib/common"
13 // ExecArgs JSON parameters of /exec command
14 type ExecArgs struct {
16 RPath string `json:"rpath"` // relative path into project
17 Cmd string `json:"cmd" binding:"required"`
18 Args []string `json:"args"`
19 CmdTimeout int `json:"timeout"` // command completion timeout in Second
22 // ExecOutMsg Message send on each output (stdout+stderr) of executed command
23 type ExecOutMsg struct {
24 CmdID string `json:"cmdID"`
25 Timestamp string `json:"timestamp"`
26 Stdout string `json:"stdout"`
27 Stderr string `json:"stderr"`
30 // ExecExitMsg Message send when executed command exited
31 type ExecExitMsg struct {
32 CmdID string `json:"cmdID"`
33 Timestamp string `json:"timestamp"`
34 Code int `json:"code"`
35 Error error `json:"error"`
38 // ExecOutEvent Event send in WS when characters are received
39 const ExecOutEvent = "exec:output"
41 // ExecExitEvent Event send in WS when program exited
42 const ExecExitEvent = "exec:exit"
46 // ExecCmd executes remotely a command
47 func (s *APIService) execCmd(c *gin.Context) {
49 if c.BindJSON(&args) != nil {
50 common.APIError(c, "Invalid arguments")
54 // TODO: add permission
56 // Retrieve session info
57 sess := s.sessions.Get(c)
59 common.APIError(c, "Unknown sessions")
64 common.APIError(c, "Websocket not established")
68 // Allow to pass id in url (/exec/:id) or as JSON argument
74 common.APIError(c, "Invalid id")
78 prj := s.mfolder.GetFolderFromID(id)
80 common.APIError(c, "Unknown id")
84 execTmo := args.CmdTimeout
86 // TODO get default timeout from config.json file
87 execTmo = 24 * 60 * 60 // 1 day
90 // Define callback for output
91 var oCB common.EmitOutputCB
92 oCB = func(sid string, id int, stdout, stderr string) {
93 // IO socket can be nil when disconnected
94 so := s.sessions.IOSocketGet(sid)
96 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%d", ExecOutEvent, sid, id)
99 s.log.Debugf("%s emitted - WS sid %s - id:%d", ExecOutEvent, sid, id)
101 // FIXME replace by .BroadcastTo a room
102 err := (*so).Emit(ExecOutEvent, ExecOutMsg{
103 CmdID: strconv.Itoa(id),
104 Timestamp: time.Now().String(),
109 s.log.Errorf("WS Emit : %v", err)
113 // Define callback for output
114 eCB := func(sid string, id int, code int, err error) {
115 s.log.Debugf("Command [Cmd ID %d] exited: code %d, error: %v", id, code, err)
117 // IO socket can be nil when disconnected
118 so := s.sessions.IOSocketGet(sid)
120 s.log.Infof("%s not emitted - WS closed (id:%d", ExecExitEvent, id)
124 // FIXME replace by .BroadcastTo a room
125 e := (*so).Emit(ExecExitEvent, ExecExitMsg{
126 CmdID: strconv.Itoa(id),
127 Timestamp: time.Now().String(),
132 s.log.Errorf("WS Emit : %v", e)
136 cmdID := execCommandID
139 cmd := "cd " + prj.GetFullPath(args.RPath) + " && " + args.Cmd
140 if len(args.Args) > 0 {
141 cmd += " " + strings.Join(args.Args, " ")
144 s.log.Debugf("Execute [Cmd ID %d]: %v %v", cmdID, cmd)
145 err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB)
147 common.APIError(c, err.Error())
151 c.JSON(http.StatusOK,