9389.6.6
by Michael Hudson
move main to its own file, add standard header to all new files |
1 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
3 |
||
9389.6.9
by Michael Hudson
docstrings, __all__s and a little whitespace |
4 |
"""Support for reading Amazon Web Service credentials from '~/.ec2/aws_id'."""
|
9389.6.6
by Michael Hudson
move main to its own file, add standard header to all new files |
5 |
|
6 |
__metaclass__ = type |
|
9389.6.9
by Michael Hudson
docstrings, __all__s and a little whitespace |
7 |
__all__ = [ |
8 |
'CredentialsError', |
|
9 |
'EC2Credentials', |
|
10 |
]
|
|
9389.6.6
by Michael Hudson
move main to its own file, add standard header to all new files |
11 |
|
9389.6.5
by Michael Hudson
move EC2Credentials and EC2TestRunner to their own files |
12 |
import os |
13 |
||
14 |
import boto |
|
9453.2.19
by Michael Hudson
make get_credentials unnecessary |
15 |
from bzrlib.errors import BzrCommandError |
9389.6.8
by Michael Hudson
delete ec2 prefix from files in ec2test |
16 |
from devscripts.ec2test.account import EC2Account |
9389.6.5
by Michael Hudson
move EC2Credentials and EC2TestRunner to their own files |
17 |
|
11962.1.2
by Gavin Panella
Format imports. |
18 |
|
9453.2.19
by Michael Hudson
make get_credentials unnecessary |
19 |
class CredentialsError(BzrCommandError): |
9389.6.5
by Michael Hudson
move EC2Credentials and EC2TestRunner to their own files |
20 |
"""Raised when AWS credentials could not be loaded."""
|
21 |
||
22 |
def __init__(self, filename, extra=None): |
|
23 |
message = ( |
|
24 |
"Please put your aws access key identifier and secret access "
|
|
25 |
"key identifier in %s. (On two lines)." % (filename,)) |
|
26 |
if extra: |
|
27 |
message += extra |
|
28 |
Exception.__init__(self, message) |
|
29 |
||
30 |
||
31 |
class EC2Credentials: |
|
32 |
"""Credentials for logging in to EC2."""
|
|
33 |
||
34 |
DEFAULT_CREDENTIALS_FILE = '~/.ec2/aws_id' |
|
35 |
||
36 |
def __init__(self, identifier, secret): |
|
37 |
self.identifier = identifier |
|
38 |
self.secret = secret |
|
39 |
||
40 |
@classmethod
|
|
41 |
def load_from_file(cls, filename=None): |
|
42 |
"""Load the EC2 credentials from 'filename'."""
|
|
43 |
if filename is None: |
|
44 |
filename = os.path.expanduser(cls.DEFAULT_CREDENTIALS_FILE) |
|
45 |
try: |
|
46 |
aws_file = open(filename, 'r') |
|
47 |
except (IOError, OSError), e: |
|
48 |
raise CredentialsError(filename, str(e)) |
|
49 |
try: |
|
50 |
identifier = aws_file.readline().strip() |
|
51 |
secret = aws_file.readline().strip() |
|
52 |
finally: |
|
53 |
aws_file.close() |
|
54 |
return cls(identifier, secret) |
|
55 |
||
56 |
def connect(self, name): |
|
57 |
"""Connect to EC2 with these credentials.
|
|
58 |
||
59 |
:param name: ???
|
|
60 |
:return: An `EC2Account` connected to EC2 with these credentials.
|
|
61 |
"""
|
|
62 |
conn = boto.connect_ec2(self.identifier, self.secret) |
|
9397.2.25
by Michael Hudson
act on comments from jml |
63 |
return EC2Account(name, conn) |
11962.1.1
by Gavin Panella
Create an S3 bucket for the given AMI name in check_bundling_prerequisites(). |
64 |
|
65 |
def connect_s3(self): |
|
66 |
"""Connect to S3 with these credentials.
|
|
67 |
||
68 |
:return: A `boto.s3.connection.S3Connection` with these credentials.
|
|
69 |
"""
|
|
70 |
return boto.connect_s3(self.identifier, self.secret) |