Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checkExist on error in step upload/postFile + Added getVersions check in step activateVersion #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 76 additions & 53 deletions lib/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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;
});
});
}
});
}
};
5 changes: 2 additions & 3 deletions lib/ocapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/webdav.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down