This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add domain_name, group_id, user_uuid fields in kernels (#148)
Migration: * 'default' group in the 'default' domain will be created. * Those fields are automatically filled in migration with values of (domain_name = 'default', group_id=<gid of the default group>, user_uuid = user's uuid whose access_key is used to run a container>) * Every user is automatically associated with the 'default' group.
- 22.03.3
- 22.03.2
- 22.03.1
- 22.03.0
- 22.03.0b2
- 22.03.0b1
- 22.03.0a2
- 22.03.0a1
- 21.09.9
- 21.09.8
- 21.09.7
- 21.09.6
- 21.09.5
- 21.09.4
- 21.09.3
- 21.09.2
- 21.09.1
- 21.09.0
- 21.09.0a2
- 21.09.0a1
- 21.03.32
- 21.03.31
- 21.03.30
- 21.03.29
- 21.03.28
- 21.03.27
- 21.03.26
- 21.03.25
- 21.03.24
- 21.03.23
- 21.03.22
- 21.03.21
- 21.03.20
- 21.03.19
- 21.03.18
- 21.03.17
- 21.03.16
- 21.03.15
- 21.03.14
- 21.03.13
- 21.03.12
- 21.03.11
- 21.03.10
- 21.03.9
- 21.03.8
- 21.03.7
- 21.03.6
- 21.03.5
- 21.03.4
- 21.03.3
- 21.03.2
- 21.03.1
- 21.03.0
- 20.09.29
- 20.09.28
- 20.09.27
- 20.09.26
- 20.09.25
- 20.09.24
- 20.09.23
- 20.09.22
- 20.09.21
- 20.09.20
- 20.09.19
- 20.09.18
- 20.09.17
- 20.09.16
- 20.09.15
- 20.09.14
- 20.09.13
- 20.09.12
- 20.09.11
- 20.09.10
- 20.09.9
- 20.09.8
- 20.09.7
- 20.09.6
- 20.09.5
- 20.09.4
- 20.09.3
- 20.09.2
- 20.09.1
- 20.09.0
- 20.09.0rc3
- 20.09.0rc2
- 20.09.0rc1
- 20.09.0b6
- 20.09.0b5
- 20.09.0b4
- 20.09.0b3
- 20.09.0b2
- 20.09.0b1
- 20.09.0a4
- 20.09.0a3
- 20.09.0a2
- 20.09.0a1
- 20.03.19
- 20.03.18
- 20.03.17
- 20.03.16
- 20.03.15
- 20.03.14
- 20.03.13
- 20.03.12
- 20.03.11
- 20.03.10
- 20.03.9
- 20.03.8
- 20.03.7
- 20.03.6
- 20.03.5
- 20.03.4
- 20.03.3
- 20.03.2
- 20.03.1
- 20.03.0
- 20.03.0rc1
- 20.03.0b2
- 20.03.0b1
- 19.12.0a2
- 19.12.0a1
- 19.09.27
- 19.09.26
- 19.09.25
- 19.09.24
- 19.09.23
- 19.09.22
- 19.09.21
- 19.09.19
- 19.09.18
- 19.09.17
- 19.09.16
- 19.09.15
- 19.09.14
- 19.09.13
- 19.09.12
- 19.09.11
- 19.09.10
- 19.09.9
- 19.09.8
- 19.09.7
- 19.09.6
- 19.09.5
- 19.09.4
- 19.09.3
- 19.09.2
- 19.09.1
- 19.09.0
- 19.09.0rc4
- 19.09.0rc3
- 19.09.0rc2
- 19.09.0rc1
- 19.09.0b14
- 19.09.0b13
- 19.09.0b12
- 19.09.0b11
- 19.09.0b10
- 19.09.0b9
- 19.09.0b8
- 19.09.0b7
- 19.09.0b6
- 19.09.0b5
- 19.06.0b4
- 19.06.0b3
- 19.06.0b2
- 19.06.0b1
- 19.06.0a1
Showing
2 changed files
with
99 additions
and
3 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
alembic/versions/c5e4e764f9e3_add_domain_group_user_fields_to_kernels.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"""add domain, group, user fields to kernels | ||
Revision ID: c5e4e764f9e3 | ||
Revises: 6f1c1b83870a | ||
Create Date: 2019-05-28 10:22:56.904061 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import ai.backend.manager.models.base # noqa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'c5e4e764f9e3' | ||
down_revision = '6f1c1b83870a' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column('kernels', sa.Column('domain_name', sa.String(length=64), nullable=True)) | ||
op.add_column('kernels', sa.Column('group_id', ai.backend.manager.models.base.GUID(), nullable=True)) | ||
op.add_column('kernels', sa.Column('user_uuid', ai.backend.manager.models.base.GUID(), nullable=True)) | ||
op.create_foreign_key(op.f('fk_kernels_group_id_groups'), 'kernels', 'groups', ['group_id'], ['id']) | ||
op.create_foreign_key(op.f('fk_kernels_user_uuid_users'), 'kernels', 'users', ['user_uuid'], ['uuid']) | ||
op.create_foreign_key(op.f('fk_kernels_domain_name_domains'), 'kernels', 'domains', ['domain_name'], ['name']) | ||
# ### end Alembic commands ### | ||
|
||
import textwrap | ||
from ai.backend.manager.models import kernels, keypairs, groups, users, association_groups_users | ||
|
||
# Create default group in the default domain. | ||
# Assumption: "default" domain must exist | ||
connection = op.get_bind() | ||
query = (sa.insert(groups) | ||
.values(name='default', description='Default group', is_active=True, | ||
domain_name='default')) | ||
query = textwrap.dedent('''\ | ||
INSERT INTO groups (name, description, is_active, domain_name) | ||
VALUES ('default', 'Default group', True, 'default') | ||
ON CONFLICT (name, domain_name) DO NOTHING | ||
RETURNING id; | ||
''') | ||
result = connection.execute(query).fetchone() | ||
gid = result.id if hasattr(result, 'id') else None | ||
if gid is None: # group already exists | ||
query = textwrap.dedent('''\ | ||
SELECT id FROM groups where name = 'default' and domain_name = 'default'; | ||
''') | ||
gid = connection.execute(query).fetchone().id | ||
|
||
# Fill in kernels' domain_name, group_id, and user_uuid. | ||
query = sa.select([kernels]).select_from(kernels) | ||
all_kernels = connection.execute(query).fetchall() | ||
for kernel in all_kernels: | ||
# Get kernel's keypair (access_key). | ||
query = (sa.select([keypairs.c.user]).select_from(keypairs) | ||
.where(keypairs.c.access_key == kernel.access_key)) | ||
kp = connection.execute(query).fetchone() | ||
# Update kernel information. | ||
query = '''\ | ||
UPDATE kernels SET domain_name = 'default', group_id = '%s', user_uuid = '%s' | ||
WHERE id = '%s'; | ||
''' % (gid, kp.user, kernel.id) | ||
connection.execute(query) | ||
|
||
# Associate every users with the default group. | ||
# NOTE: this operation is not undoable unless you drop groups table. | ||
query = sa.select([users.c.uuid]).select_from(users) | ||
all_users = connection.execute(query).fetchall() | ||
for user in all_users: | ||
query = '''\ | ||
INSERT INTO association_groups_users (group_id, user_id) | ||
VALUES ('%s', '%s') | ||
ON CONFLICT (group_id, user_id) DO NOTHING; | ||
''' % (gid, user.uuid,) | ||
connection.execute(query) | ||
|
||
# Make kernel's new fields non-nullable. | ||
op.alter_column('kernels', column_name='domain_name', nullable=False) | ||
op.alter_column('kernels', column_name='group_id', nullable=False) | ||
op.alter_column('kernels', column_name='user_uuid', nullable=False) | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint(op.f('fk_kernels_domain_name_domains'), 'kernels', type_='foreignkey') | ||
op.drop_constraint(op.f('fk_kernels_user_uuid_users'), 'kernels', type_='foreignkey') | ||
op.drop_constraint(op.f('fk_kernels_group_id_groups'), 'kernels', type_='foreignkey') | ||
op.drop_column('kernels', 'user_uuid') | ||
op.drop_column('kernels', 'group_id') | ||
op.drop_column('kernels', 'domain_name') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters