-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into nm/post-collect-step
- Loading branch information
Showing
12 changed files
with
200 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright (c) 2024. Some Engineering | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
import logging | ||
from datetime import timedelta | ||
from typing import Any, Optional | ||
|
||
from fixcloudutils.asyncio.periodic import Periodic | ||
from fixcloudutils.service import Service | ||
|
||
from fixbackend.cloud_accounts.service import CloudAccountService | ||
from fixbackend.config import ProductTierSettings | ||
from fixbackend.ids import ProductTier | ||
from fixbackend.types import AsyncSessionMaker | ||
from fixbackend.workspaces.repository import WorkspaceRepository | ||
from fixbackend.config import Config | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class FreeTierCleanupService(Service): | ||
|
||
def __init__( | ||
self, | ||
workspace_repository: WorkspaceRepository, | ||
session_maker: AsyncSessionMaker, | ||
cloud_account_service: CloudAccountService, | ||
config: Config, | ||
): | ||
self.workspace_repository = workspace_repository | ||
self.cloud_account_service = cloud_account_service | ||
self.session_maker = session_maker | ||
self.config = config | ||
self.periodic: Optional[Periodic] = Periodic( | ||
"clean_up_free_tiers", | ||
self.cleanup_free_tiers, | ||
frequency=timedelta(minutes=60), | ||
first_run=timedelta(seconds=30), | ||
) | ||
self.free_tier_cleanup_timeout = timedelta(days=self.config.free_tier_cleanup_timeout_days) | ||
|
||
async def start(self) -> Any: | ||
if self.periodic: | ||
await self.periodic.start() | ||
|
||
async def stop(self) -> None: | ||
if self.periodic: | ||
await self.periodic.stop() | ||
|
||
async def cleanup_free_tiers(self) -> None: | ||
workspaces = await self.workspace_repository.list_overdue_free_tier_cleanup( | ||
been_in_free_tier_for=self.free_tier_cleanup_timeout | ||
) | ||
for workspace in workspaces: | ||
account_limit = ProductTierSettings[ProductTier.Free].account_limit | ||
|
||
if account_limit is None: | ||
continue | ||
|
||
accounts = await self.cloud_account_service.list_accounts(workspace.id) | ||
|
||
if len(accounts) <= account_limit: | ||
continue | ||
|
||
log.info( | ||
f"Cleaning up workspace {workspace.id}" | ||
" because it has been in free tier for" | ||
f"{self.free_tier_cleanup_timeout}." | ||
) | ||
for i, account in enumerate(accounts): | ||
if i < account_limit: | ||
continue | ||
|
||
log.info( | ||
f"Deleting cloud account {account.id} on workspace {workspace.id} " "because it is over the limit." | ||
) | ||
await self.cloud_account_service.delete_cloud_account(workspace.owner_id, account.id, workspace.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
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
29 changes: 29 additions & 0 deletions
29
migrations/versions/2024-07-30T17:09:40Z_free_tier_account_removal.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,29 @@ | ||
"""free tier account removal | ||
Revision ID: dbe8f626f045 | ||
Revises: 2e39a90ed4ac | ||
Create Date: 2024-07-30 17:09:40.293980+00:00 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from fixbackend.sqlalechemy_extensions import UTCDateTime | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "dbe8f626f045" | ||
down_revision: Union[str, None] = "2e39a90ed4ac" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.add_column("organization", sa.Column("tier_updated_at", UTCDateTime(timezone=True), nullable=True)) | ||
op.create_index(op.f("ix_organization_tier_updated_at"), "organization", ["tier_updated_at"], unique=False) | ||
|
||
|
||
def downgrade() -> None: | ||
pass |
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