2 * Copyright (C) 2017-2018 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
29 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
30 "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib/eows"
31 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
32 "github.com/gin-gonic/gin"
38 // ExecCmd executes remotely a command
39 func (s *APIService) execCmd(c *gin.Context) {
40 var gdbPty, gdbTty *os.File
42 var args xsapiv1.ExecArgs
43 if c.BindJSON(&args) != nil {
44 common.APIError(c, "Invalid arguments")
48 // TODO: add permission ?
50 // Retrieve session info
51 sess := s.sessions.Get(c)
53 common.APIError(c, "Unknown sessions")
58 common.APIError(c, "Websocket not established")
62 // Allow to pass id in url (/exec/:id) or as JSON argument
63 idArg := c.Param("id")
68 common.APIError(c, "Invalid id")
71 id, err := s.mfolders.ResolveID(idArg)
73 common.APIError(c, err.Error())
76 f := s.mfolders.Get(id)
78 common.APIError(c, "Unknown id")
82 prj := fld.GetConfig()
86 // Setup env var regarding Sdk ID (used for example to setup cross toolchain)
87 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 {
88 cmd = append(cmd, envCmd...)
89 cmd = append(cmd, "&&")
91 // It's an error if no envcmd found while a sdkid has been provided
93 common.APIError(c, "Unknown sdkid")
98 cmd = append(cmd, "cd", "\""+fld.GetFullPath(args.RPath)+"\"")
99 // FIXME - add 'exec' prevents to use syntax:
100 // xds-exec -l debug -c xds-config.env -- "cd build && cmake .."
101 // but exec is mandatory to allow to pass correctly signals
102 // As workaround, exec is set for now on client side (eg. in xds-gdb)
103 //cmd = append(cmd, "&&", "exec", args.Cmd)
104 cmd = append(cmd, "&&", args.Cmd)
106 // Process command arguments
107 cmdArgs := make([]string, len(args.Args)+1)
109 // Copy and Translate path from client to server
110 for _, aa := range args.Args {
111 if strings.Contains(aa, prj.ClientPath) {
112 cmdArgs = append(cmdArgs, fld.ConvPathCli2Svr(aa))
114 cmdArgs = append(cmdArgs, aa)
118 // Allocate pts if tty if used
120 gdbPty, gdbTty, err = pty.Open()
122 common.APIError(c, err.Error())
126 s.Log.Debugf("Client command tty: %v %v\n", gdbTty.Name(), gdbTty.Name())
127 cmdArgs = append(cmdArgs, "--tty="+gdbTty.Name())
130 // Unique ID for each commands
131 if args.CmdID == "" {
132 args.CmdID = s.Config.ServerUID[:18] + "_" + strconv.Itoa(execCommandID)
136 // Create new execution over WS context
137 execWS := eows.New(strings.Join(cmd, " "), cmdArgs, sop, sess.ID, args.CmdID)
139 execWS.OutSplit = eows.SplitChar
141 // Append client project dir to environment
142 execWS.Env = append(args.Env, "CLIENT_PROJECT_DIR="+prj.ClientPath)
144 // Set command execution timeout
145 if args.CmdTimeout == 0 {
146 // 0 : default timeout
147 // TODO get default timeout from server-config.json file
148 execWS.CmdExecTimeout = 24 * 60 * 60 // 1 day
150 execWS.CmdExecTimeout = args.CmdTimeout
153 // Define callback for input (stdin)
154 execWS.InputEvent = xsapiv1.ExecInEvent
155 execWS.InputCB = func(e *eows.ExecOverWS, bStdin []byte) ([]byte, error) {
157 stdin := string(bStdin)
159 s.Log.Debugf("STDIN <<%v>>", strings.Replace(stdin, "\n", "\\n", -1))
162 if len(stdin) == 1 && stdin == "\x04" {
164 errMsg := fmt.Errorf("close stdin: %v", stdin)
165 return []byte{}, errMsg
170 prjID := (*data)["ID"].(string)
171 f := s.mfolders.Get(prjID)
173 s.Log.Errorf("InputCB: Cannot get folder ID %s", prjID)
175 // Translate paths from client to server
176 stdin = (*f).ConvPathCli2Svr(stdin)
178 return []byte(stdin), nil
181 // Define callback for output (stdout+stderr)
182 execWS.OutputCB = func(e *eows.ExecOverWS, bStdout, bStderr []byte) {
184 stdout := string(bStdout)
185 stderr := string(bStderr)
187 // IO socket can be nil when disconnected
188 so := s.sessions.IOSocketGet(e.Sid)
190 s.Log.Infof("%s not emitted: WS closed (sid:%s, CmdID:%s)", xsapiv1.ExecOutEvent, e.Sid, e.CmdID)
194 // Retrieve project ID and RootPath
196 prjID := (*data)["ID"].(string)
197 gdbServerTTY := (*data)["gdbServerTTY"].(string)
199 f := s.mfolders.Get(prjID)
201 s.Log.Errorf("OutputCB: Cannot get folder ID %s", prjID)
203 // Translate paths from server to client
204 stdout = (*f).ConvPathSvr2Cli(stdout)
205 stderr = (*f).ConvPathSvr2Cli(stderr)
208 s.Log.Debugf("%s emitted - WS sid[4:] %s - id:%s - prjID:%s", xsapiv1.ExecOutEvent, e.Sid[4:], e.CmdID, prjID)
210 s.Log.Debugf("STDOUT <<%v>>", strings.Replace(stdout, "\n", "\\n", -1))
213 s.Log.Debugf("STDERR <<%v>>", strings.Replace(stderr, "\n", "\\n", -1))
216 // FIXME replace by .BroadcastTo a room
217 err := (*so).Emit(xsapiv1.ExecOutEvent, xsapiv1.ExecOutMsg{
219 Timestamp: time.Now().String(),
224 s.Log.Errorf("WS Emit : %v", err)
227 // XXX - Workaround due to gdbserver bug that doesn't redirect
228 // inferior output (https://bugs.eclipse.org/bugs/show_bug.cgi?id=437532#c13)
229 if gdbServerTTY == "workaround" && len(stdout) > 1 && stdout[0] == '&' {
231 // Extract and cleanup string like &"bla bla\n"
232 re := regexp.MustCompile("&\"(.*)\"")
233 rer := re.FindAllStringSubmatch(stdout, -1)
235 if rer != nil && len(rer) > 0 {
236 for _, o := range rer {
238 out = strings.Replace(o[1], "\\n", "\n", -1)
239 out = strings.Replace(out, "\\r", "\r", -1)
240 out = strings.Replace(out, "\\t", "\t", -1)
242 s.Log.Debugf("STDOUT INFERIOR: <<%v>>", out)
243 err := (*so).Emit(xsapiv1.ExecInferiorOutEvent, xsapiv1.ExecOutMsg{
245 Timestamp: time.Now().String(),
250 s.Log.Errorf("WS Emit : %v", err)
255 s.Log.Errorf("INFERIOR out parsing error: stdout=<%v>", stdout)
260 // Define callback for output
261 execWS.ExitCB = func(e *eows.ExecOverWS, code int, err error) {
262 s.Log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", e.CmdID, code, err)
274 // IO socket can be nil when disconnected
275 so := s.sessions.IOSocketGet(e.Sid)
277 s.Log.Infof("%s not emitted - WS closed (id:%s)", xsapiv1.ExecExitEvent, e.CmdID)
281 // Retrieve project ID and RootPath
283 prjID := (*data)["ID"].(string)
284 exitImm := (*data)["ExitImmediate"].(bool)
286 // XXX - workaround to be sure that Syncthing detected all changes
287 if err := s.mfolders.ForceSync(prjID); err != nil {
288 s.Log.Errorf("Error while syncing folder %s: %v", prjID, err)
291 // Wait end of file sync
292 // FIXME pass as argument
294 for t := tmo; t > 0; t-- {
295 s.Log.Debugf("Wait file in-sync for %s (%d/%d)", prjID, t, tmo)
296 if sync, err := s.mfolders.IsFolderInSync(prjID); sync || err != nil {
298 s.Log.Errorf("ERROR IsFolderInSync (%s): %v", prjID, err)
302 time.Sleep(time.Second)
304 s.Log.Debugf("OK file are synchronized.")
307 // FIXME replace by .BroadcastTo a room
308 errSoEmit := (*so).Emit(xsapiv1.ExecExitEvent, xsapiv1.ExecExitMsg{
310 Timestamp: time.Now().String(),
314 if errSoEmit != nil {
315 s.Log.Errorf("WS Emit : %v", errSoEmit)
319 // User data (used within callbacks)
320 data := make(map[string]interface{})
322 data["ExitImmediate"] = args.ExitImmediate
323 if args.TTY && args.TTYGdbserverFix {
324 data["gdbServerTTY"] = "workaround"
326 data["gdbServerTTY"] = ""
328 execWS.UserData = &data
330 // Start command execution
331 s.Log.Infof("Execute [Cmd ID %s]: %v %v", execWS.CmdID, execWS.Cmd, execWS.Args)
335 common.APIError(c, err.Error())
339 c.JSON(http.StatusOK, xsapiv1.ExecResult{Status: "OK", CmdID: execWS.CmdID})
342 // ExecCmd executes remotely a command
343 func (s *APIService) execSignalCmd(c *gin.Context) {
344 var args xsapiv1.ExecSignalArgs
346 if c.BindJSON(&args) != nil {
347 common.APIError(c, "Invalid arguments")
351 s.Log.Debugf("Signal %s for command ID %s", args.Signal, args.CmdID)
353 e := eows.GetEows(args.CmdID)
355 common.APIError(c, "unknown cmdID")
359 err := e.Signal(args.Signal)
361 common.APIError(c, err.Error())
365 c.JSON(http.StatusOK, xsapiv1.ExecSigResult{Status: "OK", CmdID: args.CmdID})