From 093c8200a102891232e2da166830cd59ee133fc4 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Mon, 14 Mar 2011 13:32:22 -0500 Subject: committing to share --- nova/db/api.py | 39 +++++++++++-- nova/db/sqlalchemy/api.py | 131 +++++++++++++++++++++++++++++++++++++++---- nova/db/sqlalchemy/models.py | 76 ++++++++++++++----------- 3 files changed, 198 insertions(+), 48 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index dcaf55e8f..5b92afbcd 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -325,6 +325,37 @@ def fixed_ip_update(context, address, values): #################### +def mac_address_create(context, values): + """create a new mac address record in teh database""" + return IMPL.mac_address_create(context, values) + + +def mac_address_get(context, mac_address): + """gets a mac address from the table""" + return IMPL.mac_address_get(context, mac_address) + + +def mac_address_get_all_by_instance(context, instance_id): + """gets all mac addresses for instance""" + return IMPL.mac_address_get_all_by_instance(context, instance_id) + + +def mac_address_get_all_by_network(context, network_id): + """gets all mac addresses for instance""" + return IMPL.mac_address_get_all_by_network(context, network_id) + + +def mac_address_delete(context, mac_address): + """delete mac address record in teh database""" + return IMPL.mac_address_delete(context, mac_address) + + +def mac_address_delete_by_instance(context, instance_id): + """delete mac address record in teh database""" + return IMPL.mac_address_delete_by_instance(context, instance_id) +#################### + + def instance_create(context, values): """Create an instance from the values dictionary.""" return IMPL.instance_create(context, values) @@ -370,13 +401,13 @@ def instance_get_all_by_reservation(context, reservation_id): return IMPL.instance_get_all_by_reservation(context, reservation_id) -def instance_get_fixed_address(context, instance_id): +def instance_get_fixed_addresses(context, instance_id): """Get the fixed ip address of an instance.""" - return IMPL.instance_get_fixed_address(context, instance_id) + return IMPL.instance_get_fixed_addresses(context, instance_id) -def instance_get_fixed_address_v6(context, instance_id): - return IMPL.instance_get_fixed_address_v6(context, instance_id) +def instance_get_fixed_addresses_v6(context, instance_id): + return IMPL.instance_get_fixed_addresses_v6(context, instance_id) def instance_get_floating_address(context, instance_id): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 6df2a8843..6e59f4b1d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -630,11 +630,16 @@ def fixed_ip_get_all_by_instance(context, instance_id): @require_context def fixed_ip_get_instance_v6(context, address): session = get_session() + + # convert IPv6 address to mac mac = utils.to_mac(address) + # get mac address row + mac_ref = mac_address_get(context, mac) + + # look up instance based on instance_id from mac address row result = session.query(models.Instance).\ - filter_by(mac_address=mac).\ - first() + filter_by(id=mac_ref.instance_id) return result @@ -658,6 +663,98 @@ def fixed_ip_update(context, address, values): ################### +@require_context +def mac_address_create(context, values): + """create a new mac address record in teh database + + context = request context object + values = dict containing column values + """ + mac_address_ref = models.MacAddress() + mac_address_ref.update(values) + mac_address_ref.save() + + session = get_session() + with session.begin(): + instance = instance_get(context, instance_id, session=session) + network = network_get(context, network_id, session=session) + mac_address.instance = instance + mac_address.network = network + mac_address_ref.save(session=session) + return mac_address_ref + + +def mac_address_get(context, mac_address): + """gets a mac address from the table + + context = request context object + mac_address = the mac you're looking to get + """ + session = get_session() + with session.begin(): + mac_address_ref = session.query(models.MacAddress).\ + filter_by(mac_address=mac_address) + return mac_address_ref + + +@require_context +def mac_address_get_all_by_instance(context, instance_id): + """gets all mac addresses for instance + + context = request context object + instance_id = instance to retreive macs for + """ + session = get_session() + with session.begin(): + mac_address_refs = session.query(models.MacAddress).\ + filter_by(instance_id=instance_id) + return mac_address_refs + + +@require_context +def mac_address_get_all_by_network(context, network_id): + """gets all mac addresses for instance + + context = request context object + network_id = network to retreive macs for + """ + session = get_session() + with session.begin(): + mac_address_refs = session.query(models.MacAddress).\ + filter_by(network_id=network_id) + return mac_address_refs + + +@require_context +def mac_address_delete(context, mac_address): + """delete mac address record in teh database + + context = request context object + instance_id = instance to remove macs for + """ + ref = mac_address_get(mac_address) + session = get_session() + with session.begin(): + ref.delete(session=session) + + +@require_context +def mac_address_delete_by_instance(context, instance_id): + """delete mac address record in teh database + + context = request context object + instance_id = instance to remove macs for + """ + refs = mac_address_get_all_by_instance(instance_id) + session = get_session() + with session.begin(): + for ref in refs: + ref.delete(session=session) + + +################### + + @require_context def instance_create(context, values): """Create a new Instance record in the database. @@ -819,24 +916,35 @@ def instance_get_project_vpn(context, project_id): @require_context -def instance_get_fixed_address(context, instance_id): +def instance_get_fixed_addresses(context, instance_id): session = get_session() with session.begin(): instance_ref = instance_get(context, instance_id, session=session) - if not instance_ref.fixed_ip: + if not instance_ref.fixed_ips: return None - return instance_ref.fixed_ip['address'] + return [fixed_ip.address for fixed_ip in instance_ref.fixed_ips] @require_context -def instance_get_fixed_address_v6(context, instance_id): +def instance_get_fixed_addresses_v6(context, instance_id): session = get_session() with session.begin(): + # get instance instance_ref = instance_get(context, instance_id, session=session) - network_ref = network_get_by_instance(context, instance_id) - prefix = network_ref.cidr_v6 - mac = instance_ref.mac_address - return utils.to_global_ipv6(prefix, mac) + # get networks associated with instance + network_refs = network_get_all_by_instance(context, instance_id) + # compile a list of cidr_v6 prefixes sorted by network id + prefixes = [ref.cidr_v6 for ref in + sorted(network_refs, key=lambda ref: ref.id)] + # get mac rows associated with instance + mac_refs = mac_address_get_all_by_instance(context, instance_ref.id) + # compile of list of the mac_addresses sorted by network id + macs = [ref.mac_address for ref in + sorted(mac_refs, key=lambda ref: ref.network_id)] + # combine prefixes and macs into (prefix,mac) pairs + prefix_mac_pairs = zip(prefixes, macs) + # return list containing ipv6 address for each pair + return [utils.to_global_ipv6(pair) for pair in prefix_mac_pairs] @require_context @@ -1081,7 +1189,8 @@ def network_get(context, network_id, session=None): @require_admin_context def network_get_all(context): session = get_session() - result = session.query(models.Network) + result = session.query(models.Network).\ + filter_by(deleted=False) if not result: raise exception.NotFound(_('No networks defined')) return result diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 1882efeba..bbadbeee4 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -174,7 +174,6 @@ class Instance(BASE, NovaBase): user_data = Column(Text) reservation_id = Column(String(255)) - mac_address = Column(String(255)) scheduled_at = Column(DateTime) launched_at = Column(DateTime) @@ -404,21 +403,6 @@ class Network(BASE, NovaBase): host = Column(String(255)) # , ForeignKey('hosts.id')) -class AuthToken(BASE, NovaBase): - """Represents an authorization token for all API transactions. - - Fields are a string representing the actual token and a user id for - mapping to the actual user - - """ - __tablename__ = 'auth_tokens' - token_hash = Column(String(255), primary_key=True) - user_id = Column(String(255)) - server_manageent_url = Column(String(255)) - storage_url = Column(String(255)) - cdn_management_url = Column(String(255)) - - # TODO(vish): can these both come from the same baseclass? class FixedIp(BASE, NovaBase): """Represents a fixed ip for an instance.""" @@ -429,7 +413,7 @@ class FixedIp(BASE, NovaBase): network = relationship(Network, backref=backref('fixed_ips')) instance_id = Column(Integer, ForeignKey('instances.id'), nullable=True) instance = relationship(Instance, - backref=backref('fixed_ip', uselist=False), + backref=backref('fixed_ips'), foreign_keys=instance_id, primaryjoin='and_(' 'FixedIp.instance_id == Instance.id,' @@ -439,6 +423,48 @@ class FixedIp(BASE, NovaBase): reserved = Column(Boolean, default=False) +class FloatingIp(BASE, NovaBase): + """Represents a floating ip that dynamically forwards to a fixed ip.""" + __tablename__ = 'floating_ips' + id = Column(Integer, primary_key=True) + address = Column(String(255)) + fixed_ip_id = Column(Integer, ForeignKey('fixed_ips.id'), nullable=True) + fixed_ip = relationship(FixedIp, + backref=backref('floating_ips'), + foreign_keys=fixed_ip_id, + primaryjoin='and_(' + 'FloatingIp.fixed_ip_id == FixedIp.id,' + 'FloatingIp.deleted == False)') + project_id = Column(String(255)) + host = Column(String(255)) # , ForeignKey('hosts.id')) + + +class MacAddress(BASE, NovaBase): + """Represents a mac address used by an instance""" + __tablename__ = 'mac_addresses' + id = Column(Integer, primary_key=True) + mac_address = Column(String(255), unique=True) + network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) + network = relationship(Network, backref=backref('mac_addresses')) + instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) + instance = relationship(Instance, backref=backref('mac_addresses')) + + +class AuthToken(BASE, NovaBase): + """Represents an authorization token for all API transactions. + + Fields are a string representing the actual token and a user id for + mapping to the actual user + + """ + __tablename__ = 'auth_tokens' + token_hash = Column(String(255), primary_key=True) + user_id = Column(String(255)) + server_manageent_url = Column(String(255)) + storage_url = Column(String(255)) + cdn_management_url = Column(String(255)) + + class User(BASE, NovaBase): """Represents a user.""" __tablename__ = 'users' @@ -499,22 +525,6 @@ class UserProjectAssociation(BASE, NovaBase): project_id = Column(String(255), ForeignKey(Project.id), primary_key=True) -class FloatingIp(BASE, NovaBase): - """Represents a floating ip that dynamically forwards to a fixed ip.""" - __tablename__ = 'floating_ips' - id = Column(Integer, primary_key=True) - address = Column(String(255)) - fixed_ip_id = Column(Integer, ForeignKey('fixed_ips.id'), nullable=True) - fixed_ip = relationship(FixedIp, - backref=backref('floating_ips'), - foreign_keys=fixed_ip_id, - primaryjoin='and_(' - 'FloatingIp.fixed_ip_id == FixedIp.id,' - 'FloatingIp.deleted == False)') - project_id = Column(String(255)) - host = Column(String(255)) # , ForeignKey('hosts.id')) - - class ConsolePool(BASE, NovaBase): """Represents pool of consoles on the same physical node.""" __tablename__ = 'console_pools' -- cgit From 57890776d0d7e9172b1fa056076ce28ae4b34b7b Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Mon, 14 Mar 2011 17:54:39 -0500 Subject: added migration to repo --- .../migrate_repo/versions/010_mac_address_table.py | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/010_mac_address_table.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/010_mac_address_table.py b/nova/db/sqlalchemy/migrate_repo/versions/010_mac_address_table.py new file mode 100644 index 000000000..b8b57b284 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/010_mac_address_table.py @@ -0,0 +1,74 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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 sqlalchemy import * +from migrate import * + +from nova import log as logging + +meta = MetaData() + +# mac address table to add to DB +mac_addresses = Table('mac_addresses', meta, + Column('created_at', DateTime(timezone=False)), + Column('updated_at', DateTime(timezone=False)), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('mac_address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + ) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + c = instances.columns['mac_address'] + + # create mac_addresses table + try: + mac_addresses.create() + except Exception as e: + logging.error(_("Table |%s| not created!"), repr(mac_addresses)) + raise e + + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ['instance_id', 'mac_address', 'network_id'] + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.info("join list |%s|", join_list) + + # insert data into the table + i = mac_addresses.insert() + i.execute(join_list) + + # drop the mac_address column from instances + c.drop -- cgit From 1845c5df145251f1e90709a91cc02ee5ec787e2f Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Mon, 11 Apr 2011 14:16:30 -0500 Subject: network manager changes, compute changes, various other --- nova/db/api.py | 3 +++ nova/db/sqlalchemy/api.py | 28 +++++++++++++--------- .../migrate_repo/versions/014_mac_address_table.py | 9 +++++-- nova/db/sqlalchemy/models.py | 3 ++- 4 files changed, 29 insertions(+), 14 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 036caa585..bc146e8f1 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -313,6 +313,7 @@ def migration_get_by_instance_and_status(context, instance_id, status): return IMPL.migration_get_by_instance_and_status(context, instance_id, status) + #################### @@ -419,6 +420,8 @@ def mac_address_delete(context, mac_address): def mac_address_delete_by_instance(context, instance_id): """delete mac address record in teh database""" return IMPL.mac_address_delete_by_instance(context, instance_id) + + #################### diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e69a5c680..3b9d95752 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -773,17 +773,20 @@ def mac_address_create(context, values): mac_address_ref = models.MacAddress() mac_address_ref.update(values) mac_address_ref.save() - - session = get_session() - with session.begin(): - instance = instance_get(context, instance_id, session=session) - network = network_get(context, network_id, session=session) - mac_address.instance = instance - mac_address.network = network - mac_address_ref.save(session=session) - return mac_address_ref +# instance_id = values['instance_id'] +# network_id = values['network_id'] +# +# session = get_session() +# with session.begin(): +# instance = instance_get(context, instance_id, session=session) +# network = network_get(context, network_id, session=session) +# mac_address.instance = instance +# mac_address.network = network +# mac_address_ref.save(session=session) +# return mac_address_ref +@require_context def mac_address_get(context, mac_address): """gets a mac address from the table @@ -811,7 +814,7 @@ def mac_address_get_all_by_instance(context, instance_id): return mac_address_refs -@require_context +@require_admin_context def mac_address_get_all_by_network(context, network_id): """gets all mac addresses for instance @@ -840,7 +843,8 @@ def mac_address_delete(context, mac_address): @require_context def mac_address_delete_by_instance(context, instance_id): - """delete mac address record in teh database + """delete mac address records in the database that are associated + with the instance given by instance_id context = request context object instance_id = instance to remove macs for @@ -1407,6 +1411,8 @@ def network_get_by_cidr(context, cidr): @require_admin_context def network_get_by_instance(_context, instance_id): + # note this uses fixed IP to get to instance + # only works for networks the instance has an IP from session = get_session() rv = session.query(models.Network).\ filter_by(deleted=False).\ diff --git a/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py b/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py index b8b57b284..0c482bd71 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py @@ -27,7 +27,7 @@ mac_addresses = Table('mac_addresses', meta, Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True, nullable=False), - Column('mac_address', + Column('address', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), unique=True), @@ -62,7 +62,7 @@ def upgrade(migrate_engine): s = select([instances.c.id, instances.c.mac_address, fixed_ips.c.network_id], fixed_ips.c.instance_id == instances.c.id) - keys = ['instance_id', 'mac_address', 'network_id'] + keys = ['instance_id', 'address', 'network_id'] join_list = [dict(zip(keys, row)) for row in s.execute()] logging.info("join list |%s|", join_list) @@ -72,3 +72,8 @@ def upgrade(migrate_engine): # drop the mac_address column from instances c.drop + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 42d8c1512..544070aa9 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -209,6 +209,7 @@ class Instance(BASE, NovaBase): hostname = Column(String(255)) host = Column(String(255)) # , ForeignKey('hosts.id')) + # aka flavor instance_type = Column(String(255)) user_data = Column(Text) @@ -516,7 +517,7 @@ class MacAddress(BASE, NovaBase): """Represents a mac address used by an instance""" __tablename__ = 'mac_addresses' id = Column(Integer, primary_key=True) - mac_address = Column(String(255), unique=True) + address = Column(String(255), unique=True) network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) network = relationship(Network, backref=backref('mac_addresses')) instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) -- cgit From b93abf52587da04f8079be9be1ed0f9a473a9613 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 21 Apr 2011 11:48:47 -0500 Subject: commit to push for testing --- nova/db/api.py | 9 ++- nova/db/sqlalchemy/api.py | 83 ++++++++++++++-------- .../migrate_repo/versions/014_mac_address_table.py | 79 -------------------- .../migrate_repo/versions/015_mac_address_table.py | 80 +++++++++++++++++++++ nova/db/sqlalchemy/models.py | 4 ++ 5 files changed, 144 insertions(+), 111 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index b6e516369..ccb136c7f 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -397,9 +397,14 @@ def mac_address_create(context, values): return IMPL.mac_address_create(context, values) -def mac_address_get(context, mac_address): +def mac_address_get(context, mac_address_id): """gets a mac address from the table""" - return IMPL.mac_address_get(context, mac_address) + return IMPL.mac_address_get(context, mac_address_id) + + +def mac_address_get_by_address(context, address): + """gets a mac address from the table""" + return IMPL.mac_address_get_by_address(context, address) def mac_address_get_all_by_instance(context, instance_id): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 7db8c428a..f1e14c76d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -704,7 +704,7 @@ def fixed_ip_get_by_address(context, address, session=None): options(joinedload('instance')).\ first() if not result: - raise exception.NotFound(_('No floating ip for address %s') % address) + raise exception.NotFound(_('No fixed ip for address %s') % address) if is_user_context(context): authorize_project_context(context, result.instance.project_id) @@ -723,7 +723,8 @@ def fixed_ip_get_all_by_instance(context, instance_id): session = get_session() rv = session.query(models.FixedIp).\ filter_by(instance_id=instance_id).\ - filter_by(deleted=False) + filter_by(deleted=False).\ + all() if not rv: raise exception.NotFound(_('No address for instance %s') % instance_id) return rv @@ -737,7 +738,7 @@ def fixed_ip_get_instance_v6(context, address): mac = utils.to_mac(address) # get mac address row - mac_ref = mac_address_get(context, mac) + mac_ref = mac_address_get_by_address(context, mac) # look up instance based on instance_id from mac address row result = session.query(models.Instance).\ @@ -789,16 +790,30 @@ def mac_address_create(context, values): @require_context -def mac_address_get(context, mac_address): +def mac_address_get(context, mac_address_id): """gets a mac address from the table context = request context object - mac_address = the mac you're looking to get + mac_address_id = id of the mac_address """ session = get_session() with session.begin(): mac_address_ref = session.query(models.MacAddress).\ - filter_by(mac_address=mac_address) + filter_by(id=mac_address_id) + return mac_address_ref + + +@require_context +def mac_address_get_by_address(context, address): + """gets a mac address from the table + + context = request context object + address = the mac you're looking to get + """ + session = get_session() + with session.begin(): + mac_address_ref = session.query(models.MacAddress).\ + filter_by(address=address) return mac_address_ref @@ -812,7 +827,8 @@ def mac_address_get_all_by_instance(context, instance_id): session = get_session() with session.begin(): mac_address_refs = session.query(models.MacAddress).\ - filter_by(instance_id=instance_id) + filter_by(instance_id=instance_id).\ + all() return mac_address_refs @@ -826,18 +842,19 @@ def mac_address_get_all_by_network(context, network_id): session = get_session() with session.begin(): mac_address_refs = session.query(models.MacAddress).\ - filter_by(network_id=network_id) + filter_by(network_id=network_id).\ + all() return mac_address_refs @require_context -def mac_address_delete(context, mac_address): +def mac_address_delete(context, address): """delete mac address record in teh database context = request context object instance_id = instance to remove macs for """ - ref = mac_address_get(mac_address) + ref = mac_address_get_by_address(address) session = get_session() with session.begin(): ref.delete(session=session) @@ -927,10 +944,10 @@ def instance_get(context, instance_id, session=None): if is_admin_context(context): result = session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('security_groups.rules')).\ options(joinedload('volumes')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('metadata')).\ options(joinedload('instance_type')).\ filter_by(id=instance_id).\ @@ -938,7 +955,7 @@ def instance_get(context, instance_id, session=None): first() elif is_user_context(context): result = session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('security_groups.rules')).\ options(joinedload('volumes')).\ options(joinedload('metadata')).\ @@ -959,9 +976,9 @@ def instance_get(context, instance_id, session=None): def instance_get_all(context): session = get_session() return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ filter_by(deleted=can_read_deleted(context)).\ all() @@ -971,9 +988,9 @@ def instance_get_all(context): def instance_get_all_by_user(context, user_id): session = get_session() return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ filter_by(deleted=can_read_deleted(context)).\ filter_by(user_id=user_id).\ @@ -984,9 +1001,9 @@ def instance_get_all_by_user(context, user_id): def instance_get_all_by_host(context, host): session = get_session() return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ filter_by(host=host).\ filter_by(deleted=can_read_deleted(context)).\ @@ -999,9 +1016,9 @@ def instance_get_all_by_project(context, project_id): session = get_session() return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ filter_by(project_id=project_id).\ filter_by(deleted=can_read_deleted(context)).\ @@ -1014,18 +1031,18 @@ def instance_get_all_by_reservation(context, reservation_id): if is_admin_context(context): return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ filter_by(reservation_id=reservation_id).\ filter_by(deleted=can_read_deleted(context)).\ all() elif is_user_context(context): return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ip.network')).\ + options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ filter_by(project_id=context.project_id).\ filter_by(reservation_id=reservation_id).\ @@ -1037,7 +1054,7 @@ def instance_get_all_by_reservation(context, reservation_id): def instance_get_project_vpn(context, project_id): session = get_session() return session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload('security_groups')).\ options(joinedload('instance_type')).\ filter_by(project_id=project_id).\ @@ -1051,9 +1068,11 @@ def instance_get_fixed_addresses(context, instance_id): session = get_session() with session.begin(): instance_ref = instance_get(context, instance_id, session=session) - if not instance_ref.fixed_ips: - return None - return [fixed_ip.address for fixed_ip in instance_ref.fixed_ips] + try: + fixed_ips = fixed_ip_get_all_by_instance(context, instance_id) + except exception.NotFound: + return [] + return [fixed_ip.address for fixed_ip in fixed_ips] @require_context @@ -1062,6 +1081,7 @@ def instance_get_fixed_addresses_v6(context, instance_id): with session.begin(): # get instance instance_ref = instance_get(context, instance_id, session=session) + # assume instance has 1 mac for each network associated with it # get networks associated with instance network_refs = network_get_all_by_instance(context, instance_id) # compile a list of cidr_v6 prefixes sorted by network id @@ -1085,6 +1105,8 @@ def instance_get_floating_address(context, instance_id): instance_ref = instance_get(context, instance_id, session=session) if not instance_ref.fixed_ip: return None + # NOTE(tr3buchet): this only gets the first fixed_ip + # won't find floating ips associated with other fixed_ips if not instance_ref.fixed_ip.floating_ips: return None # NOTE(vish): this just returns the first floating ip @@ -1443,7 +1465,8 @@ def network_get_all_by_instance(_context, instance_id): filter_by(deleted=False).\ join(models.Network.fixed_ips).\ filter_by(instance_id=instance_id).\ - filter_by(deleted=False) + filter_by(deleted=False).\ + all() if not rv: raise exception.NotFound(_('No network for instance %s') % instance_id) return rv diff --git a/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py b/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py deleted file mode 100644 index 0c482bd71..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/014_mac_address_table.py +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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 sqlalchemy import * -from migrate import * - -from nova import log as logging - -meta = MetaData() - -# mac address table to add to DB -mac_addresses = Table('mac_addresses', meta, - Column('created_at', DateTime(timezone=False)), - Column('updated_at', DateTime(timezone=False)), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - ) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - c = instances.columns['mac_address'] - - # create mac_addresses table - try: - mac_addresses.create() - except Exception as e: - logging.error(_("Table |%s| not created!"), repr(mac_addresses)) - raise e - - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ['instance_id', 'address', 'network_id'] - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.info("join list |%s|", join_list) - - # insert data into the table - i = mac_addresses.insert() - i.execute(join_list) - - # drop the mac_address column from instances - c.drop - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py b/nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py new file mode 100644 index 000000000..e1a82bbed --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py @@ -0,0 +1,80 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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 sqlalchemy import * +from migrate import * + +from nova import log as logging + +meta = MetaData() + +# mac address table to add to DB +mac_addresses = Table('mac_addresses', meta, + Column('created_at', DateTime(timezone=False)), + Column('updated_at', DateTime(timezone=False)), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + ) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + c = instances.columns['mac_address'] + + # create mac_addresses table + try: + mac_addresses.create() + except Exception as e: + logging.error(_("Table |%s| not created!"), repr(mac_addresses)) + raise e + + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addressse |%s|"), join_list) + + # insert data into the table + if join_list: + i = mac_addresses.insert() + i.execute(join_list) + + # drop the mac_address column from instances + c.drop + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 742de9502..0a4fa3875 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -184,6 +184,10 @@ class Instance(BASE, NovaBase): def project(self): return auth.manager.AuthManager().get_project(self.project_id) + @property + def fixed_ip(self): + return self.fixed_ips[0] if self.fixed_ips else None + image_id = Column(String(255)) kernel_id = Column(String(255)) ramdisk_id = Column(String(255)) -- cgit From 7b5fab4382a5c02b1cead94fcd828e46c118c914 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 21 Apr 2011 14:12:39 -0500 Subject: eager loaded mac_address attributes for mac address get functions --- nova/db/sqlalchemy/api.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index f1e14c76d..e96123737 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -799,7 +799,10 @@ def mac_address_get(context, mac_address_id): session = get_session() with session.begin(): mac_address_ref = session.query(models.MacAddress).\ - filter_by(id=mac_address_id) + filter_by(id=mac_address_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + first() return mac_address_ref @@ -813,7 +816,10 @@ def mac_address_get_by_address(context, address): session = get_session() with session.begin(): mac_address_ref = session.query(models.MacAddress).\ - filter_by(address=address) + filter_by(address=address).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + first() return mac_address_ref @@ -828,6 +834,8 @@ def mac_address_get_all_by_instance(context, instance_id): with session.begin(): mac_address_refs = session.query(models.MacAddress).\ filter_by(instance_id=instance_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ all() return mac_address_refs @@ -843,6 +851,8 @@ def mac_address_get_all_by_network(context, network_id): with session.begin(): mac_address_refs = session.query(models.MacAddress).\ filter_by(network_id=network_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ all() return mac_address_refs -- cgit From 496d9543f4978a078ca75015e9c25af0aaf1df28 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 27 Apr 2011 15:20:46 -0500 Subject: added eagerloading mac adddresses for instance --- nova/db/sqlalchemy/api.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 7d272fdb4..fb05556f7 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -956,6 +956,7 @@ def instance_get(context, instance_id, session=None): if is_admin_context(context): result = session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload_all('security_groups.rules')).\ options(joinedload('volumes')).\ options(joinedload_all('fixed_ips.network')).\ @@ -967,6 +968,7 @@ def instance_get(context, instance_id, session=None): elif is_user_context(context): result = session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload_all('security_groups.rules')).\ options(joinedload('volumes')).\ options(joinedload('metadata')).\ @@ -988,6 +990,7 @@ def instance_get_all(context): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1000,6 +1003,7 @@ def instance_get_all_by_user(context, user_id): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1013,6 +1017,7 @@ def instance_get_all_by_host(context, host): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1028,6 +1033,7 @@ def instance_get_all_by_project(context, project_id): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1043,6 +1049,7 @@ def instance_get_all_by_reservation(context, reservation_id): if is_admin_context(context): return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1052,6 +1059,7 @@ def instance_get_all_by_reservation(context, reservation_id): elif is_user_context(context): return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1066,6 +1074,7 @@ def instance_get_project_vpn(context, project_id): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('mac_addresses')).\ options(joinedload('security_groups')).\ options(joinedload('instance_type')).\ filter_by(project_id=project_id).\ -- cgit From 9c0ffe7281ab6ec0acb3ef007b7c955d83007bd0 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 12 May 2011 14:13:25 -0500 Subject: misc related network manager refactor and cleanup --- nova/db/api.py | 45 ++++---- nova/db/sqlalchemy/api.py | 255 +++++++++++++++++++++++++--------------------- 2 files changed, 161 insertions(+), 139 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index e0b9bad21..7fe139b6c 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -582,9 +582,9 @@ def key_pair_get_all_by_user(context, user_id): #################### -def network_associate(context, project_id): +def network_associate(context, project_id, force=False): """Associate a free network to a project.""" - return IMPL.network_associate(context, project_id) + return IMPL.network_associate(context, project_id, force) def network_count(context): @@ -677,6 +677,11 @@ def network_get_all_by_instance(context, instance_id): return IMPL.network_get_all_by_instance(context, instance_id) +def network_get_all_by_host(context, host): + """All networks for which the given host is the network host.""" + return IMPL.network_get_all_by_host(context, host) + + def network_get_index(context, network_id): """Get non-conflicting index for network.""" return IMPL.network_get_index(context, network_id) @@ -709,23 +714,6 @@ def network_update(context, network_id, values): ################### -def project_get_network(context, project_id, associate=True): - """Return the network associated with the project. - - If associate is true, it will attempt to associate a new - network if one is not found, otherwise it returns None. - - """ - return IMPL.project_get_network(context, project_id, associate) - - -def project_get_network_v6(context, project_id): - return IMPL.project_get_network_v6(context, project_id) - - -################### - - def queue_get_for(context, topic, physical_node_id): """Return a channel to send a message to a node with a topic.""" return IMPL.queue_get_for(context, topic, physical_node_id) @@ -1049,6 +1037,9 @@ def user_update(context, user_id, values): return IMPL.user_update(context, user_id, values) +################### + + def project_get(context, id): """Get project by id.""" return IMPL.project_get(context, id) @@ -1089,15 +1080,21 @@ def project_delete(context, project_id): return IMPL.project_delete(context, project_id) -################### +def project_get_network(context, project_id, associate=True): + """Return the network associated with the project. + + If associate is true, it will attempt to associate a new + network if one is not found, otherwise it returns None. + """ + return IMPL.project_get_network(context, project_id, associate) -def host_get_networks(context, host): - """All networks for which the given host is the network host.""" - return IMPL.host_get_networks(context, host) +def project_get_network_v6(context, project_id): + return IMPL.project_get_network_v6(context, project_id) -################## + +################### def console_pool_create(context, values): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index fb05556f7..cd8068962 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1298,20 +1298,47 @@ def key_pair_get_all_by_user(context, user_id): @require_admin_context -def network_associate(context, project_id): +def network_associate(context, project_id, force=False): + """associate a project with a network + + only associates projects with networks that have configured hosts + + only associate if the project doesn't already have a network + or if force is True + + force solves race condition where a fresh project has multiple instance + builds simultaneosly picked up by multiple network hosts which attempt + to associate the project with multiple networks + """ session = get_session() with session.begin(): - network_ref = session.query(models.Network).\ - filter_by(deleted=False).\ - filter_by(project_id=None).\ - with_lockmode('update').\ - first() - # NOTE(vish): if with_lockmode isn't supported, as in sqlite, - # then this has concurrency issues - if not network_ref: - raise db.NoMoreNetworks() - network_ref['project_id'] = project_id - session.add(network_ref) + + def network_query(project_filter): + return session.query(models.Network).\ + filter_by(deleted=False).\ + filter(models.Network.host != None).\ + filter_by(project_id=project_filter).\ + with_lockmode('update').\ + first() + + if not force: + # find out if project has a network + network_ref = network_query(project_id) + + if force or not network_ref: + # in force mode or project doesn't have a network so assocaite + # with a new network + + # get new network + network_ref = network_query(None) + if not network_ref: + raise db.NoMoreNetworks() + + # associate with network + # NOTE(vish): if with_lockmode isn't supported, as in sqlite, + # then this has concurrency issues + network_ref['project_id'] = project_id + session.add(network_ref) return network_ref @@ -1492,6 +1519,16 @@ def network_get_all_by_instance(_context, instance_id): return rv +@require_admin_context +def network_get_all_by_host(context, host): + session = get_session() + with session.begin(): + return session.query(models.Network).\ + filter_by(deleted=False).\ + filter_by(host=host).\ + all() + + @require_admin_context def network_set_host(context, network_id, host_id): session = get_session() @@ -1525,37 +1562,6 @@ def network_update(context, network_id, values): ################### -@require_context -def project_get_network(context, project_id, associate=True): - session = get_session() - result = session.query(models.Network).\ - filter_by(project_id=project_id).\ - filter_by(deleted=False).\ - first() - if not result: - if not associate: - return None - try: - return network_associate(context, project_id) - except IntegrityError: - # NOTE(vish): We hit this if there is a race and two - # processes are attempting to allocate the - # network at the same time - result = session.query(models.Network).\ - filter_by(project_id=project_id).\ - filter_by(deleted=False).\ - first() - return result - - -@require_context -def project_get_network_v6(context, project_id): - return project_get_network(context, project_id) - - -################### - - def queue_get_for(_context, topic, physical_node_id): # FIXME(ja): this should be servername? return "%s.%s" % (topic, physical_node_id) @@ -2127,6 +2133,7 @@ def security_group_rule_destroy(context, security_group_rule_id): ################### + @require_admin_context def user_get(context, id, session=None): if not session: @@ -2191,6 +2198,73 @@ def user_get_all(context): all() +def user_get_roles(context, user_id): + session = get_session() + with session.begin(): + user_ref = user_get(context, user_id, session=session) + return [role.role for role in user_ref['roles']] + + +def user_get_roles_for_project(context, user_id, project_id): + session = get_session() + with session.begin(): + res = session.query(models.UserProjectRoleAssociation).\ + filter_by(user_id=user_id).\ + filter_by(project_id=project_id).\ + all() + return [association.role for association in res] + + +def user_remove_project_role(context, user_id, project_id, role): + session = get_session() + with session.begin(): + session.query(models.UserProjectRoleAssociation).\ + filter_by(user_id=user_id).\ + filter_by(project_id=project_id).\ + filter_by(role=role).\ + delete() + + +def user_remove_role(context, user_id, role): + session = get_session() + with session.begin(): + res = session.query(models.UserRoleAssociation).\ + filter_by(user_id=user_id).\ + filter_by(role=role).\ + all() + for role in res: + session.delete(role) + + +def user_add_role(context, user_id, role): + session = get_session() + with session.begin(): + user_ref = user_get(context, user_id, session=session) + models.UserRoleAssociation(user=user_ref, role=role).\ + save(session=session) + + +def user_add_project_role(context, user_id, project_id, role): + session = get_session() + with session.begin(): + user_ref = user_get(context, user_id, session=session) + project_ref = project_get(context, project_id, session=session) + models.UserProjectRoleAssociation(user_id=user_ref['id'], + project_id=project_ref['id'], + role=role).save(session=session) + + +def user_update(context, user_id, values): + session = get_session() + with session.begin(): + user_ref = user_get(context, user_id, session=session) + user_ref.update(values) + user_ref.save(session=session) + + +################### + + def project_create(_context, values): project_ref = models.Project() project_ref.update(values) @@ -2254,14 +2328,6 @@ def project_remove_member(context, project_id, user_id): project.save(session=session) -def user_update(context, user_id, values): - session = get_session() - with session.begin(): - user_ref = user_get(context, user_id, session=session) - user_ref.update(values) - user_ref.save(session=session) - - def project_update(context, project_id, values): session = get_session() with session.begin(): @@ -2283,73 +2349,32 @@ def project_delete(context, id): session.delete(project_ref) -def user_get_roles(context, user_id): - session = get_session() - with session.begin(): - user_ref = user_get(context, user_id, session=session) - return [role.role for role in user_ref['roles']] - - -def user_get_roles_for_project(context, user_id, project_id): - session = get_session() - with session.begin(): - res = session.query(models.UserProjectRoleAssociation).\ - filter_by(user_id=user_id).\ - filter_by(project_id=project_id).\ - all() - return [association.role for association in res] - - -def user_remove_project_role(context, user_id, project_id, role): - session = get_session() - with session.begin(): - session.query(models.UserProjectRoleAssociation).\ - filter_by(user_id=user_id).\ - filter_by(project_id=project_id).\ - filter_by(role=role).\ - delete() - - -def user_remove_role(context, user_id, role): - session = get_session() - with session.begin(): - res = session.query(models.UserRoleAssociation).\ - filter_by(user_id=user_id).\ - filter_by(role=role).\ - all() - for role in res: - session.delete(role) - - -def user_add_role(context, user_id, role): - session = get_session() - with session.begin(): - user_ref = user_get(context, user_id, session=session) - models.UserRoleAssociation(user=user_ref, role=role).\ - save(session=session) - - -def user_add_project_role(context, user_id, project_id, role): +@require_context +def project_get_network(context, project_id, associate=True): session = get_session() - with session.begin(): - user_ref = user_get(context, user_id, session=session) - project_ref = project_get(context, project_id, session=session) - models.UserProjectRoleAssociation(user_id=user_ref['id'], - project_id=project_ref['id'], - role=role).save(session=session) - - -################### + result = session.query(models.Network).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + first() + if not result: + if not associate: + return None + try: + return network_associate(context, project_id) + except IntegrityError: + # NOTE(vish): We hit this if there is a race and two + # processes are attempting to allocate the + # network at the same time + result = session.query(models.Network).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + first() + return result -@require_admin_context -def host_get_networks(context, host): - session = get_session() - with session.begin(): - return session.query(models.Network).\ - filter_by(deleted=False).\ - filter_by(host=host).\ - all() +@require_context +def project_get_network_v6(context, project_id): + return project_get_network(context, project_id) ################### -- cgit From d4a2a5c34ce568b5d67841c55d3034e93a418507 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 12 May 2011 16:37:35 -0500 Subject: updated previous calls referring to the flags to use the column from the networks table instead --- .../migrate_repo/versions/015_mac_address_table.py | 80 ------------------ .../migrate_repo/versions/015_multi_nic.py | 95 ++++++++++++++++++++++ nova/db/sqlalchemy/models.py | 1 + 3 files changed, 96 insertions(+), 80 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py b/nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py deleted file mode 100644 index e1a82bbed..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/015_mac_address_table.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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 sqlalchemy import * -from migrate import * - -from nova import log as logging - -meta = MetaData() - -# mac address table to add to DB -mac_addresses = Table('mac_addresses', meta, - Column('created_at', DateTime(timezone=False)), - Column('updated_at', DateTime(timezone=False)), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - ) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - c = instances.columns['mac_address'] - - # create mac_addresses table - try: - mac_addresses.create() - except Exception as e: - logging.error(_("Table |%s| not created!"), repr(mac_addresses)) - raise e - - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addressse |%s|"), join_list) - - # insert data into the table - if join_list: - i = mac_addresses.insert() - i.execute(join_list) - - # drop the mac_address column from instances - c.drop - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py new file mode 100644 index 000000000..ee5f6048c --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py @@ -0,0 +1,95 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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 sqlalchemy import * +from migrate import * + +from nova import log as logging + +meta = MetaData() + +# mac address table to add to DB +mac_addresses = Table('mac_addresses', meta, + Column('created_at', DateTime(timezone=False)), + Column('updated_at', DateTime(timezone=False)), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + ) + + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False), + nullable=True) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + try: + networks.create_column(interface) + except Exception as e: + logging.error(_("interface column not added to networks table")) + raise e + + # create mac_addresses table + try: + mac_addresses.create() + except Exception as e: + logging.error(_("Table |%s| not created!"), repr(mac_addresses)) + raise e + + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addressse |%s|"), join_list) + + # insert data into the table + if join_list: + i = mac_addresses.insert() + i.execute(join_list) + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 0a4fa3875..3d34f2a54 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -470,6 +470,7 @@ class Network(BASE, NovaBase): netmask_v6 = Column(String(255)) netmask = Column(String(255)) bridge = Column(String(255)) + bridge_interface = Column(String(255)) gateway = Column(String(255)) broadcast = Column(String(255)) dns = Column(String(255)) -- cgit From 7377b010a133d5afa1a20e36b3a1dd2914c461b2 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 12 May 2011 18:26:46 -0500 Subject: fixed_ip disassociate now also unsets mac_address_id --- nova/db/api.py | 14 +++++--- nova/db/sqlalchemy/api.py | 39 +++++++++++++--------- .../migrate_repo/versions/015_multi_nic.py | 24 ++++++++++++- nova/db/sqlalchemy/models.py | 8 ++--- 4 files changed, 60 insertions(+), 25 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 7fe139b6c..e6ffb8f41 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -413,6 +413,12 @@ def mac_address_get_all_by_instance(context, instance_id): return IMPL.mac_address_get_all_by_instance(context, instance_id) +def mac_address_get_by_instance_and_network(context, instance_id, network_id): + """gets all mac addresses for instance""" + return IMPL.mac_address_get_by_instance_and_network(context, instance_id, + network_id) + + def mac_address_get_all_by_network(context, network_id): """gets all mac addresses for instance""" return IMPL.mac_address_get_all_by_network(context, network_id) @@ -1080,18 +1086,18 @@ def project_delete(context, project_id): return IMPL.project_delete(context, project_id) -def project_get_network(context, project_id, associate=True): +def project_get_networks(context, project_id, associate=True): """Return the network associated with the project. If associate is true, it will attempt to associate a new network if one is not found, otherwise it returns None. """ - return IMPL.project_get_network(context, project_id, associate) + return IMPL.project_get_networks(context, project_id, associate) -def project_get_network_v6(context, project_id): - return IMPL.project_get_network_v6(context, project_id) +def project_get_networks_v6(context, project_id): + return IMPL.project_get_networks_v6(context, project_id) ################### diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index cd8068962..e454d4239 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -645,6 +645,7 @@ def fixed_ip_disassociate(context, address): address, session=session) fixed_ip_ref.instance = None + fixed_ip_ref.mac_address = None fixed_ip_ref.save(session=session) @@ -660,6 +661,7 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time): filter(models.FixedIp.instance_id != None).\ filter_by(allocated=0).\ update({'instance_id': None, + 'mac_address_id': None, 'leased': 0, 'updated_at': datetime.datetime.utcnow()}, synchronize_session='fetch') @@ -840,6 +842,21 @@ def mac_address_get_all_by_instance(context, instance_id): return mac_address_refs +@require_context +def mac_address_get_by_instance_and_network(context, instance_id, + network_id): + """gets mac address for instance that's associated with network""" + session = get_session() + with session.begin(): + mac_address_ref = session.query(models.MacAddress).\ + filter_by(instance_id=instance_id).\ + filter_by(network_id=network_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + first() + return mac_address_ref + + @require_admin_context def mac_address_get_all_by_network(context, network_id): """gets all mac addresses for instance @@ -2350,31 +2367,21 @@ def project_delete(context, id): @require_context -def project_get_network(context, project_id, associate=True): +def project_get_networks(context, project_id, associate=True): session = get_session() result = session.query(models.Network).\ filter_by(project_id=project_id).\ - filter_by(deleted=False).\ - first() + filter_by(deleted=False) if not result: if not associate: - return None - try: - return network_associate(context, project_id) - except IntegrityError: - # NOTE(vish): We hit this if there is a race and two - # processes are attempting to allocate the - # network at the same time - result = session.query(models.Network).\ - filter_by(project_id=project_id).\ - filter_by(deleted=False).\ - first() + return [] + return [network_associate(context, project_id)] return result @require_context -def project_get_network_v6(context, project_id): - return project_get_network(context, project_id) +def project_get_networks_v6(context, project_id): + return project_get_networks(context, project_id) ################### diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py index ee5f6048c..3130760e2 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py @@ -50,6 +50,13 @@ interface = Column('bridge_interface', nullable=True) +# mac_address column to add to fixed_ips table +mac_address = Column('mac_address_id', + Integer(), + ForeignKey('mac_addresses.id'), + nullable=False) + + def upgrade(migrate_engine): meta.bind = migrate_engine @@ -60,6 +67,7 @@ def upgrade(migrate_engine): c = instances.columns['mac_address'] # add interface column to networks table + # values will have to be set manually before running nova try: networks.create_column(interface) except Exception as e: @@ -73,19 +81,33 @@ def upgrade(migrate_engine): logging.error(_("Table |%s| not created!"), repr(mac_addresses)) raise e + # add mac_address column to fixed_ips table + try: + fixed_ips.create_column(mac_address) + except Exception as e: + logging.error(_("mac_address column not added to fixed_ips table")) + raise e + + # populate the mac_addresses table # extract data from existing instance and fixed_ip tables s = select([instances.c.id, instances.c.mac_address, fixed_ips.c.network_id], fixed_ips.c.instance_id == instances.c.id) keys = ('instance_id', 'address', 'network_id') join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addressse |%s|"), join_list) + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) # insert data into the table if join_list: i = mac_addresses.insert() i.execute(join_list) + # populate the fixed_ips mac_address column + s = select([mac_addresses.c.id], + mac_addresses.c.address == instances.c.address) + u = fixed_ips.update().values(mac_address_id=s) + u.execute() + # drop the mac_address column from instances c.drop() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 3d34f2a54..062a8cac0 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -481,10 +481,7 @@ class Network(BASE, NovaBase): vpn_private_address = Column(String(255)) dhcp_start = Column(String(255)) - # NOTE(vish): The unique constraint below helps avoid a race condition - # when associating a network, but it also means that we - # can't associate two networks with one project. - project_id = Column(String(255), unique=True) + project_id = Column(String(255)) host = Column(String(255)) # , ForeignKey('hosts.id')) @@ -496,6 +493,9 @@ class FixedIp(BASE, NovaBase): address = Column(String(255)) network_id = Column(Integer, ForeignKey('networks.id'), nullable=True) network = relationship(Network, backref=backref('fixed_ips')) + mac_address_id = Column(Integer, ForeignKey('mac_addresses.id'), + nullable=True) + mac_address = relationship(MacAddress, backref=backref('fixed_ips')) instance_id = Column(Integer, ForeignKey('instances.id'), nullable=True) instance = relationship(Instance, backref=backref('fixed_ips'), -- cgit From ec4e728487c25694200206562afc622932d8e8b7 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 13 May 2011 12:51:39 -0500 Subject: added |fixed_ip_get_all_by_mac_address| and |mac_address_get_by_fixed_ip| to db and sqlalchemy APIs --- nova/db/api.py | 10 ++++++++++ nova/db/sqlalchemy/api.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index e6ffb8f41..6cabfc234 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -371,6 +371,11 @@ def fixed_ip_get_all_by_instance(context, instance_id): return IMPL.fixed_ip_get_all_by_instance(context, instance_id) +def fixed_ip_get_all_by_mac_address(context, mac_address_id): + """Get fixed ips by mac_address or raise if none exist.""" + return IMPL.fixed_ip_get_all_by_mac_address(context, mac_address_id) + + def fixed_ip_get_instance(context, address): """Get an instance for a fixed ip by address.""" return IMPL.fixed_ip_get_instance(context, address) @@ -408,6 +413,11 @@ def mac_address_get_by_address(context, address): return IMPL.mac_address_get_by_address(context, address) +def mac_address_get_by_fixed_ip(context, fixed_ip_id): + """gets a mac address for a fixed_ip""" + return IMPL.mac_address_get_by_fixed_ip(context, fixed_ip_id) + + def mac_address_get_all_by_instance(context, instance_id): """gets all mac addresses for instance""" return IMPL.mac_address_get_all_by_instance(context, instance_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e454d4239..bebd25674 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -732,6 +732,19 @@ def fixed_ip_get_all_by_instance(context, instance_id): return rv +@require_context +def fixed_ip_get_all_by_mac_address(context, mac_address_id): + session = get_session() + rv = session.query(models.FixedIp).\ + filter_by(mac_address_id=mac_address_id).\ + filter_by(deleted=False).\ + all() + if not rv: + raise exception.NotFound(_('No fixed_ips for mac_address_id %s') % + instance_id) + return rv + + @require_context def fixed_ip_get_instance_v6(context, address): session = get_session() @@ -825,6 +838,24 @@ def mac_address_get_by_address(context, address): return mac_address_ref +@require_context +def mac_address_get_by_fixed_ip(context, fixed_ip_id): + """gets a mac address for a fixed_ip + + context = request context object + fixed_ip_id = id of the fixed_ip you're looking to get mac for + """ + session = get_session() + with session.begin(): + mac_address_ref = session.query(models.MacAddress).\ + filter_by(fixed_ip_id=fixed_ip_id).\ + options(joinedload('fixed_ips')).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + first() + return mac_address_ref + + @require_context def mac_address_get_all_by_instance(context, instance_id): """gets all mac addresses for instance -- cgit From ab4e17aca5b2cbe352c6266ae0b5c3ad7c1c2ed7 Mon Sep 17 00:00:00 2001 From: Jason Koelker Date: Mon, 16 May 2011 11:40:42 -0500 Subject: make the migration work like we expect it to --- .../migrate_repo/versions/015_multi_nic.py | 24 +++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py index 3130760e2..7057d0ccb 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py @@ -13,6 +13,8 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime + from sqlalchemy import * from migrate import * @@ -22,8 +24,10 @@ meta = MetaData() # mac address table to add to DB mac_addresses = Table('mac_addresses', meta, - Column('created_at', DateTime(timezone=False)), - Column('updated_at', DateTime(timezone=False)), + Column('created_at', DateTime(timezone=False), + default=datetime.datetime.utcnow), + Column('updated_at', DateTime(timezone=False), + onupdate=datetime.datetime.utcnow), Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True, nullable=False), @@ -54,7 +58,7 @@ interface = Column('bridge_interface', mac_address = Column('mac_address_id', Integer(), ForeignKey('mac_addresses.id'), - nullable=False) + nullable=True) def upgrade(migrate_engine): @@ -103,10 +107,16 @@ def upgrade(migrate_engine): i.execute(join_list) # populate the fixed_ips mac_address column - s = select([mac_addresses.c.id], - mac_addresses.c.address == instances.c.address) - u = fixed_ips.update().values(mac_address_id=s) - u.execute() + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([mac_addresses.c.id].\ + where(mac_addresses.c.instance_id == row['instance_id']).\ + as_scalar() + u = fixed_ips.update().values(mac_address_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() # drop the mac_address column from instances c.drop() -- cgit From 07ba43fc30f39f6f5122b1ba539c6a669bb35f34 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Mon, 16 May 2011 15:41:26 -0500 Subject: updated the mac_address delete function to actually delete the rows, and update fixed_ips --- nova/db/sqlalchemy/api.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index bebd25674..efe406a25 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -791,17 +791,8 @@ def mac_address_create(context, values): mac_address_ref = models.MacAddress() mac_address_ref.update(values) mac_address_ref.save() -# instance_id = values['instance_id'] -# network_id = values['network_id'] -# -# session = get_session() -# with session.begin(): -# instance = instance_get(context, instance_id, session=session) -# network = network_get(context, network_id, session=session) -# mac_address.instance = instance -# mac_address.network = network -# mac_address_ref.save(session=session) -# return mac_address_ref + + return mac_address_ref @require_context @@ -912,10 +903,12 @@ def mac_address_delete(context, address): context = request context object instance_id = instance to remove macs for """ - ref = mac_address_get_by_address(address) + mac_address = mac_address_get_by_address(address) session = get_session() with session.begin(): - ref.delete(session=session) + for fixed_ip in mac_address['fixed_ips']: + fixed_ip.mac_address = None + session.delete(mac_address) @require_context @@ -927,10 +920,8 @@ def mac_address_delete_by_instance(context, instance_id): instance_id = instance to remove macs for """ refs = mac_address_get_all_by_instance(instance_id) - session = get_session() - with session.begin(): - for ref in refs: - ref.delete(session=session) + for ref in refs: + self.mac_address_delete(ref) ################### -- cgit From 08a22883fd5bf58b5b74645d1b2065a0be8c733b Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 19 May 2011 10:40:49 -0500 Subject: updated the hypervisors and ec2 api to support receiving lists from pluralized mac_addresses and fixed_ips --- nova/db/sqlalchemy/api.py | 20 +++++++++----------- nova/db/sqlalchemy/models.py | 3 +++ 2 files changed, 12 insertions(+), 11 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index efe406a25..814d0b37f 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1159,17 +1159,15 @@ def instance_get_fixed_addresses_v6(context, instance_id): @require_context def instance_get_floating_address(context, instance_id): - session = get_session() - with session.begin(): - instance_ref = instance_get(context, instance_id, session=session) - if not instance_ref.fixed_ip: - return None - # NOTE(tr3buchet): this only gets the first fixed_ip - # won't find floating ips associated with other fixed_ips - if not instance_ref.fixed_ip.floating_ips: - return None - # NOTE(vish): this just returns the first floating ip - return instance_ref.fixed_ip.floating_ips[0]['address'] + fixed_ip_refs = fixed_ip_get_all_by_instance(context, instance_id) + if not fixed_ip_refs: + return None + # NOTE(tr3buchet): this only gets the first fixed_ip + # won't find floating ips associated with other fixed_ips + if not fixed_ip_refs[0].floating_ips: + return None + # NOTE(vish): this just returns the first floating ip + return fixed_ip_ref[0].floating_ips[0]['address'] @require_admin_context diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 062a8cac0..4bc9ef373 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -184,6 +184,9 @@ class Instance(BASE, NovaBase): def project(self): return auth.manager.AuthManager().get_project(self.project_id) + #TODO{tr3buchet): i don't like this shim..... + # prevents breaking ec2 api + # should go away with zones when ec2 api doesn't have compute db access @property def fixed_ip(self): return self.fixed_ips[0] if self.fixed_ips else None -- cgit From efad5e4f1475c77e0dadadc6fad8cf3ca485fd32 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Mon, 23 May 2011 10:35:48 -0500 Subject: fix typo --- nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py index 7057d0ccb..b07576df3 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py @@ -112,7 +112,7 @@ def upgrade(migrate_engine): for row in s.execute(): m = select([mac_addresses.c.id].\ - where(mac_addresses.c.instance_id == row['instance_id']).\ + where(mac_addresses.c.instance_id == row['instance_id'])).\ as_scalar() u = fixed_ips.update().values(mac_address_id=m).\ where(fixed_ips.c.id == row['id']) -- cgit From 15a02b247436ba71c4f64a8ac8d79b32cc8883f1 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Mon, 23 May 2011 13:06:55 -0500 Subject: make the migration run with tests --- nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py index b07576df3..fffcda29f 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py @@ -45,6 +45,16 @@ mac_addresses = Table('mac_addresses', meta, nullable=False), ) +# Don't autoload this table since sqlite will have issues when +# adding the column +fixed_ips = Table('fixed_ips', meta, + Column('id', Integer(), primary_key=True), + Column('address', String(255)), + Column('network_id', Integer(), ForeignKey('networks.id'), + nullable=True), + Column('instance_id', Integer(), ForeignKey('instances.id'), + nullable=True), + ) # bridge_interface column to add to networks table interface = Column('bridge_interface', @@ -66,7 +76,6 @@ def upgrade(migrate_engine): # grab tables and (column for dropping later) instances = Table('instances', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) networks = Table('networks', meta, autoload=True) c = instances.columns['mac_address'] -- cgit From 950e830df8f6e1628739424809a71b1e6a91866a Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Mon, 23 May 2011 13:38:33 -0500 Subject: must have the class defined before referencing it --- nova/db/sqlalchemy/models.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 062a8cac0..a725ee1d0 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -485,6 +485,17 @@ class Network(BASE, NovaBase): host = Column(String(255)) # , ForeignKey('hosts.id')) +class MacAddress(BASE, NovaBase): + """Represents a mac address used by an instance""" + __tablename__ = 'mac_addresses' + id = Column(Integer, primary_key=True) + address = Column(String(255), unique=True) + network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) + network = relationship(Network, backref=backref('mac_addresses')) + instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) + instance = relationship(Instance, backref=backref('mac_addresses')) + + # TODO(vish): can these both come from the same baseclass? class FixedIp(BASE, NovaBase): """Represents a fixed ip for an instance.""" @@ -524,17 +535,6 @@ class FloatingIp(BASE, NovaBase): host = Column(String(255)) # , ForeignKey('hosts.id')) -class MacAddress(BASE, NovaBase): - """Represents a mac address used by an instance""" - __tablename__ = 'mac_addresses' - id = Column(Integer, primary_key=True) - address = Column(String(255), unique=True) - network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) - network = relationship(Network, backref=backref('mac_addresses')) - instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) - instance = relationship(Instance, backref=backref('mac_addresses')) - - class AuthToken(BASE, NovaBase): """Represents an authorization token for all API transactions. -- cgit From 76ddebd1f0848803215eb8f33961e52bced5f058 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Mon, 23 May 2011 13:43:26 -0500 Subject: need to have the complete table def since sqlalchemy/sqlite won't reload the model --- nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py index fffcda29f..7620f72d7 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/015_multi_nic.py @@ -48,12 +48,21 @@ mac_addresses = Table('mac_addresses', meta, # Don't autoload this table since sqlite will have issues when # adding the column fixed_ips = Table('fixed_ips', meta, + Column('created_at', DateTime(timezone=False), + default=datetime.datetime.utcnow), + Column('updated_at', DateTime(timezone=False), + onupdate=datetime.datetime.utcnow), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True), Column('address', String(255)), Column('network_id', Integer(), ForeignKey('networks.id'), nullable=True), Column('instance_id', Integer(), ForeignKey('instances.id'), nullable=True), + Column('allocated', Boolean(), default=False), + Column('leased', Boolean(), default=False), + Column('reserved', Boolean(), default=False), ) # bridge_interface column to add to networks table -- cgit From a4c648a190e3f93b95aaa694f263125147f95633 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Mon, 23 May 2011 16:34:37 -0500 Subject: make sure to get a results, not the query --- nova/db/sqlalchemy/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e454d4239..26eac46c9 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1459,7 +1459,7 @@ def network_get(context, network_id, session=None): def network_get_all(context): session = get_session() result = session.query(models.Network).\ - filter_by(deleted=False) + filter_by(deleted=False).all() if not result: raise exception.NotFound(_('No networks defined')) return result -- cgit From 65091eb4b9718c35fdcb3d3d070dffcc4fb820a3 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Mon, 23 May 2011 18:20:18 -0500 Subject: uhhh yea --- nova/db/sqlalchemy/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 26eac46c9..115a2cffe 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -895,7 +895,7 @@ def mac_address_delete_by_instance(context, instance_id): context = request context object instance_id = instance to remove macs for """ - refs = mac_address_get_all_by_instance(instance_id) + refs = mac_address_get_all_by_instance(context, instance_id) session = get_session() with session.begin(): for ref in refs: -- cgit From a117f2212b2259c4a6658d1634f46e9c862cfea1 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Tue, 24 May 2011 13:22:11 -0500 Subject: many tests pass now --- nova/db/sqlalchemy/api.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 115a2cffe..6c333523d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -26,6 +26,7 @@ from nova import db from nova import exception from nova import flags from nova import utils +from nova import log as logging from nova.db.sqlalchemy import models from nova.db.sqlalchemy.session import get_session from sqlalchemy import or_ @@ -37,6 +38,7 @@ from sqlalchemy.sql import func from sqlalchemy.sql.expression import literal_column FLAGS = flags.FLAGS +LOG = logging.getLogger("nova.db.sqlalchemy") def is_admin_context(context): @@ -775,9 +777,11 @@ def mac_address_create(context, values): context = request context object values = dict containing column values """ - mac_address_ref = models.MacAddress() - mac_address_ref.update(values) - mac_address_ref.save() + session = get_session() + with session.begin(): + mac_address_ref = models.MacAddress() + mac_address_ref.update(values) + mac_address_ref.save(session=session) # instance_id = values['instance_id'] # network_id = values['network_id'] # @@ -2371,7 +2375,8 @@ def project_get_networks(context, project_id, associate=True): session = get_session() result = session.query(models.Network).\ filter_by(project_id=project_id).\ - filter_by(deleted=False) + filter_by(deleted=False).all() + if not result: if not associate: return [] -- cgit From 2cdad3733a6c00a8ba9246f16509f612e22e148c Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Tue, 24 May 2011 15:53:09 -0500 Subject: need to return the ref --- nova/db/sqlalchemy/api.py | 1 + 1 file changed, 1 insertion(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 6c333523d..ebcbbba6e 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -782,6 +782,7 @@ def mac_address_create(context, values): mac_address_ref = models.MacAddress() mac_address_ref.update(values) mac_address_ref.save(session=session) + return mac_address_ref # instance_id = values['instance_id'] # network_id = values['network_id'] # -- cgit From 6365f4141715c6806d40698add59c294613bc063 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 25 May 2011 09:35:35 -0400 Subject: Added model for InstanceTypeMetadata --- nova/db/sqlalchemy/models.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 1215448f8..0c0a5a479 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -635,6 +635,19 @@ class InstanceMetadata(BASE, NovaBase): 'InstanceMetadata.instance_id == Instance.id,' 'InstanceMetadata.deleted == False)') +class InstanceTypeMetadata(BASE, NovaBase): + """Represents a metadata key/value pair for an instance_type""" + __tablename__ = 'instance_type_metadata' + id = Column(Integer, primary_key=True) + key = Column(String(255)) + value = Column(String(255)) + instance_type_id = Column(Integer, ForeignKey('instance_types.id'), nullable=False) + instance = relationship(InstanceType, backref="metadata", + foreign_keys=instance_type_id, + primaryjoin='and_(' + 'InstanceTypeMetadata.instance_type_id == InstanceType.id,' + 'InstanceTypeMetadata.deleted == False)') + class Zone(BASE, NovaBase): """Represents a child zone of this zone.""" -- cgit From ffc997579166748b8c0f38c310ae5fca4dd57f96 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Wed, 25 May 2011 10:51:55 -0500 Subject: make the column name correct --- nova/db/sqlalchemy/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 089e7ecbf..b7783166c 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -551,7 +551,7 @@ class AuthToken(BASE, NovaBase): __tablename__ = 'auth_tokens' token_hash = Column(String(255), primary_key=True) user_id = Column(String(255)) - server_manageent_url = Column(String(255)) + server_management_url = Column(String(255)) storage_url = Column(String(255)) cdn_management_url = Column(String(255)) -- cgit From f694bc15b921cd4affa5b5b63ed0eb9073516b44 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 25 May 2011 12:07:40 -0400 Subject: Adding the migrate code to add the new table --- .../versions/019_add_instance_type_metadata.py | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py b/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py new file mode 100644 index 000000000..4ceb8c36d --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py @@ -0,0 +1,68 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 University of Southern California +# +# 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 sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer +from sqlalchemy import MetaData, String, Table +from nova import log as logging + +meta = MetaData() + +# Just for the ForeignKey and column creation to succeed, these are not the +# actual definitions of instances or services. +instance_types = Table('instance_types', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + + + +# +# New Tables +# + +instance_type_metadata_table = Table('instance_type_metadata', meta, + Column('created_at', DateTime(timezone=False)), + Column('updated_at', DateTime(timezone=False)), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('instance_type_id', + Integer(), + ForeignKey('instance_types.id'), + nullable=False), + Column('key', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('value', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False))) + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; + # bind migrate_engine to your metadata + meta.bind = migrate_engine + for table in (instance_type_metadata_table, ): + try: + table.create() + except Exception: + logging.info(repr(table)) + logging.exception('Exception while creating table') + raise + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + for table in (instance_type_metadata_table, ): + table.drop() + -- cgit From 354b2303e684e50cccb28f7b8af13b19a27e0415 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 25 May 2011 12:15:58 -0400 Subject: Fixing the InstanceTypesMetadata table definition --- nova/db/sqlalchemy/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 0c0a5a479..a32b22b39 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -642,10 +642,10 @@ class InstanceTypeMetadata(BASE, NovaBase): key = Column(String(255)) value = Column(String(255)) instance_type_id = Column(Integer, ForeignKey('instance_types.id'), nullable=False) - instance = relationship(InstanceType, backref="metadata", + instance_type = relationship(InstanceTypes, backref="metadata", foreign_keys=instance_type_id, primaryjoin='and_(' - 'InstanceTypeMetadata.instance_type_id == InstanceType.id,' + 'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,' 'InstanceTypeMetadata.deleted == False)') -- cgit From 7aa62856ebff4242b123f5e0888276237176d066 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 25 May 2011 12:19:52 -0400 Subject: InstanceTypesMetadata is now registered --- nova/db/sqlalchemy/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index a32b22b39..e20feb71a 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -666,12 +666,12 @@ def register_models(): connection is lost and needs to be reestablished. """ from sqlalchemy import create_engine - models = (Service, Instance, InstanceActions, InstanceTypes, + models = (Service, Instance, InstanceActions, InstanceTypes, Volume, ExportDevice, IscsiTarget, FixedIp, FloatingIp, Network, SecurityGroup, SecurityGroupIngressRule, SecurityGroupInstanceAssociation, AuthToken, User, Project, Certificate, ConsolePool, Console, Zone, - InstanceMetadata, Migration) + InstanceMetadata, InstanceTypesMetadata, Migration) engine = create_engine(FLAGS.sql_connection, echo=False) for model in models: model.metadata.create_all(engine) -- cgit From 6141221d01026ce277d34ae329767139178b1ea0 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 25 May 2011 12:29:23 -0400 Subject: pep8 fixes --- .../versions/019_add_instance_type_metadata.py | 5 ++--- nova/db/sqlalchemy/models.py | 14 ++++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py b/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py index 4ceb8c36d..22b741614 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py @@ -26,8 +26,6 @@ instance_types = Table('instance_types', meta, Column('id', Integer(), primary_key=True, nullable=False), ) - - # # New Tables # @@ -49,6 +47,7 @@ instance_type_metadata_table = Table('instance_type_metadata', meta, String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False))) + def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; # bind migrate_engine to your metadata @@ -61,8 +60,8 @@ def upgrade(migrate_engine): logging.exception('Exception while creating table') raise + def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. for table in (instance_type_metadata_table, ): table.drop() - diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index e20feb71a..f806becb5 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -635,18 +635,20 @@ class InstanceMetadata(BASE, NovaBase): 'InstanceMetadata.instance_id == Instance.id,' 'InstanceMetadata.deleted == False)') + class InstanceTypeMetadata(BASE, NovaBase): """Represents a metadata key/value pair for an instance_type""" __tablename__ = 'instance_type_metadata' id = Column(Integer, primary_key=True) key = Column(String(255)) value = Column(String(255)) - instance_type_id = Column(Integer, ForeignKey('instance_types.id'), nullable=False) + instance_type_id = Column(Integer, ForeignKey('instance_types.id'), + nullable=False) instance_type = relationship(InstanceTypes, backref="metadata", - foreign_keys=instance_type_id, - primaryjoin='and_(' - 'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,' - 'InstanceTypeMetadata.deleted == False)') + foreign_keys=instance_type_id, + primaryjoin='and_(' + 'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,' + 'InstanceTypeMetadata.deleted == False)') class Zone(BASE, NovaBase): @@ -666,7 +668,7 @@ def register_models(): connection is lost and needs to be reestablished. """ from sqlalchemy import create_engine - models = (Service, Instance, InstanceActions, InstanceTypes, + models = (Service, Instance, InstanceActions, InstanceTypes, Volume, ExportDevice, IscsiTarget, FixedIp, FloatingIp, Network, SecurityGroup, SecurityGroupIngressRule, SecurityGroupInstanceAssociation, AuthToken, User, -- cgit From 775566067a1f764baec5036357ad47a57316da03 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 25 May 2011 16:58:12 -0400 Subject: Fixed a typo --- nova/db/sqlalchemy/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index f806becb5..d57feddd5 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -673,7 +673,7 @@ def register_models(): Network, SecurityGroup, SecurityGroupIngressRule, SecurityGroupInstanceAssociation, AuthToken, User, Project, Certificate, ConsolePool, Console, Zone, - InstanceMetadata, InstanceTypesMetadata, Migration) + InstanceMetadata, InstanceTypeMetadata, Migration) engine = create_engine(FLAGS.sql_connection, echo=False) for model in models: model.metadata.create_all(engine) -- cgit From b3b2863a8f76f87a601d0b9fe7cc523ca718310a Mon Sep 17 00:00:00 2001 From: Cerberus Date: Wed, 25 May 2011 16:37:39 -0500 Subject: Fixing divergence --- nova/db/sqlalchemy/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 6883f2961..fe2c54d77 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1184,7 +1184,7 @@ def instance_get_floating_address(context, instance_id): if not fixed_ip_refs[0].floating_ips: return None # NOTE(vish): this just returns the first floating ip - return fixed_ip_ref[0].floating_ips[0]['address'] + return fixed_ip_refs[0].floating_ips[0]['address'] @require_admin_context -- cgit From 924d7a88aa9e6d81e20babc0f1d780b3e916300a Mon Sep 17 00:00:00 2001 From: Cerberus Date: Thu, 26 May 2011 16:30:47 -0500 Subject: Virt tests passing while assuming the old style single nics --- nova/db/api.py | 7 +++++++ nova/db/sqlalchemy/api.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index b49ba4860..bbc21cbad 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -376,6 +376,13 @@ def fixed_ip_get_all_by_instance(context, instance_id): return IMPL.fixed_ip_get_all_by_instance(context, instance_id) +def fixed_ip_get_by_instance_and_network(context, instance_id, + network_id): + """Get fixed ips by instance and network or raise if none exist.""" + return IMPL.fixed_ip_get_by_instance_and_network(context, instance_id, + network_id) + + def fixed_ip_get_all_by_mac_address(context, mac_address_id): """Get fixed ips by mac_address or raise if none exist.""" return IMPL.fixed_ip_get_all_by_mac_address(context, mac_address_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index fe2c54d77..cea95c387 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -748,6 +748,19 @@ def fixed_ip_get_all_by_instance(context, instance_id): raise exception.NoFixedIpsFoundForInstance(instance_id=instance_id) return rv +@require_context +def fixed_ip_get_by_instance_and_network(context, instance_id, + network_id): + session = get_session() + rv = session.query(models.FixedIp).\ + filter_by(instance_id=instance_id).\ + filter_by(network_id=network_id).\ + filter_by(deleted=False).\ + first() + if not rv: + raise exception.NoFixedIpsFoundForInstance(instance_id=instance_id) + return rv + @require_context def fixed_ip_get_all_by_mac_address(context, mac_address_id): -- cgit From 65c26759fa8607f89614a6c90a55172805359538 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 27 May 2011 13:03:36 -0400 Subject: Adding accessor methods for instance type metadata --- nova/db/api.py | 17 +++++++++++++ nova/db/sqlalchemy/api.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index ef8aa1143..4700d7f30 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1208,3 +1208,20 @@ def instance_metadata_delete(context, instance_id, key): def instance_metadata_update_or_create(context, instance_id, metadata): """Create or update instance metadata.""" IMPL.instance_metadata_update_or_create(context, instance_id, metadata) + +#################### + + +def instance_type_metadata_get(context, instance_type_id): + """Get all metadata for an instance type.""" + return IMPL.instance_type_metadata_get(context, instance_type_id) + + +def instance_type_metadata_delete(context, instance_type_id, key): + """Delete the given metadata item.""" + IMPL.instance_type_metadata_delete(context, instance_type_id, key) + + +def instance_type_metadata_update_or_create(context, instance_type_id, metadata): + """Create or update instance type metadata.""" + IMPL.instance_type_metadata_update_or_create(context, instance_type_id, metadata) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index b53e81053..9d33ff61f 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2579,3 +2579,66 @@ def instance_metadata_update_or_create(context, instance_id, metadata): "deleted": 0}) meta_ref.save(session=session) return metadata + +#################### + +@require_context +def instance_type_metadata_get(context, instance_type_id): + session = get_session() + + meta_results = session.query(models.InstanceTypeMetadata).\ + filter_by(instance_type_id=instance_type_id).\ + filter_by(deleted=False).\ + all() + + meta_dict = {} + for i in meta_results: + meta_dict[i['key']] = i['value'] + return meta_dict + + +@require_context +def instance_type_metadata_delete(context, instance_type_id, key): + session = get_session() + session.query(models.InstanceTypeMetadata).\ + filter_by(instance_type_id=instance_type_id).\ + filter_by(key=key).\ + filter_by(deleted=False).\ + update({'deleted': True, + 'deleted_at': datetime.datetime.utcnow(), + 'updated_at': literal_column('updated_at')}) + + +@require_context +def instance_type_metadata_get_item(context, instance_type_id, key): + session = get_session() + + meta_result = session.query(models.InstanceMetadata).\ + filter_by(instance_type_id=instance_type_id).\ + filter_by(key=key).\ + filter_by(deleted=False).\ + first() + + if not meta_result: + raise exception.InstanceTypeMetadataNotFound(metadata_key=key, + instance_type_id=instance_type_id) + return meta_result + + +@require_context +def instance_type_metadata_update_or_create(context, instance_type_id, + metadata): + session = get_session() + meta_ref = None + for key, value in metadata.iteritems(): + try: + meta_ref = instance_type_metadata_get_item(context, + instance_type_id, key, + session) + except: + meta_ref = models.InstanceTypeMetadata() + meta_ref.update({"key": key, "value": value, + "instance_type_id": instance_id, + "deleted": 0}) + meta_ref.save(session=session) + return metadata -- cgit From 556ccd9b0d9c7809395b7720e5dcfb6af514f69f Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 27 May 2011 13:03:57 -0400 Subject: Changed metadata to meta to avoid sqlalchemy collision --- nova/db/sqlalchemy/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index d57feddd5..b3e87e77c 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -644,7 +644,7 @@ class InstanceTypeMetadata(BASE, NovaBase): value = Column(String(255)) instance_type_id = Column(Integer, ForeignKey('instance_types.id'), nullable=False) - instance_type = relationship(InstanceTypes, backref="metadata", + instance_type = relationship(InstanceTypes, backref="meta", foreign_keys=instance_type_id, primaryjoin='and_(' 'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,' -- cgit From aa18d32cf20c0bfbbc81ddf234ac59ecf310ccb0 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 27 May 2011 13:44:40 -0400 Subject: Added test for instance type metadata update --- nova/db/sqlalchemy/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 9d33ff61f..14d53d9ed 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2638,7 +2638,7 @@ def instance_type_metadata_update_or_create(context, instance_type_id, except: meta_ref = models.InstanceTypeMetadata() meta_ref.update({"key": key, "value": value, - "instance_type_id": instance_id, + "instance_type_id": instance_type_id, "deleted": 0}) meta_ref.save(session=session) return metadata -- cgit From 19e4a081509217ec04d92ae092917d590cbd8f30 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 27 May 2011 14:20:08 -0400 Subject: Modified instance_type_create to take metadata --- nova/db/sqlalchemy/api.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 14d53d9ed..523bc2a56 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2373,7 +2373,22 @@ def console_get(context, console_id, instance_id=None): @require_admin_context def instance_type_create(_context, values): + """Create a new instance type. In order to pass in metadata, + the values dict should contain a 'meta' key/value pair: + + {'meta' : {'k1': 'v1', 'k2': 'v2', ...}} + + """ try: + metadata = values.get('meta') + metadata_refs = [] + if metadata: + for k, v in metadata.iteritems(): + metadata_ref = models.InstanceTypeMetadata() + metadata_ref['key'] = k + metadata_ref['value'] = v + metadata_refs.append(metadata_ref) + values['meta'] = metadata_refs instance_type_ref = models.InstanceTypes() instance_type_ref.update(values) instance_type_ref.save() -- cgit From 69a49743d733459e532a47e6b588045fe65a6145 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 27 May 2011 14:29:23 -0400 Subject: Fixing pep8 problems --- nova/db/api.py | 9 ++++++--- nova/db/sqlalchemy/api.py | 18 ++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 4700d7f30..3582b7a44 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1222,6 +1222,9 @@ def instance_type_metadata_delete(context, instance_type_id, key): IMPL.instance_type_metadata_delete(context, instance_type_id, key) -def instance_type_metadata_update_or_create(context, instance_type_id, metadata): - """Create or update instance type metadata.""" - IMPL.instance_type_metadata_update_or_create(context, instance_type_id, metadata) +def instance_type_metadata_update_or_create(context, instance_type_id, + metadata): + """Create or update instance type metadata. This adds or modifies the + key/value pairs specified in the metadata dict argument""" + IMPL.instance_type_metadata_update_or_create(context, instance_type_id, + metadata) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 523bc2a56..3e783f534 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2373,11 +2373,11 @@ def console_get(context, console_id, instance_id=None): @require_admin_context def instance_type_create(_context, values): - """Create a new instance type. In order to pass in metadata, - the values dict should contain a 'meta' key/value pair: - + """Create a new instance type. In order to pass in metadata, + the values dict should contain a 'meta' key/value pair: + {'meta' : {'k1': 'v1', 'k2': 'v2', ...}} - + """ try: metadata = values.get('meta') @@ -2597,6 +2597,7 @@ def instance_metadata_update_or_create(context, instance_id, metadata): #################### + @require_context def instance_type_metadata_get(context, instance_type_id): session = get_session() @@ -2635,19 +2636,20 @@ def instance_type_metadata_get_item(context, instance_type_id, key): first() if not meta_result: - raise exception.InstanceTypeMetadataNotFound(metadata_key=key, - instance_type_id=instance_type_id) + raise exception.\ + InstanceTypeMetadataNotFound(metadata_key=key, + instance_type_id=instance_type_id) return meta_result @require_context -def instance_type_metadata_update_or_create(context, instance_type_id, +def instance_type_metadata_update_or_create(context, instance_type_id, metadata): session = get_session() meta_ref = None for key, value in metadata.iteritems(): try: - meta_ref = instance_type_metadata_get_item(context, + meta_ref = instance_type_metadata_get_item(context, instance_type_id, key, session) except: -- cgit From 36c93967577578936bd99a5c9cf344390509e484 Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Tue, 31 May 2011 11:46:06 -0500 Subject: pep8 fixes --- nova/db/sqlalchemy/api.py | 3 ++- .../sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index cea95c387..640f53555 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -748,6 +748,7 @@ def fixed_ip_get_all_by_instance(context, instance_id): raise exception.NoFixedIpsFoundForInstance(instance_id=instance_id) return rv + @require_context def fixed_ip_get_by_instance_and_network(context, instance_id, network_id): @@ -2434,7 +2435,7 @@ def project_get_networks(context, project_id, associate=True): result = session.query(models.Network).\ filter_by(project_id=project_id).\ filter_by(deleted=False).all() - + if not result: if not associate: return [] diff --git a/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py b/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py index a2d8192ca..1a2a6d7ce 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py @@ -160,8 +160,7 @@ def convert_backward(migrate_engine, old_quotas, new_quotas): 'project_id': quota.project_id, 'created_at': quota.created_at, 'updated_at': quota.updated_at, - quota.resource: quota.hard_limit - } + quota.resource: quota.hard_limit} else: quotas[quota.project_id]['created_at'] = earliest( quota.created_at, quotas[quota.project_id]['created_at']) -- cgit From 1a62d0a546e15b1e4e9dbc06b0bc422734594fdb Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 1 Jun 2011 11:38:10 -0400 Subject: Bumped migration number --- .../versions/019_add_instance_type_metadata.py | 67 ---------------------- .../versions/021_add_instance_type_metadata.py | 67 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 67 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py b/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py deleted file mode 100644 index 22b741614..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py +++ /dev/null @@ -1,67 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2011 University of Southern California -# -# 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 sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer -from sqlalchemy import MetaData, String, Table -from nova import log as logging - -meta = MetaData() - -# Just for the ForeignKey and column creation to succeed, these are not the -# actual definitions of instances or services. -instance_types = Table('instance_types', meta, - Column('id', Integer(), primary_key=True, nullable=False), - ) - -# -# New Tables -# - -instance_type_metadata_table = Table('instance_type_metadata', meta, - Column('created_at', DateTime(timezone=False)), - Column('updated_at', DateTime(timezone=False)), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('instance_type_id', - Integer(), - ForeignKey('instance_types.id'), - nullable=False), - Column('key', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('value', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False))) - - -def upgrade(migrate_engine): - # Upgrade operations go here. Don't create your own engine; - # bind migrate_engine to your metadata - meta.bind = migrate_engine - for table in (instance_type_metadata_table, ): - try: - table.create() - except Exception: - logging.info(repr(table)) - logging.exception('Exception while creating table') - raise - - -def downgrade(migrate_engine): - # Operations to reverse the above upgrade go here. - for table in (instance_type_metadata_table, ): - table.drop() diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py b/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py new file mode 100644 index 000000000..22b741614 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py @@ -0,0 +1,67 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 University of Southern California +# +# 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 sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer +from sqlalchemy import MetaData, String, Table +from nova import log as logging + +meta = MetaData() + +# Just for the ForeignKey and column creation to succeed, these are not the +# actual definitions of instances or services. +instance_types = Table('instance_types', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + +# +# New Tables +# + +instance_type_metadata_table = Table('instance_type_metadata', meta, + Column('created_at', DateTime(timezone=False)), + Column('updated_at', DateTime(timezone=False)), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('instance_type_id', + Integer(), + ForeignKey('instance_types.id'), + nullable=False), + Column('key', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('value', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False))) + + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; + # bind migrate_engine to your metadata + meta.bind = migrate_engine + for table in (instance_type_metadata_table, ): + try: + table.create() + except Exception: + logging.info(repr(table)) + logging.exception('Exception while creating table') + raise + + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + for table in (instance_type_metadata_table, ): + table.drop() -- cgit From e467ca61f02d8a0adc50578db1d4ae969a1143f4 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 1 Jun 2011 15:01:47 -0500 Subject: small fixes --- nova/db/sqlalchemy/api.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 5bda38085..a07403fb5 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1345,6 +1345,8 @@ def key_pair_get_all_by_user(context, user_id): @require_admin_context def network_associate(context, project_id, force=False): """associate a project with a network + called by project_get_networks under certain conditions + and network manager add_network_to_project() only associates projects with networks that have configured hosts @@ -1354,6 +1356,8 @@ def network_associate(context, project_id, force=False): force solves race condition where a fresh project has multiple instance builds simultaneosly picked up by multiple network hosts which attempt to associate the project with multiple networks + force should only be used as a direct consequence of user request + all automated requests should not use force """ session = get_session() with session.begin(): @@ -2416,6 +2420,9 @@ def project_delete(context, id): @require_context def project_get_networks(context, project_id, associate=True): + # NOTE(tr3buchet): as before this function will associate + # a project with a network if it doesn't have one and + # associate is true session = get_session() result = session.query(models.Network).\ filter_by(project_id=project_id).\ -- cgit From 3db24f73cd5772537b9508304f8db8a7bb64f5ca Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 3 Jun 2011 12:48:40 -0500 Subject: merged koelker migration changes, renumbered migration filename --- .../migrate_repo/versions/019_multi_nic.py | 127 ------------------ .../migrate_repo/versions/021_multi_nic.py | 146 +++++++++++++++++++++ 2 files changed, 146 insertions(+), 127 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/019_multi_nic.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/019_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/019_multi_nic.py deleted file mode 100644 index 54a70d23c..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/019_multi_nic.py +++ /dev/null @@ -1,127 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging - -meta = MetaData() - -# mac address table to add to DB -mac_addresses = Table('mac_addresses', meta, - Column('created_at', DateTime(timezone=False), - default=datetime.datetime.utcnow), - Column('updated_at', DateTime(timezone=False), - onupdate=datetime.datetime.utcnow), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - ) - - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False), - nullable=True) - - -# mac_address column to add to fixed_ips table -mac_address = Column('mac_address_id', - Integer(), - ForeignKey('mac_addresses.id'), - nullable=True) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception as e: - logging.error(_("interface column not added to networks table")) - raise e - - # create mac_addresses table - try: - mac_addresses.create() - except Exception as e: - logging.error(_("Table |%s| not created!"), repr(mac_addresses)) - raise e - - # add mac_address column to fixed_ips table - try: - fixed_ips.create_column(mac_address) - except Exception as e: - logging.error(_("mac_address column not added to fixed_ips table")) - raise e - - # populate the mac_addresses table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = mac_addresses.insert() - i.execute(join_list) - - # populate the fixed_ips mac_address column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([mac_addresses.c.id]).\ - where(mac_addresses.c.instance_id == row['instance_id']).\ - as_scalar() - u = fixed_ips.update().values(mac_address_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py new file mode 100644 index 000000000..b8682c3d6 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py @@ -0,0 +1,146 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging + +meta = MetaData() + +# mac address table to add to DB +mac_addresses = Table('mac_addresses', meta, + Column('created_at', DateTime(timezone=False), + default=datetime.datetime.utcnow), + Column('updated_at', DateTime(timezone=False), + onupdate=datetime.datetime.utcnow), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + ) + +# Don't autoload this table since sqlite will have issues when +# adding the column +#TODO(tr3buchet)[wishful thinking]: remove support for sqlite +fixed_ips = Table('fixed_ips', meta, + Column('created_at', DateTime(timezone=False), + default=datetime.datetime.utcnow), + Column('updated_at', DateTime(timezone=False), + onupdate=datetime.datetime.utcnow), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True), + Column('address', String(255)), + Column('network_id', Integer(), ForeignKey('networks.id'), + nullable=True), + Column('instance_id', Integer(), ForeignKey('instances.id'), + nullable=True), + Column('allocated', Boolean(), default=False), + Column('leased', Boolean(), default=False), + Column('reserved', Boolean(), default=False), + ) + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False), + nullable=True) + + +# mac_address column to add to fixed_ips table +mac_address = Column('mac_address_id', + Integer(), + ForeignKey('mac_addresses.id'), + nullable=True) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception as e: + logging.error(_("interface column not added to networks table")) + raise e + + # create mac_addresses table + try: + mac_addresses.create() + except Exception as e: + logging.error(_("Table |%s| not created!"), repr(mac_addresses)) + raise e + + # add mac_address column to fixed_ips table + try: + fixed_ips.create_column(mac_address) + except Exception as e: + logging.error(_("mac_address column not added to fixed_ips table")) + raise e + + # populate the mac_addresses table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = mac_addresses.insert() + i.execute(join_list) + + # populate the fixed_ips mac_address column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([mac_addresses.c.id].\ + where(mac_addresses.c.instance_id == row['instance_id'])).\ + as_scalar() + u = fixed_ips.update().values(mac_address_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception -- cgit From 054182932d89c89a549c0ceddbe9549004ad4cd9 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 3 Jun 2011 15:55:09 -0500 Subject: renamed migration --- .../migrate_repo/versions/021_multi_nic.py | 146 --------------------- .../migrate_repo/versions/022_multi_nic.py | 146 +++++++++++++++++++++ 2 files changed, 146 insertions(+), 146 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py deleted file mode 100644 index b8682c3d6..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/021_multi_nic.py +++ /dev/null @@ -1,146 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging - -meta = MetaData() - -# mac address table to add to DB -mac_addresses = Table('mac_addresses', meta, - Column('created_at', DateTime(timezone=False), - default=datetime.datetime.utcnow), - Column('updated_at', DateTime(timezone=False), - onupdate=datetime.datetime.utcnow), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - ) - -# Don't autoload this table since sqlite will have issues when -# adding the column -#TODO(tr3buchet)[wishful thinking]: remove support for sqlite -fixed_ips = Table('fixed_ips', meta, - Column('created_at', DateTime(timezone=False), - default=datetime.datetime.utcnow), - Column('updated_at', DateTime(timezone=False), - onupdate=datetime.datetime.utcnow), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True), - Column('address', String(255)), - Column('network_id', Integer(), ForeignKey('networks.id'), - nullable=True), - Column('instance_id', Integer(), ForeignKey('instances.id'), - nullable=True), - Column('allocated', Boolean(), default=False), - Column('leased', Boolean(), default=False), - Column('reserved', Boolean(), default=False), - ) - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False), - nullable=True) - - -# mac_address column to add to fixed_ips table -mac_address = Column('mac_address_id', - Integer(), - ForeignKey('mac_addresses.id'), - nullable=True) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception as e: - logging.error(_("interface column not added to networks table")) - raise e - - # create mac_addresses table - try: - mac_addresses.create() - except Exception as e: - logging.error(_("Table |%s| not created!"), repr(mac_addresses)) - raise e - - # add mac_address column to fixed_ips table - try: - fixed_ips.create_column(mac_address) - except Exception as e: - logging.error(_("mac_address column not added to fixed_ips table")) - raise e - - # populate the mac_addresses table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = mac_addresses.insert() - i.execute(join_list) - - # populate the fixed_ips mac_address column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([mac_addresses.c.id].\ - where(mac_addresses.c.instance_id == row['instance_id'])).\ - as_scalar() - u = fixed_ips.update().values(mac_address_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py new file mode 100644 index 000000000..b8682c3d6 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py @@ -0,0 +1,146 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging + +meta = MetaData() + +# mac address table to add to DB +mac_addresses = Table('mac_addresses', meta, + Column('created_at', DateTime(timezone=False), + default=datetime.datetime.utcnow), + Column('updated_at', DateTime(timezone=False), + onupdate=datetime.datetime.utcnow), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + ) + +# Don't autoload this table since sqlite will have issues when +# adding the column +#TODO(tr3buchet)[wishful thinking]: remove support for sqlite +fixed_ips = Table('fixed_ips', meta, + Column('created_at', DateTime(timezone=False), + default=datetime.datetime.utcnow), + Column('updated_at', DateTime(timezone=False), + onupdate=datetime.datetime.utcnow), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True), + Column('address', String(255)), + Column('network_id', Integer(), ForeignKey('networks.id'), + nullable=True), + Column('instance_id', Integer(), ForeignKey('instances.id'), + nullable=True), + Column('allocated', Boolean(), default=False), + Column('leased', Boolean(), default=False), + Column('reserved', Boolean(), default=False), + ) + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False), + nullable=True) + + +# mac_address column to add to fixed_ips table +mac_address = Column('mac_address_id', + Integer(), + ForeignKey('mac_addresses.id'), + nullable=True) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception as e: + logging.error(_("interface column not added to networks table")) + raise e + + # create mac_addresses table + try: + mac_addresses.create() + except Exception as e: + logging.error(_("Table |%s| not created!"), repr(mac_addresses)) + raise e + + # add mac_address column to fixed_ips table + try: + fixed_ips.create_column(mac_address) + except Exception as e: + logging.error(_("mac_address column not added to fixed_ips table")) + raise e + + # populate the mac_addresses table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = mac_addresses.insert() + i.execute(join_list) + + # populate the fixed_ips mac_address column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([mac_addresses.c.id].\ + where(mac_addresses.c.instance_id == row['instance_id'])).\ + as_scalar() + u = fixed_ips.update().values(mac_address_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception -- cgit From 325d602160cb6a27801777a28d034412ef9ebaeb Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Tue, 7 Jun 2011 10:22:41 -0500 Subject: take out the host --- nova/db/sqlalchemy/api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 9dd66097e..d8c85cfcf 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -427,12 +427,11 @@ def certificate_update(context, certificate_id, values): @require_context -def floating_ip_allocate_address(context, host, project_id): +def floating_ip_allocate_address(context, project_id): authorize_project_context(context, project_id) session = get_session() with session.begin(): floating_ip_ref = session.query(models.FloatingIp).\ - filter_by(host=host).\ filter_by(fixed_ip_id=None).\ filter_by(project_id=None).\ filter_by(deleted=False).\ -- cgit From 0438855659d89133e588dd4201956a901ed85787 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 8 Jun 2011 12:41:09 -0500 Subject: removed network_info shims in vmops --- nova/db/api.py | 64 ++++--- nova/db/sqlalchemy/api.py | 213 +++++++++++---------- .../migrate_repo/versions/022_multi_nic.py | 65 ++++--- nova/db/sqlalchemy/models.py | 20 +- 4 files changed, 187 insertions(+), 175 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index c495b53a5..4c8a06403 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -378,9 +378,9 @@ def fixed_ip_get_all_by_instance(context, instance_id): return IMPL.fixed_ip_get_all_by_instance(context, instance_id) -def fixed_ip_get_all_by_mac_address(context, mac_address_id): - """Get fixed ips by mac_address or raise if none exist.""" - return IMPL.fixed_ip_get_all_by_mac_address(context, mac_address_id) +def fixed_ip_get_by_virtual_interface(context, vif_id): + """Get fixed ips by virtual interface or raise if none exist.""" + return IMPL.fixed_ip_get_by_virtual_interface(context, vif_id) def fixed_ip_get_instance(context, address): @@ -405,50 +405,52 @@ def fixed_ip_update(context, address, values): #################### -def mac_address_create(context, values): - """create a new mac address record in teh database""" - return IMPL.mac_address_create(context, values) +def virtual_interface_create(context, values): + """create a virtual interface record in the database""" + return IMPL.virtual_interface_create(context, values) -def mac_address_get(context, mac_address_id): - """gets a mac address from the table""" - return IMPL.mac_address_get(context, mac_address_id) +def virtual_interface_get(context, vif_id): + """gets a virtual interface from the table""" + return IMPL.virtual_interface_get(context, vif_id) -def mac_address_get_by_address(context, address): - """gets a mac address from the table""" - return IMPL.mac_address_get_by_address(context, address) +def virtual_interface_get_by_address(context, address): + """gets a virtual interface from the table filtering on address""" + return IMPL.virtual_interface_get_by_address(context, address) -def mac_address_get_by_fixed_ip(context, fixed_ip_id): - """gets a mac address for a fixed_ip""" - return IMPL.mac_address_get_by_fixed_ip(context, fixed_ip_id) +def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): + """gets the virtual interface fixed_ip is associated with""" + return IMPL.virtual_interface_get_by_fixed_ip(context, fixed_ip_id) -def mac_address_get_all_by_instance(context, instance_id): - """gets all mac addresses for instance""" - return IMPL.mac_address_get_all_by_instance(context, instance_id) +def virtual_interface_get_by_instance(context, instance_id): + """gets all virtual_interfaces for instance""" + return IMPL.virtual_interface_get_by_instance(context, instance_id) -def mac_address_get_by_instance_and_network(context, instance_id, network_id): - """gets all mac addresses for instance""" - return IMPL.mac_address_get_by_instance_and_network(context, instance_id, - network_id) +def virtual_interface_get_by_instance_and_network(context, instance_id, + network_id): + """gets all virtual interfaces for instance""" + return IMPL.virtual_interfaces_get_by_instance_and_network(context, + instance_id, + network_id) -def mac_address_get_all_by_network(context, network_id): - """gets all mac addresses for instance""" - return IMPL.mac_address_get_all_by_network(context, network_id) +def virtual_interface_get_by_network(context, network_id): + """gets all virtual interfaces on network""" + return IMPL.virtual_interface_get_by_network(context, network_id) -def mac_address_delete(context, mac_address): - """delete mac address record in teh database""" - return IMPL.mac_address_delete(context, mac_address) +def virtual_interface_delete(context, vif_id): + """delete virtual interface record from the database""" + return IMPL.virtual_interface_delete(context, vif_id) -def mac_address_delete_by_instance(context, instance_id): - """delete mac address record in teh database""" - return IMPL.mac_address_delete_by_instance(context, instance_id) +def virtual_interface_delete_by_instance(context, instance_id): + """delete virtual interface records associated with instance """ + return IMPL.virtual_interface_delete_by_instance(context, instance_id) #################### diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index dd7393bed..3b42dbed3 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -659,7 +659,7 @@ def fixed_ip_disassociate(context, address): address, session=session) fixed_ip_ref.instance = None - fixed_ip_ref.mac_address = None + fixed_ip_ref.virtual_interface = None fixed_ip_ref.save(session=session) @@ -675,7 +675,7 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time): filter(models.FixedIp.instance_id != None).\ filter_by(allocated=0).\ update({'instance_id': None, - 'mac_address_id': None, + 'virtual_interface_id': None, 'leased': 0, 'updated_at': utils.utcnow()}, synchronize_session='fetch') @@ -747,14 +747,14 @@ def fixed_ip_get_all_by_instance(context, instance_id): @require_context -def fixed_ip_get_all_by_mac_address(context, mac_address_id): +def fixed_ip_get_by_virtual_interface(context, vif_id): session = get_session() rv = session.query(models.FixedIp).\ - filter_by(mac_address_id=mac_address_id).\ + filter_by(virtual_interface_id=vif_id).\ filter_by(deleted=False).\ all() if not rv: - raise exception.NoFixedIpFoundForMacAddress(mac_id=mac_id) + raise exception.NoFixedIpFoundForVirtualInterface(vif_id=vif_id) return rv @@ -765,12 +765,12 @@ def fixed_ip_get_instance_v6(context, address): # convert IPv6 address to mac mac = ipv6.to_mac(address) - # get mac address row - mac_ref = mac_address_get_by_address(context, mac) + # get virtual interface + vif_ref = virtual_interface_get_by_address(context, mac) - # look up instance based on instance_id from mac address row + # look up instance based on instance_id from vif row result = session.query(models.Instance).\ - filter_by(id=mac_ref.instance_id) + filter_by(id=vif_ref['instance_id']) return result @@ -795,146 +795,146 @@ def fixed_ip_update(context, address, values): @require_context -def mac_address_create(context, values): - """create a new mac address record in teh database +def virtual_interface_create(context, values): + """create a new virtual interface record in teh database context = request context object values = dict containing column values """ - mac_address_ref = models.MacAddress() - mac_address_ref.update(values) - mac_address_ref.save() + vif_ref = models.VirtualInterface() + vif_ref.update(values) + vif_ref.save() - return mac_address_ref + return vif_ref @require_context -def mac_address_get(context, mac_address_id): - """gets a mac address from the table +def virtual_interface_get(context, vif_id): + """gets a virtual interface from the table context = request context object - mac_address_id = id of the mac_address + vif_id = id of the virtual interface """ session = get_session() - with session.begin(): - mac_address_ref = session.query(models.MacAddress).\ - filter_by(id=mac_address_id).\ - options(joinedload('network')).\ - options(joinedload('instance')).\ - first() - return mac_address_ref + vif_ref = session.query(models.VirtualInterface).\ + filter_by(id=vif_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + options(joinedload('fixed_ips')).\ + first() + return vif_ref @require_context -def mac_address_get_by_address(context, address): - """gets a mac address from the table +def virtual_interface_get_by_address(context, address): + """gets a virtual interface from the table context = request context object - address = the mac you're looking to get + address = the address of the interface you're looking to get """ session = get_session() - with session.begin(): - mac_address_ref = session.query(models.MacAddress).\ - filter_by(address=address).\ - options(joinedload('network')).\ - options(joinedload('instance')).\ - first() - return mac_address_ref + vif_ref = session.query(models.VirtualInterface).\ + filter_by(address=address).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + options(joinedload('fixed_ips')).\ + first() + return vif_ref @require_context -def mac_address_get_by_fixed_ip(context, fixed_ip_id): - """gets a mac address for a fixed_ip +def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): + """gets the virtual interface fixed_ip is associated with context = request context object - fixed_ip_id = id of the fixed_ip you're looking to get mac for + fixed_ip_id = id of the fixed_ip """ session = get_session() - with session.begin(): - mac_address_ref = session.query(models.MacAddress).\ - filter_by(fixed_ip_id=fixed_ip_id).\ - options(joinedload('fixed_ips')).\ - options(joinedload('network')).\ - options(joinedload('instance')).\ - first() - return mac_address_ref + vif_ref = session.query(models.VirtualInterface).\ + filter_by(fixed_ip_id=fixed_ip_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + options(joinedload('fixed_ips')).\ + first() + return vif_ref @require_context -def mac_address_get_all_by_instance(context, instance_id): - """gets all mac addresses for instance +def virtual_interface_get_by_instance(context, instance_id): + """gets all virtual interfaces for instance context = request context object - instance_id = instance to retreive macs for + instance_id = id of the instance to retreive vifs for """ session = get_session() - with session.begin(): - mac_address_refs = session.query(models.MacAddress).\ - filter_by(instance_id=instance_id).\ - options(joinedload('network')).\ - options(joinedload('instance')).\ - all() - return mac_address_refs + vif_refs = session.query(models.VirtualInterface).\ + filter_by(instance_id=instance_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + options(joinedload('fixed_ips')).\ + all() + return vif_refs @require_context -def mac_address_get_by_instance_and_network(context, instance_id, - network_id): - """gets mac address for instance that's associated with network""" +def virtual_interface_get_by_instance_and_network(context, instance_id, + network_id): + """gets virtual interface for instance that's associated with network""" session = get_session() - with session.begin(): - mac_address_ref = session.query(models.MacAddress).\ - filter_by(instance_id=instance_id).\ - filter_by(network_id=network_id).\ - options(joinedload('network')).\ - options(joinedload('instance')).\ - first() - return mac_address_ref + vif_ref = session.query(models.VirtualInterface).\ + filter_by(instance_id=instance_id).\ + filter_by(network_id=network_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + options(joinedload('fixed_ips')).\ + first() + return vif_ref @require_admin_context -def mac_address_get_all_by_network(context, network_id): - """gets all mac addresses for instance +def virtual_interface_get_by_network(context, network_id): + """gets all virtual_interface on network context = request context object - network_id = network to retreive macs for + network_id = network to retreive vifs for """ session = get_session() - with session.begin(): - mac_address_refs = session.query(models.MacAddress).\ - filter_by(network_id=network_id).\ - options(joinedload('network')).\ - options(joinedload('instance')).\ - all() - return mac_address_refs + vif_refs = session.query(models.VirtualInterface).\ + filter_by(network_id=network_id).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + options(joinedload('fixed_ips')).\ + all() + return vif_refs @require_context -def mac_address_delete(context, address): - """delete mac address record in teh database +def virtual_interface_delete(context, vif_id): + """delete virtual interface record from teh database context = request context object - instance_id = instance to remove macs for + vif_id = id of vif to delete """ - mac_address = mac_address_get_by_address(address) + vif_ref = virtual_interface_get(context, vif_id) session = get_session() with session.begin(): - for fixed_ip in mac_address['fixed_ips']: - fixed_ip.mac_address = None - session.delete(mac_address) + # disassociate any fixed_ips from this interface + for fixed_ip in vif_ref['fixed_ips']: + fixed_ip.virtual_interface = None + session.delete(vif_ref) @require_context -def mac_address_delete_by_instance(context, instance_id): - """delete mac address records in the database that are associated +def virtual_interface_delete_by_instance(context, instance_id): + """delete virtual interface records that are associated with the instance given by instance_id context = request context object - instance_id = instance to remove macs for + instance_id = id of instance """ - refs = mac_address_get_all_by_instance(instance_id) - for ref in refs: - self.mac_address_delete(ref) + vif_refs = virtual_interface_get_by_instance(context, instance_id) + for vif_ref in vif_refs: + self.virtual_interface_delete(vif_ref['id']) ################### @@ -1012,7 +1012,7 @@ def instance_get(context, instance_id, session=None): if is_admin_context(context): result = session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload_all('security_groups.rules')).\ options(joinedload('volumes')).\ options(joinedload_all('fixed_ips.network')).\ @@ -1024,7 +1024,7 @@ def instance_get(context, instance_id, session=None): elif is_user_context(context): result = session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload_all('security_groups.rules')).\ options(joinedload('volumes')).\ options(joinedload('metadata')).\ @@ -1044,7 +1044,7 @@ def instance_get_all(context): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('metadata')).\ @@ -1058,7 +1058,7 @@ def instance_get_all_by_user(context, user_id): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('metadata')).\ @@ -1073,7 +1073,7 @@ def instance_get_all_by_host(context, host): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1089,7 +1089,7 @@ def instance_get_all_by_project(context, project_id): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1105,7 +1105,7 @@ def instance_get_all_by_reservation(context, reservation_id): if is_admin_context(context): return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1115,7 +1115,7 @@ def instance_get_all_by_reservation(context, reservation_id): elif is_user_context(context): return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload_all('fixed_ips.network')).\ options(joinedload('instance_type')).\ @@ -1130,7 +1130,7 @@ def instance_get_project_vpn(context, project_id): session = get_session() return session.query(models.Instance).\ options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('mac_addresses')).\ + options(joinedload('virtual_interfaces')).\ options(joinedload('security_groups')).\ options(joinedload('instance_type')).\ filter_by(project_id=project_id).\ @@ -1163,16 +1163,17 @@ def instance_get_fixed_addresses_v6(context, instance_id): # compile a list of cidr_v6 prefixes sorted by network id prefixes = [ref.cidr_v6 for ref in sorted(network_refs, key=lambda ref: ref.id)] - # get mac rows associated with instance - mac_refs = mac_address_get_all_by_instance(context, instance_ref.id) - # compile of list of the mac_addresses sorted by network id - macs = [ref.mac_address for ref in - sorted(mac_refs, key=lambda ref: ref.network_id)] - # get project ids from instance + # get vifs associated with instance + vif_refs = virtual_interface_get_all_by_instance(context, + instance_ref.id) + # compile list of the mac_addresses for vifs sorted by network id + macs = [vif_ref['address'] for vif_ref in + sorted(vif_refs, key=lambda vif_ref: vif_ref['network_id'])] + # get project id from instance project_id = instance_ref.project_id # combine prefixes, macs, and project_id into (prefix,mac,p_id) tuples prefix_mac_tuples = zip(prefixes, macs, [project_id for m in macs]) - # return list containing ipv6 address for each pair + # return list containing ipv6 address for each tuple return [ipv6.to_global_ipv6(*t) for t in prefix_mac_tuples] diff --git a/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py index b8682c3d6..86ef24b3f 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py @@ -19,15 +19,16 @@ from sqlalchemy import * from migrate import * from nova import log as logging +from nova import utils meta = MetaData() -# mac address table to add to DB -mac_addresses = Table('mac_addresses', meta, +# virtual interface table to add to DB +virtual_interfaces = Table('virtual_interfaces', meta, Column('created_at', DateTime(timezone=False), - default=datetime.datetime.utcnow), + default=utils.utcnow()), Column('updated_at', DateTime(timezone=False), - onupdate=datetime.datetime.utcnow), + onupdate=utils.utcnow()), Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True, nullable=False), @@ -43,16 +44,20 @@ mac_addresses = Table('mac_addresses', meta, Integer(), ForeignKey('instances.id'), nullable=False), + Column('port_id', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True, nullable=True), ) # Don't autoload this table since sqlite will have issues when -# adding the column +# adding a column with a foreign key #TODO(tr3buchet)[wishful thinking]: remove support for sqlite fixed_ips = Table('fixed_ips', meta, Column('created_at', DateTime(timezone=False), - default=datetime.datetime.utcnow), + default=utils.utcnow()), Column('updated_at', DateTime(timezone=False), - onupdate=datetime.datetime.utcnow), + onupdate=utils.utcnow()), Column('deleted_at', DateTime(timezone=False)), Column('deleted', Boolean(create_constraint=True, name=None)), Column('id', Integer(), primary_key=True), @@ -74,11 +79,11 @@ interface = Column('bridge_interface', nullable=True) -# mac_address column to add to fixed_ips table -mac_address = Column('mac_address_id', - Integer(), - ForeignKey('mac_addresses.id'), - nullable=True) +# virtual interface id column to add to fixed_ips table +virtual_interface_id = Column('virtual_interface_id', + Integer(), + ForeignKey('virtual_interfaces.id'), + nullable=True) def upgrade(migrate_engine): @@ -93,25 +98,25 @@ def upgrade(migrate_engine): # values will have to be set manually before running nova try: networks.create_column(interface) - except Exception as e: + except Exception: logging.error(_("interface column not added to networks table")) - raise e + raise - # create mac_addresses table + # create virtual_interfaces table try: - mac_addresses.create() - except Exception as e: - logging.error(_("Table |%s| not created!"), repr(mac_addresses)) - raise e + virtual_interfaces.create() + except Exception: + logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) + raise - # add mac_address column to fixed_ips table + # add virtual_interface_id column to fixed_ips table try: - fixed_ips.create_column(mac_address) - except Exception as e: - logging.error(_("mac_address column not added to fixed_ips table")) - raise e + fixed_ips.create_column(virtual_interface_id) + except Exception: + logging.error(_("VIF column not added to fixed_ips table")) + raise - # populate the mac_addresses table + # populate the virtual_interfaces table # extract data from existing instance and fixed_ip tables s = select([instances.c.id, instances.c.mac_address, fixed_ips.c.network_id], @@ -122,18 +127,18 @@ def upgrade(migrate_engine): # insert data into the table if join_list: - i = mac_addresses.insert() + i = virtual_interfaces.insert() i.execute(join_list) - # populate the fixed_ips mac_address column + # populate the fixed_ips virtual_interface_id column s = select([fixed_ips.c.id, fixed_ips.c.instance_id], fixed_ips.c.instance_id != None) for row in s.execute(): - m = select([mac_addresses.c.id].\ - where(mac_addresses.c.instance_id == row['instance_id'])).\ + m = select([virtual_interfaces.c.id].\ + where(virtual_interfaces.c.instance_id == row['instance_id'])).\ as_scalar() - u = fixed_ips.update().values(mac_address_id=m).\ + u = fixed_ips.update().values(virtual_interface_id=m).\ where(fixed_ips.c.id == row['id']) u.execute() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index e0614a95f..cc9ce64a0 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -524,9 +524,10 @@ class FixedIp(BASE, NovaBase): address = Column(String(255)) network_id = Column(Integer, ForeignKey('networks.id'), nullable=True) network = relationship(Network, backref=backref('fixed_ips')) - mac_address_id = Column(Integer, ForeignKey('mac_addresses.id'), - nullable=True) - mac_address = relationship(MacAddress, backref=backref('fixed_ips')) + virtual_interface_id = Column(Integer, ForeignKey('virtual_interfaces.id'), + nullable=True) + virtual_interface = relationship(VirtualInterface, + backref=backref('fixed_ips')) instance_id = Column(Integer, ForeignKey('instances.id'), nullable=True) instance = relationship(Instance, backref=backref('fixed_ips'), @@ -556,15 +557,18 @@ class FloatingIp(BASE, NovaBase): auto_assigned = Column(Boolean, default=False, nullable=False) -class MacAddress(BASE, NovaBase): - """Represents a mac address used by an instance""" - __tablename__ = 'mac_addresses' +class VirtualInterface(BASE, NovaBase): + """Represents a virtual interface on an instance""" + __tablename__ = 'virtual_interfaces' id = Column(Integer, primary_key=True) address = Column(String(255), unique=True) network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) - network = relationship(Network, backref=backref('mac_addresses')) + network = relationship(Network, backref=backref('virtual_interfaces')) + port_id = Column(String(255), unique=True, nullable=True) + + # TODO(tr3buchet): cut the cord, removed foreign key and backrefs instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) - instance = relationship(Instance, backref=backref('mac_addresses')) + instance = relationship(Instance, backref=backref('virtual_interfaces')) class AuthToken(BASE, NovaBase): -- cgit From d7925b3890f651b3f6fd002a45b2add86e388d10 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 8 Jun 2011 14:46:31 -0500 Subject: updated docstring for nova-manage network create --- nova/db/api.py | 4 ++-- nova/db/sqlalchemy/api.py | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 4c8a06403..c990af094 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -373,9 +373,9 @@ def fixed_ip_get_by_address(context, address): return IMPL.fixed_ip_get_by_address(context, address) -def fixed_ip_get_all_by_instance(context, instance_id): +def fixed_ip_get_by_instance(context, instance_id): """Get fixed ips by instance or raise if none exist.""" - return IMPL.fixed_ip_get_all_by_instance(context, instance_id) + return IMPL.fixed_ip_get_by_instance(context, instance_id) def fixed_ip_get_by_virtual_interface(context, vif_id): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 93c3f8897..67c032a56 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -741,7 +741,7 @@ def fixed_ip_get_instance(context, address): @require_context -def fixed_ip_get_all_by_instance(context, instance_id): +def fixed_ip_get_by_instance(context, instance_id): session = get_session() rv = session.query(models.FixedIp).\ filter_by(instance_id=instance_id).\ @@ -1151,7 +1151,7 @@ def instance_get_fixed_addresses(context, instance_id): with session.begin(): instance_ref = instance_get(context, instance_id, session=session) try: - fixed_ips = fixed_ip_get_all_by_instance(context, instance_id) + fixed_ips = fixed_ip_get_by_instance(context, instance_id) except exception.NotFound: return [] return [fixed_ip.address for fixed_ip in fixed_ips] @@ -1170,8 +1170,7 @@ def instance_get_fixed_addresses_v6(context, instance_id): prefixes = [ref.cidr_v6 for ref in sorted(network_refs, key=lambda ref: ref.id)] # get vifs associated with instance - vif_refs = virtual_interface_get_all_by_instance(context, - instance_ref.id) + vif_refs = virtual_interface_get_by_instance(context, instance_ref.id) # compile list of the mac_addresses for vifs sorted by network id macs = [vif_ref['address'] for vif_ref in sorted(vif_refs, key=lambda vif_ref: vif_ref['network_id'])] @@ -1185,7 +1184,7 @@ def instance_get_fixed_addresses_v6(context, instance_id): @require_context def instance_get_floating_address(context, instance_id): - fixed_ip_refs = fixed_ip_get_all_by_instance(context, instance_id) + fixed_ip_refs = fixed_ip_get_by_instance(context, instance_id) if not fixed_ip_refs: return None # NOTE(tr3buchet): this only gets the first fixed_ip -- cgit From d5ff85279a8516c0a29882a133c6f6644cbe4b6d Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 9 Jun 2011 12:05:51 -0500 Subject: renamed migration again --- .../migrate_repo/versions/022_multi_nic.py | 151 --------------------- .../migrate_repo/versions/023_multi_nic.py | 151 +++++++++++++++++++++ 2 files changed, 151 insertions(+), 151 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py deleted file mode 100644 index 86ef24b3f..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/022_multi_nic.py +++ /dev/null @@ -1,151 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - -# virtual interface table to add to DB -virtual_interfaces = Table('virtual_interfaces', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - Column('port_id', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True, nullable=True), - ) - -# Don't autoload this table since sqlite will have issues when -# adding a column with a foreign key -#TODO(tr3buchet)[wishful thinking]: remove support for sqlite -fixed_ips = Table('fixed_ips', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True), - Column('address', String(255)), - Column('network_id', Integer(), ForeignKey('networks.id'), - nullable=True), - Column('instance_id', Integer(), ForeignKey('instances.id'), - nullable=True), - Column('allocated', Boolean(), default=False), - Column('leased', Boolean(), default=False), - Column('reserved', Boolean(), default=False), - ) - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False), - nullable=True) - - -# virtual interface id column to add to fixed_ips table -virtual_interface_id = Column('virtual_interface_id', - Integer(), - ForeignKey('virtual_interfaces.id'), - nullable=True) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception: - logging.error(_("interface column not added to networks table")) - raise - - # create virtual_interfaces table - try: - virtual_interfaces.create() - except Exception: - logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) - raise - - # add virtual_interface_id column to fixed_ips table - try: - fixed_ips.create_column(virtual_interface_id) - except Exception: - logging.error(_("VIF column not added to fixed_ips table")) - raise - - # populate the virtual_interfaces table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = virtual_interfaces.insert() - i.execute(join_list) - - # populate the fixed_ips virtual_interface_id column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([virtual_interfaces.c.id].\ - where(virtual_interfaces.c.instance_id == row['instance_id'])).\ - as_scalar() - u = fixed_ips.update().values(virtual_interface_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py new file mode 100644 index 000000000..86ef24b3f --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py @@ -0,0 +1,151 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + +# virtual interface table to add to DB +virtual_interfaces = Table('virtual_interfaces', meta, + Column('created_at', DateTime(timezone=False), + default=utils.utcnow()), + Column('updated_at', DateTime(timezone=False), + onupdate=utils.utcnow()), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + Column('port_id', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True, nullable=True), + ) + +# Don't autoload this table since sqlite will have issues when +# adding a column with a foreign key +#TODO(tr3buchet)[wishful thinking]: remove support for sqlite +fixed_ips = Table('fixed_ips', meta, + Column('created_at', DateTime(timezone=False), + default=utils.utcnow()), + Column('updated_at', DateTime(timezone=False), + onupdate=utils.utcnow()), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True), + Column('address', String(255)), + Column('network_id', Integer(), ForeignKey('networks.id'), + nullable=True), + Column('instance_id', Integer(), ForeignKey('instances.id'), + nullable=True), + Column('allocated', Boolean(), default=False), + Column('leased', Boolean(), default=False), + Column('reserved', Boolean(), default=False), + ) + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False), + nullable=True) + + +# virtual interface id column to add to fixed_ips table +virtual_interface_id = Column('virtual_interface_id', + Integer(), + ForeignKey('virtual_interfaces.id'), + nullable=True) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception: + logging.error(_("interface column not added to networks table")) + raise + + # create virtual_interfaces table + try: + virtual_interfaces.create() + except Exception: + logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) + raise + + # add virtual_interface_id column to fixed_ips table + try: + fixed_ips.create_column(virtual_interface_id) + except Exception: + logging.error(_("VIF column not added to fixed_ips table")) + raise + + # populate the virtual_interfaces table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = virtual_interfaces.insert() + i.execute(join_list) + + # populate the fixed_ips virtual_interface_id column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([virtual_interfaces.c.id].\ + where(virtual_interfaces.c.instance_id == row['instance_id'])).\ + as_scalar() + u = fixed_ips.update().values(virtual_interface_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception -- cgit From 7ae2b21c476099faca0b8279e4b2d8e3df88a9eb Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 9 Jun 2011 16:31:14 -0500 Subject: removed fixed_ips virtual_interface_id foreignkey constraint from multi_nic migration, and added it as a standalone migration with special sqlite files --- .../migrate_repo/versions/023_multi_nic.py | 31 ++---------- .../024_fk_fixed_ips_virtual_interface_id.py | 56 ++++++++++++++++++++++ .../migrate_repo/versions/024_sqlite_downgrade.sql | 48 +++++++++++++++++++ .../migrate_repo/versions/024_sqlite_upgrade.sql | 48 +++++++++++++++++++ 4 files changed, 157 insertions(+), 26 deletions(-) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py index 86ef24b3f..12cd7621a 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py @@ -47,43 +47,21 @@ virtual_interfaces = Table('virtual_interfaces', meta, Column('port_id', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), - unique=True, nullable=True), + unique=True), ) -# Don't autoload this table since sqlite will have issues when -# adding a column with a foreign key -#TODO(tr3buchet)[wishful thinking]: remove support for sqlite -fixed_ips = Table('fixed_ips', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True), - Column('address', String(255)), - Column('network_id', Integer(), ForeignKey('networks.id'), - nullable=True), - Column('instance_id', Integer(), ForeignKey('instances.id'), - nullable=True), - Column('allocated', Boolean(), default=False), - Column('leased', Boolean(), default=False), - Column('reserved', Boolean(), default=False), - ) # bridge_interface column to add to networks table interface = Column('bridge_interface', String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False), - nullable=True) + _warn_on_bytestring=False)) # virtual interface id column to add to fixed_ips table +# foreignkey added in next migration virtual_interface_id = Column('virtual_interface_id', - Integer(), - ForeignKey('virtual_interfaces.id'), - nullable=True) + Integer()) def upgrade(migrate_engine): @@ -92,6 +70,7 @@ def upgrade(migrate_engine): # grab tables and (column for dropping later) instances = Table('instances', meta, autoload=True) networks = Table('networks', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) c = instances.columns['mac_address'] # add interface column to networks table diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py new file mode 100644 index 000000000..56e927717 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py @@ -0,0 +1,56 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # grab tables + fixed_ips = Table('fixed_ips', meta, autoload=True) + virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) + + # add foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).create() + except Exception: + logging.error(_("foreign key constraint couldn't be added")) + raise + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # drop foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).drop() + except Exception: + logging.error(_("foreign key constraint couldn't be dropped")) + raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql new file mode 100644 index 000000000..c1d26b180 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql new file mode 100644 index 000000000..2a9362545 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; -- cgit From b425aa0c49aba5d52250d3b7d0cd282464a32141 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 10 Jun 2011 14:57:02 -0500 Subject: misc argument alterations --- nova/db/sqlalchemy/models.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index d44a91209..9455ed95a 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -517,6 +517,20 @@ class Network(BASE, NovaBase): host = Column(String(255)) # , ForeignKey('hosts.id')) +class VirtualInterface(BASE, NovaBase): + """Represents a virtual interface on an instance""" + __tablename__ = 'virtual_interfaces' + id = Column(Integer, primary_key=True) + address = Column(String(255), unique=True) + network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) + network = relationship(Network, backref=backref('virtual_interfaces')) + port_id = Column(String(255), unique=True, nullable=True) + + # TODO(tr3buchet): cut the cord, removed foreign key and backrefs + instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) + instance = relationship(Instance, backref=backref('virtual_interfaces')) + + # TODO(vish): can these both come from the same baseclass? class FixedIp(BASE, NovaBase): """Represents a fixed ip for an instance.""" @@ -558,20 +572,6 @@ class FloatingIp(BASE, NovaBase): auto_assigned = Column(Boolean, default=False, nullable=False) -class VirtualInterface(BASE, NovaBase): - """Represents a virtual interface on an instance""" - __tablename__ = 'virtual_interfaces' - id = Column(Integer, primary_key=True) - address = Column(String(255), unique=True) - network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) - network = relationship(Network, backref=backref('virtual_interfaces')) - port_id = Column(String(255), unique=True, nullable=True) - - # TODO(tr3buchet): cut the cord, removed foreign key and backrefs - instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) - instance = relationship(Instance, backref=backref('virtual_interfaces')) - - class AuthToken(BASE, NovaBase): """Represents an authorization token for all API transactions. -- cgit From 878468db557b4498528d57804a1808388d7993ec Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 10 Jun 2011 16:55:27 -0500 Subject: floating ips can now move around the network hosts --- nova/db/sqlalchemy/api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e2996ba87..076f7ba67 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -433,7 +433,7 @@ def certificate_update(context, certificate_id, values): @require_context -def floating_ip_allocate_address(context, project_id): +def floating_ip_allocate_address(context, host, project_id): authorize_project_context(context, project_id) session = get_session() with session.begin(): @@ -448,6 +448,7 @@ def floating_ip_allocate_address(context, project_id): if not floating_ip_ref: raise db.NoMoreAddresses() floating_ip_ref['project_id'] = project_id + floating_ip_ref['host'] = host session.add(floating_ip_ref) return floating_ip_ref['address'] @@ -496,6 +497,7 @@ def floating_ip_deallocate(context, address): address, session=session) floating_ip_ref['project_id'] = None + floating_ip_ref['host'] = None floating_ip_ref['auto_assigned'] = False floating_ip_ref.save(session=session) -- cgit From 9ef64c8ccbcdacfef642b2c203ffcc45b2deaf36 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Tue, 14 Jun 2011 17:52:49 -0500 Subject: fixed instance[fixed_ip] in ec2 api, removed fixed_ip shim --- nova/db/sqlalchemy/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 9455ed95a..e9689774c 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -187,9 +187,9 @@ class Instance(BASE, NovaBase): #TODO{tr3buchet): i don't like this shim..... # prevents breaking ec2 api # should go away with zones when ec2 api doesn't have compute db access - @property - def fixed_ip(self): - return self.fixed_ips[0] if self.fixed_ips else None + #@property + #def fixed_ip(self): + # return self.fixed_ips[0] if self.fixed_ips else None image_ref = Column(String(255)) kernel_id = Column(String(255)) -- cgit From e0238c30ac5bb4d2090d47608c08e2c208429055 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Tue, 14 Jun 2011 18:49:03 -0500 Subject: net base project id now from context, removed incorrect floatnig ip host assignment --- nova/db/api.py | 4 ++-- nova/db/sqlalchemy/api.py | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index c990af094..23c4daa44 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -224,13 +224,13 @@ def certificate_update(context, certificate_id, values): ################### -def floating_ip_allocate_address(context, host, project_id): +def floating_ip_allocate_address(context, project_id): """Allocate free floating ip and return the address. Raises if one is not available. """ - return IMPL.floating_ip_allocate_address(context, host, project_id) + return IMPL.floating_ip_allocate_address(context, project_id) def floating_ip_create(context, values): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 076f7ba67..46f5e7494 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -433,7 +433,7 @@ def certificate_update(context, certificate_id, values): @require_context -def floating_ip_allocate_address(context, host, project_id): +def floating_ip_allocate_address(context, project_id): authorize_project_context(context, project_id) session = get_session() with session.begin(): @@ -448,7 +448,6 @@ def floating_ip_allocate_address(context, host, project_id): if not floating_ip_ref: raise db.NoMoreAddresses() floating_ip_ref['project_id'] = project_id - floating_ip_ref['host'] = host session.add(floating_ip_ref) return floating_ip_ref['address'] -- cgit From fe96fb768de04aac6eaf4a44ac6bc4963d9028b7 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 15 Jun 2011 16:02:49 -0500 Subject: specify mysql_engine for the virtual_interfaces table in the migration --- nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py index 12cd7621a..85ab1fdd8 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py @@ -48,7 +48,7 @@ virtual_interfaces = Table('virtual_interfaces', meta, String(length=255, convert_unicode=False, assert_unicode=None, unicode_error=None, _warn_on_bytestring=False), unique=True), - ) + mysql_engine='InnoDB') # bridge_interface column to add to networks table -- cgit From e3c2a97049513e4cff1700bd87d780f6e41afc87 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 15 Jun 2011 16:47:27 -0500 Subject: syntax --- nova/db/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 23c4daa44..64b6a893e 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -433,9 +433,9 @@ def virtual_interface_get_by_instance(context, instance_id): def virtual_interface_get_by_instance_and_network(context, instance_id, network_id): """gets all virtual interfaces for instance""" - return IMPL.virtual_interfaces_get_by_instance_and_network(context, - instance_id, - network_id) + return IMPL.virtual_interface_get_by_instance_and_network(context, + instance_id, + network_id) def virtual_interface_get_by_network(context, network_id): -- cgit From a2ea6652fce1b75d61b2217676c8447327a2467e Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 15 Jun 2011 17:36:07 -0500 Subject: removed commented out shim on Instance class --- nova/db/sqlalchemy/models.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index e9689774c..605e6126a 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -184,13 +184,6 @@ class Instance(BASE, NovaBase): def project(self): return auth.manager.AuthManager().get_project(self.project_id) - #TODO{tr3buchet): i don't like this shim..... - # prevents breaking ec2 api - # should go away with zones when ec2 api doesn't have compute db access - #@property - #def fixed_ip(self): - # return self.fixed_ips[0] if self.fixed_ips else None - image_ref = Column(String(255)) kernel_id = Column(String(255)) ramdisk_id = Column(String(255)) -- cgit From b10621f5f85cccde3d159afddb78398544d4c32e Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Thu, 16 Jun 2011 17:30:36 +0400 Subject: First implementation of FloatingIpController --- nova/db/api.py | 3 +++ nova/db/sqlalchemy/api.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 4e0aa60a2..8348d90ae 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -223,6 +223,9 @@ def certificate_update(context, certificate_id, values): ################### +def floating_ip_get(context, floating_ip_id): + return IMPL.floating_ip_get(context, floating_ip_id) + def floating_ip_allocate_address(context, host, project_id): """Allocate free floating ip and return the address. diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 73870d2f3..c3b517492 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -428,6 +428,29 @@ def certificate_update(context, certificate_id, values): ################### +@require_context +def floating_ip_get(context, ip_id): + session = get_session() + result = None + if is_admin_context(context): + result = session.query(models.FloatingIp).\ + options(joinedload('fixed_ip')).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(id=ip_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() + elif is_user_context(context): + result = session.query(models.FloatingIp).\ + options(joinedload('fixed_ip')).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(project_id=context.project_id).\ + filter_by(id=ip_id).\ + filter_by(deleted=False).\ + first() + if not result: + raise exception.FloatingIpNotFound() + + return result @require_context -- cgit From 09dc2c32e18692c2e3d3743d126a52dd73cf598d Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 16 Jun 2011 14:00:14 -0500 Subject: returned two files to their trunk versions, odd that they were altered in the first place --- .../sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py b/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py index db7fb951a..a4fe3e482 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/016_make_quotas_key_and_value.py @@ -160,7 +160,8 @@ def convert_backward(migrate_engine, old_quotas, new_quotas): 'project_id': quota.project_id, 'created_at': quota.created_at, 'updated_at': quota.updated_at, - quota.resource: quota.hard_limit} + quota.resource: quota.hard_limit, + } else: quotas[quota.project_id]['created_at'] = earliest( quota.created_at, quotas[quota.project_id]['created_at']) -- cgit From c3300c29277423c28c5403d23b4a7f0a960f429d Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 16 Jun 2011 15:11:02 -0500 Subject: erroneous self in virtual_interface_delete_by_instance() sqlalchemy api --- nova/db/sqlalchemy/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index c882c587c..12044f23d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -942,7 +942,7 @@ def virtual_interface_delete_by_instance(context, instance_id): """ vif_refs = virtual_interface_get_by_instance(context, instance_id) for vif_ref in vif_refs: - self.virtual_interface_delete(vif_ref['id']) + virtual_interface_delete(vif_ref['id']) ################### -- cgit From d68f6de8d8275ec6dd9f231b9b52971f2ad15263 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Thu, 16 Jun 2011 16:33:29 -0400 Subject: Rename: intance_type_metadata -> instance_type_extra_specs --- nova/db/api.py | 24 +++---- nova/db/sqlalchemy/api.py | 77 +++++++++++----------- .../versions/021_add_instance_type_extra_specs.py | 67 +++++++++++++++++++ .../versions/021_add_instance_type_metadata.py | 67 ------------------- nova/db/sqlalchemy/models.py | 12 ++-- 5 files changed, 124 insertions(+), 123 deletions(-) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_extra_specs.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 5ade51f02..a1a434d3d 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1251,19 +1251,19 @@ def instance_metadata_update_or_create(context, instance_id, metadata): #################### -def instance_type_metadata_get(context, instance_type_id): - """Get all metadata for an instance type.""" - return IMPL.instance_type_metadata_get(context, instance_type_id) +def instance_type_extra_specs_get(context, instance_type_id): + """Get all extra specs for an instance type.""" + return IMPL.instance_type_extra_specs_get(context, instance_type_id) -def instance_type_metadata_delete(context, instance_type_id, key): - """Delete the given metadata item.""" - IMPL.instance_type_metadata_delete(context, instance_type_id, key) +def instance_type_extra_specs_delete(context, instance_type_id, key): + """Delete the given extra specs item.""" + IMPL.instance_type_extra_specs_delete(context, instance_type_id, key) -def instance_type_metadata_update_or_create(context, instance_type_id, - metadata): - """Create or update instance type metadata. This adds or modifies the - key/value pairs specified in the metadata dict argument""" - IMPL.instance_type_metadata_update_or_create(context, instance_type_id, - metadata) +def instance_type_extra_specs_update_or_create(context, instance_type_id, + extra_specs): + """Create or update instance type extra specs. This adds or modifies the + key/value pairs specified in the extra specs dict argument""" + IMPL.instance_type_extra_specs_update_or_create(context, instance_type_id, + extra_specs) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 0c7f31366..e586bbc64 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2456,22 +2456,22 @@ def console_get(context, console_id, instance_id=None): @require_admin_context def instance_type_create(_context, values): - """Create a new instance type. In order to pass in metadata, - the values dict should contain a 'meta' key/value pair: + """Create a new instance type. In order to pass in extra specs, + the values dict should contain a 'extra_specs' key/value pair: - {'meta' : {'k1': 'v1', 'k2': 'v2', ...}} + {'extra_specs' : {'k1': 'v1', 'k2': 'v2', ...}} """ try: - metadata = values.get('meta') - metadata_refs = [] - if metadata: - for k, v in metadata.iteritems(): - metadata_ref = models.InstanceTypeMetadata() - metadata_ref['key'] = k - metadata_ref['value'] = v - metadata_refs.append(metadata_ref) - values['meta'] = metadata_refs + specs = values.get('extra_specs') + specs_refs = [] + if specs: + for k, v in specs.iteritems(): + specs_ref = models.InstanceTypeExtraSpecs() + specs_ref['key'] = k + specs_ref['value'] = v + specs_refs.append(specs_ref) + values['extra_specs'] = specs_refs instance_type_ref = models.InstanceTypes() instance_type_ref.update(values) instance_type_ref.save() @@ -2697,24 +2697,24 @@ def instance_metadata_update_or_create(context, instance_id, metadata): @require_context -def instance_type_metadata_get(context, instance_type_id): +def instance_type_extra_specs_get(context, instance_type_id): session = get_session() - meta_results = session.query(models.InstanceTypeMetadata).\ + spec_results = session.query(models.InstanceTypeExtraSpecs).\ filter_by(instance_type_id=instance_type_id).\ filter_by(deleted=False).\ all() - meta_dict = {} - for i in meta_results: - meta_dict[i['key']] = i['value'] - return meta_dict + spec_dict = {} + for i in spec_results: + spec_dict[i['key']] = i['value'] + return spec_dict @require_context -def instance_type_metadata_delete(context, instance_type_id, key): +def instance_type_extra_specs_delete(context, instance_type_id, key): session = get_session() - session.query(models.InstanceTypeMetadata).\ + session.query(models.InstanceTypeExtraSpecs).\ filter_by(instance_type_id=instance_type_id).\ filter_by(key=key).\ filter_by(deleted=False).\ @@ -2724,36 +2724,37 @@ def instance_type_metadata_delete(context, instance_type_id, key): @require_context -def instance_type_metadata_get_item(context, instance_type_id, key): +def instance_type_extra_specs_get_item(context, instance_type_id, key): session = get_session() - meta_result = session.query(models.InstanceMetadata).\ + sppec_result = session.query(models.InstanceTypeExtraSpecs).\ filter_by(instance_type_id=instance_type_id).\ filter_by(key=key).\ filter_by(deleted=False).\ first() - if not meta_result: + if not spec_result: raise exception.\ - InstanceTypeMetadataNotFound(metadata_key=key, + InstanceTypeExtraSpecsNotFound(extra_specs_key=key, instance_type_id=instance_type_id) - return meta_result + return spec_result @require_context -def instance_type_metadata_update_or_create(context, instance_type_id, - metadata): +def instance_type_extra_specs_update_or_create(context, instance_type_id, + specs): session = get_session() - meta_ref = None - for key, value in metadata.iteritems(): + spec_ref = None + for key, value in specs.iteritems(): try: - meta_ref = instance_type_metadata_get_item(context, - instance_type_id, key, - session) + spec_ref = instance_type_extra_specs_get_item(context, + instance_type_id, + key, + session) except: - meta_ref = models.InstanceTypeMetadata() - meta_ref.update({"key": key, "value": value, - "instance_type_id": instance_type_id, - "deleted": 0}) - meta_ref.save(session=session) - return metadata + spec_ref = models.InstanceTypeExtraSpecs() + spec_ref.update({"key": key, "value": value, + "instance_type_id": instance_type_id, + "deleted": 0}) + spec_ref.save(session=session) + return specs diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_extra_specs.py b/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_extra_specs.py new file mode 100644 index 000000000..f26ad6d2c --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_extra_specs.py @@ -0,0 +1,67 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 University of Southern California +# +# 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 sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer +from sqlalchemy import MetaData, String, Table +from nova import log as logging + +meta = MetaData() + +# Just for the ForeignKey and column creation to succeed, these are not the +# actual definitions of instances or services. +instance_types = Table('instance_types', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + +# +# New Tables +# + +instance_type_extra_specs_table = Table('instance_type_extra_specs', meta, + Column('created_at', DateTime(timezone=False)), + Column('updated_at', DateTime(timezone=False)), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('instance_type_id', + Integer(), + ForeignKey('instance_types.id'), + nullable=False), + Column('key', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('value', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False))) + + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; + # bind migrate_engine to your metadata + meta.bind = migrate_engine + for table in (instance_type_extra_specs_table, ): + try: + table.create() + except Exception: + logging.info(repr(table)) + logging.exception('Exception while creating table') + raise + + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + for table in (instance_type_extra_specs_table, ): + table.drop() diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py b/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py deleted file mode 100644 index 22b741614..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/021_add_instance_type_metadata.py +++ /dev/null @@ -1,67 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2011 University of Southern California -# -# 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 sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer -from sqlalchemy import MetaData, String, Table -from nova import log as logging - -meta = MetaData() - -# Just for the ForeignKey and column creation to succeed, these are not the -# actual definitions of instances or services. -instance_types = Table('instance_types', meta, - Column('id', Integer(), primary_key=True, nullable=False), - ) - -# -# New Tables -# - -instance_type_metadata_table = Table('instance_type_metadata', meta, - Column('created_at', DateTime(timezone=False)), - Column('updated_at', DateTime(timezone=False)), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('instance_type_id', - Integer(), - ForeignKey('instance_types.id'), - nullable=False), - Column('key', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('value', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False))) - - -def upgrade(migrate_engine): - # Upgrade operations go here. Don't create your own engine; - # bind migrate_engine to your metadata - meta.bind = migrate_engine - for table in (instance_type_metadata_table, ): - try: - table.create() - except Exception: - logging.info(repr(table)) - logging.exception('Exception while creating table') - raise - - -def downgrade(migrate_engine): - # Operations to reverse the above upgrade go here. - for table in (instance_type_metadata_table, ): - table.drop() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index dbe448ef8..d4c217450 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -663,19 +663,19 @@ class InstanceMetadata(BASE, NovaBase): 'InstanceMetadata.deleted == False)') -class InstanceTypeMetadata(BASE, NovaBase): - """Represents a metadata key/value pair for an instance_type""" - __tablename__ = 'instance_type_metadata' +class InstanceTypeExtraSpecs(BASE, NovaBase): + """Represents additional specs as key/value pairs for an instance_type""" + __tablename__ = 'instance_type_extra_specs' id = Column(Integer, primary_key=True) key = Column(String(255)) value = Column(String(255)) instance_type_id = Column(Integer, ForeignKey('instance_types.id'), nullable=False) - instance_type = relationship(InstanceTypes, backref="meta", + instance_type = relationship(InstanceTypes, backref="extra_specs", foreign_keys=instance_type_id, primaryjoin='and_(' - 'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,' - 'InstanceTypeMetadata.deleted == False)') + 'InstanceTypeExtraSpecs.instance_type_id == InstanceTypes.id,' + 'InstanceTypeExtraSpecs.deleted == False)') class Zone(BASE, NovaBase): -- cgit From 060fd3921e876dcbd594270871ddaeee749259be Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 17 Jun 2011 09:19:55 -0400 Subject: Missed a InstanceTypeMetadata -> InstanceTypeExtraSpecs rename in register_models --- nova/db/sqlalchemy/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 52de9298a..96f85b4e5 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -701,7 +701,7 @@ def register_models(): Network, SecurityGroup, SecurityGroupIngressRule, SecurityGroupInstanceAssociation, AuthToken, User, Project, Certificate, ConsolePool, Console, Zone, - InstanceMetadata, InstanceTypeMetadata, Migration) + InstanceMetadata, InstanceTypeExtraSpecs, Migration) engine = create_engine(FLAGS.sql_connection, echo=False) for model in models: model.metadata.create_all(engine) -- cgit From 749eac4d36ff2f7a855044d677f3cde07451f32a Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 17 Jun 2011 13:47:28 -0500 Subject: bunch of docstring changes --- nova/db/api.py | 18 ++++++++--------- nova/db/sqlalchemy/api.py | 47 +++++++++++++++++++------------------------- nova/db/sqlalchemy/models.py | 2 +- 3 files changed, 30 insertions(+), 37 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 64b6a893e..b625a0b0f 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -406,50 +406,50 @@ def fixed_ip_update(context, address, values): def virtual_interface_create(context, values): - """create a virtual interface record in the database""" + """Create a virtual interface record in the database.""" return IMPL.virtual_interface_create(context, values) def virtual_interface_get(context, vif_id): - """gets a virtual interface from the table""" + """Gets a virtual interface from the table,""" return IMPL.virtual_interface_get(context, vif_id) def virtual_interface_get_by_address(context, address): - """gets a virtual interface from the table filtering on address""" + """Gets a virtual interface from the table filtering on address.""" return IMPL.virtual_interface_get_by_address(context, address) def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): - """gets the virtual interface fixed_ip is associated with""" + """Gets the virtual interface fixed_ip is associated with.""" return IMPL.virtual_interface_get_by_fixed_ip(context, fixed_ip_id) def virtual_interface_get_by_instance(context, instance_id): - """gets all virtual_interfaces for instance""" + """Gets all virtual_interfaces for instance.""" return IMPL.virtual_interface_get_by_instance(context, instance_id) def virtual_interface_get_by_instance_and_network(context, instance_id, network_id): - """gets all virtual interfaces for instance""" + """Gets all virtual interfaces for instance.""" return IMPL.virtual_interface_get_by_instance_and_network(context, instance_id, network_id) def virtual_interface_get_by_network(context, network_id): - """gets all virtual interfaces on network""" + """Gets all virtual interfaces on network.""" return IMPL.virtual_interface_get_by_network(context, network_id) def virtual_interface_delete(context, vif_id): - """delete virtual interface record from the database""" + """Delete virtual interface record from the database.""" return IMPL.virtual_interface_delete(context, vif_id) def virtual_interface_delete_by_instance(context, instance_id): - """delete virtual interface records associated with instance """ + """Delete virtual interface records associated with instance.""" return IMPL.virtual_interface_delete_by_instance(context, instance_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 12044f23d..8d12e25c0 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -804,10 +804,9 @@ def fixed_ip_update(context, address, values): @require_context def virtual_interface_create(context, values): - """create a new virtual interface record in teh database + """Create a new virtual interface record in teh database. - context = request context object - values = dict containing column values + :param values: = dict containing column values """ vif_ref = models.VirtualInterface() vif_ref.update(values) @@ -818,10 +817,9 @@ def virtual_interface_create(context, values): @require_context def virtual_interface_get(context, vif_id): - """gets a virtual interface from the table + """Gets a virtual interface from the table. - context = request context object - vif_id = id of the virtual interface + :param vif_id: = id of the virtual interface """ session = get_session() vif_ref = session.query(models.VirtualInterface).\ @@ -835,10 +833,9 @@ def virtual_interface_get(context, vif_id): @require_context def virtual_interface_get_by_address(context, address): - """gets a virtual interface from the table + """Gets a virtual interface from the table. - context = request context object - address = the address of the interface you're looking to get + :param address: = the address of the interface you're looking to get """ session = get_session() vif_ref = session.query(models.VirtualInterface).\ @@ -852,10 +849,9 @@ def virtual_interface_get_by_address(context, address): @require_context def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): - """gets the virtual interface fixed_ip is associated with + """Gets the virtual interface fixed_ip is associated with. - context = request context object - fixed_ip_id = id of the fixed_ip + :param fixed_ip_id: = id of the fixed_ip """ session = get_session() vif_ref = session.query(models.VirtualInterface).\ @@ -869,10 +865,9 @@ def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): @require_context def virtual_interface_get_by_instance(context, instance_id): - """gets all virtual interfaces for instance + """Gets all virtual interfaces for instance. - context = request context object - instance_id = id of the instance to retreive vifs for + :param instance_id: = id of the instance to retreive vifs for """ session = get_session() vif_refs = session.query(models.VirtualInterface).\ @@ -887,7 +882,7 @@ def virtual_interface_get_by_instance(context, instance_id): @require_context def virtual_interface_get_by_instance_and_network(context, instance_id, network_id): - """gets virtual interface for instance that's associated with network""" + """Gets virtual interface for instance that's associated with network.""" session = get_session() vif_ref = session.query(models.VirtualInterface).\ filter_by(instance_id=instance_id).\ @@ -901,10 +896,9 @@ def virtual_interface_get_by_instance_and_network(context, instance_id, @require_admin_context def virtual_interface_get_by_network(context, network_id): - """gets all virtual_interface on network + """Gets all virtual_interface on network. - context = request context object - network_id = network to retreive vifs for + :param network_id: = network to retreive vifs for """ session = get_session() vif_refs = session.query(models.VirtualInterface).\ @@ -918,10 +912,9 @@ def virtual_interface_get_by_network(context, network_id): @require_context def virtual_interface_delete(context, vif_id): - """delete virtual interface record from teh database + """Delete virtual interface record from teh database. - context = request context object - vif_id = id of vif to delete + :param vif_id: = id of vif to delete """ vif_ref = virtual_interface_get(context, vif_id) session = get_session() @@ -934,11 +927,10 @@ def virtual_interface_delete(context, vif_id): @require_context def virtual_interface_delete_by_instance(context, instance_id): - """delete virtual interface records that are associated - with the instance given by instance_id + """Delete virtual interface records that are associated + with the instance given by instance_id. - context = request context object - instance_id = id of instance + :param instance_id: = id of instance """ vif_refs = virtual_interface_get_by_instance(context, instance_id) for vif_ref in vif_refs: @@ -1366,7 +1358,8 @@ def key_pair_get_all_by_user(context, user_id): @require_admin_context def network_associate(context, project_id, force=False): - """associate a project with a network + """Associate a project with a network. + called by project_get_networks under certain conditions and network manager add_network_to_project() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index ddf068565..300a75ce0 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -512,7 +512,7 @@ class Network(BASE, NovaBase): class VirtualInterface(BASE, NovaBase): - """Represents a virtual interface on an instance""" + """Represents a virtual interface on an instance.""" __tablename__ = 'virtual_interfaces' id = Column(Integer, primary_key=True) address = Column(String(255), unique=True) -- cgit From 89ad3e4f219ff5e8f60624560e9a3ce3762040d5 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 17 Jun 2011 18:38:35 -0500 Subject: updated fixed ip and floating ip exceptions --- nova/db/sqlalchemy/api.py | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 8d12e25c0..2e18bdca9 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -446,7 +446,7 @@ def floating_ip_allocate_address(context, project_id): # NOTE(vish): if with_lockmode isn't supported, as in sqlite, # then this has concurrency issues if not floating_ip_ref: - raise db.NoMoreAddresses() + raise exception.NoMoreFloatingIps() floating_ip_ref['project_id'] = project_id session.add(floating_ip_ref) return floating_ip_ref['address'] @@ -545,20 +545,26 @@ def floating_ip_set_auto_assigned(context, address): @require_admin_context def floating_ip_get_all(context): session = get_session() - return session.query(models.FloatingIp).\ - options(joinedload_all('fixed_ip.instance')).\ - filter_by(deleted=False).\ - all() + floating_ip_refs = session.query(models.FloatingIp).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(deleted=False).\ + all() + if not floating_ip_refs: + raise exception.NoFloatingIpsDefined() + return floating_ip_refs @require_admin_context def floating_ip_get_all_by_host(context, host): session = get_session() - return session.query(models.FloatingIp).\ - options(joinedload_all('fixed_ip.instance')).\ - filter_by(host=host).\ - filter_by(deleted=False).\ - all() + floating_ip_refs = session.query(models.FloatingIp).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(host=host).\ + filter_by(deleted=False).\ + all() + if not floating_ip_refs: + raise exception.NoFloatingIpsDefinedForHost(host=host) + return floating_ip_refs @require_context @@ -566,12 +572,15 @@ def floating_ip_get_all_by_project(context, project_id): authorize_project_context(context, project_id) session = get_session() # TODO(tr3buchet): why do we not want auto_assigned floating IPs here? - return session.query(models.FloatingIp).\ - options(joinedload_all('fixed_ip.instance')).\ - filter_by(project_id=project_id).\ - filter_by(auto_assigned=False).\ - filter_by(deleted=False).\ - all() + floating_ip_refs = session.query(models.FloatingIp).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(project_id=project_id).\ + filter_by(auto_assigned=False).\ + filter_by(deleted=False).\ + all() + if not floating_ip_refs: + raise exception.NoFloatingIpFoundForProject(project_id=project_id) + return floating_ip_refs @require_context @@ -587,7 +596,6 @@ def floating_ip_get_by_address(context, address, session=None): first() if not result: raise exception.FloatingIpNotFound(address=address) - return result -- cgit From 1acb699a6fb0ea7a7d84ba4598790d7c9d7abd14 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Mon, 20 Jun 2011 07:45:21 -0700 Subject: working commit --- nova/db/sqlalchemy/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 239f6e96a..f28fb0778 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -21,7 +21,7 @@ SQLAlchemy models for nova data. from sqlalchemy.orm import relationship, backref, object_mapper from sqlalchemy import Column, Integer, String, schema -from sqlalchemy import ForeignKey, DateTime, Boolean, Text +from sqlalchemy import ForeignKey, DateTime, Boolean, Text, Float from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.schema import ForeignKeyConstraint @@ -670,6 +670,8 @@ class Zone(BASE, NovaBase): api_url = Column(String(255)) username = Column(String(255)) password = Column(String(255)) + weight_offset = Column(Float(), default=0.0) + weight_scale = Column(Float(), default=1.0) def register_models(): -- cgit From 0502a2b35fb1a4424e7249cb9f39d7fc98bf37b5 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Mon, 20 Jun 2011 11:56:15 -0500 Subject: updated the exceptions around virtual interface creation, updated flatDHCP manager comment --- nova/db/sqlalchemy/api.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index f86180b1f..3cb35b649 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -816,9 +816,12 @@ def virtual_interface_create(context, values): :param values: = dict containing column values """ - vif_ref = models.VirtualInterface() - vif_ref.update(values) - vif_ref.save() + try: + vif_ref = models.VirtualInterface() + vif_ref.update(values) + vif_ref.save() + except IntegrityError: + raise exception.VirtualInterfaceCreateException() return vif_ref -- cgit From 0ebfe3121c9abc00e0cb749dcc0f4b3dc5cbacb6 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Tue, 21 Jun 2011 11:09:54 -0500 Subject: some libvirt multi-nic just to get it to work, from tushar --- nova/db/sqlalchemy/api.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 3cb35b649..0187f7bc6 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -827,12 +827,14 @@ def virtual_interface_create(context, values): @require_context -def virtual_interface_get(context, vif_id): +def virtual_interface_get(context, vif_id, session=None): """Gets a virtual interface from the table. :param vif_id: = id of the virtual interface """ - session = get_session() + if not session: + session = get_session() + vif_ref = session.query(models.VirtualInterface).\ filter_by(id=vif_id).\ options(joinedload('network')).\ @@ -927,8 +929,8 @@ def virtual_interface_delete(context, vif_id): :param vif_id: = id of vif to delete """ - vif_ref = virtual_interface_get(context, vif_id) session = get_session() + vif_ref = virtual_interface_get(context, vif_id, session) with session.begin(): # disassociate any fixed_ips from this interface for fixed_ip in vif_ref['fixed_ips']: @@ -945,7 +947,7 @@ def virtual_interface_delete_by_instance(context, instance_id): """ vif_refs = virtual_interface_get_by_instance(context, instance_id) for vif_ref in vif_refs: - virtual_interface_delete(vif_ref['id']) + virtual_interface_delete(context, vif_ref['id']) ################### -- cgit From 796d3b67dcdb2670714abf9e02b278bd6898358b Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Tue, 21 Jun 2011 11:41:53 -0500 Subject: renamed migrations again --- .../migrate_repo/versions/023_multi_nic.py | 130 --------------------- .../024_fk_fixed_ips_virtual_interface_id.py | 56 --------- .../migrate_repo/versions/024_sqlite_downgrade.sql | 48 -------- .../migrate_repo/versions/024_sqlite_upgrade.sql | 48 -------- .../migrate_repo/versions/026_multi_nic.py | 130 +++++++++++++++++++++ .../027_fk_fixed_ips_virtual_interface_id.py | 56 +++++++++ .../migrate_repo/versions/027_sqlite_downgrade.sql | 48 ++++++++ .../migrate_repo/versions/027_sqlite_upgrade.sql | 48 ++++++++ 8 files changed, 282 insertions(+), 282 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py deleted file mode 100644 index 85ab1fdd8..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/023_multi_nic.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - -# virtual interface table to add to DB -virtual_interfaces = Table('virtual_interfaces', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - Column('port_id', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - mysql_engine='InnoDB') - - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False)) - - -# virtual interface id column to add to fixed_ips table -# foreignkey added in next migration -virtual_interface_id = Column('virtual_interface_id', - Integer()) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception: - logging.error(_("interface column not added to networks table")) - raise - - # create virtual_interfaces table - try: - virtual_interfaces.create() - except Exception: - logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) - raise - - # add virtual_interface_id column to fixed_ips table - try: - fixed_ips.create_column(virtual_interface_id) - except Exception: - logging.error(_("VIF column not added to fixed_ips table")) - raise - - # populate the virtual_interfaces table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = virtual_interfaces.insert() - i.execute(join_list) - - # populate the fixed_ips virtual_interface_id column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([virtual_interfaces.c.id].\ - where(virtual_interfaces.c.instance_id == row['instance_id'])).\ - as_scalar() - u = fixed_ips.update().values(virtual_interface_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py deleted file mode 100644 index 56e927717..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_fk_fixed_ips_virtual_interface_id.py +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # grab tables - fixed_ips = Table('fixed_ips', meta, autoload=True) - virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) - - # add foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).create() - except Exception: - logging.error(_("foreign key constraint couldn't be added")) - raise - - -def downgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # drop foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).drop() - except Exception: - logging.error(_("foreign key constraint couldn't be dropped")) - raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql deleted file mode 100644 index c1d26b180..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_downgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql deleted file mode 100644 index 2a9362545..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_sqlite_upgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py new file mode 100644 index 000000000..85ab1fdd8 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py @@ -0,0 +1,130 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + +# virtual interface table to add to DB +virtual_interfaces = Table('virtual_interfaces', meta, + Column('created_at', DateTime(timezone=False), + default=utils.utcnow()), + Column('updated_at', DateTime(timezone=False), + onupdate=utils.utcnow()), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + Column('port_id', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + mysql_engine='InnoDB') + + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False)) + + +# virtual interface id column to add to fixed_ips table +# foreignkey added in next migration +virtual_interface_id = Column('virtual_interface_id', + Integer()) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception: + logging.error(_("interface column not added to networks table")) + raise + + # create virtual_interfaces table + try: + virtual_interfaces.create() + except Exception: + logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) + raise + + # add virtual_interface_id column to fixed_ips table + try: + fixed_ips.create_column(virtual_interface_id) + except Exception: + logging.error(_("VIF column not added to fixed_ips table")) + raise + + # populate the virtual_interfaces table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = virtual_interfaces.insert() + i.execute(join_list) + + # populate the fixed_ips virtual_interface_id column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([virtual_interfaces.c.id].\ + where(virtual_interfaces.c.instance_id == row['instance_id'])).\ + as_scalar() + u = fixed_ips.update().values(virtual_interface_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py new file mode 100644 index 000000000..56e927717 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py @@ -0,0 +1,56 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # grab tables + fixed_ips = Table('fixed_ips', meta, autoload=True) + virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) + + # add foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).create() + except Exception: + logging.error(_("foreign key constraint couldn't be added")) + raise + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # drop foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).drop() + except Exception: + logging.error(_("foreign key constraint couldn't be dropped")) + raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql new file mode 100644 index 000000000..c1d26b180 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql new file mode 100644 index 000000000..2a9362545 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; -- cgit From ba6eb76a2ca16132d1fff4993e461fb7830b06af Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Tue, 21 Jun 2011 12:03:01 -0500 Subject: omg stop making new migrations... --- .../migrate_repo/versions/026_multi_nic.py | 130 --------------------- .../027_fk_fixed_ips_virtual_interface_id.py | 56 --------- .../migrate_repo/versions/027_multi_nic.py | 130 +++++++++++++++++++++ .../migrate_repo/versions/027_sqlite_downgrade.sql | 48 -------- .../migrate_repo/versions/027_sqlite_upgrade.sql | 48 -------- .../028_fk_fixed_ips_virtual_interface_id.py | 56 +++++++++ .../migrate_repo/versions/028_sqlite_downgrade.sql | 48 ++++++++ .../migrate_repo/versions/028_sqlite_upgrade.sql | 48 ++++++++ 8 files changed, 282 insertions(+), 282 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py deleted file mode 100644 index 85ab1fdd8..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/026_multi_nic.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - -# virtual interface table to add to DB -virtual_interfaces = Table('virtual_interfaces', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - Column('port_id', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - mysql_engine='InnoDB') - - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False)) - - -# virtual interface id column to add to fixed_ips table -# foreignkey added in next migration -virtual_interface_id = Column('virtual_interface_id', - Integer()) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception: - logging.error(_("interface column not added to networks table")) - raise - - # create virtual_interfaces table - try: - virtual_interfaces.create() - except Exception: - logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) - raise - - # add virtual_interface_id column to fixed_ips table - try: - fixed_ips.create_column(virtual_interface_id) - except Exception: - logging.error(_("VIF column not added to fixed_ips table")) - raise - - # populate the virtual_interfaces table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = virtual_interfaces.insert() - i.execute(join_list) - - # populate the fixed_ips virtual_interface_id column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([virtual_interfaces.c.id].\ - where(virtual_interfaces.c.instance_id == row['instance_id'])).\ - as_scalar() - u = fixed_ips.update().values(virtual_interface_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py deleted file mode 100644 index 56e927717..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_fk_fixed_ips_virtual_interface_id.py +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # grab tables - fixed_ips = Table('fixed_ips', meta, autoload=True) - virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) - - # add foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).create() - except Exception: - logging.error(_("foreign key constraint couldn't be added")) - raise - - -def downgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # drop foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).drop() - except Exception: - logging.error(_("foreign key constraint couldn't be dropped")) - raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py new file mode 100644 index 000000000..85ab1fdd8 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py @@ -0,0 +1,130 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + +# virtual interface table to add to DB +virtual_interfaces = Table('virtual_interfaces', meta, + Column('created_at', DateTime(timezone=False), + default=utils.utcnow()), + Column('updated_at', DateTime(timezone=False), + onupdate=utils.utcnow()), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + Column('port_id', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + mysql_engine='InnoDB') + + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False)) + + +# virtual interface id column to add to fixed_ips table +# foreignkey added in next migration +virtual_interface_id = Column('virtual_interface_id', + Integer()) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception: + logging.error(_("interface column not added to networks table")) + raise + + # create virtual_interfaces table + try: + virtual_interfaces.create() + except Exception: + logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) + raise + + # add virtual_interface_id column to fixed_ips table + try: + fixed_ips.create_column(virtual_interface_id) + except Exception: + logging.error(_("VIF column not added to fixed_ips table")) + raise + + # populate the virtual_interfaces table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = virtual_interfaces.insert() + i.execute(join_list) + + # populate the fixed_ips virtual_interface_id column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([virtual_interfaces.c.id].\ + where(virtual_interfaces.c.instance_id == row['instance_id'])).\ + as_scalar() + u = fixed_ips.update().values(virtual_interface_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql deleted file mode 100644 index c1d26b180..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_downgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql deleted file mode 100644 index 2a9362545..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_sqlite_upgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py new file mode 100644 index 000000000..56e927717 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py @@ -0,0 +1,56 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # grab tables + fixed_ips = Table('fixed_ips', meta, autoload=True) + virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) + + # add foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).create() + except Exception: + logging.error(_("foreign key constraint couldn't be added")) + raise + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # drop foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).drop() + except Exception: + logging.error(_("foreign key constraint couldn't be dropped")) + raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql new file mode 100644 index 000000000..c1d26b180 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql new file mode 100644 index 000000000..2a9362545 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; -- cgit From a1ee8e591e157a23390b1622b9c313da08ae9130 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Tue, 21 Jun 2011 12:11:16 -0700 Subject: fixed zone update --- nova/db/api.py | 2 +- nova/db/sqlalchemy/api.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 5fd081ca8..2333e4caa 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1253,7 +1253,7 @@ def zone_create(context, values): def zone_update(context, zone_id, values): """Update a child Zone entry.""" - return IMPL.zone_update(context, values) + return IMPL.zone_update(context, zone_id, values) def zone_delete(context, zone_id): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index a7e5125d5..64d67b17a 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2675,7 +2675,7 @@ def zone_update(context, zone_id, values): if not zone: raise exception.ZoneNotFound(zone_id=zone_id) zone.update(values) - zone.save() + zone.save(session=session) return zone -- cgit From 06c9a7454cc310ddcc059d685b43d75c5167a26b Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 22 Jun 2011 16:33:06 -0500 Subject: fixed ip gets now have floating IPs correctly loaded --- nova/db/sqlalchemy/api.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 805054951..31ddeaaad 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -702,7 +702,9 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time): def fixed_ip_get_all(context, session=None): if not session: session = get_session() - result = session.query(models.FixedIp).all() + result = session.query(models.FixedIp).\ + options(joinedload('floating_ips')).\ + all() if not result: raise exception.NoFixedIpsDefined() @@ -714,10 +716,11 @@ def fixed_ip_get_all_by_host(context, host=None): session = get_session() result = session.query(models.FixedIp).\ - join(models.FixedIp.instance).\ - filter_by(state=1).\ - filter_by(host=host).\ - all() + options(joinedload('floating_ips')).\ + join(models.FixedIp.instance).\ + filter_by(state=1).\ + filter_by(host=host).\ + all() if not result: raise exception.NoFixedIpsDefinedForHost(host=host) @@ -732,6 +735,7 @@ def fixed_ip_get_by_address(context, address, session=None): result = session.query(models.FixedIp).\ filter_by(address=address).\ filter_by(deleted=can_read_deleted(context)).\ + options(joinedload('floating_ips')).\ options(joinedload('network')).\ options(joinedload('instance')).\ first() @@ -744,16 +748,11 @@ def fixed_ip_get_by_address(context, address, session=None): return result -@require_context -def fixed_ip_get_instance(context, address): - fixed_ip_ref = fixed_ip_get_by_address(context, address) - return fixed_ip_ref.instance - - @require_context def fixed_ip_get_by_instance(context, instance_id): session = get_session() rv = session.query(models.FixedIp).\ + options(joinedload('floating_ips')).\ filter_by(instance_id=instance_id).\ filter_by(deleted=False).\ all() @@ -766,6 +765,7 @@ def fixed_ip_get_by_instance(context, instance_id): def fixed_ip_get_by_virtual_interface(context, vif_id): session = get_session() rv = session.query(models.FixedIp).\ + options(joinedload('floating_ips')).\ filter_by(virtual_interface_id=vif_id).\ filter_by(deleted=False).\ all() @@ -774,6 +774,12 @@ def fixed_ip_get_by_virtual_interface(context, vif_id): return rv +@require_context +def fixed_ip_get_instance(context, address): + fixed_ip_ref = fixed_ip_get_by_address(context, address) + return fixed_ip_ref.instance + + @require_context def fixed_ip_get_instance_v6(context, address): session = get_session() -- cgit From 614ab3d0e68a7998d77da1f39d1fe9bd5b080972 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 22 Jun 2011 16:54:44 -0500 Subject: added fixed ip filtering by null virtual interface_id to network get associated fixed ips --- nova/db/sqlalchemy/api.py | 1 + 1 file changed, 1 insertion(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 31ddeaaad..3cc9bbd91 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1572,6 +1572,7 @@ def network_get_associated_fixed_ips(context, network_id): options(joinedload_all('instance')).\ filter_by(network_id=network_id).\ filter(models.FixedIp.instance_id != None).\ + filter(models.FixedIp.virtual_interface_id != None).\ filter_by(deleted=False).\ all() -- cgit From 0bb41eff943b9bb5ba197dc137c3afd93c544398 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 22 Jun 2011 17:45:07 -0500 Subject: added virtual_interface_update method --- nova/db/api.py | 5 +++++ nova/db/sqlalchemy/api.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 8a12d7d63..4d036ac57 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -410,6 +410,11 @@ def virtual_interface_create(context, values): return IMPL.virtual_interface_create(context, values) +def virtual_interface_update(context, vif_id, values): + """Update a virtual interface record in the database.""" + return IMPL.virtual_interface_update(context, vif_id, values) + + def virtual_interface_get(context, vif_id): """Gets a virtual interface from the table,""" return IMPL.virtual_interface_get(context, vif_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 3cc9bbd91..e8cd3fd89 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -832,6 +832,21 @@ def virtual_interface_create(context, values): return vif_ref +@require_context +def virtual_interface_update(context, vif_id, values): + """Update a virtual interface record in the database. + + :param vif_id: = id of virtual interface to update + :param values: = values to update + """ + session = get_session() + with session.begin(): + vif_ref = virtual_interface_get(context, vif_id, session=session) + vif_ref.update(values) + vif_ref.save(session=session) + return vif_ref + + @require_context def virtual_interface_get(context, vif_id, session=None): """Gets a virtual interface from the table. -- cgit From 2e2c432e35bf9c283df83de8d76191855d2ce2be Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Wed, 22 Jun 2011 23:38:58 -0400 Subject: Returned code to original location --- nova/db/api.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 0c9f45db6..0ac3153bc 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1292,6 +1292,17 @@ def instance_metadata_update_or_create(context, instance_id, metadata): #################### +def agent_build_create(context, values): + """Create a new agent build entry.""" + return IMPL.agent_build_create(context, values) + + +def agent_build_get_by_triple(context, hypervisor, os, architecture): + """Get agent build by hypervisor/OS/architecture triple.""" + return IMPL.agent_build_get_by_triple(context, hypervisor, os, + architecture) + + def agent_build_get_all(context): """Get all agent builds.""" return IMPL.agent_build_get_all(context) @@ -1307,16 +1318,6 @@ def agent_build_update(context, agent_build_id, values): IMPL.agent_build_update(context, agent_build_id, values) -def agent_build_create(context, values): - """Create a new agent build entry.""" - return IMPL.agent_build_create(context, values) - - -def agent_build_get_by_triple(context, hypervisor, os, architecture): - """Get agent build by hypervisor/OS/architecture triple.""" - return IMPL.agent_build_get_by_triple(context, hypervisor, os, - architecture) - #################### -- cgit From c33fc283c4f75b4de745484b53a818795ad80d96 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 23 Jun 2011 17:39:40 -0500 Subject: updated the way vifs/fixed_ips are deallocated and their relationships, altered lease/release fixed_ip --- nova/db/sqlalchemy/api.py | 5 ----- nova/db/sqlalchemy/models.py | 3 +++ 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e8cd3fd89..ce8e9f39b 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -675,7 +675,6 @@ def fixed_ip_disassociate(context, address): address, session=session) fixed_ip_ref.instance = None - fixed_ip_ref.virtual_interface = None fixed_ip_ref.save(session=session) @@ -691,7 +690,6 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time): filter(models.FixedIp.instance_id != None).\ filter_by(allocated=0).\ update({'instance_id': None, - 'virtual_interface_id': None, 'leased': 0, 'updated_at': utils.utcnow()}, synchronize_session='fetch') @@ -953,9 +951,6 @@ def virtual_interface_delete(context, vif_id): session = get_session() vif_ref = virtual_interface_get(context, vif_id, session) with session.begin(): - # disassociate any fixed_ips from this interface - for fixed_ip in vif_ref['fixed_ips']: - fixed_ip.virtual_interface = None session.delete(vif_ref) diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 9e6d6472c..250e88572 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -585,7 +585,10 @@ class FixedIp(BASE, NovaBase): primaryjoin='and_(' 'FixedIp.instance_id == Instance.id,' 'FixedIp.deleted == False)') + # associated means that a fixed_ip has its instance_id column set + # allocated means that a fixed_ip has a its virtual_interface_id column set allocated = Column(Boolean, default=False) + # leased means dhcp bridge has leased the ip leased = Column(Boolean, default=False) reserved = Column(Boolean, default=False) -- cgit From 9df94a774f6f784563e87c3d1a864256c1f34eee Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Thu, 23 Jun 2011 18:13:39 -0500 Subject: freakin migration numbering --- .../migrate_repo/versions/027_multi_nic.py | 130 --------------------- .../028_fk_fixed_ips_virtual_interface_id.py | 56 --------- .../migrate_repo/versions/028_multi_nic.py | 130 +++++++++++++++++++++ .../migrate_repo/versions/028_sqlite_downgrade.sql | 48 -------- .../migrate_repo/versions/028_sqlite_upgrade.sql | 48 -------- .../029_fk_fixed_ips_virtual_interface_id.py | 56 +++++++++ .../migrate_repo/versions/029_sqlite_downgrade.sql | 48 ++++++++ .../migrate_repo/versions/029_sqlite_update.sql | 48 ++++++++ 8 files changed, 282 insertions(+), 282 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py deleted file mode 100644 index 85ab1fdd8..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_multi_nic.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - -# virtual interface table to add to DB -virtual_interfaces = Table('virtual_interfaces', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - Column('port_id', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - mysql_engine='InnoDB') - - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False)) - - -# virtual interface id column to add to fixed_ips table -# foreignkey added in next migration -virtual_interface_id = Column('virtual_interface_id', - Integer()) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception: - logging.error(_("interface column not added to networks table")) - raise - - # create virtual_interfaces table - try: - virtual_interfaces.create() - except Exception: - logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) - raise - - # add virtual_interface_id column to fixed_ips table - try: - fixed_ips.create_column(virtual_interface_id) - except Exception: - logging.error(_("VIF column not added to fixed_ips table")) - raise - - # populate the virtual_interfaces table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = virtual_interfaces.insert() - i.execute(join_list) - - # populate the fixed_ips virtual_interface_id column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([virtual_interfaces.c.id].\ - where(virtual_interfaces.c.instance_id == row['instance_id'])).\ - as_scalar() - u = fixed_ips.update().values(virtual_interface_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py deleted file mode 100644 index 56e927717..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/028_fk_fixed_ips_virtual_interface_id.py +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # grab tables - fixed_ips = Table('fixed_ips', meta, autoload=True) - virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) - - # add foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).create() - except Exception: - logging.error(_("foreign key constraint couldn't be added")) - raise - - -def downgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # drop foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).drop() - except Exception: - logging.error(_("foreign key constraint couldn't be dropped")) - raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py new file mode 100644 index 000000000..85ab1fdd8 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py @@ -0,0 +1,130 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + +# virtual interface table to add to DB +virtual_interfaces = Table('virtual_interfaces', meta, + Column('created_at', DateTime(timezone=False), + default=utils.utcnow()), + Column('updated_at', DateTime(timezone=False), + onupdate=utils.utcnow()), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + Column('port_id', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + mysql_engine='InnoDB') + + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False)) + + +# virtual interface id column to add to fixed_ips table +# foreignkey added in next migration +virtual_interface_id = Column('virtual_interface_id', + Integer()) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception: + logging.error(_("interface column not added to networks table")) + raise + + # create virtual_interfaces table + try: + virtual_interfaces.create() + except Exception: + logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) + raise + + # add virtual_interface_id column to fixed_ips table + try: + fixed_ips.create_column(virtual_interface_id) + except Exception: + logging.error(_("VIF column not added to fixed_ips table")) + raise + + # populate the virtual_interfaces table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = virtual_interfaces.insert() + i.execute(join_list) + + # populate the fixed_ips virtual_interface_id column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([virtual_interfaces.c.id].\ + where(virtual_interfaces.c.instance_id == row['instance_id'])).\ + as_scalar() + u = fixed_ips.update().values(virtual_interface_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql deleted file mode 100644 index c1d26b180..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_downgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql deleted file mode 100644 index 2a9362545..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/028_sqlite_upgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py new file mode 100644 index 000000000..56e927717 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py @@ -0,0 +1,56 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # grab tables + fixed_ips = Table('fixed_ips', meta, autoload=True) + virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) + + # add foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).create() + except Exception: + logging.error(_("foreign key constraint couldn't be added")) + raise + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # drop foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).drop() + except Exception: + logging.error(_("foreign key constraint couldn't be dropped")) + raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql new file mode 100644 index 000000000..c1d26b180 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql b/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql new file mode 100644 index 000000000..2a9362545 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; -- cgit From 7f578a0f657c076bf97c33dca15f1c78bd11b607 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Thu, 23 Jun 2011 22:55:51 -0400 Subject: Now automatically populates the instance_type dict with extra_specs upon being retrieved from the database. --- nova/db/api.py | 1 - nova/db/sqlalchemy/api.py | 26 ++++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 0ac3153bc..636748168 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1318,7 +1318,6 @@ def agent_build_update(context, agent_build_id, values): IMPL.agent_build_update(context, agent_build_id, values) - #################### diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 0692bc917..86950f109 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2581,6 +2581,18 @@ def instance_type_create(_context, values): return instance_type_ref +def _inst_type_query_to_dict(inst_type_query): + """Takes an instance type query returned by sqlalchemy + and returns it as a dictionary. + """ + extra_specs_objs = inst_type_query['extra_specs'] + extra_specs = dict([(x['key'], x['value']) for x in \ + inst_type_query['extra_specs']]) + inst_type_dict = dict(inst_type_query) + inst_type_dict['extra_specs'] = extra_specs + return inst_type_dict + + @require_context def instance_type_get_all(context, inactive=False): """ @@ -2589,17 +2601,19 @@ def instance_type_get_all(context, inactive=False): session = get_session() if inactive: inst_types = session.query(models.InstanceTypes).\ + options(joinedload('extra_specs')).\ order_by("name").\ all() else: inst_types = session.query(models.InstanceTypes).\ + options(joinedload('extra_specs')).\ filter_by(deleted=False).\ order_by("name").\ all() if inst_types: inst_dict = {} for i in inst_types: - inst_dict[i['name']] = dict(i) + inst_dict[i['name']] = _inst_type_query_to_dict(i) return inst_dict else: raise exception.NoInstanceTypesFound() @@ -2610,12 +2624,14 @@ def instance_type_get_by_id(context, id): """Returns a dict describing specific instance_type""" session = get_session() inst_type = session.query(models.InstanceTypes).\ + options(joinedload('extra_specs')).\ filter_by(id=id).\ first() + if not inst_type: raise exception.InstanceTypeNotFound(instance_type=id) else: - return dict(inst_type) + return _inst_type_query_to_dict(inst_type) @require_context @@ -2623,12 +2639,13 @@ def instance_type_get_by_name(context, name): """Returns a dict describing specific instance_type""" session = get_session() inst_type = session.query(models.InstanceTypes).\ + options(joinedload('extra_specs')).\ filter_by(name=name).\ first() if not inst_type: raise exception.InstanceTypeNotFoundByName(instance_type_name=name) else: - return dict(inst_type) + return _inst_type_query_to_dict(inst_type) @require_context @@ -2636,12 +2653,13 @@ def instance_type_get_by_flavor_id(context, id): """Returns a dict describing specific flavor_id""" session = get_session() inst_type = session.query(models.InstanceTypes).\ + options(joinedload('extra_specs')).\ filter_by(flavorid=int(id)).\ first() if not inst_type: raise exception.FlavorNotFound(flavor_id=id) else: - return dict(inst_type) + return _inst_type_query_to_dict(inst_type) @require_admin_context -- cgit From cd54be394d9b0807b68579b4630bf4c48738c506 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Fri, 24 Jun 2011 17:15:29 -0500 Subject: parenthesis issue in the migration --- nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py index 85ab1fdd8..48fb4032f 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py @@ -114,8 +114,8 @@ def upgrade(migrate_engine): fixed_ips.c.instance_id != None) for row in s.execute(): - m = select([virtual_interfaces.c.id].\ - where(virtual_interfaces.c.instance_id == row['instance_id'])).\ + m = select([virtual_interfaces.c.id]).\ + where(virtual_interfaces.c.instance_id == row['instance_id']).\ as_scalar() u = fixed_ips.update().values(virtual_interface_id=m).\ where(fixed_ips.c.id == row['id']) -- cgit From 707c64ba5cb86ae3fc72d7bdc64070d9e562d96b Mon Sep 17 00:00:00 2001 From: Josh Kearney Date: Fri, 24 Jun 2011 17:19:32 -0500 Subject: PEP8 cleanup. --- .../migrate_repo/versions/027_add_provider_firewall_rules.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py b/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py index 5aa30f7a8..7e51d93b7 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py @@ -58,8 +58,7 @@ provider_fw_rules = Table('provider_fw_rules', meta, Column('to_port', Integer()), Column('cidr', String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)) - ) + unicode_error=None, _warn_on_bytestring=False))) def upgrade(migrate_engine): -- cgit From 5e4d90b33ddb993294232eea168a768486ba0bf4 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Sat, 25 Jun 2011 03:05:09 +0400 Subject: added disassociate method to tests --- nova/db/api.py | 2 ++ nova/db/sqlalchemy/api.py | 2 ++ 2 files changed, 4 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 8d4b7c8b7..c6ba24478 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -291,10 +291,12 @@ def floating_ip_get_by_address(context, address): """Get a floating ip by address or raise if it doesn't exist.""" return IMPL.floating_ip_get_by_address(context, address) + def floating_ip_get_by_ip(context, ip): """Get a floating ip by floating address.""" return IMPL.floating_ip_get_by_ip(context, ip) + def floating_ip_update(context, address, values): """Update a floating ip by address or raise if it doesn't exist.""" return IMPL.floating_ip_update(context, address, values) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 7735ec8c2..87f112588 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -609,6 +609,7 @@ def floating_ip_get_by_address(context, address, session=None): return result + @require_context def floating_ip_get_by_ip(context, ip, session=None): if not session: @@ -624,6 +625,7 @@ def floating_ip_get_by_ip(context, ip, session=None): return result + @require_context def floating_ip_update(context, address, values): session = get_session() -- cgit From 706bfc7e3f1cb1d5c56e988abf264c71c54ac0ce Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Fri, 24 Jun 2011 23:50:12 -0400 Subject: Renamed _inst_type_query_to_dict -> _dict_with_extra_specs. pep8 version is no longer explicitly specified in pip-requires --- nova/db/sqlalchemy/api.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 116c534f1..38fe2e2c6 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2621,14 +2621,14 @@ def instance_type_create(_context, values): return instance_type_ref -def _inst_type_query_to_dict(inst_type_query): +def _dict_with_extra_specs(inst_type_query): """Takes an instance type query returned by sqlalchemy - and returns it as a dictionary. + and returns it as a dictionary, converting the extra_specs + from a list of key/value pairs to a dictionary """ - extra_specs_objs = inst_type_query['extra_specs'] + inst_type_dict = dict(inst_type_query) extra_specs = dict([(x['key'], x['value']) for x in \ inst_type_query['extra_specs']]) - inst_type_dict = dict(inst_type_query) inst_type_dict['extra_specs'] = extra_specs return inst_type_dict @@ -2653,7 +2653,7 @@ def instance_type_get_all(context, inactive=False): if inst_types: inst_dict = {} for i in inst_types: - inst_dict[i['name']] = _inst_type_query_to_dict(i) + inst_dict[i['name']] = _dict_with_extra_specs(i) return inst_dict else: raise exception.NoInstanceTypesFound() @@ -2671,7 +2671,7 @@ def instance_type_get_by_id(context, id): if not inst_type: raise exception.InstanceTypeNotFound(instance_type=id) else: - return _inst_type_query_to_dict(inst_type) + return _dict_with_extra_specs(inst_type) @require_context @@ -2685,7 +2685,7 @@ def instance_type_get_by_name(context, name): if not inst_type: raise exception.InstanceTypeNotFoundByName(instance_type_name=name) else: - return _inst_type_query_to_dict(inst_type) + return _dict_with_extra_specs(inst_type) @require_context @@ -2699,7 +2699,7 @@ def instance_type_get_by_flavor_id(context, id): if not inst_type: raise exception.FlavorNotFound(flavor_id=id) else: - return _inst_type_query_to_dict(inst_type) + return _dict_with_extra_specs(inst_type) @require_admin_context -- cgit From af4e663dee05c907d7ccddc3bb929ff114e876cc Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Sat, 25 Jun 2011 00:05:38 -0400 Subject: Updated _dict_with_extra_specs docstring --- nova/db/sqlalchemy/api.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 38fe2e2c6..55f4bf072 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2624,7 +2624,14 @@ def instance_type_create(_context, values): def _dict_with_extra_specs(inst_type_query): """Takes an instance type query returned by sqlalchemy and returns it as a dictionary, converting the extra_specs - from a list of key/value pairs to a dictionary + entry from a list of dicts: + + 'extra_specs' : [{'key': 'k1', 'value': 'v1', ...}, ...] + + to a single dict: + + 'extra_specs' : {'k1': 'v1'} + """ inst_type_dict = dict(inst_type_query) extra_specs = dict([(x['key'], x['value']) for x in \ -- cgit From 0f5e7930b48ddd48a803ff5afd25f980df2e31b6 Mon Sep 17 00:00:00 2001 From: Lorin Hochstein Date: Sat, 25 Jun 2011 00:22:59 -0400 Subject: pep8 --- nova/db/sqlalchemy/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 55f4bf072..bf5cd5e23 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2626,12 +2626,12 @@ def _dict_with_extra_specs(inst_type_query): and returns it as a dictionary, converting the extra_specs entry from a list of dicts: - 'extra_specs' : [{'key': 'k1', 'value': 'v1', ...}, ...] - + 'extra_specs' : [{'key': 'k1', 'value': 'v1', ...}, ...] + to a single dict: - + 'extra_specs' : {'k1': 'v1'} - + """ inst_type_dict = dict(inst_type_query) extra_specs = dict([(x['key'], x['value']) for x in \ -- cgit From ef1f4d33fa5763ea602c2fc1098a4b230b86e82b Mon Sep 17 00:00:00 2001 From: Ilya Alekseyev Date: Mon, 27 Jun 2011 16:33:01 +0400 Subject: review issues fixed --- nova/db/sqlalchemy/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 426ea5127..55badb9e0 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -448,7 +448,7 @@ def floating_ip_get(context, ip_id): filter_by(deleted=False).\ first() if not result: - raise exception.FloatingIpNotFound() + raise exception.FloatingIpNotFoundForFixedAddress() return result @@ -605,7 +605,7 @@ def floating_ip_get_by_address(context, address, session=None): filter_by(deleted=can_read_deleted(context)).\ first() if not result: - raise exception.FloatingIpNotFound(fixed_ip=address) + raise exception.FloatingIpNotFoundForFixedAddress(fixed_ip=address) return result @@ -621,7 +621,7 @@ def floating_ip_get_by_ip(context, ip, session=None): first() if not result: - raise exception.FloatingIpNotDefined(floating_ip=ip) + raise exception.FloatingIpNotFound(floating_ip=ip) return result @@ -761,7 +761,7 @@ def fixed_ip_get_by_address(context, address, session=None): options(joinedload('instance')).\ first() if not result: - raise exception.FloatingIpNotFound(fixed_ip=address) + raise exception.FloatingIpNotFoundForFixedAddress(fixed_ip=address) if is_user_context(context): authorize_project_context(context, result.instance.project_id) -- cgit From 75dd73b1246904fd11bf9b566bf2319d3e6bada5 Mon Sep 17 00:00:00 2001 From: Eldar Nugaev Date: Mon, 27 Jun 2011 17:05:35 +0400 Subject: fixed pep style --- nova/db/sqlalchemy/api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 55badb9e0..d20e27617 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -429,14 +429,14 @@ def certificate_update(context, certificate_id, values): ################### @require_context -def floating_ip_get(context, ip_id): +def floating_ip_get(context, id): session = get_session() result = None if is_admin_context(context): result = session.query(models.FloatingIp).\ options(joinedload('fixed_ip')).\ options(joinedload_all('fixed_ip.instance')).\ - filter_by(id=ip_id).\ + filter_by(id=id).\ filter_by(deleted=can_read_deleted(context)).\ first() elif is_user_context(context): @@ -444,7 +444,7 @@ def floating_ip_get(context, ip_id): options(joinedload('fixed_ip')).\ options(joinedload_all('fixed_ip.instance')).\ filter_by(project_id=context.project_id).\ - filter_by(id=ip_id).\ + filter_by(id=id).\ filter_by(deleted=False).\ first() if not result: -- cgit From 80e71c50e88cb5552b7f700c8946e14b915eea11 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Mon, 27 Jun 2011 17:49:07 -0500 Subject: small formatting change --- nova/db/sqlalchemy/api.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index d13efb874..8f10c4078 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1437,11 +1437,11 @@ def network_associate(context, project_id, force=False): def network_query(project_filter): return session.query(models.Network).\ - filter_by(deleted=False).\ - filter(models.Network.host != None).\ - filter_by(project_id=project_filter).\ - with_lockmode('update').\ - first() + filter_by(deleted=False).\ + filter(models.Network.host != None).\ + filter_by(project_id=project_filter).\ + with_lockmode('update').\ + first() if not force: # find out if project has a network -- cgit From 4b8bcf30f934ea91290b7fe41536ba06ee832b3f Mon Sep 17 00:00:00 2001 From: Monsyne Dragon Date: Tue, 28 Jun 2011 08:57:05 +0000 Subject: Re-merging code for generating system-usages to get around bzr merge braindeadness. --- nova/db/api.py | 5 +++++ nova/db/sqlalchemy/api.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index ef8aa1143..aca403856 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -422,6 +422,11 @@ def instance_get_all(context): return IMPL.instance_get_all(context) +def instance_get_active_by_window(context, begin, end=None): + """Get instances active during a certain time window.""" + return IMPL.instance_get_active_by_window(context, begin, end) + + def instance_get_all_by_user(context, user_id): """Get all instances.""" return IMPL.instance_get_all_by_user(context, user_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 3681f30db..873cfe4d1 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -878,6 +878,24 @@ def instance_get_all(context): all() +@require_admin_context +def instance_get_active_by_window(context, begin, end=None): + """Return instances that were continuously active over the given window""" + session = get_session() + query = session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload('security_groups')).\ + options(joinedload_all('fixed_ip.network')).\ + options(joinedload('instance_type')).\ + filter(models.Instance.launched_at < begin) + if end: + query = query.filter(or_(models.Instance.terminated_at == None, + models.Instance.terminated_at > end)) + else: + query = query.filter(models.Instance.terminated_at == None) + return query.all() + + @require_admin_context def instance_get_all_by_user(context, user_id): session = get_session() -- cgit From 72ec15baa1f1672f9ff001e6127060889dd2bc4c Mon Sep 17 00:00:00 2001 From: Jason Kölker Date: Tue, 28 Jun 2011 15:26:00 -0500 Subject: pep8 --- .../migrate_repo/versions/027_add_provider_firewall_rules.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py b/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py index 5aa30f7a8..cb3c73170 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py @@ -58,8 +58,7 @@ provider_fw_rules = Table('provider_fw_rules', meta, Column('to_port', Integer()), Column('cidr', String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)) - ) + unicode_error=None, _warn_on_bytestring=False))) def upgrade(migrate_engine): -- cgit From 834b1741b4cd5e42393a8947a5c1fea80c625ee2 Mon Sep 17 00:00:00 2001 From: Josh Kearney Date: Tue, 28 Jun 2011 17:26:08 -0500 Subject: Use milestone cut. --- .../migrate_repo/versions/027_add_provider_firewall_rules.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py b/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py index 5aa30f7a8..cb3c73170 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/027_add_provider_firewall_rules.py @@ -58,8 +58,7 @@ provider_fw_rules = Table('provider_fw_rules', meta, Column('to_port', Integer()), Column('cidr', String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)) - ) + unicode_error=None, _warn_on_bytestring=False))) def upgrade(migrate_engine): -- cgit From bd3dd9c95aef72f5e16e166af5b0ab16d39365b5 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 29 Jun 2011 13:04:04 -0500 Subject: renumbered migrations again --- .../migrate_repo/versions/028_multi_nic.py | 130 --------------------- .../029_fk_fixed_ips_virtual_interface_id.py | 56 --------- .../migrate_repo/versions/029_multi_nic.py | 130 +++++++++++++++++++++ .../migrate_repo/versions/029_sqlite_downgrade.sql | 48 -------- .../migrate_repo/versions/029_sqlite_update.sql | 48 -------- .../030_fk_fixed_ips_virtual_interface_id.py | 56 +++++++++ .../migrate_repo/versions/030_sqlite_downgrade.sql | 48 ++++++++ .../migrate_repo/versions/030_sqlite_upgrade.sql | 48 ++++++++ 8 files changed, 282 insertions(+), 282 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/030_fk_fixed_ips_virtual_interface_id.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_downgrade.sql create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_upgrade.sql (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py deleted file mode 100644 index 48fb4032f..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/028_multi_nic.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - -# virtual interface table to add to DB -virtual_interfaces = Table('virtual_interfaces', meta, - Column('created_at', DateTime(timezone=False), - default=utils.utcnow()), - Column('updated_at', DateTime(timezone=False), - onupdate=utils.utcnow()), - Column('deleted_at', DateTime(timezone=False)), - Column('deleted', Boolean(create_constraint=True, name=None)), - Column('id', Integer(), primary_key=True, nullable=False), - Column('address', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - Column('network_id', - Integer(), - ForeignKey('networks.id'), - nullable=False), - Column('instance_id', - Integer(), - ForeignKey('instances.id'), - nullable=False), - Column('port_id', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), - mysql_engine='InnoDB') - - -# bridge_interface column to add to networks table -interface = Column('bridge_interface', - String(length=255, convert_unicode=False, - assert_unicode=None, unicode_error=None, - _warn_on_bytestring=False)) - - -# virtual interface id column to add to fixed_ips table -# foreignkey added in next migration -virtual_interface_id = Column('virtual_interface_id', - Integer()) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - - # grab tables and (column for dropping later) - instances = Table('instances', meta, autoload=True) - networks = Table('networks', meta, autoload=True) - fixed_ips = Table('fixed_ips', meta, autoload=True) - c = instances.columns['mac_address'] - - # add interface column to networks table - # values will have to be set manually before running nova - try: - networks.create_column(interface) - except Exception: - logging.error(_("interface column not added to networks table")) - raise - - # create virtual_interfaces table - try: - virtual_interfaces.create() - except Exception: - logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) - raise - - # add virtual_interface_id column to fixed_ips table - try: - fixed_ips.create_column(virtual_interface_id) - except Exception: - logging.error(_("VIF column not added to fixed_ips table")) - raise - - # populate the virtual_interfaces table - # extract data from existing instance and fixed_ip tables - s = select([instances.c.id, instances.c.mac_address, - fixed_ips.c.network_id], - fixed_ips.c.instance_id == instances.c.id) - keys = ('instance_id', 'address', 'network_id') - join_list = [dict(zip(keys, row)) for row in s.execute()] - logging.debug(_("join list for moving mac_addresses |%s|"), join_list) - - # insert data into the table - if join_list: - i = virtual_interfaces.insert() - i.execute(join_list) - - # populate the fixed_ips virtual_interface_id column - s = select([fixed_ips.c.id, fixed_ips.c.instance_id], - fixed_ips.c.instance_id != None) - - for row in s.execute(): - m = select([virtual_interfaces.c.id]).\ - where(virtual_interfaces.c.instance_id == row['instance_id']).\ - as_scalar() - u = fixed_ips.update().values(virtual_interface_id=m).\ - where(fixed_ips.c.id == row['id']) - u.execute() - - # drop the mac_address column from instances - c.drop() - - -def downgrade(migrate_engine): - logging.error(_("Can't downgrade without losing data")) - raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py deleted file mode 100644 index 56e927717..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/029_fk_fixed_ips_virtual_interface_id.py +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# 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. - -import datetime - -from sqlalchemy import * -from migrate import * - -from nova import log as logging -from nova import utils - -meta = MetaData() - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # grab tables - fixed_ips = Table('fixed_ips', meta, autoload=True) - virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) - - # add foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).create() - except Exception: - logging.error(_("foreign key constraint couldn't be added")) - raise - - -def downgrade(migrate_engine): - meta.bind = migrate_engine - dialect = migrate_engine.url.get_dialect().name - - # drop foreignkey if not sqlite - try: - if not dialect.startswith('sqlite'): - ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], - refcolumns=[virtual_interfaces.c.id]).drop() - except Exception: - logging.error(_("foreign key constraint couldn't be dropped")) - raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py new file mode 100644 index 000000000..48fb4032f --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py @@ -0,0 +1,130 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + +# virtual interface table to add to DB +virtual_interfaces = Table('virtual_interfaces', meta, + Column('created_at', DateTime(timezone=False), + default=utils.utcnow()), + Column('updated_at', DateTime(timezone=False), + onupdate=utils.utcnow()), + Column('deleted_at', DateTime(timezone=False)), + Column('deleted', Boolean(create_constraint=True, name=None)), + Column('id', Integer(), primary_key=True, nullable=False), + Column('address', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + Column('network_id', + Integer(), + ForeignKey('networks.id'), + nullable=False), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + Column('port_id', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + unique=True), + mysql_engine='InnoDB') + + +# bridge_interface column to add to networks table +interface = Column('bridge_interface', + String(length=255, convert_unicode=False, + assert_unicode=None, unicode_error=None, + _warn_on_bytestring=False)) + + +# virtual interface id column to add to fixed_ips table +# foreignkey added in next migration +virtual_interface_id = Column('virtual_interface_id', + Integer()) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + + # grab tables and (column for dropping later) + instances = Table('instances', meta, autoload=True) + networks = Table('networks', meta, autoload=True) + fixed_ips = Table('fixed_ips', meta, autoload=True) + c = instances.columns['mac_address'] + + # add interface column to networks table + # values will have to be set manually before running nova + try: + networks.create_column(interface) + except Exception: + logging.error(_("interface column not added to networks table")) + raise + + # create virtual_interfaces table + try: + virtual_interfaces.create() + except Exception: + logging.error(_("Table |%s| not created!"), repr(virtual_interfaces)) + raise + + # add virtual_interface_id column to fixed_ips table + try: + fixed_ips.create_column(virtual_interface_id) + except Exception: + logging.error(_("VIF column not added to fixed_ips table")) + raise + + # populate the virtual_interfaces table + # extract data from existing instance and fixed_ip tables + s = select([instances.c.id, instances.c.mac_address, + fixed_ips.c.network_id], + fixed_ips.c.instance_id == instances.c.id) + keys = ('instance_id', 'address', 'network_id') + join_list = [dict(zip(keys, row)) for row in s.execute()] + logging.debug(_("join list for moving mac_addresses |%s|"), join_list) + + # insert data into the table + if join_list: + i = virtual_interfaces.insert() + i.execute(join_list) + + # populate the fixed_ips virtual_interface_id column + s = select([fixed_ips.c.id, fixed_ips.c.instance_id], + fixed_ips.c.instance_id != None) + + for row in s.execute(): + m = select([virtual_interfaces.c.id]).\ + where(virtual_interfaces.c.instance_id == row['instance_id']).\ + as_scalar() + u = fixed_ips.update().values(virtual_interface_id=m).\ + where(fixed_ips.c.id == row['id']) + u.execute() + + # drop the mac_address column from instances + c.drop() + + +def downgrade(migrate_engine): + logging.error(_("Can't downgrade without losing data")) + raise Exception diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql deleted file mode 100644 index c1d26b180..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_downgrade.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql b/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql deleted file mode 100644 index 2a9362545..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/029_sqlite_update.sql +++ /dev/null @@ -1,48 +0,0 @@ -BEGIN TRANSACTION; - - CREATE TEMPORARY TABLE fixed_ips_backup ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id) - ); - - INSERT INTO fixed_ips_backup - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips; - - CREATE TABLE fixed_ips ( - id INTEGER NOT NULL, - address VARCHAR(255), - virtual_interface_id INTEGER, - network_id INTEGER, - instance_id INTEGER, - allocated BOOLEAN default FALSE, - leased BOOLEAN default FALSE, - reserved BOOLEAN default FALSE, - created_at DATETIME NOT NULL, - updated_at DATETIME, - deleted_at DATETIME, - deleted BOOLEAN NOT NULL, - PRIMARY KEY (id), - FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) - ); - - INSERT INTO fixed_ips - SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted - FROM fixed_ips; - - DROP TABLE fixed_ips_backup; - -COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/030_fk_fixed_ips_virtual_interface_id.py b/nova/db/sqlalchemy/migrate_repo/versions/030_fk_fixed_ips_virtual_interface_id.py new file mode 100644 index 000000000..56e927717 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/030_fk_fixed_ips_virtual_interface_id.py @@ -0,0 +1,56 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# 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. + +import datetime + +from sqlalchemy import * +from migrate import * + +from nova import log as logging +from nova import utils + +meta = MetaData() + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # grab tables + fixed_ips = Table('fixed_ips', meta, autoload=True) + virtual_interfaces = Table('virtual_interfaces', meta, autoload=True) + + # add foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).create() + except Exception: + logging.error(_("foreign key constraint couldn't be added")) + raise + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + dialect = migrate_engine.url.get_dialect().name + + # drop foreignkey if not sqlite + try: + if not dialect.startswith('sqlite'): + ForeignKeyConstraint(columns=[fixed_ips.c.virtual_interface_id], + refcolumns=[virtual_interfaces.c.id]).drop() + except Exception: + logging.error(_("foreign key constraint couldn't be dropped")) + raise diff --git a/nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_downgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_downgrade.sql new file mode 100644 index 000000000..c1d26b180 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_downgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; diff --git a/nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_upgrade.sql b/nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_upgrade.sql new file mode 100644 index 000000000..2a9362545 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/030_sqlite_upgrade.sql @@ -0,0 +1,48 @@ +BEGIN TRANSACTION; + + CREATE TEMPORARY TABLE fixed_ips_backup ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id) + ); + + INSERT INTO fixed_ips_backup + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips; + + CREATE TABLE fixed_ips ( + id INTEGER NOT NULL, + address VARCHAR(255), + virtual_interface_id INTEGER, + network_id INTEGER, + instance_id INTEGER, + allocated BOOLEAN default FALSE, + leased BOOLEAN default FALSE, + reserved BOOLEAN default FALSE, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME, + deleted BOOLEAN NOT NULL, + PRIMARY KEY (id), + FOREIGN KEY(virtual_interface_id) REFERENCES virtual_interfaces (id) + ); + + INSERT INTO fixed_ips + SELECT id, address, virtual_interface_id, network_id, instance_id, allocated, leased, reserved, created_at, updated_at, deleted_at, deleted + FROM fixed_ips; + + DROP TABLE fixed_ips_backup; + +COMMIT; -- cgit From c49b1a8124fe63292d7c1191c094cc5921dbfaa9 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 29 Jun 2011 17:17:33 -0500 Subject: removed port_id from virtual interfaces and set network_id to nullable --- nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py | 9 ++------- nova/db/sqlalchemy/models.py | 5 ++--- 2 files changed, 4 insertions(+), 10 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py index 48fb4032f..61cd8ae51 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py @@ -39,15 +39,10 @@ virtual_interfaces = Table('virtual_interfaces', meta, Column('network_id', Integer(), ForeignKey('networks.id'), - nullable=False), + nullable=True), Column('instance_id', Integer(), - ForeignKey('instances.id'), - nullable=False), - Column('port_id', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False), - unique=True), + ForeignKey('instances.id')), mysql_engine='InnoDB') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 27cdb96ce..03bde4f99 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -568,12 +568,11 @@ class VirtualInterface(BASE, NovaBase): __tablename__ = 'virtual_interfaces' id = Column(Integer, primary_key=True) address = Column(String(255), unique=True) - network_id = Column(Integer, ForeignKey('networks.id'), nullable=False) + network_id = Column(Integer, ForeignKey('networks.id'), nullable=True) network = relationship(Network, backref=backref('virtual_interfaces')) - port_id = Column(String(255), unique=True, nullable=True) # TODO(tr3buchet): cut the cord, removed foreign key and backrefs - instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) + instance_id = Column(Integer, ForeignKey('instances.id')) instance = relationship(Instance, backref=backref('virtual_interfaces')) -- cgit From 46c321d044d6a2db44a22466624a1e7dc71d5935 Mon Sep 17 00:00:00 2001 From: Trey Morris Date: Wed, 29 Jun 2011 17:19:47 -0500 Subject: fixed incorrect assumption that nullable defaults to false --- nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py | 6 +++--- nova/db/sqlalchemy/models.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py b/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py index 61cd8ae51..4a117bb11 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/029_multi_nic.py @@ -38,11 +38,11 @@ virtual_interfaces = Table('virtual_interfaces', meta, unique=True), Column('network_id', Integer(), - ForeignKey('networks.id'), - nullable=True), + ForeignKey('networks.id')), Column('instance_id', Integer(), - ForeignKey('instances.id')), + ForeignKey('instances.id'), + nullable=False), mysql_engine='InnoDB') diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 03bde4f99..fe899cc4f 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -568,11 +568,11 @@ class VirtualInterface(BASE, NovaBase): __tablename__ = 'virtual_interfaces' id = Column(Integer, primary_key=True) address = Column(String(255), unique=True) - network_id = Column(Integer, ForeignKey('networks.id'), nullable=True) + network_id = Column(Integer, ForeignKey('networks.id')) network = relationship(Network, backref=backref('virtual_interfaces')) # TODO(tr3buchet): cut the cord, removed foreign key and backrefs - instance_id = Column(Integer, ForeignKey('instances.id')) + instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) instance = relationship(Instance, backref=backref('virtual_interfaces')) -- cgit From e73a43ae34a49258cc6d752970d52d5614c1d1a9 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Wed, 29 Jun 2011 22:01:28 -0400 Subject: Update the fixed_ip_disassociate_all_by_timeout in nova.db.api so that it supports Postgres. Fixes casting errors on postgres with this function. --- nova/db/sqlalchemy/api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 6bd16d42e..4c0b14341 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -715,9 +715,9 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time): filter(models.FixedIp.network_id.in_(inner_q)).\ filter(models.FixedIp.updated_at < time).\ filter(models.FixedIp.instance_id != None).\ - filter_by(allocated=0).\ + filter_by(allocated=False).\ update({'instance_id': None, - 'leased': 0, + 'leased': False, 'updated_at': utils.utcnow()}, synchronize_session='fetch') return result -- cgit From c12861f6068ea18156ff9c395ed40791585032d7 Mon Sep 17 00:00:00 2001 From: Alex Meade Date: Thu, 30 Jun 2011 09:28:21 -0400 Subject: refactored instance type code --- nova/db/sqlalchemy/api.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index a2500a38d..e23bd0a5d 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2581,13 +2581,11 @@ def instance_type_get_all(context, inactive=False): filter_by(deleted=False).\ order_by("name").\ all() + inst_dict = {} if inst_types: - inst_dict = {} for i in inst_types: inst_dict[i['name']] = dict(i) - return inst_dict - else: - raise exception.NoInstanceTypesFound() + return inst_dict @require_context -- cgit