~hudson-openstack/nova/trunk

« back to all changes in this revision

Viewing changes to nova/api/ec2/cloud.py

  • Committer: Tarmac
  • Author(s): Brian Lamar, Dan Prince
  • Date: 2011-08-31 22:55:34 UTC
  • mfrom: (1443.3.61 instance_states)
  • Revision ID: tarmac-20110831225534-upfhtsvcsafyql6x
Fixed and improved the way instance "states" are set. Instead of relying on solely the power_state of a VM, there are now explicitly defined VM states and VM task states which respectively define the current state of the VM and the task which is currently being performed by the VM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
from nova import volume
48
48
from nova.api.ec2 import ec2utils
49
49
from nova.compute import instance_types
 
50
from nova.compute import vm_states
50
51
from nova.image import s3
51
52
 
52
53
 
78
79
    return {'private_key': private_key, 'fingerprint': fingerprint}
79
80
 
80
81
 
 
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 = {
 
87
    None: 'pending',
 
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',
 
98
}
 
99
 
 
100
 
 
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)
 
104
 
 
105
 
81
106
# TODO(yamahata): hypervisor dependent default device name
82
107
_DEFAULT_ROOT_DEVICE_NAME = '/dev/sda1'
83
108
_DEFAULT_MAPPINGS = {'ami': 'sda1',
1039
1064
 
1040
1065
        def _format_attr_instance_initiated_shutdown_behavior(instance,
1041
1066
                                                               result):
1042
 
            state_description = instance['state_description']
1043
 
            state_to_value = {'stopping': 'stop',
1044
 
                              'stopped': 'stop',
1045
 
                              'terminating': 'terminate'}
1046
 
            value = state_to_value.get(state_description)
 
1067
            vm_state = instance['vm_state']
 
1068
            state_to_value = {
 
1069
                vm_states.STOPPED: 'stopped',
 
1070
                vm_states.DELETED: 'terminated',
 
1071
            }
 
1072
            value = state_to_value.get(vm_state)
1047
1073
            if value:
1048
1074
                result['instanceInitiatedShutdownBehavior'] = value
1049
1075
 
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']
1622
1648
 
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)
1626
1652
 
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)
1630
1656
 
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:
1634
1660
                time.sleep(1)
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?