1
# Copyright (c) 2006-2008 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 Instance
26
from boto.ec2.ec2object import EC2Object
27
from boto.resultset import ResultSet
28
from boto.ec2.address import Address
29
from boto.ec2.blockdevicemapping import BlockDeviceMapping
30
from boto.ec2.image import ProductCodes
33
class Reservation(EC2Object):
35
def __init__(self, connection=None):
36
EC2Object.__init__(self, connection)
43
return 'Reservation:%s' % self.id
45
def startElement(self, name, attrs, connection):
46
if name == 'instancesSet':
47
self.instances = ResultSet([('item', Instance)])
49
elif name == 'groupSet':
50
self.groups = ResultSet([('item', Group)])
55
def endElement(self, name, value, connection):
56
if name == 'reservationId':
58
elif name == 'ownerId':
61
setattr(self, name, value)
64
for instance in self.instances:
67
class Instance(EC2Object):
69
def __init__(self, connection=None):
70
EC2Object.__init__(self, connection)
73
self.public_dns_name = None
74
self.private_dns_name = None
76
self.state_code = None
78
self.shutdown_state = None
79
self.previous_state = None
80
self.instance_type = None
81
self.instance_class = None
82
self.launch_time = None
87
self.product_codes = ProductCodes()
88
self.ami_launch_index = None
89
self.monitored = False
90
self.instance_class = None
91
self.spot_instance_request_id = None
94
self.private_ip_address = None
95
self.ip_address = None
96
self.requester_id = None
97
self._in_monitoring_element = False
98
self.persistent = False
99
self.root_device_name = None
100
self.root_device_type = None
101
self.block_device_mapping = None
102
self.state_reason = None
105
return 'Instance:%s' % self.id
107
def startElement(self, name, attrs, connection):
108
if name == 'monitoring':
109
self._in_monitoring_element = True
110
elif name == 'blockDeviceMapping':
111
self.block_device_mapping = BlockDeviceMapping()
112
return self.block_device_mapping
113
elif name == 'productCodes':
114
return self.product_codes
115
elif name == 'stateReason':
116
self.state_reason = StateReason()
117
return self.state_reason
120
def endElement(self, name, value, connection):
121
if name == 'instanceId':
123
elif name == 'imageId':
124
self.image_id = value
125
elif name == 'dnsName' or name == 'publicDnsName':
126
self.dns_name = value # backwards compatibility
127
self.public_dns_name = value
128
elif name == 'privateDnsName':
129
self.private_dns_name = value
130
elif name == 'keyName':
131
self.key_name = value
132
elif name == 'amiLaunchIndex':
133
self.ami_launch_index = value
134
elif name == 'shutdownState':
135
self.shutdown_state = value
136
elif name == 'previousState':
137
self.previous_state = value
142
self.state_code = int(value)
144
boto.log.warning('Error converting code (%s) to int' % value)
145
self.state_code = value
146
elif name == 'instanceType':
147
self.instance_type = value
148
elif name == 'instanceClass':
149
self.instance_class = value
150
elif name == 'rootDeviceName':
151
self.root_device_name = value
152
elif name == 'rootDeviceType':
153
self.root_device_type = value
154
elif name == 'launchTime':
155
self.launch_time = value
156
elif name == 'availabilityZone':
157
self.placement = value
158
elif name == 'placement':
160
elif name == 'kernelId':
162
elif name == 'ramdiskId':
164
elif name == 'state':
165
if self._in_monitoring_element:
166
if value == 'enabled':
167
self.monitored = True
168
self._in_monitoring_element = False
169
elif name == 'instanceClass':
170
self.instance_class = value
171
elif name == 'spotInstanceRequestId':
172
self.spot_instance_request_id = value
173
elif name == 'subnetId':
174
self.subnet_id = value
175
elif name == 'vpcId':
177
elif name == 'privateIpAddress':
178
self.private_ip_address = value
179
elif name == 'ipAddress':
180
self.ip_address = value
181
elif name == 'requesterId':
182
self.requester_id = value
183
elif name == 'persistent':
185
self.persistent = True
187
self.persistent = False
189
setattr(self, name, value)
191
def _update(self, updated):
192
self.__dict__.update(updated.__dict__)
195
rs = self.connection.get_all_instances([self.id])
198
for i in r.instances:
204
rs = self.connection.terminate_instances([self.id])
208
rs = self.connection.stop_instances([self.id])
212
rs = self.connection.start_instances([self.id])
216
return self.connection.reboot_instances([self.id])
218
def get_console_output(self):
219
return self.connection.get_console_output(self.id)
221
def confirm_product(self, product_code):
222
return self.connection.confirm_product_instance(self.id, product_code)
224
def use_ip(self, ip_address):
225
if isinstance(ip_address, Address):
226
ip_address = ip_address.public_ip
227
return self.connection.associate_address(self.id, ip_address)
230
return self.connection.monitor_instance(self.id)
233
return self.connection.unmonitor_instance(self.id)
237
def __init__(self, parent=None):
240
def startElement(self, name, attrs, connection):
243
def endElement(self, name, value, connection):
244
if name == 'groupId':
247
setattr(self, name, value)
251
def __init__(self, parent=None):
253
self.instance_id = None
254
self.timestamp = None
257
def startElement(self, name, attrs, connection):
260
def endElement(self, name, value, connection):
261
if name == 'instanceId':
262
self.instance_id = value
263
elif name == 'output':
264
self.output = base64.b64decode(value)
266
setattr(self, name, value)
268
class InstanceAttribute(dict):
270
def __init__(self, parent=None):
272
self._current_value = None
274
def startElement(self, name, attrs, connection):
277
def endElement(self, name, value, connection):
279
self._current_value = value
281
self[name] = self._current_value
283
class StateReason(dict):
285
def __init__(self, parent=None):
288
def startElement(self, name, attrs, connection):
291
def endElement(self, name, value, connection):
292
if name != 'stateReason':