2 * Copyright (C) 2017 "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.
30 "github.com/Sirupsen/logrus"
31 common "github.com/iotbzh/xds-common/golib"
32 "github.com/iotbzh/xds-common/golib/eows"
33 "github.com/iotbzh/xds-server/lib/xsapiv1"
34 uuid "github.com/satori/go.uuid"
37 // Definition of scripts used to managed SDKs
40 scriptDbDump = "db-dump"
41 scriptDbUpdate = "db-update"
42 scriptGetFamConfig = "get-family-config"
43 scriptGetSdkInfo = "get-sdk-info"
44 scriptRemove = "remove"
47 var scriptsAll = []string{
58 // CrossSDK Hold SDK config
59 type CrossSDK struct {
62 scripts map[string]string
63 installCmd *eows.ExecOverWS
64 removeCmd *eows.ExecOverWS
70 // ListCrossSDK List all available and installed SDK (call "db-dump" script)
71 func ListCrossSDK(scriptDir string, log *logrus.Logger) ([]xsapiv1.SDK, error) {
72 sdksList := []xsapiv1.SDK{}
74 // Retrieve SDKs list and info
75 cmd := exec.Command(path.Join(scriptDir, scriptDbDump))
76 stdout, err := cmd.CombinedOutput()
78 return sdksList, fmt.Errorf("Cannot get sdks list: %v", err)
81 if err = json.Unmarshal(stdout, &sdksList); err != nil {
82 log.Errorf("SDK %s script output:\n%v\n", scriptDbDump, string(stdout))
83 return sdksList, fmt.Errorf("Cannot decode sdk list %v", err)
89 // GetSDKInfo Used get-sdk-info script to extract SDK get info from a SDK file/tarball
90 func GetSDKInfo(scriptDir, url, filename, md5sum string, log *logrus.Logger) (xsapiv1.SDK, error) {
95 args = append(args, "--url", url)
96 } else if filename != "" {
97 args = append(args, "--file", filename)
99 args = append(args, "--md5", md5sum)
102 return sdk, fmt.Errorf("url of filename must be set")
105 cmd := exec.Command(path.Join(scriptDir, scriptGetSdkInfo), args...)
106 stdout, err := cmd.CombinedOutput()
108 return sdk, fmt.Errorf("%v %v", string(stdout), err)
111 if err = json.Unmarshal(stdout, &sdk); err != nil {
112 log.Errorf("SDK %s script output:\n%v\n", scriptGetSdkInfo, string(stdout))
113 return sdk, fmt.Errorf("Cannot decode sdk info %v", err)
118 // NewCrossSDK creates a new instance of CrossSDK
119 func NewCrossSDK(ctx *Context, sdk xsapiv1.SDK, scriptDir string) (*CrossSDK, error) {
123 scripts: make(map[string]string),
126 // Execute get-config script to retrieve SDK configuration
127 getConfFile := path.Join(scriptDir, scriptGetFamConfig)
128 if !common.Exists(getConfFile) {
129 return &s, fmt.Errorf("'%s' script file not found in %s", scriptGetFamConfig, scriptDir)
132 cmd := exec.Command(getConfFile)
133 stdout, err := cmd.CombinedOutput()
135 return &s, fmt.Errorf("Cannot get sdk config using %s: %v", getConfFile, err)
138 err = json.Unmarshal(stdout, &s.sdk.FamilyConf)
140 s.Log.Errorf("SDK config script output:\n%v\n", string(stdout))
141 return &s, fmt.Errorf("Cannot decode sdk config %v", err)
143 famName := s.sdk.FamilyConf.FamilyName
146 if s.sdk.FamilyConf.RootDir == "" {
147 return &s, fmt.Errorf("SDK config not valid (rootDir not set)")
149 if s.sdk.FamilyConf.EnvSetupFile == "" {
150 return &s, fmt.Errorf("SDK config not valid (envSetupFile not set)")
153 // Check that other mandatory scripts are present
154 for _, scr := range scriptsAll {
155 s.scripts[scr] = path.Join(scriptDir, scr)
156 if !common.Exists(s.scripts[scr]) {
157 return &s, fmt.Errorf("Script named '%s' missing in SDK family '%s'", scr, famName)
161 // Fixed default fields value
163 if sdk.Status == "" {
164 sdk.Status = xsapiv1.SdkStatusNotInstalled
168 errMsg := "Invalid SDK definition "
170 return &s, fmt.Errorf(errMsg + "(name not set)")
171 } else if sdk.Profile == "" {
172 return &s, fmt.Errorf(errMsg + "(profile not set)")
173 } else if sdk.Version == "" {
174 return &s, fmt.Errorf(errMsg + "(version not set)")
175 } else if sdk.Arch == "" {
176 return &s, fmt.Errorf(errMsg + "(arch not set)")
178 if sdk.Status == xsapiv1.SdkStatusInstalled {
179 if sdk.SetupFile == "" {
180 return &s, fmt.Errorf(errMsg + "(setupFile not set)")
181 } else if !common.Exists(sdk.SetupFile) {
182 return &s, fmt.Errorf(errMsg + "(setupFile not accessible)")
185 return &s, fmt.Errorf(errMsg + "(path not set)")
186 } else if !common.Exists(sdk.Path) {
187 return &s, fmt.Errorf(errMsg + "(path not accessible)")
191 // Use V3 to ensure that we get same uuid on restart
194 nm = s.sdk.Profile + "_" + s.sdk.Arch + "_" + s.sdk.Version
196 s.sdk.ID = uuid.NewV3(uuid.FromStringOrNil("sdks"), nm).String()
198 s.LogSillyf("New SDK: ID=%v, Family=%s, Name=%v", s.sdk.ID[:8], s.sdk.FamilyConf.FamilyName, s.sdk.Name)
203 // Install a SDK (non blocking command, IOW run in background)
204 func (s *CrossSDK) Install(file string, force bool, timeout int, args []string, sess *ClientSession) error {
206 if s.sdk.Status == xsapiv1.SdkStatusInstalled {
207 return fmt.Errorf("already installed")
209 if s.sdk.Status == xsapiv1.SdkStatusInstalling {
210 return fmt.Errorf("installation in progress")
213 // Compute command args
214 cmdArgs := []string{}
216 cmdArgs = append(cmdArgs, "--file", file)
218 cmdArgs = append(cmdArgs, "--url", s.sdk.URL)
221 cmdArgs = append(cmdArgs, "--force")
224 // Append additional args (passthrough arguments)
226 cmdArgs = append(cmdArgs, args...)
231 cmdID := "sdk-install-" + strconv.Itoa(sdkCmdID)
233 // Create new instance to execute command and sent output over WS
234 s.installCmd = eows.New(s.scripts[scriptAdd], cmdArgs, sess.IOSocket, sess.ID, cmdID)
235 s.installCmd.Log = s.Log
237 s.installCmd.CmdExecTimeout = timeout
239 s.installCmd.CmdExecTimeout = 30 * 60 // default 30min
242 // FIXME: temporary hack
246 SizeBufStderr := 2000
247 if valS, ok := os.LookupEnv("XDS_SDK_BUF_STDOUT"); ok {
248 if valI, err := strconv.Atoi(valS); err == nil {
252 if valS, ok := os.LookupEnv("XDS_SDK_BUF_STDERR"); ok {
253 if valI, err := strconv.Atoi(valS); err == nil {
258 // Define callback for output (stdout+stderr)
259 s.installCmd.OutputCB = func(e *eows.ExecOverWS, stdout, stderr string) {
262 sdkID := (*data)["SDKID"].(string)
263 if sdkID != s.sdk.ID {
264 s.Log.Errorln("BUG: sdk ID differs: %v != %v", sdkID, s.sdk.ID)
267 // IO socket can be nil when disconnected
268 so := s.sessions.IOSocketGet(e.Sid)
270 s.Log.Infof("%s not emitted: WS closed (sid:%s, msgid:%s)", xsapiv1.EVTSDKInstall, e.Sid, e.CmdID)
275 s.Log.Debugf("%s emitted - WS sid[4:] %s - id:%s - SDK ID:%s:", xsapiv1.EVTSDKInstall, e.Sid[4:], e.CmdID, sdkID[:16])
277 s.Log.Debugf("STDOUT <<%v>>", strings.Replace(stdout, "\n", "\\n", -1))
280 s.Log.Debugf("STDERR <<%v>>", strings.Replace(stderr, "\n", "\\n", -1))
284 // Temporary "Hack": Buffered sent data to avoid freeze in web Browser
285 // FIXME: remove bufStdout & bufStderr and implement better algorithm
286 s.bufStdout += stdout
287 s.bufStderr += stderr
288 if len(s.bufStdout) > SizeBufStdout || len(s.bufStderr) > SizeBufStderr {
290 err := (*so).Emit(xsapiv1.EVTSDKInstall, xsapiv1.SDKManagementMsg{
292 Timestamp: time.Now().String(),
294 Progress: 0, // TODO add progress
300 s.Log.Errorf("WS Emit : %v", err)
307 // Define callback for output
308 s.installCmd.ExitCB = func(e *eows.ExecOverWS, code int, exitError error) {
311 sdkID := (*data)["SDKID"].(string)
312 if sdkID != s.sdk.ID {
313 s.Log.Errorln("BUG: sdk ID differs: %v != %v", sdkID, s.sdk.ID)
316 s.Log.Infof("Command SDK ID %s [Cmd ID %s] exited: code %d, exitError: %v", sdkID[:16], e.CmdID, code, exitError)
318 // IO socket can be nil when disconnected
319 so := s.sessions.IOSocketGet(e.Sid)
321 s.Log.Infof("%s (exit) not emitted - WS closed (id:%s)", xsapiv1.EVTSDKInstall, e.CmdID)
325 // Emit event remaining data in bufStdout/err
326 if len(s.bufStderr) > 0 || len(s.bufStdout) > 0 {
327 err := (*so).Emit(xsapiv1.EVTSDKInstall, xsapiv1.SDKManagementMsg{
329 Timestamp: time.Now().String(),
331 Progress: 50, // TODO add progress
337 s.Log.Errorf("WS Emit : %v", err)
344 if code == 0 && exitError == nil {
346 s.sdk.Status = xsapiv1.SdkStatusInstalled
348 // FIXME: better update it using monitoring install dir (inotify)
349 // (see sdks.go / monitorSDKInstallation )
350 // Update SetupFile when n
351 if s.sdk.SetupFile == "" {
352 sdkDef, err := GetSDKInfo(s.sdk.FamilyConf.ScriptsDir, s.sdk.URL, "", "", s.Log)
353 if err != nil || sdkDef.SetupFile == "" {
355 s.sdk.LastError = "Installation failed (cannot init SetupFile path)"
356 s.sdk.Status = xsapiv1.SdkStatusNotInstalled
358 s.sdk.SetupFile = sdkDef.SetupFile
363 s.sdk.LastError = "Installation failed (code " + strconv.Itoa(code) +
365 if exitError != nil {
366 s.sdk.LastError = ". Error: " + exitError.Error()
368 s.sdk.Status = xsapiv1.SdkStatusNotInstalled
372 if exitError != nil {
373 emitErr = exitError.Error()
375 if emitErr == "" && s.sdk.LastError != "" {
376 emitErr = s.sdk.LastError
380 errSoEmit := (*so).Emit(xsapiv1.EVTSDKInstall, xsapiv1.SDKManagementMsg{
382 Timestamp: time.Now().String(),
389 if errSoEmit != nil {
390 s.Log.Errorf("WS Emit : %v", errSoEmit)
393 // Cleanup command for the next time
397 // User data (used within callbacks)
398 data := make(map[string]interface{})
399 data["SDKID"] = s.sdk.ID
400 s.installCmd.UserData = &data
402 // Start command execution
403 s.Log.Infof("Install SDK %s: cmdID=%v, cmd=%v, args=%v", s.sdk.Name, s.installCmd.CmdID, s.installCmd.Cmd, s.installCmd.Args)
405 s.sdk.Status = xsapiv1.SdkStatusInstalling
408 err := s.installCmd.Start()
413 // AbortInstallRemove abort an install or remove command
414 func (s *CrossSDK) AbortInstallRemove(timeout int) error {
416 if s.installCmd == nil {
417 return fmt.Errorf("no installation in progress for this sdk")
420 s.sdk.Status = xsapiv1.SdkStatusNotInstalled
421 return s.installCmd.Signal("SIGKILL")
424 // Remove Used to remove/uninstall a SDK
425 func (s *CrossSDK) Remove(timeout int, sess *ClientSession) error {
427 if s.sdk.Status != xsapiv1.SdkStatusInstalled {
428 return fmt.Errorf("this sdk is not installed")
431 // IO socket can be nil when disconnected
432 so := s.sessions.IOSocketGet(sess.ID)
434 return fmt.Errorf("Cannot retrieve socket ")
437 s.sdk.Status = xsapiv1.SdkStatusUninstalling
440 if err := (*so).Emit(xsapiv1.EVTSDKStateChange, s.sdk); err != nil {
441 s.Log.Warningf("Cannot notify SDK remove: %v", err)
444 script := s.scripts[scriptRemove]
446 s.Log.Infof("Uninstall SDK %s: script=%v args=%v", s.sdk.Name, script, args)
448 cmd := exec.Command(script, args)
449 stdout, err := cmd.CombinedOutput()
451 s.sdk.Status = xsapiv1.SdkStatusNotInstalled
452 s.Log.Debugf("SDK uninstall err %v, output:\n %v", err, string(stdout))
457 evData := xsapiv1.SDKManagementMsg{
458 Timestamp: time.Now().String(),
465 if err := (*so).Emit(xsapiv1.EVTSDKRemove, evData); err != nil {
466 s.Log.Warningf("Cannot notify SDK remove end: %v", err)
469 return fmt.Errorf("Error while uninstalling sdk: %v", err)
473 evData := xsapiv1.SDKManagementMsg{
474 Timestamp: time.Now().String(),
481 if err := (*so).Emit(xsapiv1.EVTSDKRemove, evData); err != nil {
482 s.Log.Warningf("Cannot notify SDK remove end: %v", err)
488 // Get Return SDK definition
489 func (s *CrossSDK) Get() *xsapiv1.SDK {
493 // GetEnvCmd returns the command used to initialized the environment
494 func (s *CrossSDK) GetEnvCmd() []string {
495 return []string{"source", s.sdk.SetupFile}