From 2c9ae6a5a27ae2f2e23495c613e7a53aed8e786c Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Thu, 18 May 2017 11:01:13 +0200 Subject: Add Cross SDKs support (part 2) --- lib/apiv1/apiv1.go | 8 +++++- lib/apiv1/make.go | 12 +++++---- lib/apiv1/sdks.go | 31 +++++++++++++++++++++++ lib/crosssdk/sdk.go | 22 +++++++++++------ lib/crosssdk/sdks.go | 57 +++++++++++++++++++++++++++++++++++++++++-- lib/model/folder.go | 9 ++----- lib/syncthing/st.go | 2 ++ lib/syncthing/stfolder.go | 14 +++-------- lib/webserver/server.go | 2 +- lib/xdsconfig/folderconfig.go | 4 ++- 10 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 lib/apiv1/sdks.go (limited to 'lib') diff --git a/lib/apiv1/apiv1.go b/lib/apiv1/apiv1.go index c94849d..7359266 100644 --- a/lib/apiv1/apiv1.go +++ b/lib/apiv1/apiv1.go @@ -4,6 +4,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" + "github.com/iotbzh/xds-server/lib/crosssdk" "github.com/iotbzh/xds-server/lib/model" "github.com/iotbzh/xds-server/lib/session" "github.com/iotbzh/xds-server/lib/xdsconfig" @@ -16,17 +17,19 @@ type APIService struct { sessions *session.Sessions cfg *xdsconfig.Config mfolder *model.Folder + sdks *crosssdk.SDKs log *logrus.Logger } // New creates a new instance of API service -func New(sess *session.Sessions, cfg *xdsconfig.Config, mfolder *model.Folder, r *gin.Engine) *APIService { +func New(r *gin.Engine, sess *session.Sessions, cfg *xdsconfig.Config, mfolder *model.Folder, sdks *crosssdk.SDKs) *APIService { s := &APIService{ router: r, sessions: sess, apiRouter: r.Group("/api/v1"), cfg: cfg, mfolder: mfolder, + sdks: sdks, log: cfg.Log, } @@ -40,6 +43,9 @@ func New(sess *session.Sessions, cfg *xdsconfig.Config, mfolder *model.Folder, r s.apiRouter.POST("/folder", s.addFolder) s.apiRouter.DELETE("/folder/:id", s.delFolder) + s.apiRouter.GET("/sdks", s.getSdks) + s.apiRouter.GET("/sdk/:id", s.getSdk) + s.apiRouter.POST("/make", s.buildMake) s.apiRouter.POST("/make/:id", s.buildMake) diff --git a/lib/apiv1/make.go b/lib/apiv1/make.go index 1e6d937..fb6435e 100644 --- a/lib/apiv1/make.go +++ b/lib/apiv1/make.go @@ -14,8 +14,9 @@ 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"` + 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 } @@ -138,9 +139,10 @@ func (s *APIService) buildMake(c *gin.Context) { cmdID := makeCommandID makeCommandID++ - /* SEB TODO . /opt/poky-agl/3.90.0+snapshot/environment-setup-aarch64-agl-linux - env := os.Environ() - */ + // Retrieve env command regarding Sdk ID + if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); envCmd != "" { + cmd = envCmd + " && " + cmd + } s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd) err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB) diff --git a/lib/apiv1/sdks.go b/lib/apiv1/sdks.go new file mode 100644 index 0000000..5ae2b03 --- /dev/null +++ b/lib/apiv1/sdks.go @@ -0,0 +1,31 @@ +package apiv1 + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/iotbzh/xds-server/lib/common" +) + +// getSdks returns all SDKs configuration +func (s *APIService) getSdks(c *gin.Context) { + c.JSON(http.StatusOK, s.sdks.GetAll()) +} + +// getSdk returns a specific Sdk configuration +func (s *APIService) getSdk(c *gin.Context) { + id, err := strconv.Atoi(c.Param("id")) + if err != nil { + common.APIError(c, "Invalid id") + return + } + + sdk := s.sdks.Get(id) + if sdk.Profile == "" { + common.APIError(c, "Invalid id") + return + } + + c.JSON(http.StatusOK, sdk) +} diff --git a/lib/crosssdk/sdk.go b/lib/crosssdk/sdk.go index 2f22f22..9aeec90 100644 --- a/lib/crosssdk/sdk.go +++ b/lib/crosssdk/sdk.go @@ -2,17 +2,20 @@ package crosssdk import ( "fmt" - "path" "path/filepath" ) // SDK Define a cross tool chain used to build application type SDK struct { - Profile string - Version string - Arch string - Path string - EnvFile string + ID string `json:"id" binding:"required"` + Name string `json:"name"` + Profile string `json:"profile"` + Version string `json:"version"` + Arch string `json:"arch"` + Path string `json:"path"` + + // Not exported fields + EnvFile string `json:"-"` } // NewCrossSDK creates a new instance of Syncthing @@ -28,6 +31,9 @@ func NewCrossSDK(path string) (*SDK, error) { d = filepath.Dir(d) s.Profile = filepath.Base(d) + s.ID = s.Profile + "_" + s.Arch + "_" + s.Version + s.Name = s.Arch + " (" + s.Version + ")" + envFile := filepath.Join(path, "environment-setup*") ef, err := filepath.Glob(envFile) if err != nil { @@ -41,7 +47,7 @@ func NewCrossSDK(path string) (*SDK, error) { return &s, nil } -// GetEnvCmd returns the command to initialized the environment to use a cross SDK +// GetEnvCmd returns the command used to initialized the environment func (s *SDK) GetEnvCmd() string { - return ". " + path.Join(s.Path, s.EnvFile) + return ". " + s.EnvFile } diff --git a/lib/crosssdk/sdks.go b/lib/crosssdk/sdks.go index 435aae6..87bb85d 100644 --- a/lib/crosssdk/sdks.go +++ b/lib/crosssdk/sdks.go @@ -3,6 +3,7 @@ package crosssdk import ( "path" "path/filepath" + "sync" "github.com/Sirupsen/logrus" "github.com/iotbzh/xds-server/lib/common" @@ -10,7 +11,11 @@ import ( ) // SDKs List of installed SDK -type SDKs []*SDK +type SDKs struct { + Sdks []SDK + + mutex sync.Mutex +} // Init creates a new instance of Syncthing func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) { @@ -27,13 +32,61 @@ func Init(cfg *xdsconfig.Config, log *logrus.Logger) (*SDKs, error) { log.Debugf("Error while retrieving SDKs: dir=%s, error=%s", sdkRD, err.Error()) return &s, err } + s.mutex.Lock() + defer s.mutex.Unlock() + for _, d := range dirs { sdk, err := NewCrossSDK(d) if err != nil { log.Debugf("Error while processing SDK dir=%s, err=%s", d, err.Error()) } - s = append(s, sdk) + s.Sdks = append(s.Sdks, *sdk) } } + + log.Debugf("SDKs: %d cross sdks found", len(s.Sdks)) + return &s, nil } + +// GetAll returns all existing SDKs +func (s *SDKs) GetAll() []SDK { + s.mutex.Lock() + defer s.mutex.Unlock() + res := s.Sdks + return res +} + +// Get returns an SDK from id +func (s *SDKs) Get(id int) SDK { + s.mutex.Lock() + defer s.mutex.Unlock() + + if id < 0 || id > len(s.Sdks) { + return SDK{} + } + res := s.Sdks[id] + return res +} + +// GetEnvCmd returns the command used to initialized the environment for an SDK +func (s *SDKs) GetEnvCmd(id string, defaultID string) string { + if id == "" && defaultID == "" { + // no env cmd + return "" + } + + s.mutex.Lock() + defer s.mutex.Unlock() + defaultEnv := "" + for _, sdk := range s.Sdks { + if sdk.ID == id { + return sdk.GetEnvCmd() + } + if sdk.ID == defaultID { + defaultEnv = sdk.GetEnvCmd() + } + } + // Return default env that may be empty + return defaultEnv +} diff --git a/lib/model/folder.go b/lib/model/folder.go index 6687b68..be1bc33 100644 --- a/lib/model/folder.go +++ b/lib/model/folder.go @@ -71,13 +71,8 @@ func (c *Folder) UpdateFolder(newFolder xdsconfig.FolderConfig) (xdsconfig.Folde c.Conf.Folders = c.Conf.Folders.Update(xdsconfig.FoldersConfig{newFolder}) - err := c.SThg.FolderChange(st.FolderChangeArg{ - ID: newFolder.ID, - Label: newFolder.Label, - RelativePath: newFolder.RelativePath, - SyncThingID: newFolder.SyncThingID, - ShareRootDir: c.Conf.ShareRootDir, - }) + err := c.SThg.FolderChange(newFolder) + newFolder.BuilderSThgID = c.Conf.Builder.SyncThingID // FIXME - should be removed after local ST config rework newFolder.Status = xdsconfig.FolderStatusEnable diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 841901d..957dd65 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -38,6 +38,7 @@ type SyncThing struct { logsDir string exitSTChan chan ExitChan exitSTIChan chan ExitChan + conf *xdsconfig.Config client *common.HTTPClient log *logrus.Logger } @@ -85,6 +86,7 @@ func NewSyncThing(conf *xdsconfig.Config, log *logrus.Logger) *SyncThing { binDir: binDir, logsDir: conf.FileConf.LogsDir, log: log, + conf: conf, } return &s diff --git a/lib/syncthing/stfolder.go b/lib/syncthing/stfolder.go index d79e579..45ac60d 100644 --- a/lib/syncthing/stfolder.go +++ b/lib/syncthing/stfolder.go @@ -4,21 +4,13 @@ import ( "path/filepath" "strings" + "github.com/iotbzh/xds-server/lib/xdsconfig" "github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/protocol" ) -// FIXME remove and use an interface on xdsconfig.FolderConfig -type FolderChangeArg struct { - ID string - Label string - RelativePath string - SyncThingID string - ShareRootDir string -} - // FolderChange is called when configuration has changed -func (s *SyncThing) FolderChange(f FolderChangeArg) error { +func (s *SyncThing) FolderChange(f xdsconfig.FolderConfig) error { // Get current config stCfg, err := s.ConfigGet() @@ -63,7 +55,7 @@ func (s *SyncThing) FolderChange(f FolderChangeArg) error { folder := config.FolderConfiguration{ ID: id, Label: label, - RawPath: filepath.Join(f.ShareRootDir, f.RelativePath), + RawPath: filepath.Join(s.conf.ShareRootDir, f.RelativePath), } folder.Devices = append(folder.Devices, config.FolderDeviceConfiguration{ diff --git a/lib/webserver/server.go b/lib/webserver/server.go index 40ce948..0905c77 100644 --- a/lib/webserver/server.go +++ b/lib/webserver/server.go @@ -83,7 +83,7 @@ func (s *Server) Serve() error { s.sessions = session.NewClientSessions(s.router, s.log, cookieMaxAge) // Create REST API - s.api = apiv1.New(s.sessions, s.cfg, s.mfolder, s.router) + s.api = apiv1.New(s.router, s.sessions, s.cfg, s.mfolder, s.sdks) // Websocket routes s.sIOServer, err = socketio.NewServer(nil) diff --git a/lib/xdsconfig/folderconfig.go b/lib/xdsconfig/folderconfig.go index e32f46a..bb2b56f 100644 --- a/lib/xdsconfig/folderconfig.go +++ b/lib/xdsconfig/folderconfig.go @@ -29,13 +29,14 @@ type FolderConfig struct { SyncThingID string `json:"syncThingID"` BuilderSThgID string `json:"builderSThgID"` Status string `json:"status"` + DefaultSdk string `json:"defaultSdk"` // Not exported fields RootPath string `json:"-"` } // NewFolderConfig creates a new folder object -func NewFolderConfig(id, label, rootDir, path string) FolderConfig { +func NewFolderConfig(id, label, rootDir, path string, defaultSdk string) FolderConfig { return FolderConfig{ ID: id, Label: label, @@ -44,6 +45,7 @@ func NewFolderConfig(id, label, rootDir, path string) FolderConfig { SyncThingID: "", Status: FolderStatusDisable, RootPath: rootDir, + DefaultSdk: defaultSdk, } } -- cgit 1.2.3-korg