OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 |
| 22 """ |
| 23 Represents an EC2 Keypair |
| 24 """ |
| 25 |
| 26 import os |
| 27 from boto.ec2.ec2object import EC2Object |
| 28 from boto.exception import BotoClientError |
| 29 |
| 30 class KeyPair(EC2Object): |
| 31 |
| 32 def __init__(self, connection=None): |
| 33 EC2Object.__init__(self, connection) |
| 34 self.name = None |
| 35 self.fingerprint = None |
| 36 self.material = None |
| 37 |
| 38 def __repr__(self): |
| 39 return 'KeyPair:%s' % self.name |
| 40 |
| 41 def endElement(self, name, value, connection): |
| 42 if name == 'keyName': |
| 43 self.name = value |
| 44 elif name == 'keyFingerprint': |
| 45 self.fingerprint = value |
| 46 elif name == 'keyMaterial': |
| 47 self.material = value |
| 48 else: |
| 49 setattr(self, name, value) |
| 50 |
| 51 def delete(self): |
| 52 """ |
| 53 Delete the KeyPair. |
| 54 |
| 55 :rtype: bool |
| 56 :return: True if successful, otherwise False. |
| 57 """ |
| 58 return self.connection.delete_key_pair(self.name) |
| 59 |
| 60 def save(self, directory_path): |
| 61 """ |
| 62 Save the material (the unencrypted PEM encoded RSA private key) |
| 63 of a newly created KeyPair to a local file. |
| 64 |
| 65 :type directory_path: string |
| 66 :param directory_path: The fully qualified path to the directory |
| 67 in which the keypair will be saved. The |
| 68 keypair file will be named using the name |
| 69 of the keypair as the base name and .pem |
| 70 for the file extension. If a file of that |
| 71 name already exists in the directory, an |
| 72 exception will be raised and the old file |
| 73 will not be overwritten. |
| 74 |
| 75 :rtype: bool |
| 76 :return: True if successful. |
| 77 """ |
| 78 if self.material: |
| 79 directory_path = os.path.expanduser(directory_path) |
| 80 file_path = os.path.join(directory_path, '%s.pem' % self.name) |
| 81 if os.path.exists(file_path): |
| 82 raise BotoClientError('%s already exists, it will not be overwri
tten' % file_path) |
| 83 fp = open(file_path, 'wb') |
| 84 fp.write(self.material) |
| 85 fp.close() |
| 86 os.chmod(file_path, 0600) |
| 87 return True |
| 88 else: |
| 89 raise BotoClientError('KeyPair contains no material') |
| 90 |
| 91 def copy_to_region(self, region): |
| 92 """ |
| 93 Create a new key pair of the same new in another region. |
| 94 Note that the new key pair will use a different ssh |
| 95 cert than the this key pair. After doing the copy, |
| 96 you will need to save the material associated with the |
| 97 new key pair (use the save method) to a local file. |
| 98 |
| 99 :type region: :class:`boto.ec2.regioninfo.RegionInfo` |
| 100 :param region: The region to which this security group will be copied. |
| 101 |
| 102 :rtype: :class:`boto.ec2.keypair.KeyPair` |
| 103 :return: The new key pair |
| 104 """ |
| 105 if region.name == self.region: |
| 106 raise BotoClientError('Unable to copy to the same Region') |
| 107 conn_params = self.connection.get_params() |
| 108 rconn = region.connect(**conn_params) |
| 109 kp = rconn.create_key_pair(self.name) |
| 110 return kp |
| 111 |
| 112 |
| 113 |
OLD | NEW |