X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fxdsserver%2Fapiv1-exec.go;h=aea34e441f44d2501e028ea7a6e2b3955a9f0e64;hb=5e979f0ba451e64fc44ffaa8b79d714d9ad75a57;hp=ce5e7b7f9a7ec1f264b58dbb5fe77264be9e8357;hpb=2f7828d01f4c4ca2909f95f098627cd5475ed225;p=src%2Fxds%2Fxds-server.git diff --git a/lib/xdsserver/apiv1-exec.go b/lib/xdsserver/apiv1-exec.go index ce5e7b7..aea34e4 100644 --- a/lib/xdsserver/apiv1-exec.go +++ b/lib/xdsserver/apiv1-exec.go @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2017-2018 "IoT.bzh" + * Author Sebastien Douheret + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package xdsserver import ( @@ -9,10 +26,10 @@ import ( "strings" "time" + common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib/eows" + "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" "github.com/gin-gonic/gin" - common "github.com/iotbzh/xds-common/golib" - "github.com/iotbzh/xds-common/golib/eows" - "github.com/iotbzh/xds-server/lib/xsapiv1" "github.com/kr/pty" ) @@ -119,6 +136,7 @@ func (s *APIService) execCmd(c *gin.Context) { // Create new execution over WS context execWS := eows.New(strings.Join(cmd, " "), cmdArgs, sop, sess.ID, args.CmdID) execWS.Log = s.Log + execWS.OutSplit = eows.SplitChar // Append client project dir to environment execWS.Env = append(args.Env, "CLIENT_PROJECT_DIR="+prj.ClientPath) @@ -126,7 +144,7 @@ func (s *APIService) execCmd(c *gin.Context) { // Set command execution timeout if args.CmdTimeout == 0 { // 0 : default timeout - // TODO get default timeout from config.json file + // TODO get default timeout from server-config.json file execWS.CmdExecTimeout = 24 * 60 * 60 // 1 day } else { execWS.CmdExecTimeout = args.CmdTimeout @@ -134,14 +152,17 @@ func (s *APIService) execCmd(c *gin.Context) { // Define callback for input (stdin) execWS.InputEvent = xsapiv1.ExecInEvent - execWS.InputCB = func(e *eows.ExecOverWS, stdin string) (string, error) { + execWS.InputCB = func(e *eows.ExecOverWS, bStdin []byte) ([]byte, error) { + + stdin := string(bStdin) + s.Log.Debugf("STDIN <<%v>>", strings.Replace(stdin, "\n", "\\n", -1)) // Handle Ctrl-D if len(stdin) == 1 && stdin == "\x04" { // Close stdin errMsg := fmt.Errorf("close stdin: %v", stdin) - return "", errMsg + return []byte{}, errMsg } // Set correct path @@ -154,16 +175,19 @@ func (s *APIService) execCmd(c *gin.Context) { // Translate paths from client to server stdin = (*f).ConvPathCli2Svr(stdin) } - - return stdin, nil + return []byte(stdin), nil } // Define callback for output (stdout+stderr) - execWS.OutputCB = func(e *eows.ExecOverWS, stdout, stderr string) { + execWS.OutputCB = func(e *eows.ExecOverWS, bStdout, bStderr []byte) { + + stdout := string(bStdout) + stderr := string(bStderr) + // IO socket can be nil when disconnected so := s.sessions.IOSocketGet(e.Sid) if so == nil { - s.Log.Infof("%s not emitted: WS closed (sid:%s, msgid:%s)", xsapiv1.ExecOutEvent, e.Sid, e.CmdID) + s.Log.Infof("%s not emitted: WS closed (sid:%s, CmdID:%s)", xsapiv1.ExecOutEvent, e.Sid, e.CmdID) return } @@ -237,6 +261,7 @@ func (s *APIService) execCmd(c *gin.Context) { execWS.ExitCB = func(e *eows.ExecOverWS, code int, err error) { s.Log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", e.CmdID, code, err) + defer LockXdsUpdateCounter(s.Context, false) // Close client tty defer func() { if gdbPty != nil { @@ -306,8 +331,10 @@ func (s *APIService) execCmd(c *gin.Context) { // Start command execution s.Log.Infof("Execute [Cmd ID %s]: %v %v", execWS.CmdID, execWS.Cmd, execWS.Args) + LockXdsUpdateCounter(s.Context, true) err = execWS.Start() if err != nil { + LockXdsUpdateCounter(s.Context, false) common.APIError(c, err.Error()) return }