|
@@ -0,0 +1,59 @@
|
|
|
+#! /usr/bin/python
|
|
|
+#
|
|
|
+# Find an instance amongst all profile configured.
|
|
|
+#
|
|
|
+# Unbuffered, no CRLF print:
|
|
|
+from __future__ import print_function
|
|
|
+import re, sys, os, subprocess
|
|
|
+import boto3
|
|
|
+import boto3.session
|
|
|
+import threading # We may not do it yet, but developing with threadsafe in mind, as best I can
|
|
|
+
|
|
|
+try:
|
|
|
+ # Python 3
|
|
|
+ import builtins
|
|
|
+except ImportError:
|
|
|
+ # Python 2
|
|
|
+ import __builtin__ as builtins
|
|
|
+
|
|
|
+# Debug levels:
|
|
|
+# 1 = Show progress
|
|
|
+# 2 = Informational
|
|
|
+# 5 = Include boto3 logging
|
|
|
+DEBUG=2
|
|
|
+
|
|
|
+if len(sys.argv) != 2:
|
|
|
+ print("Usage: " + os.path.basename(sys.argv[0]) + " <searchstring>")
|
|
|
+ exit(1)
|
|
|
+
|
|
|
+SEARCHSTRING=sys.argv[1]
|
|
|
+
|
|
|
+# Load profiles
|
|
|
+try:
|
|
|
+ cfile = open(os.path.expanduser("~") + "/.aws/credentials", "r")
|
|
|
+except:
|
|
|
+ print("You must have a ~/.aws/credentials file with profiles configured.")
|
|
|
+ exit(3)
|
|
|
+
|
|
|
+profiles = set()
|
|
|
+for line in cfile:
|
|
|
+ profile = re.match('^\[(.+)\]', line)
|
|
|
+ if(profile):
|
|
|
+ profiles.add(profile.group(1))
|
|
|
+
|
|
|
+# End of for line
|
|
|
+FOUND=0
|
|
|
+for profile in profiles:
|
|
|
+ if DEBUG >= 2:
|
|
|
+ print("Searching profile " + profile)
|
|
|
+ returncode = subprocess.call(['FindInstanceInProfile.py', SEARCHSTRING, profile])
|
|
|
+ if(returncode == 0):
|
|
|
+ # Found one
|
|
|
+ FOUND = FOUND + 1
|
|
|
+
|
|
|
+# Searched all the profiles
|
|
|
+if(FOUND > 0):
|
|
|
+ exit(0)
|
|
|
+exit(255)
|
|
|
+
|
|
|
+
|