OpenStack Image Management (Glance)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2016 Rackspace
  2. # Copyright 2013 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. import os
  16. import sys
  17. from alembic import command as alembic_command
  18. from alembic import config as alembic_config
  19. from alembic import migration as alembic_migration
  20. from alembic import script as alembic_script
  21. from sqlalchemy import MetaData, Table
  22. from oslo_db import exception as db_exception
  23. from oslo_db.sqlalchemy import migration as sqla_migration
  24. from glance.db import migration as db_migration
  25. from glance.db.sqlalchemy import api as db_api
  26. from glance.i18n import _
  27. def get_alembic_config(engine=None):
  28. """Return a valid alembic config object"""
  29. ini_path = os.path.join(os.path.dirname(__file__), 'alembic.ini')
  30. config = alembic_config.Config(os.path.abspath(ini_path))
  31. if engine is None:
  32. engine = db_api.get_engine()
  33. config.set_main_option('sqlalchemy.url', str(engine.url))
  34. return config
  35. def get_current_alembic_heads():
  36. """Return current heads (if any) from the alembic migration table"""
  37. engine = db_api.get_engine()
  38. with engine.connect() as conn:
  39. context = alembic_migration.MigrationContext.configure(conn)
  40. heads = context.get_current_heads()
  41. def update_alembic_version(old, new):
  42. """Correct alembic head in order to upgrade DB using EMC method.
  43. :param:old: Actual alembic head
  44. :param:new: Expected alembic head to be updated
  45. """
  46. meta = MetaData(engine)
  47. alembic_version = Table('alembic_version', meta, autoload=True)
  48. alembic_version.update().values(
  49. version_num=new).where(
  50. alembic_version.c.version_num == old).execute()
  51. if ("pike01" in heads):
  52. update_alembic_version("pike01", "pike_contract01")
  53. elif ("ocata01" in heads):
  54. update_alembic_version("ocata01", "ocata_contract01")
  55. heads = context.get_current_heads()
  56. return heads
  57. def get_current_legacy_head():
  58. try:
  59. legacy_head = sqla_migration.db_version(db_api.get_engine(),
  60. db_migration.MIGRATE_REPO_PATH,
  61. db_migration.INIT_VERSION)
  62. except db_exception.DBMigrationError:
  63. legacy_head = None
  64. return legacy_head
  65. def is_database_under_alembic_control():
  66. if get_current_alembic_heads():
  67. return True
  68. return False
  69. def is_database_under_migrate_control():
  70. if get_current_legacy_head():
  71. return True
  72. return False
  73. def place_database_under_alembic_control():
  74. a_config = get_alembic_config()
  75. if not is_database_under_migrate_control():
  76. return
  77. if not is_database_under_alembic_control():
  78. print(_("Database is currently not under Alembic's migration "
  79. "control."))
  80. head = get_current_legacy_head()
  81. if head == 42:
  82. alembic_version = 'liberty'
  83. elif head == 43:
  84. alembic_version = 'mitaka01'
  85. elif head == 44:
  86. alembic_version = 'mitaka02'
  87. elif head == 45:
  88. alembic_version = 'ocata01'
  89. elif head in range(1, 42):
  90. print("Legacy head: ", head)
  91. sys.exit(_("The current database version is not supported any "
  92. "more. Please upgrade to Liberty release first."))
  93. else:
  94. sys.exit(_("Unable to place database under Alembic's migration "
  95. "control. Unknown database state, can't proceed "
  96. "further."))
  97. print(_("Placing database under Alembic's migration control at "
  98. "revision:"), alembic_version)
  99. alembic_command.stamp(a_config, alembic_version)
  100. def get_alembic_branch_head(branch):
  101. """Return head revision name for particular branch"""
  102. a_config = get_alembic_config()
  103. script = alembic_script.ScriptDirectory.from_config(a_config)
  104. return script.revision_map.get_current_head(branch)