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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # str(sqlalchemy.engine.url.URL) returns a RFC-1738 quoted URL.
  34. # This means that a password like "foo@" will be turned into
  35. # "foo%40". This causes a problem for set_main_option() here
  36. # because that uses ConfigParser.set, which (by design) uses
  37. # *python* interpolation to write the string out ... where "%" is
  38. # the special python interpolation character! Avoid this
  39. # mis-match by quoting all %'s for the set below.
  40. quoted_engine_url = str(engine.url).replace('%', '%%')
  41. config.set_main_option('sqlalchemy.url', quoted_engine_url)
  42. return config
  43. def get_current_alembic_heads():
  44. """Return current heads (if any) from the alembic migration table"""
  45. engine = db_api.get_engine()
  46. with engine.connect() as conn:
  47. context = alembic_migration.MigrationContext.configure(conn)
  48. heads = context.get_current_heads()
  49. def update_alembic_version(old, new):
  50. """Correct alembic head in order to upgrade DB using EMC method.
  51. :param:old: Actual alembic head
  52. :param:new: Expected alembic head to be updated
  53. """
  54. meta = MetaData(engine)
  55. alembic_version = Table('alembic_version', meta, autoload=True)
  56. alembic_version.update().values(
  57. version_num=new).where(
  58. alembic_version.c.version_num == old).execute()
  59. if ("pike01" in heads):
  60. update_alembic_version("pike01", "pike_contract01")
  61. elif ("ocata01" in heads):
  62. update_alembic_version("ocata01", "ocata_contract01")
  63. heads = context.get_current_heads()
  64. return heads
  65. def get_current_legacy_head():
  66. try:
  67. legacy_head = sqla_migration.db_version(db_api.get_engine(),
  68. db_migration.MIGRATE_REPO_PATH,
  69. db_migration.INIT_VERSION)
  70. except db_exception.DBMigrationError:
  71. legacy_head = None
  72. return legacy_head
  73. def is_database_under_alembic_control():
  74. if get_current_alembic_heads():
  75. return True
  76. return False
  77. def is_database_under_migrate_control():
  78. if get_current_legacy_head():
  79. return True
  80. return False
  81. def place_database_under_alembic_control():
  82. a_config = get_alembic_config()
  83. if not is_database_under_migrate_control():
  84. return
  85. if not is_database_under_alembic_control():
  86. print(_("Database is currently not under Alembic's migration "
  87. "control."))
  88. head = get_current_legacy_head()
  89. if head == 42:
  90. alembic_version = 'liberty'
  91. elif head == 43:
  92. alembic_version = 'mitaka01'
  93. elif head == 44:
  94. alembic_version = 'mitaka02'
  95. elif head == 45:
  96. alembic_version = 'ocata01'
  97. elif head in range(1, 42):
  98. print("Legacy head: ", head)
  99. sys.exit(_("The current database version is not supported any "
  100. "more. Please upgrade to Liberty release first."))
  101. else:
  102. sys.exit(_("Unable to place database under Alembic's migration "
  103. "control. Unknown database state, can't proceed "
  104. "further."))
  105. print(_("Placing database under Alembic's migration control at "
  106. "revision:"), alembic_version)
  107. alembic_command.stamp(a_config, alembic_version)
  108. def get_alembic_branch_head(branch):
  109. """Return head revision name for particular branch"""
  110. a_config = get_alembic_config()
  111. script = alembic_script.ScriptDirectory.from_config(a_config)
  112. return script.revision_map.get_current_head(branch)