From 90276dd4988b80cb0e4b6912b41e8d0c810a313c Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Wed, 24 Apr 2019 14:13:08 +0100 Subject: [PATCH] improve recovery script --- scripts/check-activiti-env.js | 219 +++++++++++++++++++++++++++++----- 1 file changed, 191 insertions(+), 28 deletions(-) diff --git a/scripts/check-activiti-env.js b/scripts/check-activiti-env.js index 550b9aeb6c..e74b1af6e3 100755 --- a/scripts/check-activiti-env.js +++ b/scripts/check-activiti-env.js @@ -6,8 +6,14 @@ let ACTIVITI7_APPS = require('../e2e/util/resources').ACTIVITI7_APPS; let config = {}; let absentApps = []; +let notRunningApps = []; let host; +let MAX_RETRY = 3; +let counter = 0; +let TIMEOUT = 180000; + + async function main() { program @@ -43,10 +49,108 @@ async function main() { console.log('Login error' + e); } - let appsDeployed = await getDeployedApplicationsByStatus(this.alfrescoJsApi, 'RUNNING'); + await deployAbsentApps(this.alfrescoJsApi); + let notRunning = await getNotRunningApps(this.alfrescoJsApi); + + if (notRunning && notRunning.length > 0) { + let notRunningAppAfterWait = await waitPossibleStaleApps(this.alfrescoJsApi, notRunning); + + await deleteStaleApps(this.alfrescoJsApi, notRunningAppAfterWait); + + await deployAbsentApps(this.alfrescoJsApi); + let notRunningSecondAttempt = await getNotRunningApps(this.alfrescoJsApi); + + if (notRunningSecondAttempt && notRunningSecondAttempt.length > 0) { + let notRunningAppAfterWaitSecondAttempt = await waitPossibleStaleApps(this.alfrescoJsApi, notRunningSecondAttempt); + + if (notRunningAppAfterWaitSecondAttempt && notRunningAppAfterWaitSecondAttempt.legnth > 0) { + console.log(`Not possible to recover the following apps in the environment`); + + notRunningAppAfterWaitSecondAttempt.forEach((currentApp) => { + console.log(`App ${currentApp.entry.name } current status ${JSON.stringify(currentApp.entry.status)}`); + }); + + process.exit(1); + } + }else{ + console.log(`Activiti 7 all ok :)`); + } + } else { + console.log(`Activiti 7 all ok :)`); + } +} + +async function deleteStaleApps(alfrescoJsApi, notRunningAppAfterWait) { + + notRunningAppAfterWait.forEach(async (currentApp) => { + await deleteApp(alfrescoJsApi, currentApp.entry.name); + }); + +} + +async function waitPossibleStaleApps(alfrescoJsApi, notRunning) { + + do { + console.log(`Wait stale app ${TIMEOUT}`); + + notRunning.forEach((currentApp) => { + console.log(`${currentApp.entry.name }`); + }); + + + sleep(TIMEOUT); + counter++; + + let runningApps = await getDeployedApplicationsByStatus(alfrescoJsApi, 'RUNNING'); + + notRunning.forEach((currentStaleApp) => { + let nowIsRunning = runningApps.find((currentRunnignApp) => { + return currentStaleApp.entry.name === currentRunnignApp.entry.name; + }); + + if (nowIsRunning) { + notRunning = notRunning.filter((item) => { + return item.entry.name !== nowIsRunning.entry.name + }) + } + + }); + } while (counter < MAX_RETRY && notRunning.length > 0); + + return notRunning; +} + +async function getNotRunningApps(alfrescoJsApi) { + let allStatusApps = await getDeployedApplicationsByStatus(alfrescoJsApi, ''); Object.keys(ACTIVITI7_APPS).forEach((key) => { - let isPresent = appsDeployed.find((currentApp) => { + let isNotRunning = allStatusApps.find((currentApp) => { + return ACTIVITI7_APPS[key].name === currentApp.entry.name && currentApp.entry.status !== 'Running'; + }); + + if (isNotRunning) { + notRunningApps.push(isNotRunning); + } + }); + + if (notRunningApps.length > 0) { + console.log(`The following apps are NOT running in the target env:`); + notRunningApps.forEach((currentApp) => { + console.log(`App ${currentApp.entry.name } current status ${JSON.stringify(currentApp.entry.status)}`); + }); + + await checkIfAppIsReleased(alfrescoJsApi, absentApps); + } + + return notRunningApps; +} + +async function deployAbsentApps(alfrescoJsApi) { + + let deployedApps = await getDeployedApplicationsByStatus(alfrescoJsApi, ''); + + Object.keys(ACTIVITI7_APPS).forEach((key) => { + let isPresent = deployedApps.find((currentApp) => { return ACTIVITI7_APPS[key].name === currentApp.entry.name; }); @@ -56,14 +160,13 @@ async function main() { }); if (absentApps.length > 0) { - console.log(`The following apps are missing in the target env ${JSON.stringify(absentApps)}`) + console.log(`The following apps are missing in the target env ${JSON.stringify(absentApps)}`); - await checkIfAppIsReleased(this.alfrescoJsApi, absentApps); - - process.exit(1); + await checkIfAppIsReleased(alfrescoJsApi, absentApps); } } + async function checkIfAppIsReleased(apiService, absentApps) { let listAppsInModeler = await getAppProjects(apiService); @@ -80,9 +183,22 @@ async function checkIfAppIsReleased(apiService, absentApps) { await releaseApp(apiService, uploadedApp); await deployApp(apiService, uploadedApp); } - }else{ - await releaseApp(apiService, app); - await deployApp(apiService, app); + } else { + let appRelease = undefined; + let appReleaseList = await getReleaseAppyProjectId(apiService, app.entry.id); + + if (!appReleaseList) { + appRelease = await releaseApp(apiService, app); + } else { + + appRelease = appReleaseList.list.entries.find((currentRelease) => { + return currentRelease.entry.version === 'latest'; + }); + } + + console.log('App to deploy ' + appRelease.entry.projectName + ' app release id ' + JSON.stringify(appRelease.entry.id)); + + await deployApp(apiService, appRelease); } } } @@ -90,25 +206,26 @@ async function checkIfAppIsReleased(apiService, absentApps) { async function deployApp(apiService, app) { const url = `${config.hostBpm}/alfresco-deployment-service/v1/applications`; - const pathParams = {}, - queryParams = { - "name": "re", - "releaseId": app.entry.id, - "security": [{"role": "APS_ADMIN", "groups": [], "users": ["admin.adf"]}, { - "role": "APS_USER", - "groups": [], - "users": ["admin.adf"] - }] - }; + const pathParams = {}; + const bodyParam = { + "name": app.entry.projectName, + "releaseId": app.entry.id, + "version": app.entry.name, + "security": [{"role": "APS_ADMIN", "groups": [], "users": ["admin.adf"]}, { + "role": "APS_USER", + "groups": [], + "users": ["admin.adf"] + }] + }; - const headerParams = {}, formParams = {}, bodyParam = {}, - contentTypes = ['multipart/form-data'], accepts = ['application/json']; + const headerParams = {}, formParams = {}, queryParams = {}, + contentTypes = ['application/json'], accepts = ['application/json']; try { return await apiService.oauth2Auth.callCustomApi(url, 'POST', pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts); } catch (error) { - console.log(`Not possible to deploy the project ${app.entry.name} ` + error); + console.log(`Not possible to deploy the project ${app.entry.projectName} status : ${JSON.stringify(error.status)} ${JSON.stringify(error)}`); process.exit(1); } } @@ -128,16 +245,34 @@ async function importApp(apiService, app) { contentTypes, accepts); } catch (error) { if (error.status !== 409) { - console.log(`Not possible to upload the project ${app.name} ` + error.status); + console.log(`Not possible to upload the project ${app.name} status : ${JSON.stringify(error.status)} ${JSON.stringify(error.text)}`); process.exit(1); } } } -async function releaseApp(apiService, app) { - const url = `${config.hostBpm}alfresco-modeling-service/v1/projects/${app.entry.id}/releases`; +async function getReleaseAppyProjectId(apiService, projectId) { + const url = `${config.hostBpm}/alfresco-modeling-service/v1/projects/${projectId}/releases`; + const pathParams = {}, queryParams = {}, + headerParams = {}, formParams = {}, bodyParam = {}, + contentTypes = ['application/json'], accepts = ['application/json']; + + try { + return await apiService.oauth2Auth.callCustomApi(url, 'GET', pathParams, queryParams, headerParams, formParams, bodyParam, + contentTypes, accepts); + } catch (error) { + console.log(`Not possible to get the release of the project ${projectId} ` + JSON.stringify(error)); + process.exit(1); + } + +} + +async function releaseApp(apiService, app) { + const url = `${config.hostBpm}/alfresco-modeling-service/v1/projects/${app.entry.id}/releases`; + + console.log('Release ID ' + app.entry.id); const pathParams = {}, queryParams = {}, headerParams = {}, formParams = {}, bodyParam = {}, contentTypes = ['application/json'], accepts = ['application/json']; @@ -146,7 +281,7 @@ async function releaseApp(apiService, app) { return await apiService.oauth2Auth.callCustomApi(url, 'POST', pathParams, queryParams, headerParams, formParams, bodyParam, contentTypes, accepts); } catch (error) { - console.log(`Not possible to release the project ${app.entry.name} ` + JSON.stringify(error)); + console.log(`Not possible to release the project ${app.entry.name} status : ${JSON.stringify(error.status)} ${JSON.stringify(error.text)}`); process.exit(1); } @@ -166,7 +301,7 @@ async function getDeployedApplicationsByStatus(apiService, status) { return data.list.entries; } catch (error) { - console.log(`Not possible get the application from alfresco-deployment-service` + error); + console.log(`Not possible get the applicationsfrom alfresco-deployment-service ${JSON.stringify(error)} `); process.exit(1); } @@ -185,9 +320,37 @@ async function getAppProjects(apiService, status) { contentTypes, accepts); return data.list.entries; } catch (error) { - console.log(`Not possible get the application from alfresco-modeling-service` + error); + console.log(`Not possible get the application from alfresco-modeling-service ` + error); process.exit(1); } } +async function deleteApp(apiService, appName) { + console.log(`Delete the app ${appName}`); + + const url = `${config.hostBpm}/alfresco-deployment-service/v1/applications/${appName}`; + + const pathParams = {}, queryParams = {}, + headerParams = {}, formParams = {}, bodyParam = {}, + contentTypes = ['application/json'], accepts = ['application/json']; + + try { + await apiService.oauth2Auth.callCustomApi(url, 'DELETE', pathParams, queryParams, headerParams, formParams, bodyParam, + contentTypes, accepts); + + ///it needs time + console.log(`Deleting apps stale wait 3 minutes`); + sleep(180000); + console.log(`App deleted`); + } catch (error) { + console.log(`Not possible to delete the application from alfresco-modeling-service` + error); + process.exit(1); + } +} + +function sleep(delay) { + var start = new Date().getTime(); + while (new Date().getTime() < start + delay) ; +} + main();