Bug 986477 - Don't require puppet or DNS to launch new instances
authorRail Aliiev <rail@mozilla.com>
Mon, 09 Jun 2014 11:45:26 -0400
changeset 429 dbdf3c0695e8de9d8be090abc6df5333fa7aa7fc
parent 428 acac9151bbb8242d28dde3ec991df4a234292009
child 430 7c61a3d3b00e19c74fb7649e43c31fc720a1f578
push id425
push userraliiev@mozilla.com
push dateMon, 09 Jun 2014 15:45:30 +0000
bugs986477
Bug 986477 - Don't require puppet or DNS to launch new instances
scripts/aws_manage_instances.py
scripts/aws_terminate_by_ami_id.py
--- a/scripts/aws_manage_instances.py
+++ b/scripts/aws_manage_instances.py
@@ -120,20 +120,20 @@ if __name__ == '__main__':
     logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s")
     if not args.quiet:
         log.setLevel(logging.INFO)
     else:
         log.setLevel(logging.ERROR)
 
     if not args.regions:
         args.regions = DEFAULT_REGIONS
+
     for region in args.regions:
         conn = get_aws_connection(region)
-        res = conn.get_all_instances()
-        instances = reduce(lambda a, b: a + b, [r.instances for r in res])
+        instances = conn.get_only_instances()
         for i in instances:
             name = i.tags.get('Name', '')
             instance_id = i.id
             if not i.private_ip_address:
                 # Terminated instances has no IP address assinged
                 log.debug("Skipping (terminated?) %s (%s)..." % (name,
                                                                  instance_id))
                 continue
new file mode 100644
--- /dev/null
+++ b/scripts/aws_terminate_by_ami_id.py
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+"""
+Kills instances with specified AMI IDs
+"""
+import site
+import os
+import logging
+import argparse
+import time
+
+site.addsitedir(os.path.join(os.path.dirname(__file__), ".."))
+from cloudtools.aws import get_aws_connection, DEFAULT_REGIONS
+
+log = logging.getLogger(__name__)
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-r", "--region", action="append", dest="regions")
+    parser.add_argument("amis", metavar="AMI", nargs="+",
+                        help="AMI IDs")
+    parser.add_argument("-v", "--verbose", action="store_true",
+                        help="Verbose logging")
+    args = parser.parse_args()
+
+    logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s")
+    if args.verbose:
+        log.setLevel(logging.DEBUG)
+    else:
+        log.setLevel(logging.INFO)
+
+    regions = args.regions
+    if not regions:
+        regions = DEFAULT_REGIONS
+
+    instances_to_kill = []
+    for r in regions:
+        log.debug("working in %s", r)
+        conn = get_aws_connection(r)
+        instances = conn.get_only_instances(filters={"image-id": args.amis})
+        log.debug("got %s instances:\n%s", len(instances), instances)
+        if instances:
+            instances_to_kill.extend(instances)
+    if instances_to_kill:
+        log.info("Preparing to terminate the following instances:")
+        for i in instances_to_kill:
+            log.info("%s (%s)", i.id, i.tags.get("Name"))
+        yesno = raw_input("Are you sure you want to kill these? ^ y/N >")
+        if yesno != "y":
+            log.info("Exiting without any changes!")
+            return
+
+        yesno = raw_input("ARE YOU SURE YOU WANT TO KILL THESE? ^"
+                          " LAST WARNING!!! y/N >")
+        if yesno != "y":
+            log.info("Exiting without any changes!")
+            return
+        log.warn("The instances mentioned above are about to be terminated")
+        log.warn("Waiting extra 60 seconds to make sure...")
+        time.sleep(60)
+        log.warn("Starting...")
+        for i in instances_to_kill:
+            log.warn("Terminating %s...", i)
+            # i.terminate()
+            log.warn("Done.")
+
+if __name__ == '__main__':
+    main()