From 66496d63e16635d72f15abe48dc3dadb473f0b6b Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Mon, 22 May 2017 23:58:31 +0200 Subject: [PATCH] Rework development page: Pre-build, Build, Populate. Signed-off-by: Sebastien Douheret --- README.md | 2 + lib/apiv1/apiv1.go | 2 - lib/apiv1/exec.go | 41 ++++++-- lib/apiv1/make.go | 31 +++--- lib/common/execPipeWs.go | 16 +-- lib/crosssdk/sdk.go | 4 +- lib/crosssdk/sdks.go | 6 +- lib/webserver/server.go | 4 +- main.go | 2 +- webapp/src/app/alert/alert.component.ts | 2 +- webapp/src/app/app.component.html | 2 +- webapp/src/app/app.module.ts | 20 ++-- webapp/src/app/app.routing.ts | 4 +- webapp/src/app/build/build.component.html | 71 ------------- webapp/src/app/config/config.component.ts | 12 +-- .../src/app/{ => devel}/build/build.component.css | 12 +++ webapp/src/app/devel/build/build.component.html | 74 ++++++++++++++ .../src/app/{ => devel}/build/build.component.ts | 111 ++++++++++++++++----- webapp/src/app/devel/deploy/deploy.component.css | 45 +++++++++ webapp/src/app/devel/deploy/deploy.component.html | 31 ++++++ webapp/src/app/devel/deploy/deploy.component.ts | 63 ++++++++++++ webapp/src/app/devel/devel.component.css | 14 +++ webapp/src/app/devel/devel.component.html | 36 +++++++ webapp/src/app/devel/devel.component.ts | 32 ++++++ webapp/src/app/projects/projectCard.component.ts | 2 +- .../projects/projectsListAccordion.component.ts | 2 +- webapp/src/app/sdks/sdkCard.component.ts | 2 +- webapp/src/app/sdks/sdkSelectDropdown.component.ts | 2 +- webapp/src/app/sdks/sdksListAccordion.component.ts | 2 +- .../src/app/{common => services}/alert.service.ts | 0 .../src/app/{common => services}/config.service.ts | 38 ++++--- webapp/src/app/{common => services}/sdk.service.ts | 2 +- .../app/{common => services}/syncthing.service.ts | 0 .../src/app/{common => services}/utils.service.ts | 0 .../app/{common => services}/xdsagent.service.ts | 39 ++++---- .../app/{common => services}/xdsserver.service.ts | 27 ++++- 36 files changed, 555 insertions(+), 198 deletions(-) delete mode 100644 webapp/src/app/build/build.component.html rename webapp/src/app/{ => devel}/build/build.component.css (75%) create mode 100644 webapp/src/app/devel/build/build.component.html rename webapp/src/app/{ => devel}/build/build.component.ts (53%) create mode 100644 webapp/src/app/devel/deploy/deploy.component.css create mode 100644 webapp/src/app/devel/deploy/deploy.component.html create mode 100644 webapp/src/app/devel/deploy/deploy.component.ts create mode 100644 webapp/src/app/devel/devel.component.css create mode 100644 webapp/src/app/devel/devel.component.html create mode 100644 webapp/src/app/devel/devel.component.ts rename webapp/src/app/{common => services}/alert.service.ts (100%) rename webapp/src/app/{common => services}/config.service.ts (89%) rename webapp/src/app/{common => services}/sdk.service.ts (94%) rename webapp/src/app/{common => services}/syncthing.service.ts (100%) rename webapp/src/app/{common => services}/utils.service.ts (100%) rename webapp/src/app/{common => services}/xdsagent.service.ts (88%) rename webapp/src/app/{common => services}/xdsserver.service.ts (88%) diff --git a/README.md b/README.md index b910e06..1697009 100644 --- a/README.md +++ b/README.md @@ -128,3 +128,5 @@ Visual Studio Code launcher settings can be found into `.vscode/launch.json`. - replace makefile by build.go to make Windows build support easier - add more tests - add more documentation +- add authentication / login (oauth) + HTTPS +- enable syncthing user/password + HTTPS diff --git a/lib/apiv1/apiv1.go b/lib/apiv1/apiv1.go index 2df8ea7..7fa69e9 100644 --- a/lib/apiv1/apiv1.go +++ b/lib/apiv1/apiv1.go @@ -50,10 +50,8 @@ func New(r *gin.Engine, sess *session.Sessions, cfg *xdsconfig.Config, mfolder * s.apiRouter.POST("/make", s.buildMake) s.apiRouter.POST("/make/:id", s.buildMake) - /* TODO: to be tested and then enabled s.apiRouter.POST("/exec", s.execCmd) s.apiRouter.POST("/exec/:id", s.execCmd) - */ return s } diff --git a/lib/apiv1/exec.go b/lib/apiv1/exec.go index 18fdc7e..895807d 100644 --- a/lib/apiv1/exec.go +++ b/lib/apiv1/exec.go @@ -12,10 +12,12 @@ import ( // ExecArgs JSON parameters of /exec command type ExecArgs struct { - ID string `json:"id"` - RPath string `json:"rpath"` // relative path into project + ID string `json:"id" binding:"required"` + SdkID string `json:"sdkid"` // sdk ID to use for setting env Cmd string `json:"cmd" binding:"required"` Args []string `json:"args"` + Env []string `json:"env"` + RPath string `json:"rpath"` // relative path into project CmdTimeout int `json:"timeout"` // command completion timeout in Second } @@ -51,7 +53,7 @@ func (s *APIService) execCmd(c *gin.Context) { return } - // TODO: add permission + // TODO: add permission ? // Retrieve session info sess := s.sessions.Get(c) @@ -89,14 +91,23 @@ func (s *APIService) execCmd(c *gin.Context) { // Define callback for output var oCB common.EmitOutputCB - oCB = func(sid string, id int, stdout, stderr string) { + oCB = func(sid string, id int, stdout, stderr string, data *map[string]interface{}) { // IO socket can be nil when disconnected so := s.sessions.IOSocketGet(sid) if so == nil { s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%d", ExecOutEvent, sid, id) return } - s.log.Debugf("%s emitted - WS sid %s - id:%d", ExecOutEvent, sid, id) + + // Retrieve project ID and RootPath + prjID := (*data)["ID"].(string) + prjRootPath := (*data)["RootPath"].(string) + + // Cleanup any references to internal rootpath in stdout & stderr + stdout = strings.Replace(stdout, prjRootPath, "", -1) + stderr = strings.Replace(stderr, prjRootPath, "", -1) + + s.log.Debugf("%s emitted - WS sid %s - id:%d - prjID:%s", ExecOutEvent, sid, id, prjID) // FIXME replace by .BroadcastTo a room err := (*so).Emit(ExecOutEvent, ExecOutMsg{ @@ -135,14 +146,26 @@ func (s *APIService) execCmd(c *gin.Context) { cmdID := execCommandID execCommandID++ + cmd := []string{} - cmd := "cd " + prj.GetFullPath(args.RPath) + " && " + args.Cmd + // Setup env var regarding Sdk ID (used for example to setup cross toolchain) + if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 { + cmd = append(cmd, envCmd...) + cmd = append(cmd, "&&") + } + + cmd = append(cmd, "cd", prj.GetFullPath(args.RPath), "&&", args.Cmd) if len(args.Args) > 0 { - cmd += " " + strings.Join(args.Args, " ") + cmd = append(cmd, args.Args...) } - s.log.Debugf("Execute [Cmd ID %d]: %v %v", cmdID, cmd) - err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB) + s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd) + + data := make(map[string]interface{}) + data["ID"] = prj.ID + data["RootPath"] = prj.RootPath + + err := common.ExecPipeWs(cmd, args.Env, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB, &data) if err != nil { common.APIError(c, err.Error()) return diff --git a/lib/apiv1/make.go b/lib/apiv1/make.go index fb6435e..098e41c 100644 --- a/lib/apiv1/make.go +++ b/lib/apiv1/make.go @@ -13,11 +13,12 @@ import ( // MakeArgs is the parameters (json format) of /make command type MakeArgs struct { - ID string `json:"id"` - RPath string `json:"rpath"` // relative path into project - Args string `json:"args"` // args to pass to make command - SdkID string `json:"sdkid"` // sdk ID to use for setting env - CmdTimeout int `json:"timeout"` // command completion timeout in Second + ID string `json:"id"` + SdkID string `json:"sdkid"` // sdk ID to use for setting env + Args []string `json:"args"` // args to pass to make command + Env []string `json:"env"` + RPath string `json:"rpath"` // relative path into project + CmdTimeout int `json:"timeout"` // command completion timeout in Second } // MakeOutMsg Message send on each output (stdout+stderr) of make command @@ -85,14 +86,9 @@ func (s *APIService) buildMake(c *gin.Context) { execTmo = 24 * 60 * 60 // 1 day } - cmd := "cd " + prj.GetFullPath(args.RPath) + " && make" - if args.Args != "" { - cmd += " " + args.Args - } - // Define callback for output var oCB common.EmitOutputCB - oCB = func(sid string, id int, stdout, stderr string) { + oCB = func(sid string, id int, stdout, stderr string, data *map[string]interface{}) { // IO socket can be nil when disconnected so := s.sessions.IOSocketGet(sid) if so == nil { @@ -138,14 +134,21 @@ func (s *APIService) buildMake(c *gin.Context) { cmdID := makeCommandID makeCommandID++ + cmd := []string{} // Retrieve env command regarding Sdk ID - if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); envCmd != "" { - cmd = envCmd + " && " + cmd + if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 { + cmd = append(cmd, envCmd...) + cmd = append(cmd, "&&") + } + + cmd = append(cmd, "cd", prj.GetFullPath(args.RPath), "&&", "make") + if len(args.Args) > 0 { + cmd = append(cmd, args.Args...) } s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd) - err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB) + err := common.ExecPipeWs(cmd, args.Env, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB, nil) if err != nil { common.APIError(c, err.Error()) return diff --git a/lib/common/execPipeWs.go b/lib/common/execPipeWs.go index 3b63cdc..4994d9d 100644 --- a/lib/common/execPipeWs.go +++ b/lib/common/execPipeWs.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "strings" "time" "syscall" @@ -14,7 +15,7 @@ import ( ) // EmitOutputCB is the function callback used to emit data -type EmitOutputCB func(sid string, cmdID int, stdout, stderr string) +type EmitOutputCB func(sid string, cmdID int, stdout, stderr string, data *map[string]interface{}) // EmitExitCB is the function callback used to emit exit proc code type EmitExitCB func(sid string, cmdID int, code int, err error) @@ -23,8 +24,8 @@ type EmitExitCB func(sid string, cmdID int, code int, err error) // https://github.com/gorilla/websocket/blob/master/examples/command/main.go // ExecPipeWs executes a command and redirect stdout/stderr into a WebSocket -func ExecPipeWs(cmd string, so *socketio.Socket, sid string, cmdID int, - cmdExecTimeout int, log *logrus.Logger, eoCB EmitOutputCB, eeCB EmitExitCB) error { +func ExecPipeWs(cmd []string, env []string, so *socketio.Socket, sid string, cmdID int, + cmdExecTimeout int, log *logrus.Logger, eoCB EmitOutputCB, eeCB EmitExitCB, data *map[string]interface{}) error { outr, outw, err := os.Pipe() if err != nil { @@ -39,9 +40,10 @@ func ExecPipeWs(cmd string, so *socketio.Socket, sid string, cmdID int, return fmt.Errorf("Pipe stdin error: " + err.Error()) } - bashArgs := []string{"/bin/bash", "-c", cmd} + bashArgs := []string{"/bin/bash", "-c", strings.Join(cmd, " ")} proc, err := os.StartProcess("/bin/bash", bashArgs, &os.ProcAttr{ Files: []*os.File{inr, outw, outw}, + Env: append(os.Environ(), env...), }) if err != nil { outr.Close() @@ -58,7 +60,7 @@ func ExecPipeWs(cmd string, so *socketio.Socket, sid string, cmdID int, defer inw.Close() stdoutDone := make(chan struct{}) - go cmdPumpStdout(so, outr, stdoutDone, sid, cmdID, log, eoCB) + go cmdPumpStdout(so, outr, stdoutDone, sid, cmdID, log, eoCB, data) // Blocking function that poll input or wait for end of process cmdPumpStdin(so, inw, proc, sid, cmdID, cmdExecTimeout, log, eeCB) @@ -133,13 +135,13 @@ func cmdPumpStdin(so *socketio.Socket, w io.Writer, proc *os.Process, } func cmdPumpStdout(so *socketio.Socket, r io.Reader, done chan struct{}, - sid string, cmdID int, log *logrus.Logger, emitFuncCB EmitOutputCB) { + sid string, cmdID int, log *logrus.Logger, emitFuncCB EmitOutputCB, data *map[string]interface{}) { defer func() { }() sc := bufio.NewScanner(r) for sc.Scan() { - emitFuncCB(sid, cmdID, string(sc.Bytes()), "") + emitFuncCB(sid, cmdID, string(sc.Bytes()), "", data) } if sc.Err() != nil { log.Errorln("scan:", sc.Err()) diff --git a/lib/crosssdk/sdk.go b/lib/crosssdk/sdk.go index 9aeec90..5a5770d 100644 --- a/lib/crosssdk/sdk.go +++ b/lib/crosssdk/sdk.go @@ -48,6 +48,6 @@ func NewCrossSDK(path string) (*SDK, error) { } // GetEnvCmd returns the command used to initialized the environment -func (s *SDK) GetEnvCmd() string { - return ". " + s.EnvFile +func (s *SDK) GetEnvCmd() []string { + return []string{"source", s.EnvFile} } diff --git a/lib/crosssdk/sdks.go b/lib/crosssdk/sdks.go index abfef82..d08afc5 100644 --- a/lib/crosssdk/sdks.go +++ b/lib/crosssdk/sdks.go @@ -71,15 +71,15 @@ func (s *SDKs) Get(id int) SDK { } // GetEnvCmd returns the command used to initialized the environment for an SDK -func (s *SDKs) GetEnvCmd(id string, defaultID string) string { +func (s *SDKs) GetEnvCmd(id string, defaultID string) []string { if id == "" && defaultID == "" { // no env cmd - return "" + return []string{} } s.mutex.Lock() defer s.mutex.Unlock() - defaultEnv := "" + defaultEnv := []string{} for _, sdk := range s.Sdks { if sdk.ID == id { return sdk.GetEnvCmd() diff --git a/lib/webserver/server.go b/lib/webserver/server.go index 774195c..4268b40 100644 --- a/lib/webserver/server.go +++ b/lib/webserver/server.go @@ -154,12 +154,10 @@ func (s *Server) middlewareXDSDetails() gin.HandlerFunc { // CORS middleware func (s *Server) middlewareCORS() gin.HandlerFunc { return func(c *gin.Context) { - if c.Request.Method == "OPTIONS" { c.Header("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Headers", "Content-Type") - c.Header("Access-Control-Allow-Methods", "POST, DELETE, GET, PUT") - c.Header("Content-Type", "application/json") + c.Header("Access-Control-Allow-Methods", "GET, POST, DELETE") c.Header("Access-Control-Max-Age", cookieMaxAge) c.AbortWithStatus(204) return diff --git a/main.go b/main.go index 69f6676..49f36a5 100644 --- a/main.go +++ b/main.go @@ -183,7 +183,7 @@ func xdsApp(cliCtx *cli.Context) error { relativePath = stFld.RawPath } - newFld := xdsconfig.NewFolderConfig(stFld.ID, stFld.Label, ctx.Config.ShareRootDir, strings.Trim(relativePath, "/"), defaultSdk) + newFld := xdsconfig.NewFolderConfig(stFld.ID, stFld.Label, ctx.Config.ShareRootDir, strings.TrimRight(relativePath, "/"), defaultSdk) ctx.Config.Folders = ctx.Config.Folders.Update(xdsconfig.FoldersConfig{newFld}) } diff --git a/webapp/src/app/alert/alert.component.ts b/webapp/src/app/alert/alert.component.ts index 449506f..672d7bf 100644 --- a/webapp/src/app/alert/alert.component.ts +++ b/webapp/src/app/alert/alert.component.ts @@ -1,7 +1,7 @@ import { Component } from '@angular/core'; import { Observable } from 'rxjs'; -import {AlertService, IAlert} from '../common/alert.service'; +import {AlertService, IAlert} from '../services/alert.service'; @Component({ selector: 'app-alert', diff --git a/webapp/src/app/app.component.html b/webapp/src/app/app.component.html index ab792be..3dc77ef 100644 --- a/webapp/src/app/app.component.html +++ b/webapp/src/app/app.component.html @@ -6,7 +6,7 @@