Skip to content

Commit

Permalink
Merge pull request #471 from alexa/cleanup-script-fix
Browse files Browse the repository at this point in the history
chore: fixed failing cleanup scripts and converted them to ts
  • Loading branch information
doiron authored May 22, 2023
2 parents 844d832 + 18e1deb commit a02b8d6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 80 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"pre-test": "npm run build && chmod +x ./dist/bin/ask.js",
"integration-test": "npm run pre-test && mocha --parallel -t 180000 test/integration/run-test.js",
"functional-test": "npm run pre-test && mocha -t 600000 -u bdd --require ts-node/register test/functional/run-test.js -R spec 'test/functional/{,**/}!(*.d).{ts,js}'",
"functional-test:clean-up": "node scripts/aws-clean-up.js; node scripts/ask-clean-up.js",
"functional-test:clean-up": "npm run build && tsc scripts/*.ts && node scripts/aws-clean-up.js; node scripts/ask-clean-up.js",
"format": "prettier --write .",
"pre-release": "standard-version",
"prepare": "husky install",
Expand Down
38 changes: 18 additions & 20 deletions scripts/ask-clean-up.js → scripts/ask-clean-up.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
/* eslint-disable no-await-in-loop */
const { CustomSmapiClientBuilder } = require('ask-smapi-sdk');
const AppConfig = require('../lib/model/app-config');
const AuthorizationController = require('../lib/controllers/authorization-controller');
const CONSTANTS = require('../lib/utils/constants');
const DynamicConfig = require('../lib/utils/dynamic-config');
const profileHelper = require("../lib/utils/profile-helper");
import { CustomSmapiClientBuilder } from 'ask-smapi-sdk';
import { PLACEHOLDER } from '../dist/lib/utils/constants';
import { isEnvProfile, resolveVendorId } from "../dist/lib/utils/profile-helper";

new AppConfig();

const authorizationController = new AuthorizationController({
const appConfig = new (require('../lib/model/app-config'))();
const authorizationController = new (require('../dist/lib/controllers/authorization-controller'))({
auth_client_type: 'LWA'
});
const dynamicConfig = require('../dist/lib/utils/dynamic-config');
const profile = _findEnvProfile() || process.env.ASK_DEFAULT_PROFILE || "default";;

function _findEnvProfile() {
if (profileHelper.isEnvProfile()) {
if (isEnvProfile()) {
// Only when user set every required parameter in ENV, we will treat profile as ENV
return CONSTANTS.PLACEHOLDER.ENVIRONMENT_VAR.PROFILE_NAME;
return PLACEHOLDER.ENVIRONMENT_VAR.PROFILE_NAME;
}
return null;
}

const refreshTokenConfig = {
clientId: authorizationController.oauthClient.config.clientId,
clientSecret: authorizationController.oauthClient.config.clientConfirmation,
refreshToken: AppConfig.getInstance().getToken(profile).refresh_token
clientId: authorizationController.oauthClient?.config.clientId,
clientSecret: authorizationController.oauthClient?.config.clientConfirmation,
refreshToken: appConfig.getToken(profile).refresh_token
};

const authEndpoint = DynamicConfig.lwaTokenHost;
const smapiEndpoint = DynamicConfig.smapiBaseUrl;
const authEndpoint = dynamicConfig.lwaTokenHost;
const smapiEndpoint = dynamicConfig.smapiBaseUrl;

const cleanUp = async () => {
console.log('cleaning ask resources');
Expand All @@ -39,11 +36,11 @@ const cleanUp = async () => {
.client();

let nextToken;
const skills = [];
const skills: any = [];
do {
vendorId = profileHelper.resolveVendorId(profile);
const vendorId = resolveVendorId(profile);
const res = await client.listSkillsForVendorV1(vendorId, nextToken);
skills.push(...res.skills);
Array.prototype.push.apply(skills, res.skills);

nextToken = res.nextToken;
} while (nextToken);
Expand All @@ -52,8 +49,9 @@ const cleanUp = async () => {

// not using promise all to avoid throttling
for (const skillId of skillIds) {
console.log(`Deleting skillid: ${skillId}`);
await client.deleteSkillV1(skillId);
console.log(`removed skill with id ${skillId}`);
console.log(` > removed skillid: ${skillId}`);
await new Promise(r => setTimeout(r, 1000));
}
console.log(`removed # skill(s) ${skillIds.length}`);
Expand Down
59 changes: 0 additions & 59 deletions scripts/aws-clean-up.js

This file was deleted.

57 changes: 57 additions & 0 deletions scripts/aws-clean-up.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { S3Client, DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectVersionsCommand } from '@aws-sdk/client-s3';
import { CloudFormationClient, DeleteStackCommand, ListStacksCommand, StackSummary } from '@aws-sdk/client-cloudformation';
import { LambdaClient, DeleteFunctionCommand, ListFunctionsCommand } from '@aws-sdk/client-lambda';

const region = 'us-east-1';
const s3 = new S3Client({ region });
const cf = new CloudFormationClient({ region });
const lambda = new LambdaClient({ region });
const prefix = 'ask-';

const deleteBucket = async (name) => {
const versions = await s3.send(new ListObjectVersionsCommand({ Bucket: name }));

if (versions.Versions) {
const deletePromises = versions.Versions.map(i => s3.send(new DeleteObjectCommand({ Bucket: name, Key: i.Key, VersionId: i.VersionId }))) || [];
await Promise.all(deletePromises);
const deleteMarkersPromises = versions.DeleteMarkers?.map(i => s3.send(new DeleteObjectCommand({ Bucket: name, Key: i.Key, VersionId: i.VersionId }))) || [];
await Promise.all(deleteMarkersPromises);
}

return s3.send(new DeleteBucketCommand({ Bucket: name }));
};

export const cleanUp = async () => {
console.log(`cleaning aws resources with prefix "${prefix}"`);

const stackResults = await cf.send(new ListStacksCommand({})).then(res => {
const stacks: StackSummary[] = res.StackSummaries?.filter(s => s.StackName?.startsWith(prefix) && !s.StackStatus?.includes('DELETE_COMPLETE')) || [];
console.log('Stacks #:', stacks.length);
const deletePromises = stacks.map(i => cf.send(new DeleteStackCommand({ StackName: i.StackName })));
return Promise.allSettled(deletePromises);
});

const functionResults = await lambda.send(new ListFunctionsCommand({})).then(res => {
const Functions = res.Functions?.filter(i => i.FunctionName?.startsWith(prefix)) || [];
console.log('Functions #:', Functions.length);
const deletePromises = Functions.map(i => lambda.send(new DeleteFunctionCommand({ FunctionName: i.FunctionName })));
return Promise.allSettled(deletePromises);
});

const bucketResults = await s3.send(new ListBucketsCommand({})).then(async (res) => {
const Buckets = res.Buckets?.filter(b => b.Name?.startsWith(prefix)) || [];
console.log('Buckets #:', Buckets.length);
const deletePromises = Buckets.map((i) => deleteBucket(i.Name));
return Promise.allSettled(deletePromises);
});

const rejected = [...stackResults, ...functionResults, ...bucketResults].filter(r => r.status === 'rejected');
if (rejected.length) {
rejected.forEach(r => {
console.error(JSON.stringify(r, null, 2));
});
}
console.log('done');
};

cleanUp();

0 comments on commit a02b8d6

Please sign in to comment.