| OLD | NEW |
| (Empty) | |
| 1 # Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2010, Eucalyptus Systems, Inc. |
| 3 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 # IN THE SOFTWARE. |
| 23 |
| 24 """ |
| 25 Represents an EC2 Elastic Block Storage Volume |
| 26 """ |
| 27 from boto.resultset import ResultSet |
| 28 from boto.ec2.tag import Tag |
| 29 from boto.ec2.ec2object import TaggedEC2Object |
| 30 |
| 31 |
| 32 class Volume(TaggedEC2Object): |
| 33 """ |
| 34 Represents an EBS volume. |
| 35 |
| 36 :ivar id: The unique ID of the volume. |
| 37 :ivar create_time: The timestamp of when the volume was created. |
| 38 :ivar status: The status of the volume. |
| 39 :ivar size: The size (in GB) of the volume. |
| 40 :ivar snapshot_id: The ID of the snapshot this volume was created |
| 41 from, if applicable. |
| 42 :ivar attach_data: An AttachmentSet object. |
| 43 :ivar zone: The availability zone this volume is in. |
| 44 :ivar type: The type of volume (standard or consistent-iops) |
| 45 :ivar iops: If this volume is of type consistent-iops, this is |
| 46 the number of IOPS provisioned (10-300). |
| 47 """ |
| 48 |
| 49 def __init__(self, connection=None): |
| 50 TaggedEC2Object.__init__(self, connection) |
| 51 self.id = None |
| 52 self.create_time = None |
| 53 self.status = None |
| 54 self.size = None |
| 55 self.snapshot_id = None |
| 56 self.attach_data = None |
| 57 self.zone = None |
| 58 self.type = None |
| 59 self.iops = None |
| 60 |
| 61 def __repr__(self): |
| 62 return 'Volume:%s' % self.id |
| 63 |
| 64 def startElement(self, name, attrs, connection): |
| 65 retval = TaggedEC2Object.startElement(self, name, attrs, connection) |
| 66 if retval is not None: |
| 67 return retval |
| 68 if name == 'attachmentSet': |
| 69 self.attach_data = AttachmentSet() |
| 70 return self.attach_data |
| 71 elif name == 'tagSet': |
| 72 self.tags = ResultSet([('item', Tag)]) |
| 73 return self.tags |
| 74 else: |
| 75 return None |
| 76 |
| 77 def endElement(self, name, value, connection): |
| 78 if name == 'volumeId': |
| 79 self.id = value |
| 80 elif name == 'createTime': |
| 81 self.create_time = value |
| 82 elif name == 'status': |
| 83 if value != '': |
| 84 self.status = value |
| 85 elif name == 'size': |
| 86 self.size = int(value) |
| 87 elif name == 'snapshotId': |
| 88 self.snapshot_id = value |
| 89 elif name == 'availabilityZone': |
| 90 self.zone = value |
| 91 elif name == 'volumeType': |
| 92 self.type = value |
| 93 elif name == 'iops': |
| 94 self.iops = int(value) |
| 95 else: |
| 96 setattr(self, name, value) |
| 97 |
| 98 def _update(self, updated): |
| 99 self.__dict__.update(updated.__dict__) |
| 100 |
| 101 def update(self, validate=False): |
| 102 """ |
| 103 Update the data associated with this volume by querying EC2. |
| 104 |
| 105 :type validate: bool |
| 106 :param validate: By default, if EC2 returns no data about the |
| 107 volume the update method returns quietly. If |
| 108 the validate param is True, however, it will |
| 109 raise a ValueError exception if no data is |
| 110 returned from EC2. |
| 111 """ |
| 112 # Check the resultset since Eucalyptus ignores the volumeId param |
| 113 unfiltered_rs = self.connection.get_all_volumes([self.id]) |
| 114 rs = [x for x in unfiltered_rs if x.id == self.id] |
| 115 if len(rs) > 0: |
| 116 self._update(rs[0]) |
| 117 elif validate: |
| 118 raise ValueError('%s is not a valid Volume ID' % self.id) |
| 119 return self.status |
| 120 |
| 121 def delete(self): |
| 122 """ |
| 123 Delete this EBS volume. |
| 124 |
| 125 :rtype: bool |
| 126 :return: True if successful |
| 127 """ |
| 128 return self.connection.delete_volume(self.id) |
| 129 |
| 130 def attach(self, instance_id, device): |
| 131 """ |
| 132 Attach this EBS volume to an EC2 instance. |
| 133 |
| 134 :type instance_id: str |
| 135 :param instance_id: The ID of the EC2 instance to which it will |
| 136 be attached. |
| 137 |
| 138 :type device: str |
| 139 :param device: The device on the instance through which the |
| 140 volume will be exposed (e.g. /dev/sdh) |
| 141 |
| 142 :rtype: bool |
| 143 :return: True if successful |
| 144 """ |
| 145 return self.connection.attach_volume(self.id, instance_id, device) |
| 146 |
| 147 def detach(self, force=False): |
| 148 """ |
| 149 Detach this EBS volume from an EC2 instance. |
| 150 |
| 151 :type force: bool |
| 152 :param force: Forces detachment if the previous detachment |
| 153 attempt did not occur cleanly. This option can lead to |
| 154 data loss or a corrupted file system. Use this option only |
| 155 as a last resort to detach a volume from a failed |
| 156 instance. The instance will not have an opportunity to |
| 157 flush file system caches nor file system meta data. If you |
| 158 use this option, you must perform file system check and |
| 159 repair procedures. |
| 160 |
| 161 :rtype: bool |
| 162 :return: True if successful |
| 163 """ |
| 164 instance_id = None |
| 165 if self.attach_data: |
| 166 instance_id = self.attach_data.instance_id |
| 167 device = None |
| 168 if self.attach_data: |
| 169 device = self.attach_data.device |
| 170 return self.connection.detach_volume(self.id, instance_id, |
| 171 device, force) |
| 172 |
| 173 def create_snapshot(self, description=None): |
| 174 """ |
| 175 Create a snapshot of this EBS Volume. |
| 176 |
| 177 :type description: str |
| 178 :param description: A description of the snapshot. |
| 179 Limited to 256 characters. |
| 180 |
| 181 :rtype: :class:`boto.ec2.snapshot.Snapshot` |
| 182 :return: The created Snapshot object |
| 183 """ |
| 184 return self.connection.create_snapshot(self.id, description) |
| 185 |
| 186 def volume_state(self): |
| 187 """ |
| 188 Returns the state of the volume. Same value as the status attribute. |
| 189 """ |
| 190 return self.status |
| 191 |
| 192 def attachment_state(self): |
| 193 """ |
| 194 Get the attachment state. |
| 195 """ |
| 196 state = None |
| 197 if self.attach_data: |
| 198 state = self.attach_data.status |
| 199 return state |
| 200 |
| 201 def snapshots(self, owner=None, restorable_by=None): |
| 202 """ |
| 203 Get all snapshots related to this volume. Note that this requires |
| 204 that all available snapshots for the account be retrieved from EC2 |
| 205 first and then the list is filtered client-side to contain only |
| 206 those for this volume. |
| 207 |
| 208 :type owner: str |
| 209 :param owner: If present, only the snapshots owned by the |
| 210 specified user will be returned. Valid values are: |
| 211 |
| 212 * self |
| 213 * amazon |
| 214 * AWS Account ID |
| 215 |
| 216 :type restorable_by: str |
| 217 :param restorable_by: If present, only the snapshots that |
| 218 are restorable by the specified account id will be returned. |
| 219 |
| 220 :rtype: list of L{boto.ec2.snapshot.Snapshot} |
| 221 :return: The requested Snapshot objects |
| 222 |
| 223 """ |
| 224 rs = self.connection.get_all_snapshots(owner=owner, |
| 225 restorable_by=restorable_by) |
| 226 mine = [] |
| 227 for snap in rs: |
| 228 if snap.volume_id == self.id: |
| 229 mine.append(snap) |
| 230 return mine |
| 231 |
| 232 |
| 233 class AttachmentSet(object): |
| 234 """ |
| 235 Represents an EBS attachmentset. |
| 236 |
| 237 :ivar id: The unique ID of the volume. |
| 238 :ivar instance_id: The unique ID of the attached instance |
| 239 :ivar status: The status of the attachment |
| 240 :ivar attach_time: Attached since |
| 241 :ivar device: The device the instance has mapped |
| 242 """ |
| 243 |
| 244 def __init__(self): |
| 245 self.id = None |
| 246 self.instance_id = None |
| 247 self.status = None |
| 248 self.attach_time = None |
| 249 self.device = None |
| 250 |
| 251 def __repr__(self): |
| 252 return 'AttachmentSet:%s' % self.id |
| 253 |
| 254 def startElement(self, name, attrs, connection): |
| 255 pass |
| 256 |
| 257 def endElement(self, name, value, connection): |
| 258 if name == 'volumeId': |
| 259 self.id = value |
| 260 elif name == 'instanceId': |
| 261 self.instance_id = value |
| 262 elif name == 'status': |
| 263 self.status = value |
| 264 elif name == 'attachTime': |
| 265 self.attach_time = value |
| 266 elif name == 'device': |
| 267 self.device = value |
| 268 else: |
| 269 setattr(self, name, value) |
| 270 |
| 271 |
| 272 class VolumeAttribute: |
| 273 |
| 274 def __init__(self, parent=None): |
| 275 self.id = None |
| 276 self._key_name = None |
| 277 self.attrs = {} |
| 278 |
| 279 def startElement(self, name, attrs, connection): |
| 280 if name == 'autoEnableIO': |
| 281 self._key_name = name |
| 282 return None |
| 283 |
| 284 def endElement(self, name, value, connection): |
| 285 if name == 'value': |
| 286 if value.lower() == 'true': |
| 287 self.attrs[self._key_name] = True |
| 288 else: |
| 289 self.attrs[self._key_name] = False |
| 290 elif name == 'volumeId': |
| 291 self.id = value |
| 292 else: |
| 293 setattr(self, name, value) |
| OLD | NEW |