From e696ab5cab51d953517ada1b7a33abcfe9bdef4f Mon Sep 17 00:00:00 2001 From: Tim Kara Date: Wed, 11 Nov 2020 13:18:33 +0100 Subject: [PATCH] Added checkExist on error in step upload/postFile + Added getVersions check in step activateVersion --- lib/code.js | 129 +++++++++++++++++++++++++++++--------------------- lib/ocapi.js | 5 +- lib/webdav.js | 16 +++++++ 3 files changed, 94 insertions(+), 56 deletions(-) diff --git a/lib/code.js b/lib/code.js index 886c09c1..98367209 100644 --- a/lib/code.js +++ b/lib/code.js @@ -129,53 +129,62 @@ function deployCode(instance, archive, token, options, callback) { // initiate the post request first... webdav.postFile(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, function (err, res, body) { - ocapi.ensureValidToken(err, res, function(err, res) { - // note, server respond with a 401 (Authorization required) in case the WebDAV Client permission is not set - if (res && res.statusCode >= 400) { - callback(new Error(`Deploy code ${file} failed (upload step): ` - + `${res.statusCode} (${res.statusMessage})`)); + webdav.checkExist(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, function (err, res, body) { + if(err) { + callback(new Error('Code upload failed')); return; - } else if (err) { - callback(new Error(`Deploy code ${file} failed (upload step): ${err}`)); + } else if(res.statusCode !== 200) { + callback(new Error('Code upload failed - statusCode: ' + res.statusCode)); return; } - // ...and unzip the archive afterwards - webdav.unzip(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, - function (err, res, body) { - // note, server respond with a 401 (Authorization required) in case the WebDAV Client permission is not set - if (res && res.statusCode >= 400) { - callback(new Error(`Deploy code ${file} failed (unzip step): ` - + `${res.statusCode} (${res.statusMessage})`)); - return; - } else if (err) { - callback(new Error(`Deploy code ${file} failed (unzip step): ${err}`)); - return; - } - - // this assumes that archive file carries the same name as the packaged code version - var newVersion = require('path').basename(file); - if (ignoreLocalFilePath) { - newVersion = require('path').basename(file, '.zip'); - } - - // If the code is successfully deployed, we need to remove the uploaded ZIP file - webdav.deleteFile(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, - function(err, res, body) { - if (err) { - callback(new Error(`Delete ZIP file ${file} after deployment ` - + `failed (deleteFile step): ${err}`)); - return; - } else { - if (res && res.statusCode === 204) { - callback(undefined, newVersion); + ocapi.ensureValidToken(err, res, function(err, res) { + // note, server respond with a 401 (Authorization required) in case the WebDAV Client permission is not set + if (res && res.statusCode >= 400) { + callback(new Error(`Deploy code ${file} failed (upload step): ` + + `${res.statusCode} (${res.statusMessage})`)); + return; + } else if (err) { + callback(new Error(`Deploy code ${file} failed (upload step): ${err}`)); + return; + } + // ...and unzip the archive afterwards + webdav.unzip(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, + function (err, res, body) { + // note, server respond with a 401 (Authorization required) in case the WebDAV Client permission is not set + if (res && res.statusCode >= 400) { + callback(new Error(`Deploy code ${file} failed (unzip step): ` + + `${res.statusCode} (${res.statusMessage})`)); + return; + } else if (err) { + callback(new Error(`Deploy code ${file} failed (unzip step): ${err}`)); + return; + } + + // this assumes that archive file carries the same name as the packaged code version + var newVersion = require('path').basename(file); + if (ignoreLocalFilePath) { + newVersion = require('path').basename(file, '.zip'); + } + + // If the code is successfully deployed, we need to remove the uploaded ZIP file + webdav.deleteFile(instance, webdav.WEBDAV_CODE, file, token, ignoreLocalFilePath, options, + function(err, res, body) { + if (err) { + callback(new Error(`Delete ZIP file ${file} after deployment ` + + `failed (deleteFile step): ${err}`)); return; } else { - callback(new Error(`Delete ZIP file ${file} after deployment failed - (deleteFile step): ${res.statusCode} (${res.statusMessage})`)); + if (res && res.statusCode === 204) { + callback(undefined, newVersion); + return; + } else { + callback(new Error(`Delete ZIP file ${file} after deployment failed + (deleteFile step): ${res.statusCode} (${res.statusMessage})`)); + } } - } - }); - }); + }); + }); + }); }, function() { deployCode(instance, archive, token, options, callback); }); @@ -411,20 +420,34 @@ module.exports.api = { throw new TypeError('Parameter callback is missing or not of type Function'); } activateVersion(instance, code_version, token, function (err, res) { - ocapi.ensureValidToken(err, res, function(err, res) { - if (!err && res.statusCode == 200 && !res.fault) { - // Success - callback(undefined); + if(err) { + getVersions(instance, token, function(err, res, body) { + for (let index = 0; index < body.data.length; index++) { + const version = body.data[index]; + if(version.active && version.id == code_version){ + callback(undefined) + return; + } + } + callback(new Error(err)); return; - } else if (res.statusCode == 400 && res.body.fault.type == 'CodeVersionModificationException') { - // Exception: Code version already active won't end up in error - callback(undefined); + }) + } else { + ocapi.ensureValidToken(err, res, function(err, res) { + console.log({res, err}) + if (!err && res.statusCode == 200 && !res.fault) { + // Success + callback(undefined); + return; + } else if (res.statusCode == 400 && res.body.fault.type == 'CodeVersionModificationException') { + // Exception: Code version already active won't end up in error + callback(undefined); + return; + } + callback(new Error(err)); return; - } - - callback(new Error(err)); - return; - }); + }); + } }); } }; \ No newline at end of file diff --git a/lib/ocapi.js b/lib/ocapi.js index 85be7eff..d259450c 100644 --- a/lib/ocapi.js +++ b/lib/ocapi.js @@ -89,10 +89,9 @@ function ensureValidToken(err, res, success, repeat) { } console.error('An error occured. Try running the command again with -D,--debug flag.'); console.debug('Error code: %s, message: %s, stack: %s', err.code, err.message, err.stack); - } else { - // valid token or different error, trigger callback - success(err, res); } + // valid token or different error, trigger callback + success(err, res); } /** diff --git a/lib/webdav.js b/lib/webdav.js index 50ed0c53..4b02ec3c 100644 --- a/lib/webdav.js +++ b/lib/webdav.js @@ -94,6 +94,21 @@ function deleteFile(instance, path, file, token, ignoreLocalFilePath, options, c request(options, callback); } +function checkExist(instance, path, file, token, ignoreLocalFilePath, options, callback){ + if (ignoreLocalFilePath) { + path += '/' + require('path').basename(file); + } else { + path += '/' + file; + } + + // build the request options + var options = getOptions(instance, path, token, options, 'HEAD'); + + // do the delete request + request(options, callback); +} + + function unzip(instance, path, file, token, ignoreLocalFilePath, options, callback) { // append file to post to request uri // we preserve the local file path, if any passed with the file @@ -159,6 +174,7 @@ function uploadInstanceImport(instance, archive, options) { module.exports.uploadInstanceImport = uploadInstanceImport; module.exports.postFile = postFile; module.exports.deleteFile = deleteFile; +module.exports.checkExist = checkExist; module.exports.unzip = unzip; module.exports.WEBDAV_CODE = WEBDAV_CODE; module.exports.WEBDAV_INSTANCE_IMPEX = WEBDAV_INSTANCE_IMPEX;