From cf6db0fa0a094ed3333d3f8cb2c5581f03badd5d Mon Sep 17 00:00:00 2001 From: KenF Date: Thu, 22 Jan 2026 10:13:22 +0800 Subject: [PATCH] feat: porting shell scripts to python --- aws-emr-inventory.sh | 18 ------------------ aws-emr.py | 32 ++++++++++++++++++++++++++++++++ aws-subnets.py | 5 ++--- 3 files changed, 34 insertions(+), 21 deletions(-) delete mode 100755 aws-emr-inventory.sh create mode 100755 aws-emr.py diff --git a/aws-emr-inventory.sh b/aws-emr-inventory.sh deleted file mode 100755 index f39f155..0000000 --- a/aws-emr-inventory.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash - -function listRes() { - aws --region $1 emr list-clusters --query Clusters[].Id --output text | sed 's/\t/\n/g' | while read i; do - aws --region $1 emr describe-cluster --cluster-id $i | jq -cr '.Cluster | [.Id, .Name, .ReleaseLabel, .Ec2InstanceAttributes.Ec2AvailabilityZone] | @csv' | tr -d \" - done -} - -export -f listRes - -# Generate inventory of ec2 in all regions - -echo "ClusterId,ClusterName,ReleaseLabel,AZ" -aws --region=us-east-1 ec2 describe-regions --query Regions[].RegionName --output text | sed -e 's/\t/\n/g' | while read r; do - sem -j6 listRes $r -done - -sem --wait diff --git a/aws-emr.py b/aws-emr.py new file mode 100755 index 0000000..85beae3 --- /dev/null +++ b/aws-emr.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +r""" +Documentation + +License: This program is released under the MIT License +""" + +# Imports +import boto3 +import concurrent.futures + +def getRegions(all_regions=False): + ec2 = boto3.client('ec2') + response = ec2.describe_regions(AllRegions=all_regions) + return [region['RegionName'] for region in response['Regions']] + +def printResources(region_name: str): + client = boto3.client('emr', region_name=region_name) + response = client.list_clusters() + for i in response['Clusters']: + print(f"{i['Name']}, {i['Status']}, {i['NormalizedInstanceHours']}, {region_name}") + +# Main function +def main() -> None: + print("Name, Status, NormalizedInstanceHours, Region") + with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor: + futures = [executor.submit(printResources, region_name=r) for r in getRegions()] + results = [future.result() for future in concurrent.futures.wait(futures).done] + +# Call main function +if __name__ == '__main__': + main() diff --git a/aws-subnets.py b/aws-subnets.py index 706d250..568be0a 100755 --- a/aws-subnets.py +++ b/aws-subnets.py @@ -20,12 +20,11 @@ def printResources(region_name: str): for i in response['Subnets']: default_vpc_check = client.describe_vpcs(VpcIds=[i['VpcId']]) for v in default_vpc_check['Vpcs']: - if not v['IsDefault']: - print(f"{i['SubnetId']}, {i['VpcId']}, {i['CidrBlock']}, {i['AvailabilityZone']}") + print(f"{i['SubnetId']}, {i['VpcId']}, {i['CidrBlock']}, {i['AvailabilityZone']}, {v['IsDefault']}") # Main function def main() -> None: - print("SubnetId, VpcId, CidrBlock, AvailabilityZone") + print("SubnetId, VpcId, CidrBlock, AvailabilityZone, InDefaultVpc") with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor: futures = [executor.submit(printResources, region_name=r) for r in getRegions()] results = [future.result() for future in concurrent.futures.wait(futures).done]