Browse Source

Small fixes to update-ami-accounts

Duane Waddle 4 years ago
parent
commit
d383d63ffd
1 changed files with 27 additions and 6 deletions
  1. 27 6
      bin/update-ami-accounts

+ 27 - 6
bin/update-ami-accounts

@@ -17,7 +17,7 @@ Example 1: Let's just run a report of all AMIs matching '*Duane*' in all regions
 profile has access to.  Notice the wildcards in quotes so bash won't try to expand them
 profile has access to.  Notice the wildcards in quotes so bash won't try to expand them
 out to filenames.
 out to filenames.
 
 
-[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./duane.py '*Duane*'
+[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./update-ami-accounts '*Duane*'
 Looking for AMIs matching "*Duane*" in the following regions:
 Looking for AMIs matching "*Duane*" in the following regions:
     us-gov-east-1
     us-gov-east-1
     us-gov-west-1
     us-gov-west-1
@@ -31,7 +31,7 @@ us-gov-west-1  |ami-0ee37a86b09aefad0 |Duane_Testing_20201124233617
 
 
 Example 2: Regions can be specified with a list or wildcard.  This is just a report too:
 Example 2: Regions can be specified with a list or wildcard.  This is just a report too:
 
 
-[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./duane.py --region us-gov-east-1 --region '*west*' '*Duane*'
+[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./update-ami-accounts --region us-gov-east-1 --region '*west*' '*Duane*'
 Looking for AMIs matching "*Duane*" in the following regions:
 Looking for AMIs matching "*Duane*" in the following regions:
     us-gov-east-1
     us-gov-east-1
     us-gov-west-1
     us-gov-west-1
@@ -44,7 +44,7 @@ us-gov-west-1  |ami-0ee37a86b09aefad0 |Duane_Testing_20201124233617
 
 
 Example 3: If we list one or more accounts then sharing is updated
 Example 3: If we list one or more accounts then sharing is updated
 
 
-[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./duane.py --region '*1' '*Duane*' 738800754746 721817724804
+[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./update-ami-accounts --region '*1' '*Duane*' 738800754746 721817724804
 Looking for AMIs matching "*Duane*" in the following regions:
 Looking for AMIs matching "*Duane*" in the following regions:
  us-gov-east-1
  us-gov-east-1
  us-gov-west-1
  us-gov-west-1
@@ -62,7 +62,7 @@ us-gov-west-1  |ami-0ee37a86b09aefad0 |Duane_Testing_20201124233617            |
 Example 4: Sharing updates are atomic so if you could get a failure because
 Example 4: Sharing updates are atomic so if you could get a failure because
 one of several accounts you listed does not exist:
 one of several accounts you listed does not exist:
 
 
-[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./duane.py --region '*1' '*Duane*' 738800754746 72181772480
+[duane.e.waddle@DPS0591 bin]$ AWS_PROFILE=gov-common-services-terraformer ./update-ami-accounts --region '*1' '*Duane*' 738800754746 72181772480
 Looking for AMIs matching "*Duane*" in the following regions:
 Looking for AMIs matching "*Duane*" in the following regions:
  us-gov-east-1
  us-gov-east-1
  us-gov-west-1
  us-gov-west-1
@@ -83,6 +83,8 @@ message as to which one caused the error.  Maybe one day I'll improve that...
 """
 """
 
 
 import argparse
 import argparse
+import re
+import sys
 import boto3
 import boto3
 import botocore
 import botocore
 from botocore.config import Config
 from botocore.config import Config
@@ -196,8 +198,7 @@ def runmain(ami_filter,accounts,region_filters):
             else:
             else:
                 print(report_format.format(region,ami.get('ImageId'),ami.get('Name')))
                 print(report_format.format(region,ami.get('ImageId'),ami.get('Name')))
 
 
-
-if __name__ == "__main__":
+def cli():
 
 
     parser = argparse.ArgumentParser()
     parser = argparse.ArgumentParser()
     parser.add_argument('--region',action='append',required=False,
     parser.add_argument('--region',action='append',required=False,
@@ -206,4 +207,24 @@ if __name__ == "__main__":
     parser.add_argument('accounts',nargs='*',help='list of AWS accounts to add AMIs to')
     parser.add_argument('accounts',nargs='*',help='list of AWS accounts to add AMIs to')
     args = parser.parse_args()
     args = parser.parse_args()
 
 
+    # Remove dashes from account IDs as a nice thing for user
+    args.accounts = [ f.replace("-","") for f in args.accounts ]
+
+    # AWS Accounts are 12 digits, all digits
+    invalid_accounts = []
+    digit_check = re.compile(r"^\d+$")
+    for account in args.accounts:
+        if len(account) != 12:
+            invalid_accounts.append("{0}: {1}".format(account,"Account length not 12"))
+        elif digit_check.fullmatch(account) is None:
+            invalid_accounts.append("{0}: {1}".format(account,"Account contains non-digit chars"))
+
+    if len(invalid_accounts) > 0:
+        for message in invalid_accounts:
+            print(message)
+        sys.exit(1)
+
     runmain(args.ami_filter,args.accounts,args.region)
     runmain(args.ami_filter,args.accounts,args.region)
+
+if __name__ == "__main__":
+    cli()