Add a script to copy AMIs across the regions
authorRail Aliiev <rail@mozilla.com>
Mon, 16 Jun 2014 13:47:29 -0400
changeset 443 b3c8b5dc5f9cf89f437fdc5e67f4fe75cef144d7
parent 442 43e862a1dcb10fc0fe26cd84f9df67e6a5cb85d7
child 444 f5d0cb4b975dfe472bd07a016d9067f01d77c2d6
push id438
push userraliiev@mozilla.com
push dateMon, 16 Jun 2014 17:47:34 +0000
Add a script to copy AMIs across the regions
scripts/copy_ami.py
new file mode 100644
--- /dev/null
+++ b/scripts/copy_ami.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+import argparse
+import logging
+import os
+import site
+
+site.addsitedir(os.path.join(os.path.dirname(__file__), ".."))
+from cloudtools.aws import get_aws_connection
+from cloudtools.aws.ami import copy_ami
+
+log = logging.getLogger(__name__)
+
+
+def main():
+    logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s")
+    log.setLevel(logging.INFO)
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-f", "--from-region", required=True,
+                        help="Region to copy from")
+    parser.add_argument("-t", "--to-region", action="append", required=True,
+                        dest="to_regions", help="Regions to copy to")
+    parser.add_argument("amis", metavar="AMI", nargs="+",
+                        help="AMI IDs to be copied")
+    args = parser.parse_args()
+
+    conn_from = get_aws_connection(args.from_region)
+    amis_to_copy = conn_from.get_all_images(image_ids=args.amis)
+    for ami in amis_to_copy:
+        for r in args.to_regions:
+            log.info("Copying %s (%s) to %s", ami.id, ami.tags.get("Name"), r)
+            new_ami = copy_ami(ami, r)
+            log.info("New AMI created. AMI ID: %s", new_ami.id)
+
+if __name__ == '__main__':
+    main()