diff --git a/ironic/db/sqlalchemy/alembic/versions/c14cef6dfedf_populate_node_network_interface.py b/ironic/db/sqlalchemy/alembic/versions/c14cef6dfedf_populate_node_network_interface.py new file mode 100644 index 0000000000..ac49571f79 --- /dev/null +++ b/ironic/db/sqlalchemy/alembic/versions/c14cef6dfedf_populate_node_network_interface.py @@ -0,0 +1,44 @@ +# 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. + +"""Populate node.network_interface + +Revision ID: c14cef6dfedf +Revises: dd34e1f1303b +Create Date: 2016-08-01 14:05:24.197314 + +""" + +# revision identifiers, used by Alembic. +revision = 'c14cef6dfedf' +down_revision = 'dd34e1f1303b' + +from alembic import op +from sqlalchemy import String +from sqlalchemy.sql import table, column, null + +from ironic.conf import CONF + + +node = table('nodes', + column('uuid', String(36)), + column('network_interface', String(255))) + + +def upgrade(): + network_iface = (CONF.default_network_interface or + ('flat' if CONF.dhcp.dhcp_provider == 'neutron' + else 'noop')) + op.execute( + node.update().where( + node.c.network_interface == null()).values( + {'network_interface': network_iface})) diff --git a/ironic/tests/unit/db/sqlalchemy/test_migrations.py b/ironic/tests/unit/db/sqlalchemy/test_migrations.py index 8b1acf158f..b456ddc382 100644 --- a/ironic/tests/unit/db/sqlalchemy/test_migrations.py +++ b/ironic/tests/unit/db/sqlalchemy/test_migrations.py @@ -34,6 +34,7 @@ For postgres on Ubuntu this can be done with the following commands: """ +import collections import contextlib from alembic import script @@ -440,6 +441,37 @@ class MigrationCheckersMixin(object): self.assertIsInstance(nodes.c.resource_class.type, sqlalchemy.types.String) + def _pre_upgrade_c14cef6dfedf(self, engine): + # add some nodes. + nodes = db_utils.get_table(engine, 'nodes') + data = [{'uuid': uuidutils.generate_uuid(), + 'network_interface': None}, + {'uuid': uuidutils.generate_uuid(), + 'network_interface': None}, + {'uuid': uuidutils.generate_uuid(), + 'network_interface': 'neutron'}] + nodes.insert().values(data).execute() + return data + + def _check_c14cef6dfedf(self, engine, data): + nodes = db_utils.get_table(engine, 'nodes') + result = engine.execute(nodes.select()) + counts = collections.defaultdict(int) + + def _was_inserted(uuid): + for row in data: + if row['uuid'] == uuid: + return True + + for row in result: + if _was_inserted(row['uuid']): + counts[row['network_interface']] += 1 + + # using default config values, we should have 2 flat and one neutron + self.assertEqual(2, counts['flat']) + self.assertEqual(1, counts['neutron']) + self.assertEqual(0, counts[None]) + def test_upgrade_and_version(self): with patch_with_engine(self.engine): self.migration_api.upgrade('head') diff --git a/releasenotes/notes/add-network-interfaces-0a13c4aba252573e.yaml b/releasenotes/notes/add-network-interfaces-0a13c4aba252573e.yaml index d77c5ade47..7522fa1c19 100644 --- a/releasenotes/notes/add-network-interfaces-0a13c4aba252573e.yaml +++ b/releasenotes/notes/add-network-interfaces-0a13c4aba252573e.yaml @@ -27,3 +27,13 @@ upgrade: set. If it is not set, the network interface is determined by looking at the ``[dhcp]dhcp_provider`` value. If it is ``neutron`` - ``flat`` network interface is the default, ``noop`` otherwise. + The network interface will be set for all nodes without network_interface + already set via a database migration. This will be set following the logic + above. When running database migrations for an existing deployment, it's + important to check the above configuration options to ensure the existing + nodes will have the expected network_interface. If + ``[DEFAULT]default_network_interface`` is not set, everything should go as + expected. If it is set, ensure that it is set to the value that you wish + existing nodes to use. + - Note that if ``[DEFAULT]default_network_interface`` is set, it must be set + in the configuration file for both the API and conductor hosts.