-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompute-stats.sh
executable file
·48 lines (37 loc) · 1.05 KB
/
compute-stats.sh
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
#!/bin/bash
set -e
set -x
PYTHON_SCRIPT="/home/ubuntu/freeze_bits_and_compute_stats.py"
TEMP_OUTPUT="/tmp/some-numbers.html"
FINAL_OUTPUT="/var/www/generated-content/some-numbers.html"
CONTENT_DIR="/var/www/generated-content"
STAGING_OUTPUT="/tmp/some-numbers-staging.html"
UBUNTU_USER="ubuntu"
WWW_USER="www-data"
WWW_GROUP="www-data"
VENV_PATH="/home/ubuntu/venv/bin/activate"
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root" >&2
exit 1
fi
cleanup() {
rm -f "$TEMP_OUTPUT" "$STAGING_OUTPUT"
}
trap cleanup EXIT
mkdir -p "$CONTENT_DIR"
chown "$WWW_USER:$WWW_GROUP" "$CONTENT_DIR"
chmod 775 "$CONTENT_DIR"
sudo -u "$UBUNTU_USER" bash -c "
source $VENV_PATH
source /home/ubuntu/.redis-creds
python $PYTHON_SCRIPT
"
if [ ! -f "$TEMP_OUTPUT" ]; then
echo "Error: Python script did not generate the output file." >&2
exit 1
fi
cp "$TEMP_OUTPUT" "$STAGING_OUTPUT"
chown "$WWW_USER:$WWW_GROUP" "$STAGING_OUTPUT"
chmod 644 "$STAGING_OUTPUT"
mv "$STAGING_OUTPUT" "$FINAL_OUTPUT"
echo "Stats file updated successfully at $FINAL_OUTPUT"