From 0a53346ecd92e1281587c724631fdf76dc756fc7 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Tue, 6 Feb 2018 17:07:30 +0100 Subject: [PATCH] Auto update SDK DB on startup. Auto update db is enable by default (setting sdkDbUpdate = "startup") but it can be disable by setting sdkDbUpdate to "disable" in server config file. Signed-off-by: Sebastien Douheret --- lib/xdsconfig/config.go | 2 ++ lib/xdsconfig/fileconfig.go | 4 ++++ lib/xdsserver/sdk.go | 20 +++++++++++++++++++- lib/xdsserver/sdks.go | 9 ++++++++- scripts/sdks/agl/db-update | 19 ++++++++++++++++++- 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 11960c9..335c116 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -56,6 +56,7 @@ const ( DefaultShareDir = "${HOME}/.xds/server/projects" DefaultSTHomeDir = "${HOME}/.xds/server/syncthing-config" DefaultSdkScriptsDir = "${EXEPATH}/sdks" + DefaultSdkDbUpdate = "startup" ) // Init loads the configuration on start-up @@ -98,6 +99,7 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { WebAppDir: "webapp/dist", ShareRootDir: dfltShareDir, SdkScriptsDir: DefaultSdkScriptsDir, + SdkDbUpdate: DefaultSdkDbUpdate, HTTPPort: DefaultPort, SThgConf: &SyncThingConf{Home: dfltSTHomeDir}, LogsDir: "", diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 69467b4..ef207ec 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -54,6 +54,7 @@ type FileConfig struct { WebAppDir string `json:"webAppDir"` ShareRootDir string `json:"shareRootDir"` SdkScriptsDir string `json:"sdkScriptsDir"` + SdkDbUpdate string `json:"sdkDbUpdate"` HTTPPort string `json:"httpPort"` SThgConf *SyncThingConf `json:"syncthing"` LogsDir string `json:"logsDir"` @@ -139,6 +140,9 @@ func readGlobalConfig(c *Config, confFile string) error { if fCfg.SdkScriptsDir == "" { fCfg.SdkScriptsDir = c.FileConf.SdkScriptsDir } + if fCfg.SdkDbUpdate == "" { + fCfg.SdkDbUpdate = c.FileConf.SdkDbUpdate + } if fCfg.HTTPPort == "" { fCfg.HTTPPort = c.FileConf.HTTPPort } diff --git a/lib/xdsserver/sdk.go b/lib/xdsserver/sdk.go index 2736246..374fb12 100644 --- a/lib/xdsserver/sdk.go +++ b/lib/xdsserver/sdk.go @@ -64,9 +64,18 @@ type CrossSDK struct { } // ListCrossSDK List all available and installed SDK (call "db-dump" script) -func ListCrossSDK(scriptDir string, log *logrus.Logger) ([]xsapiv1.SDK, error) { +func ListCrossSDK(scriptDir string, update bool, log *logrus.Logger) ([]xsapiv1.SDK, error) { sdksList := []xsapiv1.SDK{} + // First update sdk DB when requested + if update { + out, err := UpdateSDKDb(scriptDir, log) + if err != nil { + log.Errorf("SDK DB update failure (%v): %v", err, out) + return sdksList, fmt.Errorf("Error while updating SDK DB (%v)", err) + } + } + // Retrieve SDKs list and info cmd := exec.Command(path.Join(scriptDir, scriptDbDump)) stdout, err := cmd.CombinedOutput() @@ -111,6 +120,15 @@ func GetSDKInfo(scriptDir, url, filename, md5sum string, log *logrus.Logger) (xs return sdk, nil } +// UpdateSDKDb Used db-update script to update SDK database +func UpdateSDKDb(scriptDir string, log *logrus.Logger) (string, error) { + args := []string{} + cmd := exec.Command(path.Join(scriptDir, scriptDbUpdate), args...) + stdout, err := cmd.CombinedOutput() + + return string(stdout), err +} + // NewCrossSDK creates a new instance of CrossSDK func NewCrossSDK(ctx *Context, sdk xsapiv1.SDK, scriptDir string) (*CrossSDK, error) { s := CrossSDK{ diff --git a/lib/xdsserver/sdks.go b/lib/xdsserver/sdks.go index c006861..6094045 100644 --- a/lib/xdsserver/sdks.go +++ b/lib/xdsserver/sdks.go @@ -63,6 +63,12 @@ func NewSDKs(ctx *Context) (*SDKs, error) { return &s, err } + // Update SDK DB on startup by default (can be disable using config file) + update := true + if s.Config.FileConf.SdkDbUpdate != "startup" { + update = false + } + s.mutex.Lock() defer s.mutex.Unlock() @@ -73,10 +79,11 @@ func NewSDKs(ctx *Context) (*SDKs, error) { continue } - sdksList, err := ListCrossSDK(d, s.Log) + sdksList, err := ListCrossSDK(d, update, s.Log) if err != nil { // allow to use XDS even if error on list s.Log.Errorf("Cannot retrieve SDK list: %v", err) + sdksList, _ = ListCrossSDK(d, false, s.Log) } s.LogSillyf("'%s' SDKs list: %v", d, sdksList) diff --git a/scripts/sdks/agl/db-update b/scripts/sdks/agl/db-update index 68bd26a..7ebd928 100755 --- a/scripts/sdks/agl/db-update +++ b/scripts/sdks/agl/db-update @@ -23,6 +23,17 @@ sdksDBFile=${1} [ "${sdksDBFile}" = "" ] && sdksDBFile=${SDK_ROOT_DIR}/sdks_latest.json +# Restore previous file on error +exitCode=0 +trap "OnExit" 0 1 2 15 +OnExit () +{ + if [ "${exitCode}" != "0" ] && [ -f "${sdksDBFile}.old" ]; then + echo "Error detected, restore previous database version" + mv "${sdksDBFile}.old" "${sdksDBFile}" + fi +} + # Backup previous file [ -f "${sdksDBFile}" ] && mv "${sdksDBFile}" "${sdksDBFile}.old" @@ -31,4 +42,10 @@ instDir=$(dirname "${sdksDBFile}") [ ! -d "${instDir}" ] && mkdir -p "${instDir}" # Get database -wget -q --connect-timeout=30 "${SDK_DATABASE}" -O "${sdksDBFile}" +echo "Updating AGL SDK database..." +wget --no-verbose --connect-timeout=30 "${SDK_DATABASE}" -O "${sdksDBFile}" +exitCode=$? + +[ "${exitCode}" = "0" ] && echo "Done: AGL SDK database is up-to-date" + +exit $exitCode -- 2.16.6