Skip to content

Commit

Permalink
add build quota
Browse files Browse the repository at this point in the history
Signed-off-by: Li Chaoran <[email protected]>
  • Loading branch information
pkking committed Aug 17, 2023
1 parent cb128c5 commit 8c1e18b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions frontend/coprs_frontend/coprs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ class Config(object):

PACKAGES_COUNT = False

BUILD_QUOTA = 0

class ProductionConfig(Config):
DEBUG = False
# SECRET_KEY = "put_some_secret_here"
Expand Down
3 changes: 3 additions & 0 deletions frontend/coprs_frontend/coprs/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ class NoPackageSourceException(Exception):

class UnrepeatableBuildException(Exception):
pass

class InsufficientBuildQuota(Exception):
pass
27 changes: 27 additions & 0 deletions frontend/coprs_frontend/coprs/logic/builds_logic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import tempfile
import shutil
import json
Expand Down Expand Up @@ -28,6 +29,7 @@
ConflictingRequest,
DuplicateException,
InsufficientRightsException,
InsufficientBuildQuota,
InsufficientStorage,
MalformedArgumentException,
UnrepeatableBuildException,
Expand Down Expand Up @@ -804,6 +806,8 @@ def add(cls, user, pkgs, copr, source_type=None, source_json=None,
users_logic.UsersLogic.raise_if_cant_build_in_copr(
user, copr,
"You don't have permissions to build in this copr.")
if BuildsLogic.is_out_of_quota(user):
raise InsufficientBuildQuota("Not enough build quota within today, please try tomorrow.")

batch = cls._setup_batch(batch, after_build_id, with_build_id, user)

Expand Down Expand Up @@ -1285,6 +1289,29 @@ def filter_is_finished(cls, query, is_finished):
else:
return query.join(models.BuildChroot).filter(models.BuildChroot.ended_on.is_(None))

@classmethod
def is_out_of_quota(cls, user):
if not user:
return False

quota = app.config["BUILD_QUOTA"]
builds_count = 0
if quota > 0:
current_date = datetime.date.today()
day_begin = int(time.mktime(datetime.datetime.combine(
current_date, datetime.datetime.min.time()).timetuple()))
day_end = int(time.mktime(datetime.datetime.combine(
current_date, datetime.datetime.max.time()).timetuple()))
builds_count = models.Build.query.filter(
models.Build.submitted_on >= day_begin, models.Build.submitted_on <= day_end, models.Build.user_id == user.id).count()
if builds_count > quota:
app.logger.warning(
f"builds count: {builds_count} exceed limit:{quota}")
return True
app.logger.info(
f"builds count: {builds_count} not exceed limit:{quota}")
return False

@classmethod
def filter_by_group_name(cls, query, group_name):
return query.filter(models.Group.name == group_name)
Expand Down
9 changes: 5 additions & 4 deletions frontend/coprs_frontend/coprs/views/coprs_ns/coprs_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
ConflictingRequest,
InsufficientRightsException,
UnrepeatableBuildException,
InsufficientBuildQuota,
)


Expand Down Expand Up @@ -101,13 +102,13 @@ def copr_builds(copr, page=1):
@req_with_copr
def copr_add_build(copr, form=None):
return render_add_build(
copr, form, view='coprs_ns.copr_new_build')
copr, form, view='coprs_ns.copr_new_build_scm')


def render_add_build(copr, form, view):
if not form:
form = forms.BuildFormUrlFactory(copr.active_chroots)()
return flask.render_template("coprs/detail/add_build/url.html",
form = forms.BuildFormScmFactory(copr.active_chroots)()
return flask.render_template("coprs/detail/add_build/scm.html",
copr=copr, view=view, form=form)


Expand Down Expand Up @@ -153,7 +154,7 @@ def process_new_build(copr, form, create_new_build_factory, add_function, add_vi
try:
create_new_build_factory(**build_options)
db.session.commit()
except (ActionInProgressException, InsufficientRightsException, UnrepeatableBuildException, BadRequest) as e:
except (ActionInProgressException, InsufficientRightsException, InsufficientBuildQuota, UnrepeatableBuildException, BadRequest) as e:
db.session.rollback()
flask.flash(str(e), "error")
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from coprs.logic.packages_logic import PackagesLogic
from coprs.logic.users_logic import UsersLogic
from coprs.exceptions import (ActionInProgressException, ObjectNotFound, NoPackageSourceException,
InsufficientRightsException, MalformedArgumentException)
InsufficientRightsException, MalformedArgumentException,
InsufficientBuildQuota)



Expand Down Expand Up @@ -108,7 +109,8 @@ def copr_rebuild_all_packages(copr):
)

except (ObjectNotFound, ActionInProgressException, NoPackageSourceException, \
InsufficientRightsException, MalformedArgumentException) as e:
InsufficientRightsException, MalformedArgumentException, \
InsufficientBuildQuota) as e:
db.session.rollback()
flask.flash(str(e), "error")
else:
Expand Down

0 comments on commit 8c1e18b

Please sign in to comment.