Skip to content

Commit

Permalink
Merge pull request #231 from CMSTrackerDPG/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingface0 authored Jul 1, 2023
2 parents d2aeb81 + bed4882 commit 8cf600e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .s2i/bin/assemble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
echo "Before assembling"
git config --global url."https://$CERN_GITLAB_USER:$CERN_GITLAB_TOKEN@gitlab.cern.ch".insteadOf https://gitlab.cern.ch
git config --global url."https://$CERN_GITLAB_USER:$CERN_GITLAB_TOKEN@gitlab.cern.ch".insteadOf https://gitlab.cern.ch
/usr/libexec/s2i/assemble
rc=$?

Expand Down
3 changes: 2 additions & 1 deletion dqmhelper/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
messages.ERROR: "danger",
}
# Version to display in order to keep track of changes
CERTHELPER_VERSION = "1.10.2"
CERTHELPER_VERSION = "1.10.3"

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -48,6 +48,7 @@
CSRF_TRUSTED_ORIGINS = [config("CSRF_TRUSTED_ORIGINS", default="")]

INSTALLED_APPS = [
"daphne",
"channels",
"remotescripts",
"openruns.apps.OpenrunsConfig",
Expand Down
1 change: 0 additions & 1 deletion openshift-start-up-script.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

python manage.py collectstatic --noinput
python manage.py migrate --run-syncdb
python manage.py clear_scripts_running_status
Expand Down
32 changes: 27 additions & 5 deletions remotescripts/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import lru_cache
from django import forms
from django.http import QueryDict
from django.core.validators import integer_validator
from remotescripts.models import (
ScriptConfigurationBase,
RemoteScriptConfiguration,
Expand All @@ -10,28 +11,45 @@
ScriptOutputFile,
)
from remotescripts.utilities import split_with_spaces_commas
from remotescripts.validators import string_validator, choice_validator


class ScriptExecutionForm(forms.ModelForm):
# Map type of argument to appropriate form field.
ARG_TO_FIELD_MAP = {
ScriptArgumentBase.ARGUMENT_INT: forms.IntegerField,
ScriptArgumentBase.ARGUMENT_STR: forms.CharField,
ScriptArgumentBase.ARGUMENT_CHO: forms.ChoiceField,
}

# Map type of argument to specific validators, can be extended as needed.
ARG_TO_VALIDATOR_MAP = {
ScriptArgumentBase.ARGUMENT_INT: [integer_validator],
ScriptArgumentBase.ARGUMENT_STR: [string_validator],
ScriptArgumentBase.ARGUMENT_CHO: [choice_validator],
}

POSITIONAL_FIELD_NAME_PREFIX = "pos"

def __init__(self, *args, **kwargs):
"""
Prepares a form to display to the user to run a command, by dynamically adding fields
for each type of argument. Arguments are added by the admin in the Database.
"""
super().__init__(*args, **kwargs)
i = 0
# Add positional arguments
# Add positional arguments to the form
for arg in ScriptPositionalArgument.objects.filter(
mother_script=self.instance
).order_by("position"):
field_name = (
arg.name if arg.name else f"{self.POSITIONAL_FIELD_NAME_PREFIX}{i}"
)
self.fields[field_name] = self.ARG_TO_FIELD_MAP[arg.type]()
self.fields[field_name] = self.ARG_TO_FIELD_MAP[arg.type](
validators=[
*self.ARG_TO_VALIDATOR_MAP[arg.type]
] # Add validators specific to the type of argument
)
self.fields[field_name].widget.attrs["class"] = "form-control"
self.fields[field_name].widget.attrs["placeholder"] = (
arg.help_text if arg.help_text else ""
Expand All @@ -43,12 +61,16 @@ def __init__(self, *args, **kwargs):

if arg.default_value:
self.fields[field_name].initial = arg.default_value
i += 1
i += 1 # Count position of the argument

# Add keyword arguments
# Add keyword arguments. They are added after the positional ones.
for kwarg in ScriptKeywordArgument.objects.filter(mother_script=self.instance):
field_name = kwarg.name if kwarg.name else kwarg.keyword
self.fields[field_name] = self.ARG_TO_FIELD_MAP[kwarg.type]()
self.fields[field_name] = self.ARG_TO_FIELD_MAP[kwarg.type](
validators=[
*self.ARG_TO_VALIDATOR_MAP[arg.type]
] # Add validators specific to the type of argument
)
self.fields[field_name].widget.attrs["class"] = "form-control"
self.fields[field_name].widget.attrs["placeholder"] = (
kwarg.help_text if kwarg.help_text else ""
Expand Down
1 change: 1 addition & 0 deletions remotescripts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def _form_command(self, *args, **kwargs) -> str:
if _k.keyword not in kwargs:
raise TypeError(f"Missing keyword argument '{_k.keyword}'")
cmd += f" {str(_k)}{kwargs[_k.keyword]}"
logger.debug(f"Command: {cmd}")
return cmd

def save(self, *args, **kwargs):
Expand Down
11 changes: 9 additions & 2 deletions remotescripts/templates/remotescripts/remote.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,17 @@ <h5>{{ output_file.filename_regex }}</h5>
// url: "",// Not needed
data: values,
})
.always(function(){
.always(function(data){
if (data.errors){
console.log(data.errors)
for (const [key, value] of Object.entries(data.errors)){
alert(`${key} : ${value}`);
}
enableControls();
}
})
.fail(function(data){
let msg = `Command failed with error ${data.status}: ${JSON.parse(data.responseText).message}`;
let msg = `Request failed with error ${data.status}: ${JSON.parse(data.responseText).message}`;
console.error(msg);
alert(msg);
});
Expand Down
18 changes: 17 additions & 1 deletion remotescripts/validators.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
from django.core.exceptions import ValidationError
from remotescripts.utilities import split_with_spaces_commas
from django.utils.regex_helper import _lazy_re_compile
from django.core.validators import RegexValidator


def validate_bash_script(value):
BANNED_STR = ["&"]
BANNED_STR = ["&", "&&", ";"]
if any((s in value for s in BANNED_STR)):
raise ValidationError(
f"Invalid characters detected. Banned strings are: {BANNED_STR}'"
)


string_validator = RegexValidator(
_lazy_re_compile(r"^[a-zA-Z0-9_,]*$"),
message=("Value must be an alphanumeric string."),
code="invalid",
)

# Same as string, no comma
choice_validator = RegexValidator(
_lazy_re_compile(r"^[a-zA-Z0-9_]*$"),
message=("Value must be an alphanumeric string."),
code="invalid",
)


def validate_comma_space_separated_values_string(value):
if not value:
raise ValidationError("At least one value should be entered")
Expand Down
5 changes: 3 additions & 2 deletions remotescripts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ def post(self, request, pk: int):
).start()
except Exception as e:
logger.error(e)
else:
logger.error(form.errors.as_data())

return JsonResponse({"success": success})
return JsonResponse({"success": success, "errors": form.errors})


class TrackerMapsView(ScriptExecutionBaseView):
Expand All @@ -171,7 +173,6 @@ def setup(self, request, *args, **kwargs):
def post(self, request):
success = False
if request.META.get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest":

run_type = request.POST.get("type", None)
runs_list = request.POST.get("list", None)
logger.info(
Expand Down
9 changes: 5 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
numba==0.48.0
llvmlite==0.31.0
numba
llvmlite
asgiref>=3.2.10
cernrequests>=0.3.0
cernrequests==0.3.3
certifi==2018.11.29
channels[daphne]
channels_redis
Django>=3.0.3
Django<4.2.0
django-allauth>=0.40.0
django-bootstrap3>=9.1.0
django-filter>=2.2.0
Expand Down

0 comments on commit 8cf600e

Please sign in to comment.