FindInstance.py 1.2 KB

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