Skip to content

Commit

Permalink
Merge pull request #114 from zowe/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
1000TurquoisePogs authored Apr 26, 2020
2 parents 50965eb + f546d99 commit ff1d09c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 18 deletions.
69 changes: 51 additions & 18 deletions lib/initInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
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
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/

Expand All @@ -13,6 +13,7 @@ const path = require('path');
const argParser = require('../../zlux-server-framework/utils/argumentParser');
const jsonUtils = require('../../zlux-server-framework/lib/jsonUtils');
const mergeUtils = require('../../zlux-server-framework/utils/mergeUtils');
const upgradeInstance = require('./upgradeInstance');
const os = require('os');
const ncp = require('ncp').ncp;
const { execSync } = require('child_process');
Expand All @@ -23,20 +24,19 @@ const FILE_MODE = 0o0770;



let destination;
//Where are we: dev environment? Official install?
if (process.env.WORKSPACE_DIR && process.env.WORKSPACE_DIR != '""') {
destination = path.join(process.env.WORKSPACE_DIR, 'app-server');
} else if (process.env.INSTANCE_DIR && process.env.INSTANCE_DIR != '""') {
destination = path.join(process.env.INSTANCE_DIR, 'workspace', 'app-server');
} else {
destination = process.env.USERPROFILE
? path.join(process.env.USERPROFILE, '.zowe', 'workspace', 'app-server')
: path.join(process.env.HOME, '.zowe', 'workspace', 'app-server');
}
const instanceLocation = process.env.INSTANCE_DIR && process.env.INSTANCE_DIR != '""' ? process.env.INSTANCE_DIR
: process.env.USERPROFILE ? path.join(process.env.USERPROFILE, '.zowe')
: path.join(process.env.HOME, '.zowe');
const workspaceLocation = process.env.WORKSPACE_DIR && process.env.WORKSPACE_DIR != '""' ? process.env.WORKSPACE_DIR
: path.join(instanceLocation, 'workspace');
const destination = path.join(workspaceLocation, 'app-server');
const versionLocation = path.join(destination, 'component.json');

let createJson = false;
let currentJsonConfig;
try {
let currentJsonConfig = fs.readFileSync(path.join(destination, 'serverConfig', 'server.json'));
currentJsonConfig = fs.readFileSync(path.join(destination, 'serverConfig', 'server.json'), 'utf8');
} catch (e) {
if (e.code == 'ENOENT') {
createJson = true;
Expand All @@ -45,6 +45,7 @@ try {
}
}


let config = argParser.environmentVarsToObject("ZWED_");
if (createJson) {
let defaultConfig = jsonUtils.parseJSONWithComments('../defaults/serverConfig/server.json');
Expand Down Expand Up @@ -97,7 +98,7 @@ mkdirp.sync(config.pluginsDir, {mode: FOLDER_MODE});
//Write new config json only if one does not exist
if (createJson) {
console.log('ZWED5010I - Creating new config=', config);
fs.writeFileSync(path.join(destination, 'serverConfig', 'server.json'), JSON.stringify(config, null, 2),
fs.writeFileSync(path.join(destination, 'serverConfig', 'server.json'), JSON.stringify(config, null, 2),
{encoding: 'utf8', mode: 0o0740});
//740 specifically, to keep server config secure
}
Expand Down Expand Up @@ -130,7 +131,7 @@ if (instanceItems.indexOf('org.zowe.configjs.json') == -1) {
'{"identifier":"'+identifier
+'",\n"pluginLocation":"'+location
+'"}', {encoding: 'utf8' , mode: FILE_MODE});

//small hack for 2 terminals configuration
if (identifier == 'org.zowe.terminal.vt' && process.env['ZOWE_ZLUX_SSH_PORT']) {
let defaultConfigDir = path.join(instancePluginStorage,'org.zowe.terminal.vt','sessions');
Expand Down Expand Up @@ -158,7 +159,7 @@ if (instanceItems.indexOf('org.zowe.configjs.json') == -1) {
} catch (e) {
console.log('ZWED5017E - Could not customize tn3270-ng2, error writing json=',e);
}
}
}
}
});
}
Expand Down Expand Up @@ -187,6 +188,38 @@ if (siteStorage.length == 0 && instanceStorage.length == 0) {
execSync("chmod -R 770 "+instancePluginStorage);
process.exit(0);
}
} else {
process.exit(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 fromVersion;
try {
fromVersion = process.env.ZOWE_UPGRADE_VERSION ? process.env.ZOWE_UPGRADE_VERSION : require(versionLocation).version;
} catch (e) {
//pre 1.11
fromVersion = "1.10.99";
}
if (serverConfig) {
//upgrades based on what WAS there, not what we added above
const upgradedTo = upgradeInstance.doUpgrade(fromVersion, destination, serverConfig, instanceItems);
if (upgradedTo) {
let componentJsonContent;
try {
componentJsonContent = require(versionLocation);
} catch (e) {
componentJsonContent = {};
//doesnt exist, create new
}
componentJsonContent.version = upgradedTo;
fs.writeFileSync(versionLocation, JSON.stringify(componentJsonContent));
} else {
console.log("Could not perform app-server upgrade");
process.exit(1);
}
}
} catch (e) {
//skip process
}
61 changes: 61 additions & 0 deletions lib/upgradeInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
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
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
*/
const fs = require('fs');
const path = require('path');
const semver = require('semver');

const versions = [
{
v: '1.11.0',
upgrade: function(toLocation, serverConfig, instanceItems) {
try {
let hasOutdatedZosmfPluginConfig = serverConfig.dataserviceAuthentication.implementationDefaults.zosmf
&& !serverConfig.dataserviceAuthentication.implementationDefaults.zosmf.plugins;
if (hasOutdatedZosmfPluginConfig) {
if (instanceItems.indexOf('org.zowe.zlux.auth.zosmf.json') != -1) {
console.log('Removing org.zowe.zlux.auth.zosmf.json from defaults');
fs.unlinkSync(path.join(toLocation, 'plugins', 'org.zowe.zlux.auth.zosmf.json'));
}
if (instanceItems.indexOf('org.zowe.zlux.proxy.zosmf.json') != -1) {
console.log('Removing org.zowe.zlux.proxy.zosmf.json from defaults');
fs.unlinkSync(path.join(toLocation, 'plugins', 'org.zowe.zlux.proxy.zosmf.json'));
}
}
} catch (e) {
//change not needed
}
}
}
];


module.exports.doUpgrade = function doUpgrade(fromVersion, toLocation, serverConfig, instanceItems) {
let firstIndex = 0;
let upgradingTo;
let upgradedTo;
for (let i = 0; i < versions.length; i++) {
if (semver.gt(versions[i].v, fromVersion)) {
firstIndex = i;
break;
}
}
try {
for (let i = firstIndex; i < versions.length; i++) {
upgradingTo = versions[i].v;
versions[i].upgrade(toLocation, serverConfig, instanceItems);
upgradedTo = versions[i].v;
}
} catch (e) {
console.log('app-server config could not upgrade to version='+upgradingTo);
} finally {
console.log('app-server config upgraded to version='+upgradedTo);
return upgradedTo;
}
};

0 comments on commit ff1d09c

Please sign in to comment.