~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to utilities/ec2test-share-images.py

  • Committer: Jonathan Lange
  • Date: 2009-08-31 05:15:30 UTC
  • mto: This revision was merged to the branch mainline in revision 9271.
  • Revision ID: jml@canonical.com-20090831051530-pex7jge5d54ixay2
Add lp-dev-tools that lack license confusion, changing the license to match
the rest of the Launchpad source tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
13
owners = {
 
14
    'gary': 255383312499,
 
15
    'francis': 559320013529}
 
16
 
 
17
_image_match = re.compile(
 
18
    r'launchpad-ec2test(\d+)/image.manifest.xml$').match
 
19
 
 
20
if __name__ == '__main__':
 
21
    if len(sys.argv) == 1:
 
22
        owner = os.environ['USER']
 
23
    elif len(sys.argv) == 2:
 
24
        owner = sys.argv[1]
 
25
    else:
 
26
        raise RuntimeError('Too many arguments')
 
27
    try:
 
28
        owner = int(owner)
 
29
    except ValueError:
 
30
        owner = owners[owner]
 
31
    # Get the AWS identifier and secret identifier.
 
32
    aws_id = os.path.join(os.environ['HOME'], '.ec2', 'aws_id')
 
33
    if not os.path.exists(aws_id):
 
34
        raise RuntimeError(
 
35
            "Please put your aws access key identifier and secret access "
 
36
            "key identifier in %s. (On two lines).\n" % (aws_id,))
 
37
    aws_file = open(aws_id, 'r')
 
38
    try:
 
39
        identifier = aws_file.readline().strip()
 
40
        secret = aws_file.readline().strip()
 
41
    finally:
 
42
        aws_file.close()
 
43
    # Make the EC2 connection.
 
44
    conn = boto.connect_ec2(identifier, secret)
 
45
    # get images
 
46
    images = {}
 
47
    for image in conn.get_all_images(owners=owners.values()):
 
48
        match = _image_match(image.location)
 
49
        if match:
 
50
            val = int(match.group(1))
 
51
            if val not in images:
 
52
                images[val] = [image]
 
53
            else:
 
54
                images[val].append(image)
 
55
    images = images.items()
 
56
    images.sort()
 
57
    # set
 
58
    last, recent = images[-2:]
 
59
    for tmp in (last, recent):
 
60
        if len(tmp[1]) > 1:
 
61
            raise ValueError(
 
62
                'more than one image of value %d found: %r' % tmp)
 
63
    recent = recent[1][0]
 
64
    last = last[1][0]
 
65
    perms = last.get_launch_permissions()
 
66
    perms['user_ids'].extend(owners.values())
 
67
    recent.set_launch_permissions(**perms)
 
68
    # done
 
69
    pprint.pprint(recent.get_launch_permissions())