improved garbage collection cli options
parent
606102aae4
commit
a47d096f5c
|
|
@ -2005,30 +2005,53 @@ const cli_functions_repository_1 = __nccwpck_require__(85301);
|
|||
const input_1 = __importDefault(__nccwpck_require__(91933));
|
||||
const cloud_runner_logger_1 = __importDefault(__nccwpck_require__(22855));
|
||||
const base_stack_formation_1 = __nccwpck_require__(29643);
|
||||
const task_definition_formation_1 = __nccwpck_require__(97647);
|
||||
class AwsCliCommands {
|
||||
static awsListAll() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.awsListStacks();
|
||||
yield AwsCliCommands.awsListStacks(undefined, true);
|
||||
yield AwsCliCommands.awsListTasks();
|
||||
yield AwsCliCommands.awsListLogGroups();
|
||||
yield AwsCliCommands.awsListLogGroups(undefined, true);
|
||||
});
|
||||
}
|
||||
static awsListStacks(perResultCallback = false) {
|
||||
static garbageCollectAws() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.cleanup(false);
|
||||
});
|
||||
}
|
||||
static garbageCollectAwsAll() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.cleanup(true);
|
||||
});
|
||||
}
|
||||
static garbageCollectAwsAllOlderThanOneDay() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.cleanup(true, true);
|
||||
});
|
||||
}
|
||||
static isOlderThan1day(date) {
|
||||
const ageDate = new Date(date.getTime() - Date.now());
|
||||
return ageDate.getDay() > 0;
|
||||
}
|
||||
static awsListStacks(perResultCallback = false, verbose = false) {
|
||||
var _a, _b;
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
process.env.AWS_REGION = input_1.default.region;
|
||||
const CF = new aws_sdk_1.default.CloudFormation();
|
||||
const stacks = ((_a = (yield CF.listStacks().promise()).StackSummaries) === null || _a === void 0 ? void 0 : _a.filter((_x) => _x.StackStatus !== 'DELETE_COMPLETE' &&
|
||||
_x.TemplateDescription === task_definition_formation_1.TaskDefinitionFormation.description.replace('\n', ''))) || [];
|
||||
const stacks = ((_a = (yield CF.listStacks().promise()).StackSummaries) === null || _a === void 0 ? void 0 : _a.filter((_x) => _x.StackStatus !== 'DELETE_COMPLETE')) || [];
|
||||
cloud_runner_logger_1.default.log(`Stacks ${stacks.length}`);
|
||||
for (const element of stacks) {
|
||||
const ageDate = new Date(element.CreationTime.getTime() - Date.now());
|
||||
if (verbose)
|
||||
cloud_runner_logger_1.default.log(`Task Stack ${element.StackName} - Age D${ageDate.getDay()} H${ageDate.getHours()} M${ageDate.getMinutes()}`);
|
||||
if (perResultCallback)
|
||||
yield perResultCallback(element);
|
||||
}
|
||||
const baseStacks = ((_b = (yield CF.listStacks().promise()).StackSummaries) === null || _b === void 0 ? void 0 : _b.filter((_x) => _x.StackStatus !== 'DELETE_COMPLETE' && _x.TemplateDescription === base_stack_formation_1.BaseStackFormation.baseStackDecription)) || [];
|
||||
cloud_runner_logger_1.default.log(`Base Stacks ${baseStacks.length}`);
|
||||
for (const element of baseStacks) {
|
||||
const ageDate = new Date(element.CreationTime.getTime() - Date.now());
|
||||
if (verbose)
|
||||
cloud_runner_logger_1.default.log(`Base Stack ${element.StackName} - Age D${ageDate.getHours()} H${ageDate.getHours()} M${ageDate.getMinutes()}`);
|
||||
if (perResultCallback)
|
||||
yield perResultCallback(element);
|
||||
}
|
||||
|
|
@ -2072,64 +2095,79 @@ class AwsCliCommands {
|
|||
}
|
||||
});
|
||||
}
|
||||
static awsListLogGroups(perResultCallback = false) {
|
||||
static awsListLogGroups(perResultCallback = false, verbose = false) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
process.env.AWS_REGION = input_1.default.region;
|
||||
const ecs = new aws_sdk_1.default.CloudWatchLogs();
|
||||
const logStreamInput = { logGroupNamePrefix: 'game-ci' };
|
||||
const logGroups = (yield ecs.describeLogGroups(logStreamInput).promise()).logGroups || [];
|
||||
let logStreamInput = {
|
||||
/* logGroupNamePrefix: 'game-ci' */
|
||||
};
|
||||
let logGroupsDescribe = yield ecs.describeLogGroups(logStreamInput).promise();
|
||||
const logGroups = logGroupsDescribe.logGroups || [];
|
||||
while (logGroupsDescribe.nextToken) {
|
||||
logStreamInput = { /* logGroupNamePrefix: 'game-ci',*/ nextToken: logGroupsDescribe.nextToken };
|
||||
logGroupsDescribe = yield ecs.describeLogGroups(logStreamInput).promise();
|
||||
logGroups.push(...((logGroupsDescribe === null || logGroupsDescribe === void 0 ? void 0 : logGroupsDescribe.logGroups) || []));
|
||||
}
|
||||
cloud_runner_logger_1.default.log(`Log Groups ${logGroups.length}`);
|
||||
for (const element of logGroups) {
|
||||
if (element.creationTime === undefined) {
|
||||
cloud_runner_logger_1.default.log(`Skipping ${element.logGroupName} no createdAt date`);
|
||||
continue;
|
||||
}
|
||||
const ageDate = new Date(new Date(element.creationTime).getTime() - Date.now());
|
||||
if (verbose)
|
||||
cloud_runner_logger_1.default.log(`Log Group Name ${element.logGroupName} - Age D${ageDate.getDay()} H${ageDate.getHours()} M${ageDate.getMinutes()} - 1d old ${AwsCliCommands.isOlderThan1day(new Date(element.creationTime))}`);
|
||||
if (perResultCallback)
|
||||
yield perResultCallback(element, element);
|
||||
}
|
||||
});
|
||||
}
|
||||
static garbageCollectAws() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.cleanup(false);
|
||||
});
|
||||
}
|
||||
static garbageCollectAwsAll() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.cleanup(true);
|
||||
});
|
||||
}
|
||||
static garbageCollectAwsAllOlderThanOneDay() {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
yield AwsCliCommands.cleanup(true);
|
||||
});
|
||||
}
|
||||
static cleanup(deleteResources = false) {
|
||||
static cleanup(deleteResources = false, OneDayOlderOnly = false) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
process.env.AWS_REGION = input_1.default.region;
|
||||
const CF = new aws_sdk_1.default.CloudFormation();
|
||||
const ecs = new aws_sdk_1.default.ECS();
|
||||
const cwl = new aws_sdk_1.default.CloudWatchLogs();
|
||||
yield AwsCliCommands.awsListStacks((element) => __awaiter(this, void 0, void 0, function* () {
|
||||
if (deleteResources) {
|
||||
if (deleteResources && (!OneDayOlderOnly || AwsCliCommands.isOlderThan1day(element.CreationTime))) {
|
||||
if (element.StackName === 'game-ci' || element.TemplateDescription === 'Game-CI base stack') {
|
||||
cloud_runner_logger_1.default.log(`Skipping ${element.StackName} ignore list`);
|
||||
return;
|
||||
}
|
||||
cloud_runner_logger_1.default.log(`Deleting ${element.logGroupName}`);
|
||||
const deleteStackInput = { StackName: element.StackName };
|
||||
yield CF.deleteStack(deleteStackInput).promise();
|
||||
}
|
||||
}));
|
||||
yield AwsCliCommands.awsListTasks((taskElement, element) => __awaiter(this, void 0, void 0, function* () {
|
||||
if (deleteResources) {
|
||||
var _a;
|
||||
if (deleteResources && (!OneDayOlderOnly || AwsCliCommands.isOlderThan1day(taskElement.CreatedAt))) {
|
||||
cloud_runner_logger_1.default.log(`Stopping task ${(_a = taskElement.containers) === null || _a === void 0 ? void 0 : _a[0].name}`);
|
||||
yield ecs.stopTask({ task: taskElement.taskArn || '', cluster: element }).promise();
|
||||
}
|
||||
}));
|
||||
yield AwsCliCommands.awsListLogGroups((element) => __awaiter(this, void 0, void 0, function* () {
|
||||
if (deleteResources && (!OneDayOlderOnly || AwsCliCommands.isOlderThan1day(new Date(element.createdAt)))) {
|
||||
cloud_runner_logger_1.default.log(`Deleting ${element.logGroupName}`);
|
||||
yield cwl.deleteLogGroup({ logGroupName: element.logGroupName || '' }).promise();
|
||||
}
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-list-all`, `List all resources`)
|
||||
], AwsCliCommands, "awsListAll", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-garbage-collect`, `garbage collect aws resources not in use !WIP!`)
|
||||
], AwsCliCommands, "garbageCollectAws", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-garbage-collect-all`, `garbage collect aws resources regardless of whether they are in use`)
|
||||
], AwsCliCommands, "garbageCollectAwsAll", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-garbage-collect-all-1d-older`, `garbage collect aws resources created more than 1d ago (ignore if they are in use)`)
|
||||
], AwsCliCommands, "garbageCollectAwsAllOlderThanOneDay", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-list-stacks`, `List stacks`)
|
||||
], AwsCliCommands, "awsListStacks", null);
|
||||
|
|
@ -2139,15 +2177,6 @@ __decorate([
|
|||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-list-log-groups`, `List tasks`)
|
||||
], AwsCliCommands, "awsListLogGroups", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-garbage-collect`, `garbage collect aws`)
|
||||
], AwsCliCommands, "garbageCollectAws", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-garbage-collect-all`, `garbage collect aws`)
|
||||
], AwsCliCommands, "garbageCollectAwsAll", null);
|
||||
__decorate([
|
||||
cli_functions_repository_1.CliFunction(`aws-garbage-collect-all-1d-older`, `garbage collect aws`)
|
||||
], AwsCliCommands, "garbageCollectAwsAllOlderThanOneDay", null);
|
||||
exports.AwsCliCommands = AwsCliCommands;
|
||||
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -3,27 +3,50 @@ import { CliFunction } from '../../../../cli/cli-functions-repository';
|
|||
import Input from '../../../../input';
|
||||
import CloudRunnerLogger from '../../../services/cloud-runner-logger';
|
||||
import { BaseStackFormation } from '../cloud-formations/base-stack-formation';
|
||||
import { TaskDefinitionFormation } from '../cloud-formations/task-definition-formation';
|
||||
|
||||
export class AwsCliCommands {
|
||||
@CliFunction(`aws-list-all`, `List all resources`)
|
||||
static async awsListAll() {
|
||||
await AwsCliCommands.awsListStacks();
|
||||
await AwsCliCommands.awsListStacks(undefined, true);
|
||||
await AwsCliCommands.awsListTasks();
|
||||
await AwsCliCommands.awsListLogGroups();
|
||||
await AwsCliCommands.awsListLogGroups(undefined, true);
|
||||
}
|
||||
@CliFunction(`aws-garbage-collect`, `garbage collect aws resources not in use !WIP!`)
|
||||
static async garbageCollectAws() {
|
||||
await AwsCliCommands.cleanup(false);
|
||||
}
|
||||
@CliFunction(`aws-garbage-collect-all`, `garbage collect aws resources regardless of whether they are in use`)
|
||||
static async garbageCollectAwsAll() {
|
||||
await AwsCliCommands.cleanup(true);
|
||||
}
|
||||
@CliFunction(
|
||||
`aws-garbage-collect-all-1d-older`,
|
||||
`garbage collect aws resources created more than 1d ago (ignore if they are in use)`,
|
||||
)
|
||||
static async garbageCollectAwsAllOlderThanOneDay() {
|
||||
await AwsCliCommands.cleanup(true, true);
|
||||
}
|
||||
static isOlderThan1day(date: any) {
|
||||
const ageDate = new Date(date.getTime() - Date.now());
|
||||
|
||||
return ageDate.getDay() > 0;
|
||||
}
|
||||
@CliFunction(`aws-list-stacks`, `List stacks`)
|
||||
static async awsListStacks(perResultCallback: any = false) {
|
||||
static async awsListStacks(perResultCallback: any = false, verbose: boolean = false) {
|
||||
process.env.AWS_REGION = Input.region;
|
||||
const CF = new AWS.CloudFormation();
|
||||
const stacks =
|
||||
(await CF.listStacks().promise()).StackSummaries?.filter(
|
||||
(_x) =>
|
||||
_x.StackStatus !== 'DELETE_COMPLETE' &&
|
||||
_x.TemplateDescription === TaskDefinitionFormation.description.replace('\n', ''),
|
||||
(_x) => _x.StackStatus !== 'DELETE_COMPLETE', // &&
|
||||
// _x.TemplateDescription === TaskDefinitionFormation.description.replace('\n', ''),
|
||||
) || [];
|
||||
CloudRunnerLogger.log(`Stacks ${stacks.length}`);
|
||||
for (const element of stacks) {
|
||||
const ageDate = new Date(element.CreationTime.getTime() - Date.now());
|
||||
if (verbose)
|
||||
CloudRunnerLogger.log(
|
||||
`Task Stack ${element.StackName} - Age D${ageDate.getDay()} H${ageDate.getHours()} M${ageDate.getMinutes()}`,
|
||||
);
|
||||
if (perResultCallback) await perResultCallback(element);
|
||||
}
|
||||
const baseStacks =
|
||||
|
|
@ -33,6 +56,13 @@ export class AwsCliCommands {
|
|||
) || [];
|
||||
CloudRunnerLogger.log(`Base Stacks ${baseStacks.length}`);
|
||||
for (const element of baseStacks) {
|
||||
const ageDate = new Date(element.CreationTime.getTime() - Date.now());
|
||||
if (verbose)
|
||||
CloudRunnerLogger.log(
|
||||
`Base Stack ${
|
||||
element.StackName
|
||||
} - Age D${ageDate.getHours()} H${ageDate.getHours()} M${ageDate.getMinutes()}`,
|
||||
);
|
||||
if (perResultCallback) await perResultCallback(element);
|
||||
}
|
||||
if (stacks === undefined) {
|
||||
|
|
@ -49,6 +79,7 @@ export class AwsCliCommands {
|
|||
const input: AWS.ECS.ListTasksRequest = {
|
||||
cluster: element,
|
||||
};
|
||||
|
||||
const list = (await ecs.listTasks(input).promise()).taskArns || [];
|
||||
if (list.length > 0) {
|
||||
const describeInput: AWS.ECS.DescribeTasksRequest = { tasks: list, cluster: element };
|
||||
|
|
@ -73,53 +104,67 @@ export class AwsCliCommands {
|
|||
}
|
||||
}
|
||||
@CliFunction(`aws-list-log-groups`, `List tasks`)
|
||||
static async awsListLogGroups(perResultCallback: any = false) {
|
||||
static async awsListLogGroups(perResultCallback: any = false, verbose: boolean = false) {
|
||||
process.env.AWS_REGION = Input.region;
|
||||
const ecs = new AWS.CloudWatchLogs();
|
||||
const logStreamInput: AWS.CloudWatchLogs.DescribeLogGroupsRequest = { logGroupNamePrefix: 'game-ci' };
|
||||
const logGroups = (await ecs.describeLogGroups(logStreamInput).promise()).logGroups || [];
|
||||
let logStreamInput: AWS.CloudWatchLogs.DescribeLogGroupsRequest = {
|
||||
/* logGroupNamePrefix: 'game-ci' */
|
||||
};
|
||||
let logGroupsDescribe = await ecs.describeLogGroups(logStreamInput).promise();
|
||||
const logGroups = logGroupsDescribe.logGroups || [];
|
||||
while (logGroupsDescribe.nextToken) {
|
||||
logStreamInput = { /* logGroupNamePrefix: 'game-ci',*/ nextToken: logGroupsDescribe.nextToken };
|
||||
logGroupsDescribe = await ecs.describeLogGroups(logStreamInput).promise();
|
||||
logGroups.push(...(logGroupsDescribe?.logGroups || []));
|
||||
}
|
||||
|
||||
CloudRunnerLogger.log(`Log Groups ${logGroups.length}`);
|
||||
for (const element of logGroups) {
|
||||
if (element.creationTime === undefined) {
|
||||
CloudRunnerLogger.log(`Skipping ${element.logGroupName} no createdAt date`);
|
||||
continue;
|
||||
}
|
||||
const ageDate = new Date(new Date(element.creationTime).getTime() - Date.now());
|
||||
if (verbose)
|
||||
CloudRunnerLogger.log(
|
||||
`Log Group Name ${
|
||||
element.logGroupName
|
||||
} - Age D${ageDate.getDay()} H${ageDate.getHours()} M${ageDate.getMinutes()} - 1d old ${AwsCliCommands.isOlderThan1day(
|
||||
new Date(element.creationTime),
|
||||
)}`,
|
||||
);
|
||||
if (perResultCallback) await perResultCallback(element, element);
|
||||
}
|
||||
}
|
||||
|
||||
@CliFunction(`aws-garbage-collect`, `garbage collect aws`)
|
||||
static async garbageCollectAws() {
|
||||
await AwsCliCommands.cleanup(false);
|
||||
}
|
||||
@CliFunction(`aws-garbage-collect-all`, `garbage collect aws`)
|
||||
static async garbageCollectAwsAll() {
|
||||
await AwsCliCommands.cleanup(true);
|
||||
}
|
||||
@CliFunction(`aws-garbage-collect-all-1d-older`, `garbage collect aws`)
|
||||
static async garbageCollectAwsAllOlderThanOneDay() {
|
||||
await AwsCliCommands.cleanup(true);
|
||||
}
|
||||
|
||||
private static async cleanup(deleteResources = false) {
|
||||
private static async cleanup(deleteResources = false, OneDayOlderOnly: boolean = false) {
|
||||
process.env.AWS_REGION = Input.region;
|
||||
const CF = new AWS.CloudFormation();
|
||||
const ecs = new AWS.ECS();
|
||||
const cwl = new AWS.CloudWatchLogs();
|
||||
await AwsCliCommands.awsListStacks(async (element) => {
|
||||
if (deleteResources) {
|
||||
if (deleteResources && (!OneDayOlderOnly || AwsCliCommands.isOlderThan1day(element.CreationTime))) {
|
||||
if (element.StackName === 'game-ci' || element.TemplateDescription === 'Game-CI base stack') {
|
||||
CloudRunnerLogger.log(`Skipping ${element.StackName} ignore list`);
|
||||
|
||||
return;
|
||||
}
|
||||
CloudRunnerLogger.log(`Deleting ${element.logGroupName}`);
|
||||
const deleteStackInput: AWS.CloudFormation.DeleteStackInput = { StackName: element.StackName };
|
||||
await CF.deleteStack(deleteStackInput).promise();
|
||||
}
|
||||
});
|
||||
await AwsCliCommands.awsListTasks(async (taskElement, element) => {
|
||||
if (deleteResources) {
|
||||
if (deleteResources && (!OneDayOlderOnly || AwsCliCommands.isOlderThan1day(taskElement.CreatedAt))) {
|
||||
CloudRunnerLogger.log(`Stopping task ${taskElement.containers?.[0].name}`);
|
||||
await ecs.stopTask({ task: taskElement.taskArn || '', cluster: element }).promise();
|
||||
}
|
||||
});
|
||||
await AwsCliCommands.awsListLogGroups(async (element) => {
|
||||
if (deleteResources && (!OneDayOlderOnly || AwsCliCommands.isOlderThan1day(new Date(element.createdAt)))) {
|
||||
CloudRunnerLogger.log(`Deleting ${element.logGroupName}`);
|
||||
await cwl.deleteLogGroup({ logGroupName: element.logGroupName || '' }).promise();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue