~cbehrens/nova/lp844155

« back to all changes in this revision

Viewing changes to nova/db/sqlalchemy/migrate_repo/versions/044_update_instance_states.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:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 OpenStack LLC.
 
4
#
 
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
#    not use this file except in compliance with the License. You may obtain
 
7
#    a copy of the License at
 
8
#
 
9
#         http://www.apache.org/licenses/LICENSE-2.0
 
10
#
 
11
#    Unless required by applicable law or agreed to in writing, software
 
12
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
13
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
#    License for the specific language governing permissions and limitations
 
15
#    under the License.
 
16
 
 
17
import sqlalchemy
 
18
from sqlalchemy import MetaData, Table, Column, String
 
19
 
 
20
from nova.compute import task_states
 
21
from nova.compute import vm_states
 
22
 
 
23
 
 
24
meta = MetaData()
 
25
 
 
26
 
 
27
c_task_state = Column('task_state',
 
28
                      String(length=255, convert_unicode=False,
 
29
                             assert_unicode=None, unicode_error=None,
 
30
                             _warn_on_bytestring=False),
 
31
                      nullable=True)
 
32
 
 
33
 
 
34
_upgrade_translations = {
 
35
    "stopping": {
 
36
        "state_description": vm_states.ACTIVE,
 
37
        "task_state": task_states.STOPPING,
 
38
    },
 
39
    "stopped": {
 
40
        "state_description": vm_states.STOPPED,
 
41
        "task_state": None,
 
42
    },
 
43
    "terminated": {
 
44
        "state_description": vm_states.DELETED,
 
45
        "task_state": None,
 
46
    },
 
47
    "terminating": {
 
48
        "state_description": vm_states.ACTIVE,
 
49
        "task_state": task_states.DELETING,
 
50
    },
 
51
    "running": {
 
52
        "state_description": vm_states.ACTIVE,
 
53
        "task_state": None,
 
54
    },
 
55
    "scheduling": {
 
56
        "state_description": vm_states.BUILDING,
 
57
        "task_state": task_states.SCHEDULING,
 
58
    },
 
59
    "migrating": {
 
60
        "state_description": vm_states.MIGRATING,
 
61
        "task_state": None,
 
62
    },
 
63
    "pending": {
 
64
        "state_description": vm_states.BUILDING,
 
65
        "task_state": task_states.SCHEDULING,
 
66
    },
 
67
}
 
68
 
 
69
 
 
70
_downgrade_translations = {
 
71
    vm_states.ACTIVE: {
 
72
        None: "running",
 
73
        task_states.DELETING: "terminating",
 
74
        task_states.STOPPING: "stopping",
 
75
    },
 
76
    vm_states.BUILDING: {
 
77
        None: "pending",
 
78
        task_states.SCHEDULING: "scheduling",
 
79
    },
 
80
    vm_states.STOPPED: {
 
81
        None: "stopped",
 
82
    },
 
83
    vm_states.REBUILDING: {
 
84
        None: "pending",
 
85
    },
 
86
    vm_states.DELETED: {
 
87
        None: "terminated",
 
88
    },
 
89
    vm_states.MIGRATING: {
 
90
        None: "migrating",
 
91
    },
 
92
}
 
93
 
 
94
 
 
95
def upgrade(migrate_engine):
 
96
    meta.bind = migrate_engine
 
97
 
 
98
    instance_table = Table('instances', meta, autoload=True,
 
99
                           autoload_with=migrate_engine)
 
100
 
 
101
    c_state = instance_table.c.state
 
102
    c_state.alter(name='power_state')
 
103
 
 
104
    c_vm_state = instance_table.c.state_description
 
105
    c_vm_state.alter(name='vm_state')
 
106
 
 
107
    instance_table.create_column(c_task_state)
 
108
 
 
109
    for old_state, values in _upgrade_translations.iteritems():
 
110
        instance_table.update().\
 
111
            values(**values).\
 
112
            where(c_vm_state == old_state).\
 
113
            execute()
 
114
 
 
115
 
 
116
def downgrade(migrate_engine):
 
117
    meta.bind = migrate_engine
 
118
 
 
119
    instance_table = Table('instances', meta, autoload=True,
 
120
                           autoload_with=migrate_engine)
 
121
 
 
122
    c_task_state = instance_table.c.task_state
 
123
 
 
124
    c_state = instance_table.c.power_state
 
125
    c_state.alter(name='state')
 
126
 
 
127
    c_vm_state = instance_table.c.vm_state
 
128
    c_vm_state.alter(name='state_description')
 
129
 
 
130
    for old_vm_state, old_task_states in _downgrade_translations.iteritems():
 
131
        for old_task_state, new_state_desc in old_task_states.iteritems():
 
132
            instance_table.update().\
 
133
                where(c_task_state == old_task_state).\
 
134
                where(c_vm_state == old_vm_state).\
 
135
                values(vm_state=new_state_desc).\
 
136
                execute()
 
137
 
 
138
    instance_table.drop_column('task_state')