-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for provisioning of tool models
The already known provisioning feature for read-only sessions will be extended to persistent workspace sessions. Per session, a user can request a persistent workspace session and can pass optionally one model to provision. It's not possible to provision more than one model (aka one Git repository). The provisioning takes place once. We'll not touch the provisioned workspace until the user explicently resets the provisioning. The provisioning revision and date are stored in the database. When the users reset their workspace, we'll remove the provisioning model from the database. During the next session start, the matching workspace will be re-initialized and a copy will be saved to a `.bak` directory. This feature is essential for the start-up of trainings.
- Loading branch information
1 parent
ce0f9de
commit 1a5b4b4
Showing
40 changed files
with
933 additions
and
622 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
backend/capellacollab/alembic/versions/014438261702_add_provisioning_feature.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,51 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Add provisioning feature | ||
Revision ID: 014438261702 | ||
Revises: 3818a5009130 | ||
Create Date: 2024-10-11 17:34:05.210906 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "014438261702" | ||
down_revision = "3818a5009130" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"model_provisioning", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("user_id", sa.Integer(), nullable=False), | ||
sa.Column("tool_model_id", sa.Integer(), nullable=False), | ||
sa.Column("revision", sa.String(), nullable=False), | ||
sa.Column("commit_hash", sa.String(), nullable=False), | ||
sa.Column("provisioned_at", sa.DateTime(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["tool_model_id"], | ||
["models.id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], | ||
["users.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
op.f("ix_model_provisioning_id"), | ||
"model_provisioning", | ||
["id"], | ||
unique=False, | ||
) | ||
op.add_column( | ||
"sessions", sa.Column("provisioning_id", sa.Integer(), nullable=True) | ||
) | ||
op.create_foreign_key( | ||
None, "sessions", "model_provisioning", ["provisioning_id"], ["id"] | ||
) |
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
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
2 changes: 2 additions & 0 deletions
2
backend/capellacollab/projects/toolmodels/provisioning/__init__.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,2 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 |
36 changes: 36 additions & 0 deletions
36
backend/capellacollab/projects/toolmodels/provisioning/crud.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,36 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.projects.toolmodels import models as toolmodels_models | ||
from capellacollab.users import models as users_models | ||
|
||
from . import models | ||
|
||
|
||
def create_project_provisioning( | ||
db: orm.Session, model: models.DatabaseModelProvisioning | ||
): | ||
db.add(model) | ||
db.commit() | ||
|
||
|
||
def get_project_provisioning( | ||
db: orm.Session, | ||
tool_model: toolmodels_models.DatabaseToolModel, | ||
user: users_models.DatabaseUser, | ||
) -> models.DatabaseModelProvisioning | None: | ||
return db.execute( | ||
sa.select(models.DatabaseModelProvisioning) | ||
.where(models.DatabaseModelProvisioning.tool_model == tool_model) | ||
.where(models.DatabaseModelProvisioning.user == user) | ||
).scalar_one_or_none() | ||
|
||
|
||
def delete_project_provisioning( | ||
db: orm.Session, provisioning: models.DatabaseModelProvisioning | ||
): | ||
db.delete(provisioning) | ||
db.commit() |
28 changes: 28 additions & 0 deletions
28
backend/capellacollab/projects/toolmodels/provisioning/injectables.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,28 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
import fastapi | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.core import database | ||
from capellacollab.users import injectables as users_injectables | ||
from capellacollab.users import models as users_models | ||
|
||
from .. import injectables as toolmodels_injectables | ||
from .. import models as toolmodels_models | ||
from . import crud, models | ||
|
||
|
||
def get_model_provisioning( | ||
model: toolmodels_models.DatabaseToolModel = fastapi.Depends( | ||
toolmodels_injectables.get_existing_capella_model | ||
), | ||
current_user: users_models.DatabaseUser = fastapi.Depends( | ||
users_injectables.get_own_user | ||
), | ||
db: orm.Session = fastapi.Depends(database.get_db), | ||
) -> models.DatabaseModelProvisioning | None: | ||
return crud.get_project_provisioning( | ||
db, tool_model=model, user=current_user | ||
) |
60 changes: 60 additions & 0 deletions
60
backend/capellacollab/projects/toolmodels/provisioning/models.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,60 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import datetime | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.core import database | ||
from capellacollab.core import pydantic as core_pydantic | ||
from capellacollab.projects.toolmodels import ( | ||
models as projects_toolmodels_models, | ||
) | ||
from capellacollab.sessions import models as sessions_models | ||
from capellacollab.users import models as users_models | ||
|
||
|
||
class ModelProvisioning(core_pydantic.BaseModel): | ||
session: sessions_models.Session | None | ||
provisioned_at: datetime.datetime | ||
revision: datetime.datetime | ||
|
||
|
||
class DatabaseModelProvisioning(database.Base): | ||
__tablename__ = "model_provisioning" | ||
|
||
id: orm.Mapped[int] = orm.mapped_column( | ||
init=False, primary_key=True, index=True | ||
) | ||
|
||
user_id: orm.Mapped[int] = orm.mapped_column( | ||
sa.ForeignKey("users.id"), | ||
init=False, | ||
) | ||
user: orm.Mapped[users_models.DatabaseUser] = orm.relationship( | ||
foreign_keys=[user_id] | ||
) | ||
|
||
tool_model_id: orm.Mapped[int] = orm.mapped_column( | ||
sa.ForeignKey("models.id"), | ||
init=False, | ||
) | ||
tool_model: orm.Mapped[projects_toolmodels_models.DatabaseToolModel] = ( | ||
orm.relationship( | ||
foreign_keys=[tool_model_id], | ||
) | ||
) | ||
|
||
revision: orm.Mapped[str] | ||
commit_hash: orm.Mapped[str] | ||
|
||
provisioned_at: orm.Mapped[datetime.datetime] = orm.mapped_column( | ||
default=datetime.datetime.now(datetime.UTC) | ||
) | ||
|
||
session: orm.Mapped[sessions_models.DatabaseSession | None] = ( | ||
orm.relationship( | ||
uselist=False, back_populates="provisioning", default=None | ||
) | ||
) |
44 changes: 44 additions & 0 deletions
44
backend/capellacollab/projects/toolmodels/provisioning/routes.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,44 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import fastapi | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.core import database | ||
from capellacollab.core.authentication import injectables as auth_injectables | ||
from capellacollab.projects.users import models as projects_users_models | ||
|
||
from . import crud, injectables, models | ||
|
||
router = fastapi.APIRouter( | ||
dependencies=[ | ||
fastapi.Depends( | ||
auth_injectables.ProjectRoleVerification( | ||
required_role=projects_users_models.ProjectUserRole.USER | ||
) | ||
) | ||
], | ||
) | ||
|
||
|
||
@router.get("", response_model=models.ModelProvisioning) | ||
def get_provisioning( | ||
provisioning: models.DatabaseModelProvisioning = fastapi.Depends( | ||
injectables.get_model_provisioning | ||
), | ||
) -> models.DatabaseModelProvisioning: | ||
return provisioning | ||
|
||
|
||
@router.delete("", status_code=204) | ||
def reset_provisioning( | ||
provisioning: models.DatabaseModelProvisioning = fastapi.Depends( | ||
injectables.get_model_provisioning | ||
), | ||
db: orm.Session = fastapi.Depends(database.get_db), | ||
): | ||
"""This will delete the provisioning data from the workspace. | ||
During the next session request, the existing provisioning will be overwritten in the workspace. | ||
""" | ||
|
||
crud.delete_project_provisioning(db, provisioning) |
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
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
Oops, something went wrong.