Skip to content

Commit dd10d8f

Browse files
authored
Initial push (#207)
Signed-off-by: stefan_pofahl <[email protected]>
1 parent c279845 commit dd10d8f

File tree

2 files changed

+538
-0
lines changed

2 files changed

+538
-0
lines changed

deploy/apache/build_basil_api.sh

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
#! /bin/bash -e
2+
## --- variables -----------------------------------------------------------------------------------
3+
SERVER_NAME=Your.Host.IP.Num
4+
SERVER_ALIAS=hostname.domain.local
5+
6+
## ---
7+
BASIL_REPOSITORY=https://github.com/elisa-tech/BASIL.git
8+
BASIL_BUILD_DIR=/tmp/basil
9+
BASIL_API=/var/www/basil-api
10+
WSGI_SCRIPT=$BASIL_API/api/wsgi.py
11+
BASIL_API_CONF=/etc/apache2/sites-available/basil-api.conf
12+
VIRTUAL_ENV=/opt/virtenv
13+
DEFAULT_TEST_RUNS_BASE_DIR=/var/test-runs
14+
15+
# --- Dates according to ISO8601, "JJJJ-MM-DD ..."
16+
COMMITBEFORE="2025-09-08 14:45:51 +0200"
17+
## --- If $CLONEBASIL == 1, perform a fresh clone.
18+
CLONEBASIL=-1
19+
## --- get current version (HEAD) $GITGOBACK == 0 or go back in time > 0
20+
## --- $GITGOBACK < 0: take specified time $COMMITBEFORE to determine the value of $GITGOBACK
21+
GITGOBACK=11
22+
# --- Dates according to ISO8601, "JJJJ-MM-DD ..."
23+
COMMITBEFORE="2025-08-31 14:45:51 +0200"
24+
25+
### #################################### main ################################################
26+
## --- constants:
27+
REFERENCEDATE01="2025-08-31 14:45:51 +0200" # Still with sqlite-database
28+
## --- local functions -----------------------------------------------------------------------------
29+
rcpdir() {
30+
local source_dir="$1"
31+
local dest_dir="$2"
32+
## --- remove trailing slashes
33+
source_dir="${source_dir%/}"
34+
dest_dir="${dest_dir%/}"
35+
## --- Rsync with error treatment
36+
if ! rsync -avq --partial --timeout=30 \
37+
"$source_dir"/ "$dest_dir"/ 2>/tmp/rsync_error; then
38+
echo "Error: rsync operation failed"
39+
echo "Error details:"
40+
cat /tmp/rsync_error
41+
rm -f /tmp/rsync_error
42+
return 1
43+
exit 1;
44+
fi
45+
echo "Rsync completed successfully"$'\n'
46+
rm -f /tmp/rsync_error
47+
return 0
48+
}
49+
### --- main -------------------------------------------------------------------
50+
clear
51+
# --- check if rsync is available
52+
if ! command -v rsync &> /dev/null; then
53+
echo "Error: rsync is not installed, Execution terminates!"$'\n'; exit 1;
54+
fi
55+
echo ===================================================================
56+
echo install BASIL-API:
57+
echo -------------------------------------------------------------------
58+
cd ~
59+
echo $(pwd)
60+
61+
# --- check if BASIL repository should be cloned ---------------------------------------------------
62+
if [ $CLONEBASIL -eq 0 ]; then
63+
# rm -R $BASIL_BUILD_DIR
64+
if ! [ -d $BASIL_BUILD_DIR ]; then
65+
echo "--- BASIL repository not found! Repository will be cloned! ---"
66+
git clone $BASIL_REPOSITORY $BASIL_BUILD_DIR
67+
else
68+
echo "--- Keep current version of BASIL. --------------------------------------------"$'\n'
69+
fi
70+
else
71+
echo $'\n'"--- re-clone BASIL git repository. -----------------------------------"
72+
if [ -d $BASIL_BUILD_DIR ]; then
73+
echo --- re-establish folder $BASIL_BUILD_DIR -------------------
74+
rm -R $BASIL_BUILD_DIR
75+
fi
76+
mkdir -p $BASIL_BUILD_DIR
77+
## --- clone git repository --------------------------------------------
78+
git clone $BASIL_REPOSITORY $BASIL_BUILD_DIR
79+
echo $'\n'"=== END git clone =============================================================="$'\n'
80+
fi
81+
82+
# --- Decide which mode to find the correct commit of BASIL go back in time or in commits / HEADS
83+
cd "$BASIL_BUILD_DIR" || { echo "Error: Folder $BASIL_BUILD_DIR not found!"; exit 1; }
84+
if [ $GITGOBACK -lt 0 ]; then
85+
# --- validate correct time format of $COMMITBEFORE
86+
if [[ ! "$COMMITBEFORE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}\ [+-][0-9]{4}$ ]]; then
87+
echo "Error: Invalid date format: $COMMITBEFORE"$'\n'; exit 1;
88+
fi
89+
# --- Check if latest commit date is after time: $COMMITBEFORE
90+
latest_commit_date=$(git log -1 --format=%cd --date=format:%s)
91+
commit_before_date=$(date -d "$COMMITBEFORE" +%s 2>/dev/null)
92+
if [ "$commit_before_date" -gt "$latest_commit_date" ]; then
93+
echo "Warning: Date $COMMITBEFORE is after the latest commit"
94+
echo "This will return the latest commit (HEAD)"
95+
# Optional: exit 1 falls gewünscht
96+
fi
97+
echo "--- Go back to commit that lies before: $COMMITBEFORE ---"
98+
# --- get the latest commit that is strictly before the reference date
99+
ref_commit=$(git rev-list -1 --before="$COMMITBEFORE" HEAD)
100+
if [ -z "$ref_commit" ]; then
101+
echo "No commit found before $COMMITBEFORE"
102+
exit 1
103+
fi
104+
# --- set $GITGOBACK correctly to catch last commit that lies before $COMMITBEFORE
105+
GITGOBACK=$(git rev-list --count "$ref_commit"..HEAD)
106+
fi
107+
# ---
108+
TOTALNUMBEROFCOMMITS=$(git rev-list --count HEAD)
109+
if [ $GITGOBACK -gt $TOTALNUMBEROFCOMMITS ]; then
110+
echo $'\n'" ---- GITGOBACK = $GITGOBACK too high, maximum is $TOTALNUMBEROFCOMMITS ----"$'\n'
111+
echo $'\n'"=========================================================================="$'\n'
112+
exit 1;
113+
fi
114+
# --- Check if it is a "merge commit" --------------------------------------------------------------
115+
if ! git revert --no-commit HEAD~$GITGOBACK..HEAD 2>/dev/null; then
116+
echo $'\n'"=========================================================================="$'\n'
117+
echo "--- Error: git revert failed likely due to merge commits or other issues."
118+
echo "--- Specify another commit! --- Maybe Refresh local copy of repository. "
119+
echo "--- Specified or determined GITGOBACK is $GITGOBACK. --- Execution terminates now! ---"
120+
echo $'\n'"=========================================================================="$'\n'
121+
fi
122+
# --- open with specified or determined HEAD=$GITGOBACK --------------------------------------------
123+
# echo "TOTALNUMBEROFCOMMITS: $TOTALNUMBEROFCOMMITS"$'\n'
124+
COMMITDATE=$(git show -s --format=%ci HEAD~$GITGOBACK)
125+
## --- Get the original commit message (subject + body)
126+
ORIGMSG=$(git show -s --format=%B HEAD~$GITGOBACK)
127+
## --- Extract commit number if present, don’t exit if missing
128+
COMMITNUMBER=$(grep -oP '#\K[0-9]+' <<<"$ORIGMSG" || true)
129+
## --- Combine them into one commit message (quote properly!)
130+
COMMITMESSAGE="Revert of commit HEAD~${GITGOBACK} from: ${COMMITDATE}"
131+
echo "--- Commit message: -------------------------------------------------------------"$'\n'
132+
echo ${ORIGMSG}
133+
echo $'\n'"------------------------------------------------------------------------------"$'\n'
134+
135+
# echo "DBG --- This is line $LINENO, next: git commit -m "
136+
## --- revert and commit, if $GITGOBACK is greater then 0: -----------------------------------------
137+
if [ $GITGOBACK -gt 0 ]; then
138+
echo " --- GITGOBACK = $GITGOBACK is greater then 0: --- "
139+
# --- Following commands should not stop the script execution, true = Exit-Code 0
140+
git revert --no-commit HEAD~$GITGOBACK..HEAD 2>/dev/null || true
141+
git commit -m "$COMMITMESSAGE" 2>/dev/null || true
142+
fi
143+
## --- COPY local copy of repository to $BASIL_API and change to that directory ------------
144+
if ! [ -d $BASIL_API ]; then
145+
mkdir -p $BASIL_API
146+
fi
147+
rcpdir "$BASIL_BUILD_DIR" "$BASIL_API"
148+
cd $BASIL_API
149+
## --- Compare Commit and Reference Date, to compare convert date to seconds since Unix epoch
150+
epoch1=$(date -d "$COMMITDATE" +%s)
151+
epoch2=$(date -d "$REFERENCEDATE01" +%s)
152+
echo "*******************************************************************************************"
153+
if (( epoch1 > epoch2 )); then
154+
echo "COMMITDATE: $COMMITDATE is AFTER REFERENCEDATE: " $REFERENCEDATE01
155+
echo "New database engine postgreSQL"
156+
VERSIONID=1
157+
else
158+
echo "COMMITDATE: $COMMITDATE is BEFORE REFERENCEDATE: " $REFERENCEDATE01
159+
echo "Old data-base engine sqlite_1_7_1"
160+
VERSIONID=0
161+
fi
162+
echo "-------------------------------------------------------------------------------------------"
163+
164+
## --- set rights for TEST_RUN_DIRECTORY -------------------------------
165+
if ! [ -d $TEST_RUNS_BASE_DIR ]; then
166+
mkdir -p $TEST_RUNS_BASE_DIR
167+
fi
168+
if [[ -v $TEST_RUNS_BASE_DIR ]]; then
169+
chown -R www-data:www-data $BASIL_API && chmod -R 755 $TEST_RUNS_BASE_DIR
170+
else
171+
chown -R www-data:www-data $BASIL_API && chmod -R 755 $DEFAULT_TEST_RUNS_BASE_DIR
172+
fi
173+
174+
## --- configure directory $BASIL_API and add folder -------------------
175+
if ! [ -d $BASIL_API/api/ssh_keys ]; then
176+
mkdir -p $BASIL_API/api/ssh_keys
177+
fi
178+
chown -R www-data:www-data $BASIL_API && chmod -R 755 $BASIL_API
179+
## --- create content of "wsgi.py":
180+
cd $BASIL_API/api
181+
echo $(pwd)
182+
echo create wsgi-pythonscript $WSGI_SCRIPT
183+
## --- create wsgi-pythonscript $WSGI_SCRIPT ----------------------------
184+
printf "import sys \n\
185+
import os \n\
186+
sys.path.insert(0, '$BASIL_API/api') \n\
187+
os.environ['BASIL_DB_PASSWORD'] = 'rm_Zero' \n\
188+
import api \n\
189+
application = api.app \n\
190+
import logging\n\
191+
logging.basicConfig(stream=sys.stderr)" > $WSGI_SCRIPT
192+
## --- create virtual python environment:
193+
if [ -d $VIRTUAL_ENV ]; then
194+
echo --- re-establish $VIRTUAL_ENV exist ---------------------------
195+
rm -R $VIRTUAL_ENV
196+
fi
197+
## --- Create virtual environment
198+
mkdir -p $VIRTUAL_ENV
199+
chown -R www-data:www-data $VIRTUAL_ENV
200+
python3 -m venv $VIRTUAL_ENV
201+
## --- add to path, if not yet done
202+
if [ -d "$VIRTUAL_ENV" ] && [[ ":$PATH:" != *":$VIRTUAL_ENV:"* ]]; then
203+
PATH="${PATH:+"$PATH:"}$VIRTUAL_ENV"
204+
fi
205+
echo $PATH
206+
## --- Install Flask inside the virtual environment
207+
$VIRTUAL_ENV/bin/pip install --no-cache-dir -r $BASIL_API/requirements.txt
208+
## --- configure folder structure:
209+
echo --- configure folder structure:
210+
chown -R www-data:www-data $BASIL_API/api/ssh_keys
211+
chmod -R 750 $BASIL_API/api/ssh_keys
212+
mkdir -p /var/test-runs
213+
chmod -R 750 /var/test-runs
214+
mkdir -p $BASIL_API/api/user-files
215+
chmod -R 750 $BASIL_API/api/user-files
216+
mkdir -p $BASIL_API/db/sqlite3
217+
mkdir -p $BASIL_API/db/models
218+
chmod -R a+rw $BASIL_API/db
219+
## --- activate virtual python environment, important is the leading point "."
220+
echo --- activate virtual python environment, important is the leading point "."
221+
echo $(pwd)
222+
. $VIRTUAL_ENV/bin/activate
223+
## --- write BASIL-API apache2-config file:
224+
echo --- write BASIL-API apache2-config file:
225+
printf "<VirtualHost *:5000> \n\
226+
ServerName $SERVER_NAME \n\
227+
ServerAlias $SERVER_ALIAS \n\
228+
ServerAdmin $SERVER_ADMIN \n\
229+
# --- WSGI - stuff:
230+
WSGIProcessGroup basil-api \n\
231+
WSGIDaemonProcess basil-api python-home=$VIRTUAL_ENV python-path=$BASIL_API user=www-data group=www-data threads=5 \n\
232+
WSGIScriptAlias / $WSGI_SCRIPT \n\
233+
# ---
234+
<Directory \"$BASIL_API\"> \n\
235+
Options Indexes FollowSymLinks \n\
236+
AllowOverride All \n\
237+
Require all granted \n\
238+
</Directory> \n\
239+
# --- Logging ---
240+
ErrorLog \${APACHE_LOG_DIR}/error.log \n\
241+
CustomLog \${APACHE_LOG_DIR}/access.log combined \n\
242+
</VirtualHost>" > $BASIL_API_CONF
243+
echo
244+
## --- activate BASIL-API apache2-config file and restart apache2 server
245+
echo --- send Apache2 to FOREGROUND: apache2ctl -D FOREGROUND ----------
246+
apache2ctl -D FOREGROUND
247+
echo --- activate BASIL-API apache2-config file
248+
sudo a2ensite basil-api.conf
249+
echo --- restart apache2 via command: sudo apachectl graceful ---------------------------------$'\n'
250+
sudo apachectl graceful
251+
echo --- end main --- rights of folder: $BASIL_API -------------------------------------------$'\n'
252+
ls -ld $BASIL_API/
253+
echo --- $WSGI_SCRIPT -----------------------------------------------------------------------$'\n'
254+
cat $WSGI_SCRIPT
255+
echo $'\n'--- run gunicorn: -------------------------------------------------------------------$'\n'
256+
cd $BASIL_API/api
257+
/opt/virtenv/bin/gunicorn --bind 0.0.0.0:5000 api:app
258+
cd ~
259+
echo --- END ----------------------------------------------------------------------------------$'\n'

0 commit comments

Comments
 (0)