From 79779eae788dee00af8523a1f6a0fb9dce07a68e Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Fri, 27 May 2011 11:09:20 +0900 Subject: db: add a table for block device mapping This patch adds a new table for block device mapping and helper APIs for it. --- nova/db/api.py | 28 +++++++ nova/db/sqlalchemy/api.py | 56 ++++++++++++++ .../versions/021_add_block_device_mapping.py | 85 ++++++++++++++++++++++ nova/db/sqlalchemy/models.py | 40 ++++++++++ 4 files changed, 209 insertions(+) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 3597732b9..858c8eb10 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -920,6 +920,34 @@ def snapshot_update(context, snapshot_id, values): #################### +def block_device_mapping_create(context, values): + """Create an entry of block device mapping""" + return IMPL.block_device_mapping_create(context, values) + +def block_device_mapping_update(context, bdm_id, values): + """Create an entry of block device mapping""" + return IMPL.block_device_mapping_update(context, bdm_id, values) + +def block_device_mapping_get_all_by_instance(context, instance_id): + """Get all block device mapping blonging to a instance""" + return IMPL.block_device_mapping_get_all_by_instance(context, instance_id) + + +def block_device_mapping_destroy(context, bdm_id): + """Destroy the block device mapping.""" + return IMPL.block_device_mapping_destroy(context, bdm_id) + + +def block_device_mapping_destroy_by_instance_and_volume(context, instance_id, + volume_id): + """Destroy the block device mapping or raise if it does not exist.""" + return IMPL.block_device_mapping_destroy_by_instance_and_volume( + context, instance_id, volume_id) + + +#################### + + def security_group_get_all(context): """Get all security groups.""" return IMPL.security_group_get_all(context) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index cf8fc62db..e0904b3c6 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1864,6 +1864,62 @@ def snapshot_update(context, snapshot_id, values): ################### +@require_context +def block_device_mapping_create(context, values): + bdm_ref = models.BlockDeviceMapping() + bdm_ref.update(values) + + session = get_session() + with session.begin(): + bdm_ref.save(session=session) + +@require_context +def block_device_mapping_update(context, bdm_id, values): + session = get_session() + with session.begin(): + session.query(models.BlockDeviceMapping).\ + filter_by(id=bdm_id).\ + filter_by(deleted=False).\ + update(values) + +@require_context +def block_device_mapping_get_all_by_instance(context, instance_id): + session = get_session() + result = session.query(models.BlockDeviceMapping).\ + filter_by(instance_id=instance_id).\ + filter_by(deleted=False).\ + all() + if not result: + raise exception.NotFound() + return result + +@require_context +def block_device_mapping_destroy(context, bdm_id): + session = get_session() + with session.begin(): + session.query(models.BlockDeviceMapping).\ + filter_by(id=bdm_id).\ + update({'deleted': 1, + 'deleted_at': datetime.datetime.utcnow(), + 'updated_at': literal_column('updated_at')}) + +@require_context +def block_device_mapping_destroy_by_instance_and_volume(context, instance_id, + volume_id): + session = get_session() + with session.begin(): + session.query(models.BlockDeviceMapping).\ + filter_by(instance_id=instance_id).\ + filter_by(volume_id=volume_id).\ + filter_by(deleted=False).\ + update({'deleted': 1, + 'deleted_at': datetime.datetime.utcnow(), + 'updated_at': literal_column('updated_at')}) + + +################### + + @require_context def security_group_get_all(context): session = get_session() diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py b/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py new file mode 100644 index 000000000..ed4296334 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py @@ -0,0 +1,85 @@ +# Copyright 2011 OpenStack LLC. +# Copyright 2011 Isaku Yamahata +# +# 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 MetaData, Table, Column +from sqlalchemy import DateTime, Boolean, Integer, String +from sqlalchemy import ForeignKey +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. +instances = Table('instances', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + +volumes = Table('volumes', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + +snapshots = Table('snapshots', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + + +block_device_mapping = Table('block_device_mapping', 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, autoincrement=True), + Column('instance_id', + Integer(), + ForeignKey('instances.id'), + nullable=False), + Column('device_name', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + nullable=False), + Column('delete_on_termination', + Boolean(create_constraint=True, name=None), + default=False), + Column('virtual_name', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False), + nullable=True), + Column('snapshot_id', + Integer(), + ForeignKey('snapshots.id'), + nullable=True), + Column('volume_id', Integer(), ForeignKey('volumes.id'), nullable=True), + Column('volume_size', Integer(), nullable=True), + Column('no_device', + Boolean(create_constraint=True, name=None), + nullable=True), + ) + + +def upgrade(migrate_engine): + # Upgrade operations go here. Don't create your own engine; + # bind migrate_engine to your metadata + meta.bind = migrate_engine + try: + block_device_mapping.create() + except Exception: + logging.info(repr(block_device_mapping)) + logging.exception('Exception while creating table') + meta.drop_all(tables=[block_device_mapping]) + raise + +def downgrade(migrate_engine): + # Operations to reverse the above upgrade go here. + block_device_mapping.drop() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 32838d386..a74db811f 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -355,6 +355,46 @@ class Snapshot(BASE, NovaBase): display_name = Column(String(255)) display_description = Column(String(255)) + +class BlockDeviceMapping(BASE, NovaBase): + """Represents block device mapping that is defined by EC2""" + __tablename__ = "block_device_mapping" + id = Column(Integer, primary_key=True, autoincrement=True) + + instance_id = Column(Integer, ForeignKey('instances.id'), nullable=False) + instance = relationship(Instance, + backref=backref('balock_device_mapping'), + foreign_keys=instance_id, + primaryjoin='and_(BlockDeviceMapping.instance_id==' + 'Instance.id,' + 'BlockDeviceMapping.deleted==' + 'False)') + device_name = Column(String(255), nullable=False) + + # default=False for compatilibity of the existing code. + # With EC2 API, + # default True for ami specified device. + # default False for created with other timing. + delete_on_termination = Column(Boolean, default=False) + + # for ephemeral device + virtual_name = Column(String(255), nullable=True) + + # for snapshot or volume + snapshot_id = Column(Integer, ForeignKey('snapshots.id'), nullable=True) + # outer join + snapshot = relationship(Snapshot, + foreign_keys=snapshot_id) + + volume_id = Column(Integer, ForeignKey('volumes.id'), nullable=True) + volume = relationship(Volume, + foreign_keys=volume_id) + volume_size = Column(Integer, nullable=True) + + # for no device to supress devices. + no_device = Column(Boolean, nullable=True) + + class ExportDevice(BASE, NovaBase): """Represates a shelf and blade that a volume can be exported on.""" __tablename__ = 'export_devices' -- cgit From ab938bf376efe7a93b54e4ca595d3102d04b0080 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Fri, 27 May 2011 11:10:24 +0900 Subject: compute: implement ec2 stop/start instances This patch implements ec2 stop/start instances with block device mapping support. --- 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 858c8eb10..7fb7f336f 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -414,6 +414,11 @@ def instance_destroy(context, instance_id): return IMPL.instance_destroy(context, instance_id) +def instance_stop(context, instance_id): + """Stop the instance or raise if it does not exist.""" + return IMPL.instance_stop(context, instance_id) + + def instance_get(context, instance_id): """Get an instance or raise if it does not exist.""" return IMPL.instance_get(context, instance_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index e0904b3c6..be47e2c2e 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -831,6 +831,24 @@ def instance_destroy(context, instance_id): 'deleted_at': datetime.datetime.utcnow(), 'updated_at': literal_column('updated_at')}) +@require_context +def instance_stop(context, instance_id): + session = get_session() + with session.begin(): + from nova.compute import power_state + session.query(models.Instance).\ + filter_by(id=instance_id).\ + update({'host': None, + 'state': power_state.SHUTOFF, + 'state_description': 'stopped', + 'updated_at': literal_column('updated_at')}) + session.query(models.SecurityGroupInstanceAssociation).\ + filter_by(instance_id=instance_id).\ + update({'updated_at': literal_column('updated_at')}) + session.query(models.InstanceMetadata).\ + filter_by(instance_id=instance_id).\ + update({'updated_at': literal_column('updated_at')}) + @require_context def instance_get(context, instance_id, session=None): -- cgit From f732831bf4f0c5581b28322d76fb13a17cd65839 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Thu, 9 Jun 2011 20:11:55 +0000 Subject: Record architecture of image for matching to agent build later. Add code to automatically update agent running on instance on instance creation. --- nova/db/api.py | 23 +++++++ nova/db/sqlalchemy/api.py | 50 ++++++++++++++ .../migrate_repo/versions/023_add_agent_table.py | 78 ++++++++++++++++++++++ nova/db/sqlalchemy/models.py | 15 ++++- 4 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 4e0aa60a2..30663662b 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1247,3 +1247,26 @@ 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 agent_build_create(context, values): + return IMPL.agent_build_create(context, values) + + +def agent_build_get_by_triple(context, hypervisor, os, architecture): + return IMPL.agent_build_get_by_triple(context, hypervisor, os, architecture) + + +def agent_build_get_all(context): + return IMPL.agent_build_get_all(context) + + +def agent_build_destroy(context, agent_update_id): + IMPL.agent_build_destroy(context, agent_update_id) + + +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 73870d2f3..d7a3b4c49 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2682,3 +2682,53 @@ def instance_metadata_update_or_create(context, instance_id, metadata): meta_ref.save(session=session) return metadata + + +@require_admin_context +def agent_build_create(context, values): + agent_build_ref = models.AgentBuild() + agent_build_ref.update(values) + agent_build_ref.save() + return agent_build_ref + + +@require_admin_context +def agent_build_get_by_triple(context, hypervisor, os, architecture, session=None): + if not session: + session = get_session() + return session.query(models.AgentBuild).\ + filter_by(hypervisor=hypervisor).\ + filter_by(os=os).\ + filter_by(architecture=architecture).\ + filter_by(deleted=False).\ + first() + + +@require_admin_context +def agent_build_get_all(context): + session = get_session() + return session.query(models.AgentBuild).\ + filter_by(deleted=False).\ + all() + + +@require_admin_context +def agent_build_destroy(context, agent_build_id): + session = get_session() + with session.begin(): + session.query(models.AgentBuild).\ + filter_by(id=agent_build_id).\ + update({'deleted': 1, + 'deleted_at': datetime.datetime.utcnow(), + 'updated_at': literal_column('updated_at')}) + + +@require_admin_context +def agent_build_update(context, agent_build_id, values): + session = get_session() + with session.begin(): + agent_build_ref = session.query(models.AgentBuild).\ + filter_by(id=agent_build_id). \ + first() + agent_build_ref.update(values) + agent_build_ref.save(session=session) diff --git a/nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py new file mode 100644 index 000000000..33979ca79 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py @@ -0,0 +1,78 @@ +# Copyright 2010 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 Boolean, Column, DateTime, Integer +from sqlalchemy import MetaData, String, Table +from nova import log as logging + +meta = MetaData() + +# +# New Tables +# +builds = Table('agent_builds', 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('hypervisor', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('os', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('architecture', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('version', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('url', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('md5hash', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + ) + + +# Table stub-definitions +# Just for the ForeignKey and column creation to succeed, these are not the +# actual definitions of instances or services. +# +instances = Table('instances', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + +# +# New Column +# + +architecture = Column('architecture', String(length=255)) + + +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 (builds, ): + try: + table.create() + except Exception: + logging.info(repr(table)) + + # Add columns to existing tables + instances.create_column(architecture) diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 239f6e96a..3aabfb58f 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -232,6 +232,7 @@ class Instance(BASE, NovaBase): locked = Column(Boolean) os_type = Column(String(255)) + architecture = Column(String(255)) # TODO(vish): see Ewan's email about state improvements, probably # should be in a driver base class or some such @@ -672,6 +673,18 @@ class Zone(BASE, NovaBase): password = Column(String(255)) +class AgentBuild(BASE, NovaBase): + """Represents an agent build.""" + __tablename__ = 'agent_builds' + id = Column(Integer, primary_key=True) + hypervisor = Column(String(255)) + os = Column(String(255)) + architecture = Column(String(255)) + version = Column(String(255)) + url = Column(String(255)) + md5hash = Column(String(255)) + + def register_models(): """Register Models and create metadata. @@ -685,7 +698,7 @@ def register_models(): Network, SecurityGroup, SecurityGroupIngressRule, SecurityGroupInstanceAssociation, AuthToken, User, Project, Certificate, ConsolePool, Console, Zone, - InstanceMetadata, Migration) + AgentBuild, InstanceMetadata, Migration) engine = create_engine(FLAGS.sql_connection, echo=False) for model in models: model.metadata.create_all(engine) -- cgit From 61f539dfd3f1f13a775ec837da5646ef16c270d7 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Thu, 9 Jun 2011 22:06:09 +0000 Subject: Add some docstrings for new agent build DB functions --- nova/db/api.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 30663662b..ff4339351 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1253,20 +1253,25 @@ 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) def agent_build_destroy(context, agent_update_id): + """Destroy agent build entry.""" IMPL.agent_build_destroy(context, agent_update_id) def agent_build_update(context, agent_build_id, values): + """Update agent build entry.""" IMPL.agent_build_update(context, agent_build_id, values) -- cgit From b0fdb4a2326f6e7c92bba80e6b80857ba2a61612 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Wed, 15 Jun 2011 22:58:22 +0900 Subject: typo --- nova/db/api.py | 2 +- nova/db/sqlalchemy/models.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 215fa5009..3f5e9a54a 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -934,7 +934,7 @@ def block_device_mapping_update(context, bdm_id, values): return IMPL.block_device_mapping_update(context, bdm_id, values) def block_device_mapping_get_all_by_instance(context, instance_id): - """Get all block device mapping blonging to a instance""" + """Get all block device mapping belonging to a instance""" return IMPL.block_device_mapping_get_all_by_instance(context, instance_id) diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index a74db811f..0c3ec40c7 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -371,7 +371,7 @@ class BlockDeviceMapping(BASE, NovaBase): 'False)') device_name = Column(String(255), nullable=False) - # default=False for compatilibity of the existing code. + # default=False for compatibility of the existing code. # With EC2 API, # default True for ami specified device. # default False for created with other timing. @@ -391,7 +391,7 @@ class BlockDeviceMapping(BASE, NovaBase): foreign_keys=volume_id) volume_size = Column(Integer, nullable=True) - # for no device to supress devices. + # for no device to suppress devices. no_device = Column(Boolean, nullable=True) -- cgit From f48f35183f6bc30c0e053ea9569f5348799ed451 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Wed, 15 Jun 2011 23:11:03 +0900 Subject: pep8 --- nova/db/api.py | 2 ++ nova/db/sqlalchemy/api.py | 5 +++++ .../migrate_repo/versions/021_add_block_device_mapping.py | 6 ++++-- nova/db/sqlalchemy/models.py | 4 ++-- 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 3f5e9a54a..cf58e87e5 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -929,10 +929,12 @@ def block_device_mapping_create(context, values): """Create an entry of block device mapping""" return IMPL.block_device_mapping_create(context, values) + def block_device_mapping_update(context, bdm_id, values): """Create an entry of block device mapping""" return IMPL.block_device_mapping_update(context, bdm_id, values) + def block_device_mapping_get_all_by_instance(context, instance_id): """Get all block device mapping belonging to a instance""" return IMPL.block_device_mapping_get_all_by_instance(context, instance_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 9385519df..915b34016 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -832,6 +832,7 @@ def instance_destroy(context, instance_id): 'deleted_at': datetime.datetime.utcnow(), 'updated_at': literal_column('updated_at')}) + @require_context def instance_stop(context, instance_id): session = get_session() @@ -1892,6 +1893,7 @@ def block_device_mapping_create(context, values): with session.begin(): bdm_ref.save(session=session) + @require_context def block_device_mapping_update(context, bdm_id, values): session = get_session() @@ -1901,6 +1903,7 @@ def block_device_mapping_update(context, bdm_id, values): filter_by(deleted=False).\ update(values) + @require_context def block_device_mapping_get_all_by_instance(context, instance_id): session = get_session() @@ -1912,6 +1915,7 @@ def block_device_mapping_get_all_by_instance(context, instance_id): raise exception.NotFound() return result + @require_context def block_device_mapping_destroy(context, bdm_id): session = get_session() @@ -1922,6 +1926,7 @@ def block_device_mapping_destroy(context, bdm_id): 'deleted_at': datetime.datetime.utcnow(), 'updated_at': literal_column('updated_at')}) + @require_context def block_device_mapping_destroy_by_instance_and_volume(context, instance_id, volume_id): diff --git a/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py b/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py index ed4296334..6e9b806cb 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/021_add_block_device_mapping.py @@ -60,7 +60,8 @@ block_device_mapping = Table('block_device_mapping', meta, Integer(), ForeignKey('snapshots.id'), nullable=True), - Column('volume_id', Integer(), ForeignKey('volumes.id'), nullable=True), + Column('volume_id', Integer(), ForeignKey('volumes.id'), + nullable=True), Column('volume_size', Integer(), nullable=True), Column('no_device', Boolean(create_constraint=True, name=None), @@ -79,7 +80,8 @@ def upgrade(migrate_engine): logging.exception('Exception while creating table') meta.drop_all(tables=[block_device_mapping]) raise - + + def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. block_device_mapping.drop() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 0c3ec40c7..104f31143 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -385,12 +385,12 @@ class BlockDeviceMapping(BASE, NovaBase): # outer join snapshot = relationship(Snapshot, foreign_keys=snapshot_id) - + volume_id = Column(Integer, ForeignKey('volumes.id'), nullable=True) volume = relationship(Volume, foreign_keys=volume_id) volume_size = Column(Integer, nullable=True) - + # for no device to suppress devices. no_device = Column(Boolean, nullable=True) -- cgit From d298ba8c1f9fbd47e4d30364e0b1a894c8c5c424 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 15 Jun 2011 14:34:32 +0000 Subject: Rename to 024 since 023 was added already --- .../migrate_repo/versions/023_add_agent_table.py | 78 ---------------------- .../migrate_repo/versions/024_add_agent_table.py | 78 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 78 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py deleted file mode 100644 index 33979ca79..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/023_add_agent_table.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright 2010 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 Boolean, Column, DateTime, Integer -from sqlalchemy import MetaData, String, Table -from nova import log as logging - -meta = MetaData() - -# -# New Tables -# -builds = Table('agent_builds', 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('hypervisor', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('os', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('architecture', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('version', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('url', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('md5hash', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - ) - - -# Table stub-definitions -# Just for the ForeignKey and column creation to succeed, these are not the -# actual definitions of instances or services. -# -instances = Table('instances', meta, - Column('id', Integer(), primary_key=True, nullable=False), - ) - -# -# New Column -# - -architecture = Column('architecture', String(length=255)) - - -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 (builds, ): - try: - table.create() - except Exception: - logging.info(repr(table)) - - # Add columns to existing tables - instances.create_column(architecture) diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py new file mode 100644 index 000000000..33979ca79 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py @@ -0,0 +1,78 @@ +# Copyright 2010 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 Boolean, Column, DateTime, Integer +from sqlalchemy import MetaData, String, Table +from nova import log as logging + +meta = MetaData() + +# +# New Tables +# +builds = Table('agent_builds', 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('hypervisor', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('os', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('architecture', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('version', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('url', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('md5hash', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + ) + + +# Table stub-definitions +# Just for the ForeignKey and column creation to succeed, these are not the +# actual definitions of instances or services. +# +instances = Table('instances', meta, + Column('id', Integer(), primary_key=True, nullable=False), + ) + +# +# New Column +# + +architecture = Column('architecture', String(length=255)) + + +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 (builds, ): + try: + table.create() + except Exception: + logging.info(repr(table)) + + # Add columns to existing tables + instances.create_column(architecture) -- cgit From 8ecf36310d35a880a0ee95d4c7fbaf3324646d58 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Wed, 15 Jun 2011 14:41:09 +0000 Subject: PEP8 cleanups --- nova/db/api.py | 3 ++- nova/db/sqlalchemy/api.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index ff4339351..4649e7ec1 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -1259,7 +1259,8 @@ def 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) + return IMPL.agent_build_get_by_triple(context, hypervisor, os, + architecture) def agent_build_get_all(context): diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index d7a3b4c49..12f63b4bf 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -2693,7 +2693,8 @@ def agent_build_create(context, values): @require_admin_context -def agent_build_get_by_triple(context, hypervisor, os, architecture, session=None): +def agent_build_get_by_triple(context, hypervisor, os, architecture, + session=None): if not session: session = get_session() return session.query(models.AgentBuild).\ -- cgit From a9800765f7eb8430c67d15953bed202752c2e199 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Thu, 16 Jun 2011 00:32:03 +0900 Subject: db/block_device_mapping_get_all_by_instance: don't raise. Make db.block_device_mapping_get_all_by_instance() not raise, but returns empty list when no column found as all users catch notfound exception and ignore it. Thus ugly 'catch NotFound: pass' is eliminated. --- 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 915b34016..ddaae8ca5 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1912,7 +1912,7 @@ def block_device_mapping_get_all_by_instance(context, instance_id): filter_by(deleted=False).\ all() if not result: - raise exception.NotFound() + return [] return result -- cgit From da5e5106565f4999c1856be9c3230ba1a1505b82 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Wed, 15 Jun 2011 17:52:44 +0000 Subject: Adding UUID test --- nova/db/api.py | 5 ++++ nova/db/sqlalchemy/api.py | 60 ++++++++++++++++++++++++++------------------ nova/db/sqlalchemy/models.py | 1 + 3 files changed, 41 insertions(+), 25 deletions(-) (limited to 'nova/db') diff --git a/nova/db/api.py b/nova/db/api.py index 4e0aa60a2..5610227bd 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -414,6 +414,11 @@ def instance_destroy(context, instance_id): return IMPL.instance_destroy(context, instance_id) +def instance_get_by_uuid(context, uuid): + """Get an instance or raise if it does not exist.""" + return IMPL.instance_get_by_uuid(context, uuid) + + def instance_get(context, instance_id): """Get an instance or raise if it does not exist.""" return IMPL.instance_get(context, instance_id) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 73870d2f3..985035cc2 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -839,38 +839,48 @@ def instance_destroy(context, instance_id): 'updated_at': literal_column('updated_at')}) + +@require_context +def instance_get_by_uuid(context, uuid, session=None): + partial = _instance_get(context, session=session) + result = partial.filter_by(uuid=uuid) + result = result.first() + if not result: + # FIXME(sirp): it would be nice if InstanceNotFound would accept a + # uuid parameter as well + raise exception.InstanceNotFound(instance_id=uuid) + return result + + @require_context def instance_get(context, instance_id, session=None): + partial = _instance_get(context, session=session) + result = partial.filter_by(id=instance_id) + result = result.first() + if not result: + raise exception.InstanceNotFound(instance_id=instance_id) + return result + + +@require_context +def _instance_get(context, session=None): if not session: session = get_session() - result = None + + partial = session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload_all('security_groups.rules')).\ + options(joinedload('volumes')).\ + options(joinedload_all('fixed_ip.network')).\ + options(joinedload('metadata')).\ + options(joinedload('instance_type')) if is_admin_context(context): - result = session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ - options(joinedload_all('security_groups.rules')).\ - options(joinedload('volumes')).\ - options(joinedload_all('fixed_ip.network')).\ - options(joinedload('metadata')).\ - options(joinedload('instance_type')).\ - filter_by(id=instance_id).\ - filter_by(deleted=can_read_deleted(context)).\ - first() + partial = partial.filter_by(deleted=can_read_deleted(context)) elif is_user_context(context): - result = session.query(models.Instance).\ - options(joinedload_all('fixed_ip.floating_ips')).\ - options(joinedload_all('security_groups.rules')).\ - options(joinedload('volumes')).\ - options(joinedload('metadata')).\ - options(joinedload('instance_type')).\ - filter_by(project_id=context.project_id).\ - filter_by(id=instance_id).\ - filter_by(deleted=False).\ - first() - if not result: - raise exception.InstanceNotFound(instance_id=instance_id) - - return result + partial = partial.filter_by(project_id=context.project_id).\ + filter_by(deleted=False) + return partial @require_admin_context diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index 612ccc93f..a1b47eded 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -233,6 +233,7 @@ class Instance(BASE, NovaBase): os_type = Column(String(255)) vm_mode = Column(String(255)) + uuid = Column(String(32)) # TODO(vish): see Ewan's email about state improvements, probably # should be in a driver base class or some such -- cgit From 161507acc320f64f0581ac3242f08b3e2c258740 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Wed, 15 Jun 2011 19:43:58 +0000 Subject: Pep8 Fixes --- nova/db/sqlalchemy/api.py | 1 - 1 file changed, 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 985035cc2..32494fee7 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -839,7 +839,6 @@ def instance_destroy(context, instance_id): 'updated_at': literal_column('updated_at')}) - @require_context def instance_get_by_uuid(context, uuid, session=None): partial = _instance_get(context, session=session) -- cgit From edb2c7b518845b194c647f580e644be90984556e Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Wed, 15 Jun 2011 20:11:34 +0000 Subject: Adding uuid test --- nova/db/sqlalchemy/api.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 32494fee7..355707951 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -797,6 +797,8 @@ def instance_create(context, values): values['metadata'] = _metadata_refs(values.get('metadata')) instance_ref = models.Instance() + instance_ref['uuid'] = str(utils.gen_uuid()) + instance_ref.update(values) session = get_session() -- cgit From 70685ba0ed01685f8643c499ca78ef57763ed3b5 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Thu, 16 Jun 2011 15:02:18 +0000 Subject: Small tweaks --- 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 a1b47eded..7bdfbacd0 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -233,7 +233,7 @@ class Instance(BASE, NovaBase): os_type = Column(String(255)) vm_mode = Column(String(255)) - uuid = Column(String(32)) + uuid = Column(String(36)) # TODO(vish): see Ewan's email about state improvements, probably # should be in a driver base class or some such -- cgit From 7e63dff37b01500a90f60f9c54e45d29d959a207 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Thu, 16 Jun 2011 16:03:04 +0000 Subject: Added UUID migration --- .../versions/024_add_uuid_to_instances.py | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py new file mode 100644 index 000000000..fff194531 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py @@ -0,0 +1,41 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# +# 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 uuid + +from sqlalchemy import Column, Integer, MetaData, String, Table + + +meta = MetaData() + +instances = Table("instances", meta, + Column("id", Integer(), primary_key=True, nullable=False)) +uuid_column = Column("uuid", String(36)) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + instances.create_column(uuid_column) + + rows = migrate_engine.execute(instances.select()) + for row in rows: + instance_uuid = uuid.uuid4().hex + migrate_engine.execute(instances.update().values(uuid=instance_uuid)) + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + instances.drop_column(uuid_column) -- cgit From 277b8897cd93bf9e9c074b7a092ed35f209a83da Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Thu, 16 Jun 2011 16:06:05 +0000 Subject: Fixed UUID migration --- nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py index fff194531..ae8a83200 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py @@ -32,7 +32,7 @@ def upgrade(migrate_engine): rows = migrate_engine.execute(instances.select()) for row in rows: - instance_uuid = uuid.uuid4().hex + instance_uuid = uuid.uuid4() migrate_engine.execute(instances.update().values(uuid=instance_uuid)) -- cgit From d85aa5344935a9ba5ec5a2081ef08f09f2ceda26 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Thu, 16 Jun 2011 18:38:40 +0000 Subject: Fix copyright date --- nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py index 33979ca79..b8a10c235 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py @@ -1,4 +1,4 @@ -# Copyright 2010 OpenStack LLC. +# Copyright 2011 OpenStack LLC. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may -- cgit From f96eb29e8fca5781bdbcc70e66c48c457ce09601 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Thu, 16 Jun 2011 14:42:50 -0500 Subject: Renaming to _build_instance_get --- 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 355707951..67e4cf7f0 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -843,7 +843,7 @@ def instance_destroy(context, instance_id): @require_context def instance_get_by_uuid(context, uuid, session=None): - partial = _instance_get(context, session=session) + partial = _build_instance_get(context, session=session) result = partial.filter_by(uuid=uuid) result = result.first() if not result: @@ -855,7 +855,7 @@ def instance_get_by_uuid(context, uuid, session=None): @require_context def instance_get(context, instance_id, session=None): - partial = _instance_get(context, session=session) + partial = _build_instance_get(context, session=session) result = partial.filter_by(id=instance_id) result = result.first() if not result: @@ -864,7 +864,7 @@ def instance_get(context, instance_id, session=None): @require_context -def _instance_get(context, session=None): +def _build_instance_get(context, session=None): if not session: session = get_session() -- cgit From 1d815c177df76eb4f497a67fbdbd58fb170ca880 Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Fri, 17 Jun 2011 14:48:58 +0000 Subject: Fixed migration per review feedback. --- .../migrate_repo/versions/024_add_uuid_to_instances.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py index ae8a83200..27f30d536 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py @@ -14,10 +14,10 @@ # License for the specific language governing permissions and limitations # under the License. -import uuid - from sqlalchemy import Column, Integer, MetaData, String, Table +from nova import utils + meta = MetaData() @@ -32,8 +32,10 @@ def upgrade(migrate_engine): rows = migrate_engine.execute(instances.select()) for row in rows: - instance_uuid = uuid.uuid4() - migrate_engine.execute(instances.update().values(uuid=instance_uuid)) + instance_uuid = str(utils.gen_uuid()) + migrate_engine.execute(instances.update()\ + .where(instances.c.id == row[0])\ + .values(uuid=instance_uuid)) def downgrade(migrate_engine): -- cgit From e628ce781b7fa54f87eba919f59bccf34bd8faac Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Fri, 17 Jun 2011 14:49:54 +0000 Subject: auto load table schema instead of stubbing it out --- .../sqlalchemy/migrate_repo/versions/024_add_agent_table.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py index b8a10c235..640e96138 100644 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py +++ b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py @@ -49,14 +49,6 @@ builds = Table('agent_builds', meta, ) -# Table stub-definitions -# Just for the ForeignKey and column creation to succeed, these are not the -# actual definitions of instances or services. -# -instances = Table('instances', meta, - Column('id', Integer(), primary_key=True, nullable=False), - ) - # # New Column # @@ -74,5 +66,8 @@ def upgrade(migrate_engine): except Exception: logging.info(repr(table)) + instances = Table('instances', meta, autoload=True, + autoload_with=migrate_engine) + # Add columns to existing tables instances.create_column(architecture) -- cgit From e2fa70fb9d2b6684823328a491e18c0f98184665 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Fri, 17 Jun 2011 17:40:48 -0400 Subject: moving instance existance logic down to api layer --- nova/db/sqlalchemy/api.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 73870d2f3..7cea3ba70 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -18,7 +18,7 @@ """ Implementation of SQLAlchemy backend. """ - +import traceback import warnings from nova import db @@ -2609,7 +2609,17 @@ def zone_get_all(context): #################### + +def require_instance_exists(func): + def new_func(context, instance_id, *args, **kwargs): + db.api.instance_get(context, instance_id) + return func(context, instance_id, *args, **kwargs) + new_func.__name__ = func.__name__ + return new_func + + @require_context +@require_instance_exists def instance_metadata_get(context, instance_id): session = get_session() @@ -2625,6 +2635,7 @@ def instance_metadata_get(context, instance_id): @require_context +@require_instance_exists def instance_metadata_delete(context, instance_id, key): session = get_session() session.query(models.InstanceMetadata).\ @@ -2637,6 +2648,7 @@ def instance_metadata_delete(context, instance_id, key): @require_context +@require_instance_exists def instance_metadata_delete_all(context, instance_id): session = get_session() session.query(models.InstanceMetadata).\ @@ -2648,6 +2660,7 @@ def instance_metadata_delete_all(context, instance_id): @require_context +@require_instance_exists def instance_metadata_get_item(context, instance_id, key): session = get_session() @@ -2664,6 +2677,7 @@ def instance_metadata_get_item(context, instance_id, key): @require_context +@require_instance_exists def instance_metadata_update_or_create(context, instance_id, metadata): session = get_session() -- cgit From 56042d3a60bb76108b21261c3a4dbd8f67d6549c Mon Sep 17 00:00:00 2001 From: Rick Harris Date: Mon, 20 Jun 2011 15:15:49 +0000 Subject: Moving add_uuid migration to 025 --- .../versions/024_add_uuid_to_instances.py | 43 ---------------------- .../versions/025_add_uuid_to_instances.py | 43 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 43 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/025_add_uuid_to_instances.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py deleted file mode 100644 index 27f30d536..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_add_uuid_to_instances.py +++ /dev/null @@ -1,43 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2011 OpenStack LLC. -# -# 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 Column, Integer, MetaData, String, Table - -from nova import utils - - -meta = MetaData() - -instances = Table("instances", meta, - Column("id", Integer(), primary_key=True, nullable=False)) -uuid_column = Column("uuid", String(36)) - - -def upgrade(migrate_engine): - meta.bind = migrate_engine - instances.create_column(uuid_column) - - rows = migrate_engine.execute(instances.select()) - for row in rows: - instance_uuid = str(utils.gen_uuid()) - migrate_engine.execute(instances.update()\ - .where(instances.c.id == row[0])\ - .values(uuid=instance_uuid)) - - -def downgrade(migrate_engine): - meta.bind = migrate_engine - instances.drop_column(uuid_column) diff --git a/nova/db/sqlalchemy/migrate_repo/versions/025_add_uuid_to_instances.py b/nova/db/sqlalchemy/migrate_repo/versions/025_add_uuid_to_instances.py new file mode 100644 index 000000000..27f30d536 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/025_add_uuid_to_instances.py @@ -0,0 +1,43 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 OpenStack LLC. +# +# 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 Column, Integer, MetaData, String, Table + +from nova import utils + + +meta = MetaData() + +instances = Table("instances", meta, + Column("id", Integer(), primary_key=True, nullable=False)) +uuid_column = Column("uuid", String(36)) + + +def upgrade(migrate_engine): + meta.bind = migrate_engine + instances.create_column(uuid_column) + + rows = migrate_engine.execute(instances.select()) + for row in rows: + instance_uuid = str(utils.gen_uuid()) + migrate_engine.execute(instances.update()\ + .where(instances.c.id == row[0])\ + .values(uuid=instance_uuid)) + + +def downgrade(migrate_engine): + meta.bind = migrate_engine + instances.drop_column(uuid_column) -- cgit From f94041278e22acc557dc878bbf3f1b1f70351446 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Mon, 20 Jun 2011 21:01:14 +0000 Subject: Other migrations have been merged in before us, so renumber --- .../migrate_repo/versions/024_add_agent_table.py | 73 ---------------------- .../migrate_repo/versions/026_add_agent_table.py | 73 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 73 deletions(-) delete mode 100644 nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py create mode 100644 nova/db/sqlalchemy/migrate_repo/versions/026_add_agent_table.py (limited to 'nova/db') diff --git a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py deleted file mode 100644 index 640e96138..000000000 --- a/nova/db/sqlalchemy/migrate_repo/versions/024_add_agent_table.py +++ /dev/null @@ -1,73 +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 Boolean, Column, DateTime, Integer -from sqlalchemy import MetaData, String, Table -from nova import log as logging - -meta = MetaData() - -# -# New Tables -# -builds = Table('agent_builds', 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('hypervisor', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('os', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('architecture', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('version', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('url', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - Column('md5hash', - String(length=255, convert_unicode=False, assert_unicode=None, - unicode_error=None, _warn_on_bytestring=False)), - ) - - -# -# New Column -# - -architecture = Column('architecture', String(length=255)) - - -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 (builds, ): - try: - table.create() - except Exception: - logging.info(repr(table)) - - instances = Table('instances', meta, autoload=True, - autoload_with=migrate_engine) - - # Add columns to existing tables - instances.create_column(architecture) diff --git a/nova/db/sqlalchemy/migrate_repo/versions/026_add_agent_table.py b/nova/db/sqlalchemy/migrate_repo/versions/026_add_agent_table.py new file mode 100644 index 000000000..640e96138 --- /dev/null +++ b/nova/db/sqlalchemy/migrate_repo/versions/026_add_agent_table.py @@ -0,0 +1,73 @@ +# 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 Boolean, Column, DateTime, Integer +from sqlalchemy import MetaData, String, Table +from nova import log as logging + +meta = MetaData() + +# +# New Tables +# +builds = Table('agent_builds', 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('hypervisor', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('os', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('architecture', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('version', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('url', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + Column('md5hash', + String(length=255, convert_unicode=False, assert_unicode=None, + unicode_error=None, _warn_on_bytestring=False)), + ) + + +# +# New Column +# + +architecture = Column('architecture', String(length=255)) + + +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 (builds, ): + try: + table.create() + except Exception: + logging.info(repr(table)) + + instances = Table('instances', meta, autoload=True, + autoload_with=migrate_engine) + + # Add columns to existing tables + instances.create_column(architecture) -- cgit