add instance stop/start

This commit is contained in:
Steve Pulec 2013-02-18 21:56:22 -05:00
parent 4283cca63c
commit 3a9c757e46
4 changed files with 85 additions and 9 deletions

View File

@ -20,6 +20,24 @@ class EC2Backend(BaseBackend):
self.reservations[new_reservation.id] = new_reservation
return new_reservation
def start_instances(self, instance_ids):
started_instances = []
for instance in self.all_instances():
if instance.id in instance_ids:
instance._state = InstanceState(0, 'pending')
started_instances.append(instance)
return started_instances
def stop_instances(self, instance_ids):
stopped_instances = []
for instance in self.all_instances():
if instance.id in instance_ids:
instance._state = InstanceState(64, 'stopping')
stopped_instances.append(instance)
return stopped_instances
def terminate_instances(self, instance_ids):
terminated_instances = []
for instance in self.all_instances():

View File

@ -21,8 +21,18 @@ def instances(uri, body, headers):
instances = ec2_backend.terminate_instances(instance_ids)
template = Template(EC2_TERMINATE_INSTANCES)
return template.render(instances=instances)
elif action == 'StopInstances':
instance_ids = querystring.get('InstanceId.1')[0]
instances = ec2_backend.stop_instances(instance_ids)
template = Template(EC2_STOP_INSTANCES)
return template.render(instances=instances)
elif action == 'StartInstances':
instance_ids = querystring.get('InstanceId.1')[0]
instances = ec2_backend.start_instances(instance_ids)
template = Template(EC2_START_INSTANCES)
return template.render(instances=instances)
else:
raise ValueError("Not implemented", action)
import pdb;pdb.set_trace()
EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
@ -222,3 +232,43 @@ EC2_TERMINATE_INSTANCES = """
{% endfor %}
</instancesSet>
</TerminateInstancesResponse>"""
EC2_STOP_INSTANCES = """
<StopInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<instancesSet>
{% for instance in instances %}
<item>
<instanceId>{{ instance.id }}</instanceId>
<currentState>
<code>32</code>
<name>{{ instance.state }}</name>
</currentState>
<previousState>
<code>16</code>
<name>running</name>
</previousState>
</item>
{% endfor %}
</instancesSet>
</StopInstancesResponse>"""
EC2_START_INSTANCES = """
<StartInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<instancesSet>
{% for instance in instances %}
<item>
<instanceId>{{ instance.id }}</instanceId>
<currentState>
<code>32</code>
<name>{{ instance.state }}</name>
</currentState>
<previousState>
<code>16</code>
<name>running</name>
</previousState>
</item>
{% endfor %}
</instancesSet>
</StartInstancesResponse>"""

View File

@ -15,13 +15,6 @@ def bucket_response(uri, body, headers):
hostname = uri.hostname
method = uri.method
# s3_base_url = "s3.amazonaws.com"
# if hostname == s3_base_url:
# # No bucket specified. Listing all buckets
# all_buckets = s3_backend.get_all_buckets()
# template = Template(S3_ALL_BUCKETS)
# return template.render(buckets=all_buckets)
bucket_name = bucket_name_from_hostname(hostname)
if method == 'GET':

View File

@ -27,3 +27,18 @@ def test_instance_launch_and_terminate():
reservations = conn.get_all_instances()
instance = reservations[0].instances[0]
instance.state.should.equal('shutting-down')
@mock_ec2
def test_instance_start_and_stop():
conn = boto.connect_ec2('the_key', 'the_secret')
reservation = conn.run_instances('<ami-image-id>', '<ami-image-id2>')
instances = reservation.instances
stopped_instances = conn.stop_instances([instance.id for instance in instances])
for instance in stopped_instances:
instance.state.should.equal('stopping')
started_instances = conn.start_instances(instances[0].id)
started_instances[0].state.should.equal('pending')