Skip to content

Commit

Permalink
Merge pull request #1143 from gammasim/db-scripts-robustness
Browse files Browse the repository at this point in the history
Improve robustness of database scripts
  • Loading branch information
GernotMaier authored Sep 24, 2024
2 parents 06a11aa + 5253891 commit f9b4808
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ simtools/_version.py
# Coverage files
coverage*
.coverage*

# Ignore temporary output of database scripts
database_scripts/mongo-data
26 changes: 20 additions & 6 deletions database_scripts/upload_from_model_repository_to_db.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash
# Upload model parameter from repository to a local or remote mongoDB.
#
# Cover 'source .env': the script ensure that this file exists:
# shellcheck disable=SC1091

SIMTOOLS_DB_SIMULATION_MODEL_URL="https://gitlab.cta-observatory.org/cta-science/simulations/simulation-model/model_parameters.git"
SIMTOOLS_DB_SIMULATION_MODEL_BRANCH="main"
DB_SIMULATION_MODEL_URL="https://gitlab.cta-observatory.org/cta-science/simulations/simulation-model/model_parameters.git"
DB_SIMULATION_MODEL_BRANCH="main"

# Check that this script is not sourced but executed
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
Expand All @@ -19,23 +21,35 @@ if [ "$#" -ne 1 ]; then
exit 1
fi

SIMTOOLS_DB_SIMULATION_MODEL="$1"
DB_SIMULATION_MODEL="$1"

echo "Cloning model parameters from $SIMTOOLS_DB_SIMULATION_MODEL_URL"
echo "Cloning model parameters from $DB_SIMULATION_MODEL_URL"
rm -rf ./tmp_model_parameters
git clone --depth=1 -b $SIMTOOLS_DB_SIMULATION_MODEL_BRANCH $SIMTOOLS_DB_SIMULATION_MODEL_URL ./tmp_model_parameters
git clone --depth=1 -b $DB_SIMULATION_MODEL_BRANCH $DB_SIMULATION_MODEL_URL ./tmp_model_parameters

CURRENTDIR=$(pwd)
cd ./tmp_model_parameters/ || exit
cp -f "$CURRENTDIR"/../.env .env

# ask for confirmation before uploading to remote DB
source .env
regex='^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$'
echo "DB_SERVER: $SIMTOOLS_DB_SERVER"
if [[ $SIMTOOLS_DB_SERVER =~ $regex ]]; then
read -r -p "Do you really want to upload to remote DB $SIMTOOLS_DB_SERVER? Type 'yes' to confirm: " user_input
if [ "$user_input" != "yes" ]; then
echo "Operation aborted."
exit 1
fi
fi

# upload files to DB
model_directory="./model_versions/"
for dir in "${model_directory}"*/; do
simtools-db-add-model-parameters-from-repository-to-db \
--model_version "$(basename "${dir}")" \
--input_path "${dir}" \
--db_name "$SIMTOOLS_DB_SIMULATION_MODEL" \
--db_name "$DB_SIMULATION_MODEL" \
--type "model_parameters"
done

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _parse(label=None, description=None):
config.parser.add_argument(
"--db_name",
help="Name of the new model parameter database to be created.",
type=str,
type=str.strip,
required=True,
)
config.parser.add_argument(
Expand Down
2 changes: 1 addition & 1 deletion simtools/configuration/commandline_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def initialize_db_config_arguments(self):
_job_group.add_argument(
"--db_simulation_model",
help="name of simulation model database",
type=str,
type=str.strip,
required=False,
default=None,
)
Expand Down
8 changes: 8 additions & 0 deletions tests/unit_tests/configuration/test_commandline_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,11 @@ def test_simulation_configuration():
_parser_10.initialize_default_arguments(
simulation_configuration={"software": None, "corsika_configuration": ["wrong_parameter"]}
)


def test_initialize_db_config_arguments_strip_string():
parser_10 = parser.CommandLineParser()
parser_10.initialize_db_config_arguments()
for test_string in ["test", " test", "test ", " test "]:
args = parser_10.parse_args(["--db_simulation_model", test_string])
assert args.db_simulation_model == "test"

0 comments on commit f9b4808

Please sign in to comment.