Skip to content

Commit

Permalink
cli: add command to update users disk quota usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidalgarcia committed Oct 7, 2020
1 parent 8203b6a commit ad8ee37
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions reana_db/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from alembic import config as alembic_config
from reana_db.database import init_db
from reana_db.models import Resource
from reana_db.utils import update_users_disk_quota


@click.group()
Expand Down Expand Up @@ -219,3 +220,17 @@ def create_default_resources():
"No action to be taken: default resources already exist.", fg="yellow"
)
sys.exit(1)


@quota_group.command()
def disk_usage_update():
"""Update users disk quota usage based on user workspace."""
try:
update_users_disk_quota()
click.secho("Users disk quota usage updated successfully.", fg="green")
except Exception as e:
click.secho(
f"[ERROR]: An error occurred when updating users disk quota usage: {repr(e)}",
fg="red",
)
sys.exit(1)
35 changes: 35 additions & 0 deletions reana_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,38 @@ def _get_workflow_by_uuid(workflow_uuid):
"variable appropriately.".format(workflow_uuid)
)
return workflow


def update_users_disk_quota(user=None):
"""Update users disk quota usage.
:param user: User whose disk quota will be updated. If None, applies to all users.
:type user: reana_db.models.User.
"""
from reana_commons.utils import get_disk_usage

from reana_db.config import DEFAULT_QUOTA_RESOURCES
from reana_db.models import Resource, User, UserResource

users = [user] if user else User.query.all()

for u in users:
workspace_path = u.get_user_workspace()
disk_usage_bytes = get_disk_usage(
workspace_path, summarize=True, block_size="b"
)
disk_usage_bytes = int(disk_usage_bytes[0]["size"])

disk_resource = Resource.query.filter_by(
name=DEFAULT_QUOTA_RESOURCES["disk"]
).one_or_none()

if disk_resource:
from .database import Session

user_resource_quota = UserResource.query.filter_by(
user_id=u.id_, resource_id=disk_resource.id_
).first()
user_resource_quota.quota_used = disk_usage_bytes
Session.commit()

0 comments on commit ad8ee37

Please sign in to comment.