summaryrefslogtreecommitdiff
path: root/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/030_require_cell_setup.py
blob: e59b12433abf18b1843fd2981bdffed4509edb65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from oslo_log import log as logging
from sqlalchemy import MetaData, Table, func, select

from nova import exception
from nova.i18n import _, _LW
from nova import objects

LOG = logging.getLogger(__name__)


def upgrade(migrate_engine):
    meta = MetaData()
    meta.bind = migrate_engine

    flavors = Table('flavors', meta, autoload=True)
    count = select([func.count()]).select_from(flavors).scalar()
    if count == 0:
        # NOTE(danms): We need to be careful here if this is a new
        # installation, which can't possibly have any mappings. Check
        # to see if any flavors are defined to determine if we are
        # upgrading an existing system. If not, then don't obsess over
        # the lack of mappings
        return

    cell_mappings = Table('cell_mappings', meta, autoload=True)
    count = select([func.count()]).select_from(cell_mappings).scalar()
    # Two mappings are required at a minimum, cell0 and your first cell
    if count < 2:
        msg = _('Cell mappings are not created, but required for Ocata. '
                'Please run nova-manage cell_v2 simple_cell_setup before '
                'continuing.')
        raise exception.ValidationError(detail=msg)

    count = select([func.count()]).select_from(cell_mappings).where(
        cell_mappings.c.uuid == objects.CellMapping.CELL0_UUID).scalar()
    if count != 1:
        msg = _('A mapping for Cell0 was not found, but is required for '
                'Ocata. Please run nova-manage cell_v2 simple_cell_setup '
                'before continuing.')
        raise exception.ValidationError(detail=msg)

    host_mappings = Table('host_mappings', meta, autoload=True)
    count = select([func.count()]).select_from(host_mappings).scalar()
    if count == 0:
        msg = _LW('No host mappings were found, but are required for Ocata. '
                  'Please run nova-manage cell_v2 simple_cell_setup before '
                  'continuing.')
        LOG.warning(msg)