FindSecurityGroup.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #! /usr/bin/python
  2. #
  3. # Find a security group amongst all profile configured. Note that it makes multiple calls
  4. # to the FindSecurityGroupInProfile.py in parallel.
  5. #
  6. # Unbuffered, no CRLF print:
  7. from __future__ import print_function
  8. import re, sys, os, subprocess
  9. import boto3
  10. import boto3.session
  11. import threading # We may not do it yet, but developing with threadsafe in mind, as best I can
  12. try:
  13. # Python 3
  14. import builtins
  15. except ImportError:
  16. # Python 2
  17. import __builtin__ as builtins
  18. # Debug levels:
  19. # 1 = Show progress
  20. # 2 = Informational
  21. # 5 = Include boto3 logging
  22. DEBUG=0
  23. if len(sys.argv) != 2:
  24. print("Usage: " + os.path.basename(sys.argv[0]) + " <searchstring>")
  25. exit(1)
  26. SEARCHSTRING=sys.argv[1]
  27. # Load profiles
  28. try:
  29. cfile = open(os.path.expanduser("~") + "/.aws/credentials", "r")
  30. except:
  31. print("You must have a ~/.aws/credentials file with profiles configured.")
  32. exit(3)
  33. profiles = set()
  34. for line in cfile:
  35. profile = re.match('^\[(.+)\]', line)
  36. if(profile):
  37. profiles.add(profile.group(1))
  38. # End of for line
  39. FOUND=0
  40. processes = set()
  41. for profile in profiles:
  42. if DEBUG >= 2:
  43. print("Searching profile " + profile)
  44. process = subprocess.Popen(['FindSecurityGroupInProfile.py', SEARCHSTRING, profile])
  45. if DEBUG >= 2:
  46. print("PID = " + str(process.pid))
  47. processes.add(process)
  48. # We should have spawned all child processes. Let's wait for them.
  49. for process in processes:
  50. if DEBUG >= 2:
  51. print("Waiting on process " + str(process.pid))
  52. returncode = process.wait()
  53. if(returncode == 0):
  54. # Found one
  55. FOUND = FOUND + 1
  56. # Searched all the profiles
  57. if(FOUND > 0):
  58. exit(0)
  59. exit(255)