4 "github.com/gin-gonic/gin"
5 common "github.com/iotbzh/xds-common/golib"
8 /* TODO: Deprecated - should be removed
9 // MakeArgs is the parameters (json format) of /make command
10 type MakeArgs struct {
12 SdkID string `json:"sdkID"` // sdk ID to use for setting env
13 CmdID string `json:"cmdID"` // command unique ID
14 Args []string `json:"args"` // args to pass to make command
15 Env []string `json:"env"`
16 RPath string `json:"rpath"` // relative path into project
17 ExitImmediate bool `json:"exitImmediate"` // when true, exit event sent immediately when command exited (IOW, don't wait file synchronization)
18 CmdTimeout int `json:"timeout"` // command completion timeout in Second
21 // MakeOutMsg Message send on each output (stdout+stderr) of make command
22 type MakeOutMsg struct {
23 CmdID string `json:"cmdID"`
24 Timestamp string `json:"timestamp"`
25 Stdout string `json:"stdout"`
26 Stderr string `json:"stderr"`
29 // MakeExitMsg Message send on make command exit
30 type MakeExitMsg struct {
31 CmdID string `json:"cmdID"`
32 Timestamp string `json:"timestamp"`
33 Code int `json:"code"`
34 Error error `json:"error"`
37 // MakeOutEvent Event send in WS when characters are received on stdout/stderr
38 const MakeOutEvent = "make:output"
40 // MakeExitEvent Event send in WS when command exited
41 const MakeExitEvent = "make:exit"
46 func (s *APIService) buildMake(c *gin.Context) {
47 common.APIError(c, "/make route is not longer supported, use /exec instead")
52 if c.BindJSON(&args) != nil {
53 common.APIError(c, "Invalid arguments")
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 (/make/:id) or as JSON argument
69 idArg := c.Param("id")
74 common.APIError(c, "Invalid id")
77 id, err := s.mfolders.ResolveID(idArg)
79 common.APIError(c, err.Error())
82 pf := s.mfolders.Get(id)
84 common.APIError(c, "Unknown id")
88 prj := folder.GetConfig()
90 execTmo := args.CmdTimeout
92 // TODO get default timeout from config.json file
93 execTmo = 24 * 60 * 60 // 1 day
96 // TODO merge all code below with exec.go
98 // Define callback for output
99 var oCB common.EmitOutputCB
100 oCB = func(sid string, cmdID string, stdout, stderr string, data *map[string]interface{}) {
101 // IO socket can be nil when disconnected
102 so := s.sessions.IOSocketGet(sid)
104 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%s", MakeOutEvent, sid, cmdID)
108 // Retrieve project ID and RootPath
109 prjID := (*data)["ID"].(string)
110 prjRootPath := (*data)["RootPath"].(string)
112 // Cleanup any references to internal rootpath in stdout & stderr
113 stdout = strings.Replace(stdout, prjRootPath, "", -1)
114 stderr = strings.Replace(stderr, prjRootPath, "", -1)
116 s.log.Debugf("%s emitted - WS sid %s - id:%d - prjID:%s", MakeOutEvent, sid, id, prjID)
118 // FIXME replace by .BroadcastTo a room
119 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
121 Timestamp: time.Now().String(),
126 s.log.Errorf("WS Emit : %v", err)
130 // Define callback for output
131 eCB := func(sid string, cmdID string, code int, err error, data *map[string]interface{}) {
132 s.log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", cmdID, code, err)
134 // IO socket can be nil when disconnected
135 so := s.sessions.IOSocketGet(sid)
137 s.log.Infof("%s not emitted - WS closed (id:%s", MakeExitEvent, cmdID)
141 // Retrieve project ID and RootPath
142 prjID := (*data)["ID"].(string)
143 exitImm := (*data)["ExitImmediate"].(bool)
145 // XXX - workaround to be sure that Syncthing detected all changes
146 if err := s.mfolders.ForceSync(prjID); err != nil {
147 s.log.Errorf("Error while syncing folder %s: %v", prjID, err)
150 // Wait end of file sync
151 // FIXME pass as argument
153 for t := tmo; t > 0; t-- {
154 s.log.Debugf("Wait file insync for %s (%d/%d)", prjID, t, tmo)
155 if sync, err := s.mfolders.IsFolderInSync(prjID); sync || err != nil {
157 s.log.Errorf("ERROR IsFolderInSync (%s): %v", prjID, err)
161 time.Sleep(time.Second)
165 // FIXME replace by .BroadcastTo a room
166 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
168 Timestamp: time.Now().String(),
173 s.log.Errorf("WS Emit : %v", e)
177 // Unique ID for each commands
178 if args.CmdID == "" {
179 args.CmdID = s.cfg.ServerUID[:18] + "_" + strconv.Itoa(makeCommandID)
184 // Retrieve env command regarding Sdk ID
185 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 {
186 cmd = append(cmd, envCmd...)
187 cmd = append(cmd, "&&")
190 cmd = append(cmd, "cd", folder.GetFullPath(args.RPath), "&&", "make")
191 if len(args.Args) > 0 {
192 cmd = append(cmd, args.Args...)
195 s.log.Debugf("Execute [Cmd ID %d]: %v", args.CmdID, cmd)
197 data := make(map[string]interface{})
199 data["RootPath"] = prj.RootPath
200 data["ExitImmediate"] = args.ExitImmediate
202 err = common.ExecPipeWs(cmd, args.Env, sop, sess.ID, args.CmdID, execTmo, s.log, oCB, eCB, &data)
204 common.APIError(c, err.Error())
208 c.JSON(http.StatusOK,