~launchpad-pqm/launchpad/devel

9265.1.1 by Jonathan Lange
Add lp-dev-tools that lack license confusion, changing the license to match
1
#!/usr/bin/env python
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
6
import os
7
import pprint
8
import re
9
import sys
10
11
import boto
12
14612.2.6 by William Grant
utilities
13
9265.1.1 by Jonathan Lange
Add lp-dev-tools that lack license confusion, changing the license to match
14
owners = {
15
    'gary': 255383312499,
16
    'francis': 559320013529}
17
18
_image_match = re.compile(
19
    r'launchpad-ec2test(\d+)/image.manifest.xml$').match
20
21
if __name__ == '__main__':
22
    if len(sys.argv) == 1:
23
        owner = os.environ['USER']
24
    elif len(sys.argv) == 2:
25
        owner = sys.argv[1]
26
    else:
27
        raise RuntimeError('Too many arguments')
28
    try:
29
        owner = int(owner)
30
    except ValueError:
31
        owner = owners[owner]
32
    # Get the AWS identifier and secret identifier.
33
    aws_id = os.path.join(os.environ['HOME'], '.ec2', 'aws_id')
34
    if not os.path.exists(aws_id):
35
        raise RuntimeError(
36
            "Please put your aws access key identifier and secret access "
37
            "key identifier in %s. (On two lines).\n" % (aws_id,))
38
    aws_file = open(aws_id, 'r')
39
    try:
40
        identifier = aws_file.readline().strip()
41
        secret = aws_file.readline().strip()
42
    finally:
43
        aws_file.close()
44
    # Make the EC2 connection.
45
    conn = boto.connect_ec2(identifier, secret)
46
    # get images
47
    images = {}
48
    for image in conn.get_all_images(owners=owners.values()):
49
        match = _image_match(image.location)
50
        if match:
51
            val = int(match.group(1))
52
            if val not in images:
53
                images[val] = [image]
54
            else:
55
                images[val].append(image)
56
    images = images.items()
57
    images.sort()
58
    # set
59
    last, recent = images[-2:]
60
    for tmp in (last, recent):
61
        if len(tmp[1]) > 1:
62
            raise ValueError(
63
                'more than one image of value %d found: %r' % tmp)
64
    recent = recent[1][0]
65
    last = last[1][0]
66
    perms = last.get_launch_permissions()
67
    perms['user_ids'].extend(owners.values())
68
    recent.set_launch_permissions(**perms)
69
    # done
70
    pprint.pprint(recent.get_launch_permissions())