2021-03-05 00:12:31 +00:00
|
|
|
const msRestNodeAuth = require("@azure/ms-rest-nodeauth");
|
|
|
|
const { CosmosDBManagementClient } = require("@azure/arm-cosmosdb");
|
2021-03-05 04:49:52 +00:00
|
|
|
const ms = require("ms");
|
2021-01-28 02:37:14 +00:00
|
|
|
|
2024-04-09 18:55:08 +01:00
|
|
|
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];
|
2021-03-05 00:12:31 +00:00
|
|
|
const resourceGroupName = "runners";
|
2021-01-28 02:37:14 +00:00
|
|
|
|
2021-03-15 03:53:16 +00:00
|
|
|
const thirtyMinutesAgo = new Date(Date.now() - 1000 * 60 * 30).getTime();
|
2021-01-28 02:37:14 +00:00
|
|
|
|
2021-03-10 22:03:50 +00:00
|
|
|
function friendlyTime(date) {
|
|
|
|
try {
|
|
|
|
return ms(date);
|
|
|
|
} catch (error) {
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:12:31 +00:00
|
|
|
async function main() {
|
2024-04-09 18:55:08 +01:00
|
|
|
const credentials = await msRestNodeAuth.AzureCliCredentials.create();
|
2021-03-05 00:12:31 +00:00
|
|
|
const client = new CosmosDBManagementClient(credentials, subscriptionId);
|
|
|
|
const accounts = await client.databaseAccounts.list(resourceGroupName);
|
|
|
|
for (const account of accounts) {
|
|
|
|
if (account.kind === "MongoDB") {
|
|
|
|
const mongoDatabases = await client.mongoDBResources.listMongoDBDatabases(resourceGroupName, account.name);
|
|
|
|
for (const database of mongoDatabases) {
|
2021-04-20 04:08:25 +01:00
|
|
|
// Unfortunately Mongo does not provide a timestamp in ARM. There is no way to tell how old the DB is other thn encoding it in the ID :(
|
2021-03-08 16:14:31 +00:00
|
|
|
const timestamp = Number(database.name.split("-")[1]);
|
2021-03-15 03:53:16 +00:00
|
|
|
if (timestamp && timestamp < thirtyMinutesAgo) {
|
2021-03-05 00:12:31 +00:00
|
|
|
await client.mongoDBResources.deleteMongoDBDatabase(resourceGroupName, account.name, database.name);
|
2021-03-10 22:03:50 +00:00
|
|
|
console.log(`DELETED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
2021-03-05 00:12:31 +00:00
|
|
|
} else {
|
2021-03-10 22:03:50 +00:00
|
|
|
console.log(`SKIPPED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
2021-03-05 00:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-20 04:08:25 +01:00
|
|
|
} else if (account.capabilities.find((c) => c.name === "EnableCassandra")) {
|
|
|
|
const cassandraDatabases = await client.cassandraResources.listCassandraKeyspaces(
|
|
|
|
resourceGroupName,
|
2024-04-09 18:55:08 +01:00
|
|
|
account.name,
|
2021-04-20 04:08:25 +01:00
|
|
|
);
|
|
|
|
for (const database of cassandraDatabases) {
|
|
|
|
const timestamp = Number(database.resource._ts) * 1000;
|
|
|
|
if (timestamp && timestamp < thirtyMinutesAgo) {
|
|
|
|
await client.cassandraResources.deleteCassandraKeyspace(resourceGroupName, account.name, database.name);
|
|
|
|
console.log(`DELETED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
|
|
|
} else {
|
|
|
|
console.log(`SKIPPED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (account.capabilities.find((c) => c.name === "EnableTable")) {
|
|
|
|
const tablesDatabase = await client.tableResources.listTables(resourceGroupName, account.name);
|
|
|
|
for (const database of tablesDatabase) {
|
|
|
|
const timestamp = Number(database.resource._ts) * 1000;
|
|
|
|
if (timestamp && timestamp < thirtyMinutesAgo) {
|
|
|
|
await client.tableResources.deleteTable(resourceGroupName, account.name, database.name);
|
|
|
|
console.log(`DELETED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
|
|
|
} else {
|
|
|
|
console.log(`SKIPPED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (account.capabilities.find((c) => c.name === "EnableGremlin")) {
|
|
|
|
const graphDatabases = await client.gremlinResources.listGremlinDatabases(resourceGroupName, account.name);
|
|
|
|
for (const database of graphDatabases) {
|
|
|
|
const timestamp = Number(database.resource._ts) * 1000;
|
|
|
|
if (timestamp && timestamp < thirtyMinutesAgo) {
|
|
|
|
await client.gremlinResources.deleteGremlinDatabase(resourceGroupName, account.name, database.name);
|
|
|
|
console.log(`DELETED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
|
|
|
} else {
|
|
|
|
console.log(`SKIPPED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 00:12:31 +00:00
|
|
|
} else if (account.kind === "GlobalDocumentDB") {
|
|
|
|
const sqlDatabases = await client.sqlResources.listSqlDatabases(resourceGroupName, account.name);
|
|
|
|
for (const database of sqlDatabases) {
|
2021-04-20 04:08:25 +01:00
|
|
|
const timestamp = Number(database.resource._ts) * 1000;
|
2021-03-15 03:53:16 +00:00
|
|
|
if (timestamp && timestamp < thirtyMinutesAgo) {
|
2021-03-05 00:12:31 +00:00
|
|
|
await client.sqlResources.deleteSqlDatabase(resourceGroupName, account.name, database.name);
|
2021-03-10 22:03:50 +00:00
|
|
|
console.log(`DELETED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
2021-03-05 00:12:31 +00:00
|
|
|
} else {
|
2021-03-10 22:03:50 +00:00
|
|
|
console.log(`SKIPPED: ${account.name} | ${database.name} | Age: ${friendlyTime(Date.now() - timestamp)}`);
|
2021-03-05 00:12:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:12:31 +00:00
|
|
|
main()
|
2021-01-28 02:37:14 +00:00
|
|
|
.then(() => {
|
2021-03-05 00:12:31 +00:00
|
|
|
console.log("Completed");
|
2021-01-28 02:37:14 +00:00
|
|
|
process.exit(0);
|
|
|
|
})
|
2021-03-05 00:12:31 +00:00
|
|
|
.catch((err) => {
|
2021-03-08 16:14:31 +00:00
|
|
|
console.log(err);
|
|
|
|
console.log("Cleanup failed! Exiting with success. Cleanup should always fail safe.");
|
|
|
|
process.exit(0);
|
2021-01-28 02:37:14 +00:00
|
|
|
});
|