Fixed sdk from local file installation
[src/xds/xds-server.git] / lib / xdsserver / sdk.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package xdsserver
19
20 import (
21         "encoding/json"
22         "fmt"
23         "os/exec"
24         "path"
25         "strconv"
26         "strings"
27         "time"
28
29         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
30         "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib/eows"
31         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
32         "github.com/Sirupsen/logrus"
33         uuid "github.com/satori/go.uuid"
34 )
35
36 // Definition of scripts used to managed SDKs
37 const (
38         scriptAdd          = "add"
39         scriptDbDump       = "db-dump"
40         scriptDbUpdate     = "db-update"
41         scriptGetFamConfig = "get-family-config"
42         scriptGetSdkInfo   = "get-sdk-info"
43         scriptRemove       = "remove"
44 )
45
46 var scriptsAll = []string{
47         scriptAdd,
48         scriptDbDump,
49         scriptDbUpdate,
50         scriptGetFamConfig,
51         scriptGetSdkInfo,
52         scriptRemove,
53 }
54
55 var sdkCmdID = 0
56
57 // CrossSDK Hold SDK config
58 type CrossSDK struct {
59         *Context
60         sdk        xsapiv1.SDK
61         scripts    map[string]string
62         installCmd *eows.ExecOverWS
63         removeCmd  *eows.ExecOverWS
64 }
65
66 // ListCrossSDK List all available and installed SDK  (call "db-dump" script)
67 func ListCrossSDK(scriptDir string, update bool, log *logrus.Logger) ([]xsapiv1.SDK, error) {
68         sdksList := []xsapiv1.SDK{}
69
70         // First update sdk DB when requested
71         if update {
72                 out, err := UpdateSDKDb(scriptDir, log)
73                 if err != nil {
74                         log.Errorf("SDK DB update failure (%v): %v", err, out)
75                         return sdksList, fmt.Errorf("Error while updating SDK DB (%v)", err)
76                 }
77         }
78
79         // Retrieve SDKs list and info
80         cmd := exec.Command(path.Join(scriptDir, scriptDbDump))
81         stdout, err := cmd.CombinedOutput()
82         if err != nil {
83                 return sdksList, fmt.Errorf("Cannot get sdks list: %v", err)
84         }
85
86         if err = json.Unmarshal(stdout, &sdksList); err != nil {
87                 log.Errorf("SDK %s script output:\n%v\n", scriptDbDump, string(stdout))
88                 return sdksList, fmt.Errorf("Cannot decode sdk list %v", err)
89         }
90
91         return sdksList, nil
92 }
93
94 // GetSDKInfo Used get-sdk-info script to extract SDK get info from a SDK file/tarball
95 func GetSDKInfo(scriptDir, url, filename, md5sum, uuid string, log *logrus.Logger) (xsapiv1.SDK, error) {
96         sdk := xsapiv1.SDK{}
97
98         args := []string{}
99         if url != "" {
100                 args = append(args, "--url", url)
101         } else if filename != "" {
102                 args = append(args, "--file", filename)
103                 if md5sum != "" {
104                         args = append(args, "--md5", md5sum)
105                 }
106         } else {
107                 return sdk, fmt.Errorf("url of filename must be set")
108         }
109         if uuid != "" {
110                 args = append(args, "--uuid", uuid)
111         }
112
113         cmd := exec.Command(path.Join(scriptDir, scriptGetSdkInfo), args...)
114         stdout, err := cmd.CombinedOutput()
115         if err != nil {
116                 return sdk, fmt.Errorf("%v %v", string(stdout), err)
117         }
118
119         if err = json.Unmarshal(stdout, &sdk); err != nil {
120                 log.Errorf("SDK %s script output:\n%v\n", scriptGetSdkInfo, string(stdout))
121                 return sdk, fmt.Errorf("Cannot decode sdk info %v", err)
122         }
123         return sdk, nil
124 }
125
126 // UpdateSDKDb Used db-update script to update SDK database
127 func UpdateSDKDb(scriptDir string, log *logrus.Logger) (string, error) {
128         args := []string{}
129         cmd := exec.Command(path.Join(scriptDir, scriptDbUpdate), args...)
130         stdout, err := cmd.CombinedOutput()
131
132         return string(stdout), err
133 }
134
135 // NewCrossSDK creates a new instance of CrossSDK
136 func NewCrossSDK(ctx *Context, sdk xsapiv1.SDK, scriptDir string) (*CrossSDK, error) {
137         s := CrossSDK{
138                 Context: ctx,
139                 sdk:     sdk,
140                 scripts: make(map[string]string),
141         }
142
143         // Execute get-config script to retrieve SDK configuration
144         getConfFile := path.Join(scriptDir, scriptGetFamConfig)
145         if !common.Exists(getConfFile) {
146                 return &s, fmt.Errorf("'%s' script file not found in %s", scriptGetFamConfig, scriptDir)
147         }
148
149         cmd := exec.Command(getConfFile)
150         stdout, err := cmd.CombinedOutput()
151         if err != nil {
152                 return &s, fmt.Errorf("Cannot get sdk config using %s: %v", getConfFile, err)
153         }
154
155         err = json.Unmarshal(stdout, &s.sdk.FamilyConf)
156         if err != nil {
157                 s.Log.Errorf("SDK config script output:\n%v\n", string(stdout))
158                 return &s, fmt.Errorf("Cannot decode sdk config %v", err)
159         }
160         famName := s.sdk.FamilyConf.FamilyName
161
162         // Sanity check
163         if s.sdk.FamilyConf.RootDir == "" {
164                 return &s, fmt.Errorf("SDK config not valid (rootDir not set)")
165         }
166         if s.sdk.FamilyConf.EnvSetupFile == "" {
167                 return &s, fmt.Errorf("SDK config not valid (envSetupFile not set)")
168         }
169
170         // Check that other mandatory scripts are present
171         for _, scr := range scriptsAll {
172                 s.scripts[scr] = path.Join(scriptDir, scr)
173                 if !common.Exists(s.scripts[scr]) {
174                         return &s, fmt.Errorf("Script named '%s' missing in SDK family '%s'", scr, famName)
175                 }
176         }
177
178         // Fixed default fields value
179         sdk.LastError = ""
180         if sdk.Status == "" {
181                 sdk.Status = xsapiv1.SdkStatusNotInstalled
182         }
183
184         // Sanity check
185         errMsg := "Invalid SDK definition "
186         if sdk.Name == "" {
187                 return &s, fmt.Errorf(errMsg + "(name not set)")
188         } else if sdk.Profile == "" {
189                 return &s, fmt.Errorf(errMsg + "(profile not set)")
190         } else if sdk.Version == "" {
191                 return &s, fmt.Errorf(errMsg + "(version not set)")
192         } else if sdk.Arch == "" {
193                 return &s, fmt.Errorf(errMsg + "(arch not set)")
194         }
195         if sdk.Status == xsapiv1.SdkStatusInstalled {
196                 if sdk.SetupFile == "" {
197                         return &s, fmt.Errorf(errMsg + "(setupFile not set)")
198                 } else if !common.Exists(sdk.SetupFile) {
199                         return &s, fmt.Errorf(errMsg + "(setupFile not accessible)")
200                 }
201                 if sdk.Path == "" {
202                         return &s, fmt.Errorf(errMsg + "(path not set)")
203                 } else if !common.Exists(sdk.Path) {
204                         return &s, fmt.Errorf(errMsg + "(path not accessible)")
205                 }
206         }
207
208         // Use V3 to ensure that we get same uuid on restart
209         nm := s.sdk.Name
210         if nm == "" {
211                 nm = s.sdk.Profile + "_" + s.sdk.Arch + "_" + s.sdk.Version
212         }
213         s.sdk.ID = uuid.NewV3(uuid.FromStringOrNil("sdks"), nm).String()
214
215         s.LogSillyf("New SDK: ID=%v, Family=%s, Name=%v", s.sdk.ID[:8], s.sdk.FamilyConf.FamilyName, s.sdk.Name)
216
217         return &s, nil
218 }
219
220 // Install a SDK (non blocking command, IOW run in background)
221 func (s *CrossSDK) Install(file string, force bool, timeout int, args []string, sess *ClientSession) error {
222
223         if s.sdk.Status == xsapiv1.SdkStatusInstalled {
224                 return fmt.Errorf("already installed")
225         }
226         if s.sdk.Status == xsapiv1.SdkStatusInstalling {
227                 return fmt.Errorf("installation in progress")
228         }
229
230         // Compute command args
231         cmdArgs := []string{}
232         if file != "" {
233                 cmdArgs = append(cmdArgs, "--file", file)
234         } else {
235                 cmdArgs = append(cmdArgs, "--url", s.sdk.URL)
236         }
237         if force {
238                 cmdArgs = append(cmdArgs, "--force")
239         }
240
241         // Append additional args (passthrough arguments)
242         if len(args) > 0 {
243                 cmdArgs = append(cmdArgs, args...)
244         }
245
246         // Unique command id
247         sdkCmdID++
248         cmdID := "sdk-install-" + strconv.Itoa(sdkCmdID)
249
250         // Create new instance to execute command and sent output over WS
251         s.installCmd = eows.New(s.scripts[scriptAdd], cmdArgs, sess.IOSocket, sess.ID, cmdID)
252         s.installCmd.Log = s.Log
253         // TODO: enable Term s.installCmd.PtyMode = true
254         s.installCmd.LineTimeSpan = 500 * time.Millisecond.Nanoseconds()
255         if timeout > 0 {
256                 s.installCmd.CmdExecTimeout = timeout
257         } else {
258                 s.installCmd.CmdExecTimeout = 30 * 60 // default 30min
259         }
260
261         // Define callback for output (stdout+stderr)
262         s.installCmd.OutputCB = func(e *eows.ExecOverWS, bStdout, bStderr []byte) {
263
264                 stdout := string(bStdout)
265                 stderr := string(bStderr)
266
267                 // paranoia
268                 data := e.UserData
269                 sdkID := (*data)["SDKID"].(string)
270                 if sdkID != s.sdk.ID {
271                         s.Log.Errorln("BUG: sdk ID differs: %v != %v", sdkID, s.sdk.ID)
272                 }
273
274                 // IO socket can be nil when disconnected
275                 so := s.sessions.IOSocketGet(e.Sid)
276                 if so == nil {
277                         s.Log.Infof("%s not emitted: WS closed (sid:%s, msgid:%s)", xsapiv1.EVTSDKManagement, e.Sid, e.CmdID)
278                         return
279                 }
280
281                 if s.LogLevelSilly {
282                         s.Log.Debugf("%s emitted - WS sid[4:] %s - id:%s - SDK ID:%s:", xsapiv1.EVTSDKManagement, e.Sid[4:], e.CmdID, sdkID[:16])
283                         if stdout != "" {
284                                 s.Log.Debugf("STDOUT <<%v>>", strings.Replace(stdout, "\n", "\\n", -1))
285                         }
286                         if stderr != "" {
287                                 s.Log.Debugf("STDERR <<%v>>", strings.Replace(stderr, "\n", "\\n", -1))
288                         }
289                 }
290
291                 err := (*so).Emit(xsapiv1.EVTSDKManagement, xsapiv1.SDKManagementMsg{
292                         CmdID:     e.CmdID,
293                         Timestamp: time.Now().String(),
294                         Action:    xsapiv1.SdkMgtActionInstall,
295                         Sdk:       s.sdk,
296                         Progress:  0, // TODO add progress
297                         Exited:    false,
298                         Stdout:    stdout,
299                         Stderr:    stderr,
300                 })
301                 if err != nil {
302                         s.Log.Errorf("WS Emit : %v", err)
303                 }
304         }
305
306         // Define callback for output
307         s.installCmd.ExitCB = func(e *eows.ExecOverWS, code int, exitError error) {
308                 // paranoia
309                 data := e.UserData
310                 sdkID := (*data)["SDKID"].(string)
311                 if sdkID != s.sdk.ID {
312                         s.Log.Errorln("BUG: sdk ID differs: %v != %v", sdkID, s.sdk.ID)
313                 }
314
315                 s.Log.Infof("Command SDK ID %s [Cmd ID %s]  exited: code %d, exitError: %v", sdkID[:16], e.CmdID, code, exitError)
316
317                 // IO socket can be nil when disconnected
318                 so := s.sessions.IOSocketGet(e.Sid)
319                 if so == nil {
320                         s.Log.Infof("%s (exit) not emitted - WS closed (id:%s)", xsapiv1.EVTSDKManagement, e.CmdID)
321                         return
322                 }
323
324                 // Update SDK status
325                 if code == 0 && exitError == nil {
326                         s.sdk.LastError = ""
327                         s.sdk.Status = xsapiv1.SdkStatusInstalled
328
329                         // FIXME: better update it using monitoring install dir (inotify)
330                         // (see sdks.go / monitorSDKInstallation )
331                         // Update SetupFile when n
332                         if s.sdk.SetupFile == "" {
333                                 sdkDef, err := GetSDKInfo(s.sdk.FamilyConf.ScriptsDir, s.sdk.URL, "", "", s.sdk.UUID, s.Log)
334                                 if err != nil || sdkDef.SetupFile == "" {
335                                         s.Log.Errorf("GetSDKInfo error: %v", err)
336                                         code = 1
337                                         s.sdk.LastError = "Installation failed (cannot init SetupFile path)"
338                                         s.sdk.Status = xsapiv1.SdkStatusNotInstalled
339                                 } else {
340                                         s.sdk.SetupFile = sdkDef.SetupFile
341                                 }
342                         }
343
344                 } else {
345                         s.sdk.LastError = "Installation failed (code " + strconv.Itoa(code) +
346                                 ")"
347                         if exitError != nil {
348                                 s.sdk.LastError = ". Error: " + exitError.Error()
349                         }
350                         s.sdk.Status = xsapiv1.SdkStatusNotInstalled
351                 }
352
353                 emitErr := ""
354                 if exitError != nil {
355                         emitErr = exitError.Error()
356                 }
357                 if emitErr == "" && s.sdk.LastError != "" {
358                         emitErr = s.sdk.LastError
359                 }
360
361                 // Emit event
362                 errSoEmit := (*so).Emit(xsapiv1.EVTSDKManagement, xsapiv1.SDKManagementMsg{
363                         CmdID:     e.CmdID,
364                         Timestamp: time.Now().String(),
365                         Action:    xsapiv1.SdkMgtActionInstall,
366                         Sdk:       s.sdk,
367                         Progress:  100,
368                         Exited:    true,
369                         Code:      code,
370                         Error:     emitErr,
371                 })
372                 if errSoEmit != nil {
373                         s.Log.Errorf("WS Emit EVTSDKManagement : %v", errSoEmit)
374                 }
375
376                 errSoEmit = s.events.Emit(xsapiv1.EVTSDKStateChange, s.sdk, e.Sid)
377                 if errSoEmit != nil {
378                         s.Log.Errorf("WS Emit EVTSDKStateChange : %v", errSoEmit)
379                 }
380
381                 // Cleanup command for the next time
382                 s.installCmd = nil
383         }
384
385         // User data (used within callbacks)
386         data := make(map[string]interface{})
387         data["SDKID"] = s.sdk.ID
388         s.installCmd.UserData = &data
389
390         // Start command execution
391         s.Log.Infof("Install SDK %s: cmdID=%v, cmd=%v, args=%v", s.sdk.Name, s.installCmd.CmdID, s.installCmd.Cmd, s.installCmd.Args)
392
393         s.sdk.Status = xsapiv1.SdkStatusInstalling
394         s.sdk.LastError = ""
395
396         err := s.installCmd.Start()
397
398         return err
399 }
400
401 // AbortInstallRemove abort an install or remove command
402 func (s *CrossSDK) AbortInstallRemove(timeout int) error {
403
404         if s.installCmd == nil {
405                 return fmt.Errorf("no installation in progress for this sdk")
406         }
407
408         s.sdk.Status = xsapiv1.SdkStatusNotInstalled
409         return s.installCmd.Signal("SIGKILL")
410 }
411
412 // Remove Used to remove/uninstall a SDK
413 func (s *CrossSDK) Remove(timeout int, sess *ClientSession) error {
414
415         if s.sdk.Status != xsapiv1.SdkStatusInstalled {
416                 return fmt.Errorf("this sdk is not installed")
417         }
418
419         // IO socket can be nil when disconnected
420         so := s.sessions.IOSocketGet(sess.ID)
421         if so == nil {
422                 return fmt.Errorf("Cannot retrieve socket ")
423         }
424
425         s.sdk.Status = xsapiv1.SdkStatusUninstalling
426
427         // Notify state change
428         if err := s.events.Emit(xsapiv1.EVTSDKStateChange, s.sdk, sess.ID); err != nil {
429                 s.Log.Warningf("Cannot notify SDK remove: %v", err)
430         }
431
432         script := s.scripts[scriptRemove]
433         args := s.sdk.Path
434         s.Log.Infof("Uninstall SDK %s: script=%v args=%v", s.sdk.Name, script, args)
435
436         // Notify start removing
437         evData := xsapiv1.SDKManagementMsg{
438                 Timestamp: time.Now().String(),
439                 Action:    xsapiv1.SdkMgtActionRemove,
440                 Sdk:       s.sdk,
441                 Progress:  0,
442                 Exited:    false,
443                 Code:      0,
444                 Error:     "",
445         }
446         if errEmit := (*so).Emit(xsapiv1.EVTSDKManagement, evData); errEmit != nil {
447                 s.Log.Warningf("Cannot notify EVTSDKManagement end: %v", errEmit)
448         }
449
450         // Run command to remove SDK
451         cmd := exec.Command(script, args)
452         stdout, err := cmd.CombinedOutput()
453
454         s.sdk.Status = xsapiv1.SdkStatusNotInstalled
455         s.Log.Debugf("SDK uninstall err %v, output:\n %v", err, string(stdout))
456
457         // Emit end of removing process
458         evData = xsapiv1.SDKManagementMsg{
459                 Timestamp: time.Now().String(),
460                 Action:    xsapiv1.SdkMgtActionRemove,
461                 Sdk:       s.sdk,
462                 Progress:  100,
463                 Exited:    true,
464                 Code:      0,
465                 Error:     "",
466         }
467
468         // Update error code on error
469         if err != nil {
470                 evData.Code = 1
471                 evData.Error = err.Error()
472         }
473
474         if errEmit := (*so).Emit(xsapiv1.EVTSDKManagement, evData); errEmit != nil {
475                 s.Log.Warningf("Cannot notify EVTSDKManagement end: %v", errEmit)
476         }
477
478         // Notify state change
479         if errEmit := s.events.Emit(xsapiv1.EVTSDKStateChange, s.sdk, sess.ID); errEmit != nil {
480                 s.Log.Warningf("Cannot notify EVTSDKStateChange end: %v", errEmit)
481         }
482
483         if err != nil {
484                 return fmt.Errorf("Error while uninstalling sdk: %v", err)
485         }
486         return nil
487 }
488
489 // Get Return SDK definition
490 func (s *CrossSDK) Get() *xsapiv1.SDK {
491         return &s.sdk
492 }
493
494 // GetEnvCmd returns the command used to initialized the environment
495 func (s *CrossSDK) GetEnvCmd() []string {
496         if s.sdk.SetupFile == "" {
497                 return []string{}
498         }
499         return []string{"source", s.sdk.SetupFile}
500
501 }