From 54c13efe6ae1bcc00cb439782dd893bc4ecb9d59 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 20 Jan 2016 14:40:12 -0800 Subject: [PATCH] add migration for stack manager content --- server-console/package.json | 2 +- server-console/src/main.js | 152 +++++++++++++++++++++-- server-console/src/modules/hf-process.js | 5 +- 3 files changed, 147 insertions(+), 12 deletions(-) diff --git a/server-console/package.json b/server-console/package.json index 3e74186122..44f7b3d05e 100644 --- a/server-console/package.json +++ b/server-console/package.json @@ -27,7 +27,7 @@ "always-tail": "0.2.0", "cheerio": "^0.19.0", "extend": "^3.0.0", - "mkdirp": "^0.5.1", + "fs-extra": "^0.26.4", "node-notifier": "^4.4.0", "os-homedir": "^1.0.1", "request": "2.67.0", diff --git a/server-console/src/main.js b/server-console/src/main.js index e6eba4d605..594f5afddc 100644 --- a/server-console/src/main.js +++ b/server-console/src/main.js @@ -13,7 +13,7 @@ const shell = require('shell'); const os = require('os'); const childProcess = require('child_process'); const path = require('path'); -const fs = require('fs'); +const fs = require('fs-extra'); const Tail = require('always-tail'); const http = require('http'); const unzip = require('unzip'); @@ -72,7 +72,6 @@ const buildInfo = getBuildInfo(); console.log("build info", buildInfo); - function getRootHifiDataDirectory() { var organization = "High Fidelity"; if (buildInfo.releaseType != "PRODUCTION") { @@ -260,12 +259,6 @@ global.domainServer = null; global.acMonitor = null; global.userConfig = userConfig; -const GO_HOME_INDEX = 2; -const SERVER_LABEL_INDEX = 0; -const RESTART_INDEX = 4; -const STOP_INDEX = 5; -const SETTINGS_INDEX = 6; - var LogWindow = function(ac, ds) { this.ac = ac; this.ds = ds; @@ -308,6 +301,132 @@ function goHomeClicked() { } } +function stackManagerBasePath() { + var dataPath = 'High Fidelity/Stack Manager/resources'; + + if (process.platform == "win32") { + return path.resolve(osHomeDir(), 'AppData/Local', dataPath); + } else if (process.platform == "darwin") { + return path.resolve(osHomeDir(), 'Library/Application Support', dataPath); + } else { + return "" + } +} + +function isStackManagerContentPresent() { + var modelsPath = path.resolve(stackManagerBasePath(), 'models.json.gz'); + + try { + var stats = fs.lstatSync(modelsPath); + + if (stats.isFile()) { + console.log("Stack Manager entities file discovered at " + modelsPath) + // we found a content file + return true; + } + } catch (e) { + console.log("Stack Manager entities file not found at " + modelsPath); + } +} + +function promptToMigrateContent() { + var idx = dialog.showMessageBox({ + type: 'question', + buttons: ['Yes', 'No'], + title: 'Migrate Content', + message: 'Are you sure?\n\nThis will stop your home server and replace everything in your home with your content from Stack Manager.' + }); + + if (idx == 0) { + if (homeServer.state != ProcessGroupStates.STOPPED) { + homeServer.on('state-update', function(processGroup) { + if (processGroup.state == ProcessGroupStates.STOPPED) { + performContentMigration(); + } + }); + + homeServer.stop(); + + } else { + performContentMigration(); + } + } +} + +function performContentMigration() { + // check if there is a models file to migrate + var modelsPath = path.resolve(stackManagerBasePath(), 'models.json.gz'); + + try { + var stats = fs.lstatSync(modelsPath); + } catch (e) { + // no entities file + dialog.showMessageBox({ + type: 'info', + buttons: ['OK'], + title: 'Models File Not Found', + message: 'There is no models file at ' + modelsPath + '\n\nStack Manager content migration can not proceed.' + }); + + return; + } + + var copyError = null; + + function showMigrationCompletionDialog(copyError) { + if (!copyError) { + // show message for successful migration + dialog.showMessageBox({ + type: 'info', + buttons: ['OK'], + title: 'Migration Complete', + message: 'Your Stack Manager content has been migrated.\n\nYour home server will now be restarted.' + }); + } else { + // show error message for copy fail + dialog.showMessageBox({ + type: 'info', + buttons: ['OK'], + title: 'Migration Failed', + message: 'There was an error copying your Stack Manager content: ' + copyError + '\n\nPlease try again.' + }); + } + } + + // we have a models file, try and copy it + var newModelsPath = path.resolve(getAssignmentClientResourcesDirectory(), 'entities/models.json.gz') + console.log("Copying Stack Manager entity file from " + modelsPath + " to " + newModelsPath); + + fs.copy(modelsPath, newModelsPath, function(error){ + if (!error) { + // check if there are any assets to copy + var oldAssetsPath = path.resolve(stackManagerBasePath(), 'assets'); + + fs.readdir(oldAssetsPath, function(error, data){ + if (error) { + showMigrationCompletionDialog(error); + } else if (data.length > 0) { + + // assume this means the directory is not empty + // and that we should copy it + var newAssetsPath = path.resolve(getAssignmentClientResourcesDirectory(), 'assets'); + + console.log("Copying Stack Manager assets from " + oldAssetsPath + " to " + newAssetsPath); + + // attempt to copy the assets folder, show correct dialog depending on success/failure + fs.copy(oldAssetsPath, newAssetsPath, showMigrationCompletionDialog); + } else { + showMigrationCompletionDialog(null); + } + }); + } else { + showMigrationCompletionDialog(error); + } + }); + + homeServer.start(); +} + var logWindow = null; function buildMenuArray(serverState) { @@ -372,12 +491,29 @@ function buildMenuArray(serverState) { } ]; + var foundStackManagerContent = isStackManagerContentPresent(); + if (foundStackManagerContent) { + // add a separator and the stack manager content migration option + menuArray.splice(menuArray.length - 1, 0, { + label: 'Migrate Stack Manager Content', + click: function() { promptToMigrateContent(); } + }, { + type: 'separator' + }); + } + updateMenuArray(menuArray, serverState); } return menuArray; } +const GO_HOME_INDEX = 2; +const SERVER_LABEL_INDEX = 0; +const RESTART_INDEX = 4; +const STOP_INDEX = 5; +const SETTINGS_INDEX = 6; + function updateMenuArray(menuArray, serverState) { // update the tray menu state var running = serverState == ProcessGroupStates.STARTED; diff --git a/server-console/src/modules/hf-process.js b/server-console/src/modules/hf-process.js index 5192e44bb6..c7f21ebbe5 100755 --- a/server-console/src/modules/hf-process.js +++ b/server-console/src/modules/hf-process.js @@ -5,8 +5,7 @@ const extend = require('extend'); const util = require('util'); const events = require('events'); const childProcess = require('child_process'); -const fs = require('fs'); -const mkdirp = require('mkdirp'); +const fs = require('fs-extra'); const os = require('os'); const path = require('path'); @@ -133,7 +132,7 @@ Process.prototype = extend(Process.prototype, { var logDirectoryCreated = false; try { - mkdirp.sync(this.logDirectory); + fs.mkdirsSync(this.logDirectory); logDirectoryCreated = true; } catch (e) { if (e.code == 'EEXIST') {