diff --git a/lib/initInstance.js b/lib/initInstance.js index 04080d9..b7d60f2 100644 --- a/lib/initInstance.js +++ b/lib/initInstance.js @@ -1,4 +1,5 @@ -/* + + /* This program and the accompanying materials are made available under the terms of the Eclipse Public License v2.0 which accompanies this distribution, and is available at https://www.eclipse.org/legal/epl-v20.html @@ -19,8 +20,6 @@ const initUtils = require('./initUtils'); const os = require('os'); const { execSync } = require('child_process'); -initUtils.printFormattedDebug(`Started initInstance.js, platform=${os.platform()}`); - const haInstanceId = yamlConfig.getCurrentHaInstanceId(); let config = {}; if (process.env.CONFIG_FILE) { @@ -39,48 +38,54 @@ const destination = path.join(workspaceLocation, 'app-server'); const versionLocation = path.join(destination, 'component.json'); - - config.productDir = path.join(__dirname, '..', 'defaults'); -//Begin generate any missing folders -initUtils.mkdirp(destination, initUtils.FOLDER_MODE); +// Helper function to ensure directories exist +function ensureDirSync(dirPath, mode = initUtils.FOLDER_MODE) { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { mode, recursive: true }); + } +} + +// Begin generate any missing folders +ensureDirSync(destination); if (!config.siteDir) { config.siteDir = path.join(destination, 'site'); } const sitePluginStorage = path.join(config.siteDir, 'ZLUX', 'pluginStorage'); -initUtils.mkdirp(sitePluginStorage, initUtils.FOLDER_MODE); +ensureDirSync(sitePluginStorage); if (!config.instanceDir) { config.instanceDir = destination; } const instancePluginStorage = path.join(config.instanceDir, 'ZLUX', 'pluginStorage'); -initUtils.mkdirp(instancePluginStorage, initUtils.FOLDER_MODE); +ensureDirSync(instancePluginStorage); const recognizersPluginStorage = path.join(config.instanceDir, 'ZLUX/pluginStorage', 'org.zowe.zlux.ng2desktop/recognizers'); -initUtils.mkdirp(recognizersPluginStorage, initUtils.FOLDER_MODE); +ensureDirSync(recognizersPluginStorage); const actionsPluginStorage = path.join(config.instanceDir, 'ZLUX/pluginStorage/org.zowe.zlux.ng2desktop', 'actions'); -initUtils.mkdirp(actionsPluginStorage, initUtils.FOLDER_MODE); +ensureDirSync(actionsPluginStorage); const instanceConfig = path.join(config.instanceDir, 'serverConfig'); -//750 specifically, to keep server config secure -initUtils.mkdirp(instanceConfig, 0o0750); +// 750 specifically, to keep server config secure +if (!fs.existsSync(instanceConfig)) { + fs.mkdirSync(instanceConfig, { mode: 0o0750 }); +} if (!config.groupsDir) { config.groupsDir = path.join(config.instanceDir, 'groups'); } -initUtils.mkdirp(config.groupsDir, initUtils.FOLDER_MODE); +ensureDirSync(config.groupsDir); if (!config.usersDir) { config.usersDir = path.join(config.instanceDir, 'users'); } -initUtils.mkdirp(config.usersDir, initUtils.FOLDER_MODE); +ensureDirSync(config.usersDir); if (!config.pluginsDir) { config.pluginsDir = path.join(destination, 'plugins'); } - -initUtils.mkdirp(config.pluginsDir, initUtils.FOLDER_MODE); +ensureDirSync(config.pluginsDir); function generateComponentJson() { let componentJsonContent; @@ -88,39 +93,38 @@ function generateComponentJson() { componentJsonContent = require(versionLocation); } catch (e) { componentJsonContent = {}; - //doesnt exist, create new + // Doesn't exist, create new } let currentManifestJson; try { currentManifestJson = fs.readFileSync(path.join(__dirname, "../../../../../manifest.json")); } catch (e) { if (e.code == 'ENOENT') { - // createJson = true; + // File does not exist } else { - console.log('Warning: Could not read manifest.json, error='+e.message); + console.log('Warning: Could not read manifest.json, error=' + e.message); } } try { componentJsonContent.version = currentManifestJson.version; } catch (e) { - console.log('Warning: Could not read version from manifest.json, error='+e.message); + console.log('Warning: Could not read version from manifest.json, error=' + e.message); componentJsonContent.version = "0.0.0.0"; } - fs.writeFileSync(versionLocation, JSON.stringify(componentJsonContent)); + fs.writeFileSync(versionLocation, JSON.stringify(componentJsonContent)); } - function getPluginJsonNames() { try { return fs.readdirSync(config.pluginsDir); } catch (e) { - console.warn("ZWED5003W - Warning: couldn't read plugin directory",e); + console.warn("ZWED5003W - Warning: couldn't read plugin directory", e); } return []; } let instanceItems = getPluginJsonNames(); -//Copy default plugins if could not find zlux-server - implies something wrong with environment. +// Copy default plugins if could not find zlux-server - implies something wrong with environment. if (instanceItems.indexOf('org.zowe.zlux.json') == -1) { initUtils.registerBundledPlugins(config.pluginsDir, instancePluginStorage, instanceItems, initUtils.FILE_MODE); instanceItems = getPluginJsonNames(); @@ -131,30 +135,32 @@ if (instanceItems.indexOf('org.zowe.zlux.json') == -1) { } initUtils.setTerminalDefaults(instancePluginStorage, instanceItems); - + let siteStorage = []; let instanceStorage = []; try { siteStorage = fs.readdirSync(sitePluginStorage); instanceStorage = fs.readdirSync(instancePluginStorage); } catch (e) { - console.warn("ZWED5004W - Warning: couldn't read site or instance storage",e); - //couldnt read, treat as empty + console.warn("ZWED5004W - Warning: couldn't read site or instance storage", e); + // Couldn't read, treat as empty } if (siteStorage.length == 0 && instanceStorage.length == 0) { console.log("ZWED5012I - Copying default plugin preferences into instance"); if (os.platform() == 'win32') { - fs.cp(path.join(config.productDir, 'ZLUX', 'pluginStorage'), instancePluginStorage, {recursive: true, force: true}, function(err){ + fs.cp(path.join(config.productDir, 'ZLUX', 'pluginStorage'), instancePluginStorage, { recursive: true, force: true }, function (err) { if (err) { - console.warn('ZWED5005W - Warning: error while copying plugin preferences into instance',err); + console.warn('ZWED5005W - Warning: error while copying plugin preferences into instance', err); process.exit(1); } - generateComponentJson() + generateComponentJson(); + process.exit(0); }); } else { - execSync("cp -r "+path.join(config.productDir, 'ZLUX', 'pluginStorage')+" "+path.join(config.instanceDir, 'ZLUX')); - execSync("chmod -R 770 "+instancePluginStorage); - generateComponentJson() + execSync("cp -r " + path.join(config.productDir, 'ZLUX', 'pluginStorage') + " " + path.join(config.instanceDir, 'ZLUX')); + execSync("chmod -R 770 " + instancePluginStorage); + generateComponentJson(); + process.exit(0); } } @@ -163,16 +169,16 @@ if (siteStorage.length == 0 && instanceStorage.length == 0) { // Upgrade logic: If instance contains code from an older version that needs updating, apply the change here. // */ // try { -// let serverConfig = currentJsonConfig ? jsonUtils.readJSONStringWithComments(currentJsonConfig, 'server.json'): undefined; +// let serverConfig = currentJsonConfig ? jsonUtils.readJSONStringWithComments(currentJsonConfig, 'server.json') : undefined; // let fromVersion; // try { // fromVersion = process.env.ZOWE_UPGRADE_VERSION ? process.env.ZOWE_UPGRADE_VERSION : require(versionLocation).version; // } catch (e) { -// //pre 1.11 +// // Pre 1.11 // fromVersion = "1.10.99"; // } // if (serverConfig) { -// //upgrades based on what WAS there, not what we added above +// // Upgrades based on what WAS there, not what we added above // const result = upgradeInstance.doUpgrade(fromVersion, destination, serverConfig, instanceItems); // if (result) { // let componentJsonContent; @@ -180,12 +186,12 @@ if (siteStorage.length == 0 && instanceStorage.length == 0) { // componentJsonContent = require(versionLocation); // } catch (e) { // componentJsonContent = {}; -// //doesnt exist, create new +// // Doesn't exist, create new // } // componentJsonContent.version = result.upgradedTo; // fs.writeFileSync(versionLocation, JSON.stringify(componentJsonContent)); // if (result.serverConfig) { -// fs.writeFileSync(path.join(destination, 'serverConfig', 'server.json'), JSON.stringify(result.serverConfig,null,2)); +// fs.writeFileSync(path.join(destination, 'serverConfig', 'server.json'), JSON.stringify(result.serverConfig, null, 2)); // } // if (fromVersion != result.upgradedTo) { // initUtils.registerBundledPlugins(config.pluginsDir, instancePluginStorage, instanceItems, initUtils.FILE_MODE); @@ -196,49 +202,5 @@ if (siteStorage.length == 0 && instanceStorage.length == 0) { // } // } // } catch (e) { -// //skip process +// // Skip process // } - -const RUNTIME_DIRECTORY=process.env.ZWE_zowe_runtimeDirectory; -const EXTENSION_DIRECTORY=process.env.ZWE_zowe_extensionDirectory; - -const INSTALLED_COMPONENTS_ENV=process.env.ZWE_INSTALLED_COMPONENTS; -const INSTALLED_COMPONENTS = INSTALLED_COMPONENTS_ENV ? INSTALLED_COMPONENTS_ENV.split(',') : []; - -const ENABLED_COMPONENTS_ENV=process.env.ZWE_ENABLED_COMPONENTS; -const ENABLED_COMPONENTS = ENABLED_COMPONENTS_ENV ? ENABLED_COMPONENTS_ENV.split(',') : []; - - -initUtils.printFormattedDebug("Start component iteration"); - -INSTALLED_COMPONENTS.forEach(function(installedComponent) { - const componentDirectory = initUtils.findComponentDirectory(RUNTIME_DIRECTORY, EXTENSION_DIRECTORY, installedComponent); - if (componentDirectory) { - const enabled = ENABLED_COMPONENTS.includes(installedComponent); - initUtils.printFormattedDebug(`Checking plugins for component=${installedComponent}, enabled=${enabled}`); - - const manifest = YAML.parse(fs.readFileSync(initUtils.getManifestPath(componentDirectory), 'utf8')); - if (manifest.appfwPlugins) { - manifest.appfwPlugins.forEach(function (manifestPluginRef) { - const path = manifestPluginRef.path; - const fullPath = `${componentDirectory}/${path}` - const pluginDefinition = `${fullPath}/pluginDefinition.json`; - if (pluginDefinition && initUtils.fileExists(pluginDefinition)) { - const pluginDefinitionJson = JSON.parse(fs.readFileSync(pluginDefinition, 'utf8')); - if (enabled) { - initUtils.printFormattedInfo(`Registering plugin ${fullPath}`); - initUtils.registerPlugin(fullPath, pluginDefinitionJson, config.pluginsDir, actionsPluginStorage, recognizersPluginStorage, RUNTIME_DIRECTORY); - } else { - initUtils.printFormattedDebug(`Deregistering plugin ${fullPath}`); - initUtils.deregisterPlugin(pluginDefinitionJson, config.pluginsDir, actionsPluginStorage); - } - } else { - initUtils.printFormattedError(`Skipping plugin at ${fullPath} due to pluginDefinition missing or invalid`); - } - }); - } - } else { - initUtils.printFormattedError(`Warning: Could not remove app framework plugins for extension ${installedComponent} because its directory could not be found within ${EXTENSION_DIRECTORY}`); - } -}); - diff --git a/package-lock.json b/package-lock.json index c57f16e..b337710 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,31 @@ "@rocketsoftware/express-ws": "^5.0.0", "accept-language-parser": "~1.5.0", "axios": "^1.6.8", +<<<<<<< HEAD + "bluebird": "~3.5.1", + "body-parser": "~1.20.0", + "cookie-parser": "~1.4.3", + "diffie-hellman": "^5.0.3", + "express": "~4.19.2", + "express-session": "~1.15.6", + "express-static-gzip": "~1.1.3", + "glob": "~7.1.3", + "graceful-fs": "~4.1.15", + "ipaddr.js": "~1.8.0", + "js-yaml": "~3.13.1", + "lodash": "~4.17.21", + "mkdirp": "~0.5.1", + "ncp": "~2.0.0", + "node-forge": "~1.3.0", + "normalize-url": "~7.0.0", + "require-from-string": "~2.0.2", + "rimraf": "~2.6.3", + "semver": "~5.7.2", + "swagger-parser": "~10.0.3", + "ws": "^6.2.2", + "yaml": "~1.10.2", + "yauzl": "~2.10.0" +======= "bluebird": "3.7.2", "body-parser": "~1.20.2", "cookie-parser": "~1.4.6", @@ -37,13 +62,18 @@ "ws": "^6.2.2", "yaml": "~2.4.1", "yauzl": "~3.1.2" +>>>>>>> 497eaefd9562d4d5b2259f9195e26fde28ec9643 }, "devDependencies": { "@types/connect": "3.4.35", "@types/express": "4.17.17", "@types/express-serve-static-core": "4.17.35", "@types/mime": "3.0.1", +<<<<<<< HEAD + "@types/node": "~14.0.0", +======= "@types/node": "~16.18.0", +>>>>>>> 497eaefd9562d4d5b2259f9195e26fde28ec9643 "@types/qs": "6.9.3", "chai": "~4.2.0", "chai-http": "~4.2.0", @@ -55,6 +85,8 @@ "keyring_js": "~1.1.0" } }, +<<<<<<< HEAD +======= "node_modules/yaml": { "version": "2.4.5", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.5.tgz", @@ -66,6 +98,7 @@ "node": ">= 14" } }, +>>>>>>> 497eaefd9562d4d5b2259f9195e26fde28ec9643 "node_modules/zlux-server-framework": { "resolved": "../zlux-server-framework", "link": true