Ability to clean app using the like and interval time (#5515)

This commit is contained in:
Maurizio Vitale
2020-02-26 13:09:53 +00:00
committed by GitHub
parent 5fb7879900
commit 6ef07fe73f

View File

@@ -40,6 +40,9 @@ export interface ConfigArgs {
host: string;
oauth: string;
identityHost: boolean;
enableLike: boolean;
intervalTime: string;
}
function getAlfrescoJsApiInstance(args: ConfigArgs) {
@@ -172,9 +175,14 @@ function useContext(args: ConfigArgs) {
logger.info(response);
}
function createTmpTable(podName: string, appName: string) {
function createTmpTable(podName: string, appName: string, enableLike: boolean = false, intervalTime: string = '20 min') {
logger.info('Perform create tmp table...');
const query = `select project_id into query_table from release where name = '${appName}'`;
let whereCondition = `where name = '${appName}'`;
let query = `select project_id into query_table from release `;
if (enableLike) {
whereCondition = `where name like '${appName}%' and creation_date < NOW() - '${intervalTime}'::INTERVAL `;
}
query = query + whereCondition;
const response = exec('kubectl', [`exec`, `${podName}`, `--`, `psql`, `-U`, `alfresco`, `-d`, `postgres`, `-c`, `${query}` ], {});
logger.info(response);
}
@@ -214,10 +222,10 @@ function deleteInvolvedUsersRelease(podName: string) {
logger.info(response);
}
function deleteAllReleases(podName: string, appName: string) {
function deleteAllReleases(podName: string, appName: string, enableLike: boolean = false, intervalTime: string = '20 min') {
logger.info(podName, appName);
dropTmpTable(podName);
createTmpTable(podName, appName);
createTmpTable(podName, appName, enableLike, intervalTime);
deleteLatestRelease(podName);
deleteInvolvedGroupsRelease(podName);
deleteInvolvedUsersRelease(podName);
@@ -225,6 +233,21 @@ function deleteAllReleases(podName: string, appName: string) {
dropTmpTable(podName);
}
function getProjectWithLike(podName, appName, intervalTime = '20 min') {
logger.info('Perform get project with like...');
let query = `select name from release `;
const whereCondition = `where name like '${appName}%' and creation_date < NOW() - '${intervalTime}'::INTERVAL `;
query = query + whereCondition;
const response = exec('kubectl', [`exec`, `${podName}`, `--`, `psql`, `-U`, `alfresco`, `-d`, `postgres`, `-c`, `${query}`], {});
const responseToArray = convertSQLStringIntoArray(response);
logger.info(response);
return responseToArray;
}
function convertSQLStringIntoArray(response: string): string [] {
return response.split('\n').filter( (text) => (!text.includes(' name ') && !text.includes('------') && !text.includes('rows)') && !text.includes('row)') && text !== '')).map( (value) => value.trim());
}
function getPodName(args: ConfigArgs): string {
logger.info('Perform get pod name...');
const response = exec('kubectl', [`get`, `pod`, `-l`, `app=${args.label}`, `-o`, `jsonpath={.items[0].metadata.name}`], {});
@@ -249,6 +272,8 @@ async function main(args) {
.option('--devopsPassword [type]', 'password of user with ACTIVITI_DEVOPS role')
.option('--rancherUsername [type]', 'rancher username')
.option('--rancherPassword [type]', 'rancher password')
.option('--enableLike [boolean]', 'Enable the like for app name')
.option('--intervalTime [string]', 'In case of enableLike it specify the time related to the createDate')
.parse(process.argv);
if (process.argv.includes('-h') || process.argv.includes('--help')) {
@@ -272,11 +297,21 @@ async function main(args) {
for (let i = 0; i < applications.length; i++ ) {
logger.info(`Perform action on app: ${applications[i]}`);
if (args.enableLike) {
const projectWithLike = getProjectWithLike(podName, applications[i], args.intervalTime);
deleteAllReleases(podName, applications[i], args.enableLike, args.intervalTime);
for (let y = 0; y < projectWithLike.length; y++ ) {
await undeployApplication(args, alfrescoJsApiDevops, projectWithLike[y]);
await deleteDescriptor(args, alfrescoJsApiDevops, projectWithLike[y]);
await deleteProjectByName(args, alfrescoJsApiModeler, projectWithLike[y]);
}
} else {
deleteAllReleases(podName, applications[i]);
await undeployApplication(args, alfrescoJsApiDevops, applications[i]);
await deleteDescriptor(args, alfrescoJsApiDevops, applications[i]);
await deleteProjectByName(args, alfrescoJsApiModeler, applications[i]);
}
}
}
}