Repository navigation
Expand file tree
/
Copy pathcreateEnv.sh
More file actions
106 lines (88 loc) · 3.36 KB
/
Copy pathcreateEnv.sh
File metadata and controls
106 lines (88 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
#
# Copyright 2026 Deutsches Zentrum für Luft- und Raumfahrt e.V.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# check if a requirements file name was passed as an argument
REQ_FILE=${1:-requirements.txt}
VENV_NAME=${2:-target/venv}
PYTHON=${3:-python3}
CLIENTDIR=${4:-./target/src}
VENV_ACTIVATE_SCRIPT="$VENV_NAME/bin/activate"
MAX_SLEEP_SECONDS=300
LOCK_FILE=$VENV_NAME/.lock
createEnv() {
# create venv
"$PYTHON" -m venv "$VENV_NAME" || exit 1
# create lock file so that on parallel execution only one call will
# execute environment creation (others just wait until .lock file is gone)
touch "$LOCK_FILE"
# activate virtual environment
. "$VENV_ACTIVATE_SCRIPT" || exit 10
# install dependencies
pip install -r "$REQ_FILE" || exit 1
# deactivate environment
deactivate
# trigger wmp-external-task-api client generation if not present
CLIENT_IN_SRC="src/de/dlr/wmp/generated"
if [ ! -d ${CLIENT_IN_SRC} ]; then
./generateClient.sh "${VENV_NAME}" "${CLIENTDIR}/de/dlr/wmp/generated"
# (re-)create symlink for direct execution
rm -f "${CLIENT_IN_SRC}"
ln -s "$(readlink -f "${CLIENTDIR}/de/dlr/wmp/generated")" "${CLIENT_IN_SRC}"
fi
# release lock, so that other scripts can continue
rm "$LOCK_FILE"
}
resetAndRetry() {
# release lock, so that other scripts (and this) can continue
rm "$LOCK_FILE"
creatEnvOrAwaitCreation
}
waitForEnvCreationComplete() {
# wait for lock and skip env creation (because env is already created by parallel call)
while [[ -f "$LOCK_FILE" && $MAX_SLEEP_SECONDS -gt 0 ]]; do
sleep 1
((MAX_SLEEP_SECONDS--))
done
# check if run in timeout if timeout, then trigger creatEvOrAwaitCreation
if [[ ! $MAX_SLEEP_SECONDS -le 0 ]]; then
creatEnvOrAwaitCreation
fi
}
creatEnvOrAwaitCreation() {
# check if environment is present (and maybe wait until env creation of parallel execution finishes)
# or else create environment
if [[ -d "$VENV_NAME" ]]; then
echo "environment folder '$VENV_NAME' already exists: checking for lock file..."
# check if other process is already creating this environment (lock file exists)
if [[ -f "$LOCK_FILE" ]]; then
# check if lock file is older than 5 minutes (assuming environment creation of parallel exec might have failed)
if [ -n "$(find "$LOCK_FILE" -prune -mmin +5)" ]; then
echo "lockfile to old, assuming failed environment creation, retry create environment"
resetAndRetry
else
echo "lockfile present: await environment creation..."
# wait for lock and skip env creation (because env is already created by parallel call)
waitForEnvCreationComplete
fi
else
echo "environment '$VENV_NAME' already exists and has no lock file: skip creation..."
fi
else
echo "creating environment..."
createEnv
fi
}
creatEnvOrAwaitCreation