#!/bin/env zx

const regions = JSON.parse(await $`aws ec2 describe-regions`).Regions.map(r => r.RegionName);

for (const region of regions) {
    console.log(`Checking for active fleets in region ${region}...`);
    const fleets = JSON.parse(await $`aws ec2 describe-fleets --region ${region} --filters Name=fleet-state,Values=active`).Fleets
    if (fleets.length > 0) {
        //        console.log(fleets);
        console.log(`Terminating fleets in region ${region}:`);
        for (const fleet of fleets) {
            console.log(`- ${fleet.FleetId}`);
            await $`aws ec2 delete-fleets --region ${region} --fleet-id ${fleet.FleetId} --terminate-instances`;
        }
    }
}
