Created
October 28, 2012 15:34
-
-
Save pcn/3968925 to your computer and use it in GitHub Desktop.
patch to use permission_amis.py in the west
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ~/tmp/permission_amis.py | |
| --- bin/permission_amis.py 2012-03-26 14:00:05.000000000 -0400 | |
| +++ /Users/pn/tmp/permission_amis.py 2012-10-27 16:24:24.000000000 -0400 | |
| @@ -14,10 +14,12 @@ | |
| import sys | |
| import boto | |
| from optparse import OptionParser | |
| +import boto.ec2 | |
| +import boto.exception | |
| import chef # PyChef api must be installed | |
| -import pbs # easier/more convenient way to access command line. MUCH easier | |
| +import sh # easier/more convenient way to access command line. MUCH easier | |
| # Let's be indiscriminate here. Every AMI that we produce should be usable by all of our | |
| # accounts | |
| @@ -32,13 +34,16 @@ | |
| } | |
| databag_name = "aws_ami" | |
| -region= 'us-east-1' # make this more flexible in the future | |
| +region= 'us-west-1' # make this more flexible in the future | |
| + | |
| +class UselessAMIError(Exception): | |
| + pass | |
| def get_ami_list(match_accountid='util', name_match=None): | |
| # All amis names starting with the below prefix and which | |
| # are amis will have permissions set across accounts | |
| prefix="knewton-ami/" | |
| - image_list = pbs.ec2_describe_images().split("\n") | |
| + image_list = sh.ec2_describe_images().split("\n") | |
| ami_list = list() | |
| @@ -102,7 +107,10 @@ | |
| useful_name = name_field.split('/')[1] | |
| root_storage_type = useful_name.split('_')[0] | |
| - date_info = "{0[2]}_{0[3]}".format(useful_name.split('_')).split('-')[0] | |
| + try: | |
| + date_info = "{0[2]}_{0[3]}".format(useful_name.split('_')).split('-')[0] | |
| + except Exception as e: | |
| + raise UselessAMIError | |
| abi_bits = useful_name.split('-')[1] | |
| ubuntu_distro = useful_name.split('-')[2] | |
| @@ -126,11 +134,19 @@ | |
| return (date_info, key, value,) | |
| chef_ami_data = list() | |
| - e = boto.connect_ec2() | |
| + e = boto.ec2.connect_to_region(region) | |
| for ami in ami_list: | |
| - i = e.get_image(ami[0]) | |
| + try: | |
| + i = e.get_image(ami[0]) | |
| + except boto.exception.EC2ResponseError as err: | |
| + print str(err) | |
| + continue | |
| i.set_launch_permissions(account_ids.values()) | |
| - meaning = meaning_from_ami_name(ami) | |
| + try: | |
| + meaning = meaning_from_ami_name(ami) | |
| + except UselessAMIError: | |
| + continue | |
| + | |
| chef_ami_data.append(meaning) | |
| return chef_ami_data | |
| @@ -152,7 +168,7 @@ | |
| item_data["id"] = item_name | |
| - post_to_databag(databag_name, item_name, item_data, ) | |
| + # post_to_databag(databag_name, item_name, item_data, ) | |
| def main(argv=sys.argv[:]): | |
| parser = OptionParser() | |
| @@ -176,4 +192,4 @@ | |
| if __name__ == '__main__': | |
| - main() | |
| \ No newline at end of file | |
| + main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment