1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
23
Represents an EC2 Elastic Block Storage Volume
25
from boto.ec2.ec2object import EC2Object
27
class Volume(EC2Object):
29
def __init__(self, connection=None):
30
EC2Object.__init__(self, connection)
32
self.create_time = None
35
self.snapshot_id = None
36
self.attach_data = None
40
return 'Volume:%s' % self.id
42
def startElement(self, name, attrs, connection):
43
if name == 'attachmentSet':
44
self.attach_data = AttachmentSet()
45
return self.attach_data
49
def endElement(self, name, value, connection):
50
if name == 'volumeId':
52
elif name == 'createTime':
53
self.create_time = value
54
elif name == 'status':
58
self.size = int(value)
59
elif name == 'snapshotId':
60
self.snapshot_id = value
61
elif name == 'availabilityZone':
64
setattr(self, name, value)
66
def _update(self, updated):
67
self.__dict__.update(updated.__dict__)
70
rs = self.connection.get_all_volumes([self.id])
77
Delete this EBS volume.
80
:return: True if successful
82
return self.connection.delete_volume(self.id)
84
def attach(self, instance_id, device):
86
Attach this EBS volume to an EC2 instance.
88
:type instance_id: str
89
:param instance_id: The ID of the EC2 instance to which it will
93
:param device: The device on the instance through which the
94
volume will be exposted (e.g. /dev/sdh)
97
:return: True if successful
99
return self.connection.attach_volume(self.id, instance_id, device)
101
def detach(self, force=False):
103
Detach this EBS volume from an EC2 instance.
106
:param force: Forces detachment if the previous detachment attempt did
107
not occur cleanly. This option can lead to data loss or
108
a corrupted file system. Use this option only as a last
109
resort to detach a volume from a failed instance. The
110
instance will not have an opportunity to flush file system
111
caches nor file system meta data. If you use this option,
112
you must perform file system check and repair procedures.
115
:return: True if successful
119
instance_id = self.attach_data.instance_id
122
device = self.attach_data.device
123
return self.connection.detach_volume(self.id, instance_id, device, force)
125
def create_snapshot(self, description=None):
127
Create a snapshot of this EBS Volume.
129
:type description: str
130
:param description: A description of the snapshot. Limited to 256 characters.
133
:return: True if successful
135
return self.connection.create_snapshot(self.id, description)
137
def volume_state(self):
139
Returns the state of the volume. Same value as the status attribute.
143
def attachment_state(self):
145
Get the attachment state.
149
state = self.attach_data.status
152
def snapshots(self, owner=None, restorable_by=None):
154
Get all snapshots related to this volume. Note that this requires
155
that all available snapshots for the account be retrieved from EC2
156
first and then the list is filtered client-side to contain only
157
those for this volume.
160
:param owner: If present, only the snapshots owned by the specified user
161
will be returned. Valid values are:
162
self | amazon | AWS Account ID
164
:type restorable_by: str
165
:param restorable_by: If present, only the snapshots that are restorable
166
by the specified account id will be returned.
168
:rtype: list of L{boto.ec2.snapshot.Snapshot}
169
:return: The requested Snapshot objects
172
rs = self.connection.get_all_snapshots(owner=owner,
173
restorable_by=restorable_by)
176
if snap.volume_id == self.id:
180
class AttachmentSet(object):
184
self.instance_id = None
186
self.attach_time = None
190
return 'AttachmentSet:%s' % self.id
192
def startElement(self, name, attrs, connection):
195
def endElement(self, name, value, connection):
196
if name == 'volumeId':
198
elif name == 'instanceId':
199
self.instance_id = value
200
elif name == 'status':
202
elif name == 'attachTime':
203
self.attach_time = value
204
elif name == 'device':
207
setattr(self, name, value)