diff --git a/api-daemon/migrations/README b/api-daemon/migrations/README new file mode 100644 index 00000000..98e4f9c4 --- /dev/null +++ b/api-daemon/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/api-daemon/migrations/alembic.ini b/api-daemon/migrations/alembic.ini new file mode 100644 index 00000000..f8ed4801 --- /dev/null +++ b/api-daemon/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/api-daemon/migrations/env.py b/api-daemon/migrations/env.py new file mode 100644 index 00000000..23663ff2 --- /dev/null +++ b/api-daemon/migrations/env.py @@ -0,0 +1,87 @@ +from __future__ import with_statement +from alembic import context +from sqlalchemy import engine_from_config, pool +from logging.config import fileConfig +import logging + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +from flask import current_app +config.set_main_option('sqlalchemy.url', + current_app.config.get('SQLALCHEMY_DATABASE_URI')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure(url=url) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + engine = engine_from_config(config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool) + + connection = engine.connect() + context.configure(connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args) + + try: + with context.begin_transaction(): + context.run_migrations() + finally: + connection.close() + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/api-daemon/migrations/script.py.mako b/api-daemon/migrations/script.py.mako new file mode 100644 index 00000000..2c015630 --- /dev/null +++ b/api-daemon/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/api-daemon/migrations/versions/2d1daa722a0a_pvc_version_0_6.py b/api-daemon/migrations/versions/2d1daa722a0a_pvc_version_0_6.py new file mode 100644 index 00000000..8a0da4bd --- /dev/null +++ b/api-daemon/migrations/versions/2d1daa722a0a_pvc_version_0_6.py @@ -0,0 +1,112 @@ +"""PVC version 0.6 + +Revision ID: 2d1daa722a0a +Revises: +Create Date: 2020-02-15 23:14:14.733134 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2d1daa722a0a' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('network_template', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('mac_template', sa.Text(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('script', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('script', sa.Text(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('storage_template', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('system_template', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('vcpu_count', sa.Integer(), nullable=False), + sa.Column('vram_mb', sa.Integer(), nullable=False), + sa.Column('serial', sa.Boolean(), nullable=False), + sa.Column('vnc', sa.Boolean(), nullable=False), + sa.Column('vnc_bind', sa.Text(), nullable=True), + sa.Column('node_limit', sa.Text(), nullable=True), + sa.Column('node_selector', sa.Text(), nullable=True), + sa.Column('node_autostart', sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('userdata', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('userdata', sa.Text(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('network', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('network_template', sa.Integer(), nullable=True), + sa.Column('vni', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['network_template'], ['network_template.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('profile', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.Text(), nullable=False), + sa.Column('system_template', sa.Integer(), nullable=True), + sa.Column('network_template', sa.Integer(), nullable=True), + sa.Column('storage_template', sa.Integer(), nullable=True), + sa.Column('userdata', sa.Integer(), nullable=True), + sa.Column('script', sa.Integer(), nullable=True), + sa.Column('arguments', sa.Text(), nullable=True), + sa.ForeignKeyConstraint(['network_template'], ['network_template.id'], ), + sa.ForeignKeyConstraint(['script'], ['script.id'], ), + sa.ForeignKeyConstraint(['storage_template'], ['storage_template.id'], ), + sa.ForeignKeyConstraint(['system_template'], ['system_template.id'], ), + sa.ForeignKeyConstraint(['userdata'], ['userdata.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('name') + ) + op.create_table('storage', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('storage_template', sa.Integer(), nullable=True), + sa.Column('pool', sa.Text(), nullable=False), + sa.Column('disk_id', sa.Text(), nullable=False), + sa.Column('source_volume', sa.Text(), nullable=True), + sa.Column('disk_size_gb', sa.Integer(), nullable=True), + sa.Column('mountpoint', sa.Text(), nullable=True), + sa.Column('filesystem', sa.Text(), nullable=True), + sa.Column('filesystem_args', sa.Text(), nullable=True), + sa.ForeignKeyConstraint(['storage_template'], ['storage_template.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('storage') + op.drop_table('profile') + op.drop_table('network') + op.drop_table('userdata') + op.drop_table('system_template') + op.drop_table('storage_template') + op.drop_table('script') + op.drop_table('network_template') + # ### end Alembic commands ### diff --git a/api-daemon/pvc-api-db-init b/api-daemon/pvc-api-db-init deleted file mode 100755 index a5e69128..00000000 --- a/api-daemon/pvc-api-db-init +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -# Initialize the PVC database and migrations for future upgrades -# Part of the Parallel Virtual Cluster (PVC) system - -export PVC_CONFIG_FILE="/etc/pvc/pvcapid.yaml" - -if [[ ! -f ${PVC_CONFIG_FILE} ]]; then - echo "Create a configuration file at ${PVC_CONFIG_FILE} before initializing the database." - exit 1 -fi - -pushd /usr/share/pvc -./pvcapid-manage.py db init -./pvcapid-manage.py db migrate -./pvcapid-manage.py db upgrade -popd diff --git a/api-daemon/pvc-api-db-upgrade b/api-daemon/pvc-api-db-upgrade index 6db47c10..54a16bbe 100755 --- a/api-daemon/pvc-api-db-upgrade +++ b/api-daemon/pvc-api-db-upgrade @@ -11,6 +11,5 @@ if [[ ! -f ${PVC_CONFIG_FILE} ]]; then fi pushd /usr/share/pvc -./pvcapid-manage.py db migrate ./pvcapid-manage.py db upgrade popd diff --git a/debian/pvc-daemon-api.install b/debian/pvc-daemon-api.install index 57054cbf..c6aff3d4 100644 --- a/debian/pvc-daemon-api.install +++ b/debian/pvc-daemon-api.install @@ -7,3 +7,4 @@ api-daemon/pvcapid usr/share/pvc api-daemon/pvcapid.service lib/systemd/system api-daemon/pvcapid-worker.service lib/systemd/system api-daemon/provisioner usr/share/pvc +api-daemon/migrations usr/share/pvc