78
79
return {'private_key': private_key, 'fingerprint': fingerprint}
82
# EC2 API can return the following values as documented in the EC2 API
83
# http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/
84
# ApiReference-ItemType-InstanceStateType.html
85
# pending | running | shutting-down | terminated | stopping | stopped
86
_STATE_DESCRIPTION_MAP = {
88
vm_states.ACTIVE: 'running',
89
vm_states.BUILDING: 'pending',
90
vm_states.REBUILDING: 'pending',
91
vm_states.DELETED: 'terminated',
92
vm_states.STOPPED: 'stopped',
93
vm_states.MIGRATING: 'migrate',
94
vm_states.RESIZING: 'resize',
95
vm_states.PAUSED: 'pause',
96
vm_states.SUSPENDED: 'suspend',
97
vm_states.RESCUED: 'rescue',
101
def state_description_from_vm_state(vm_state):
102
"""Map the vm state to the server status string"""
103
return _STATE_DESCRIPTION_MAP.get(vm_state, vm_state)
81
106
# TODO(yamahata): hypervisor dependent default device name
82
107
_DEFAULT_ROOT_DEVICE_NAME = '/dev/sda1'
83
108
_DEFAULT_MAPPINGS = {'ami': 'sda1',
1040
1065
def _format_attr_instance_initiated_shutdown_behavior(instance,
1042
state_description = instance['state_description']
1043
state_to_value = {'stopping': 'stop',
1045
'terminating': 'terminate'}
1046
value = state_to_value.get(state_description)
1067
vm_state = instance['vm_state']
1069
vm_states.STOPPED: 'stopped',
1070
vm_states.DELETED: 'terminated',
1072
value = state_to_value.get(vm_state)
1048
1074
result['instanceInitiatedShutdownBehavior'] = value
1198
1224
self._format_kernel_id(instance, i, 'kernelId')
1199
1225
self._format_ramdisk_id(instance, i, 'ramdiskId')
1200
1226
i['instanceState'] = {
1201
'code': instance['state'],
1202
'name': instance['state_description']}
1227
'code': instance['power_state'],
1228
'name': state_description_from_vm_state(instance['vm_state'])}
1203
1229
fixed_addr = None
1204
1230
floating_addr = None
1205
1231
if instance['fixed_ips']:
1618
1644
# stop the instance if necessary
1619
1645
restart_instance = False
1620
1646
if not no_reboot:
1621
state_description = instance['state_description']
1647
vm_state = instance['vm_state']
1623
1649
# if the instance is in subtle state, refuse to proceed.
1624
if state_description not in ('running', 'stopping', 'stopped'):
1650
if vm_state not in (vm_states.ACTIVE, vm_states.STOPPED):
1625
1651
raise exception.InstanceNotRunning(instance_id=ec2_instance_id)
1627
if state_description == 'running':
1653
if vm_state == vm_states.ACTIVE:
1628
1654
restart_instance = True
1629
1655
self.compute_api.stop(context, instance_id=instance_id)
1631
1657
# wait instance for really stopped
1632
1658
start_time = time.time()
1633
while state_description != 'stopped':
1659
while vm_state != vm_states.STOPPED:
1635
1661
instance = self.compute_api.get(context, instance_id)
1636
state_description = instance['state_description']
1662
vm_state = instance['vm_state']
1637
1663
# NOTE(yamahata): timeout and error. 1 hour for now for safety.
1638
1664
# Is it too short/long?
1639
1665
# Or is there any better way?