Skip to content

Commit 7b447b3

Browse files
committed
Setup Suma to run in Docker
Also allows for the dynamic generation of all the config files based on ENV variables Fixes #153
1 parent 11481f6 commit 7b447b3

File tree

6 files changed

+246
-0
lines changed

6 files changed

+246
-0
lines changed

.env.exmple

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
DB_HOST=db
2+
DB_NAME=suma
3+
DB_PASS=password
4+
DB_PORT=3306
5+
DB_ROOT_PASSWORD=rootpassword
6+
DB_USER=suma
7+
SERVICE_URL=http://YOUR_SERVER
8+
SUMA_ADMIN_USER=admin
9+
SUMA_ADMIN_PASS=admin
10+
SUMA_ANALYTICS_TIMEZONE=America/New_York
11+
SUMA_ANALYTICS_DISPLAY_FORMAT=m-d-Y
12+
13+
14+
SUMA_ANALYTICS_EMAIL_FROM=Suma Reports <[email protected]>
15+
SUMA_ANALYTICS_EMAIL_SUBJECT=Suma Nightly Report:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ analysis/test/js/coverage
1616
analysis/test/php/coverage
1717
docs/site
1818
/vendor
19+
.env
1920

2021
# Configuration Files
2122
service/config/config.ini

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM php:8.1-apache
2+
3+
# Install dependencies
4+
RUN apt-get update && apt-get install -y \
5+
libzip-dev libonig-dev zip unzip curl default-mysql-client && \
6+
docker-php-ext-install pdo_mysql mbstring zip && \
7+
a2enmod rewrite && \
8+
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
9+
rm -rf /var/lib/apt/lists/*
10+
11+
# Copy local suma code into image at /app/suma
12+
COPY --chown=www-data:www-data . /app/suma
13+
14+
# Set working dir
15+
WORKDIR /app/suma
16+
17+
# Create web root dirs (will be symlinked)
18+
RUN mkdir -p /var/www/html/suma/
19+
20+
# Entrypoint & startup script
21+
COPY docker-entrypoint.sh /usr/local/bin/
22+
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
23+
24+
EXPOSE 80
25+
26+
ENTRYPOINT ["docker-entrypoint.sh"]
27+
CMD ["apache2-foreground"]

docker-compose.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# version: '3.8'
2+
3+
services:
4+
db:
5+
image: mysql:8.4
6+
restart: unless-stopped
7+
environment:
8+
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
9+
MYSQL_DATABASE: ${DB_NAME}
10+
MYSQL_USER: ${DB_USER}
11+
MYSQL_PASSWORD: ${DB_PASS}
12+
volumes:
13+
- db_data:/var/lib/mysql
14+
15+
init-db:
16+
image: mysql:8.4
17+
depends_on:
18+
- db
19+
entrypoint: ["/bin/sh", "-c"]
20+
command: >
21+
"
22+
for i in {1..30}; do
23+
mysqladmin ping -h db --silent && break || sleep 2;
24+
done;
25+
if ! mysql -h db -u root -p${DB_ROOT_PASSWORD} -e 'SELECT 1 FROM location LIMIT 1;' ${DB_NAME}; then
26+
echo 'Initializing database...';
27+
mysql -h db -u root -p${DB_ROOT_PASSWORD} ${DB_NAME} < /docker-entrypoint-initdb.d/schema.sql;
28+
else
29+
echo 'Schema already initialized, skipping.';
30+
fi
31+
"
32+
volumes:
33+
- ./service/config/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
34+
environment:
35+
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
36+
37+
suma:
38+
build: .
39+
ports:
40+
- "80:80"
41+
depends_on:
42+
- db
43+
- init-db
44+
environment:
45+
DB_HOST: db
46+
DB_PORT: 3306
47+
DB_NAME: ${DB_NAME}
48+
DB_USER: ${DB_USER}
49+
DB_PASS: ${DB_PASS}
50+
restart: unless-stopped
51+
env_file:
52+
- .env
53+
54+
volumes:
55+
db_data:

docker-entrypoint.sh

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Paths
5+
WEB_DIR="/var/www/html"
6+
SUMA_DIR="/app/suma"
7+
SUMA_LOG="/var/log/suma.log"
8+
9+
# Generate Apache config with environment variables
10+
cat > /etc/apache2/sites-available/000-default.conf <<EOF
11+
<VirtualHost *:80>
12+
DocumentRoot ${WEB_DIR}
13+
14+
Alias /sumaserver ${SUMA_DIR}/service/web
15+
Alias /suma/client ${SUMA_DIR}/web
16+
Alias /suma/analysis ${SUMA_DIR}/analysis
17+
18+
<Location "/suma">
19+
Options -Indexes +FollowSymLinks
20+
Require all granted
21+
</Location>
22+
23+
<Location "/sumaserver">
24+
Options -Indexes +FollowSymLinks
25+
AllowOverride All
26+
RewriteEngine On
27+
RewriteCond %{REQUEST_FILENAME} -s [OR]
28+
RewriteCond %{REQUEST_FILENAME} -l [OR]
29+
RewriteCond %{REQUEST_FILENAME} -d
30+
RewriteRule ^.*$ - [NC,L]
31+
RewriteRule ^.*$ index.php [NC,L]
32+
Require all granted
33+
</Location>
34+
35+
ErrorLog \${APACHE_LOG_DIR}/error.log
36+
CustomLog \${APACHE_LOG_DIR}/access.log combined
37+
</VirtualHost>
38+
EOF
39+
40+
# Enable rewrite module (already enabled in Dockerfile but safe here)
41+
a2enmod rewrite
42+
43+
# Generate Suma config.yaml from env vars if not exists
44+
CONFIG_PATH="${SUMA_DIR}/service/web/config/config.yaml"
45+
if [ ! -f "$CONFIG_PATH" ]; then
46+
echo "Generating Suma config.yaml..."
47+
cat > "$CONFIG_PATH" <<EOC
48+
SUMA_SERVER_PATH: $SUMA_DIR/service
49+
SUMA_CONTROLLER_PATH: $SUMA_DIR/service/controllers
50+
SUMA_BASE_URL: /sumaserver
51+
SUMA_DEBUG: true
52+
EOC
53+
fi
54+
55+
# Generate Suma config.yaml from env vars if not exists
56+
ANALYSIS_CONFIG_PATH="${SUMA_DIR}/analysis/config/config.yaml"
57+
if [ ! -f "$ANALYSIS_CONFIG_PATH" ]; then
58+
echo "Generating Suma Analysis config.yaml..."
59+
cat > "$ANALYSIS_CONFIG_PATH" <<EOC
60+
showErrors: false
61+
serverIO:
62+
baseUrl: ${SERVICE_URL}/sumaserver/query
63+
analysisBaseUrl: ${SERVICE_URL}/suma/analysis/reports
64+
nightly:
65+
timezone: ${SUMA_ANALYTICS_TIMEZONE}
66+
displayFormat: Y-m-d
67+
recipients: ${SUMA_ANALYTICS_RECIPIENTS}
68+
errorRecipients: ${SUMA_ANALYTICS_ERROR_RECIPIENTS}
69+
emailFrom: "${SUMA_ANALYTICS_EMAIL_FROM}"
70+
emailSubj: "${SUMA_ANALYTICS_EMAIL_SUBJECT}"
71+
EOC
72+
fi
73+
74+
# Generate Suma Database config.yaml from env vars if not exists
75+
DB_CONFIG_PATH="${SUMA_DIR}/service/config/config.yaml"
76+
if [ ! -f "$DB_CONFIG_PATH" ]; then
77+
echo "Generating Suma DB config.yaml..."
78+
cat > "$DB_CONFIG_PATH" <<EOC
79+
production:
80+
sumaserver:
81+
db:
82+
host: ${DB_HOST}
83+
platform: Pdo_Mysql
84+
dbname: ${DB_NAME}
85+
user: ${DB_USER}
86+
pword: ${DB_PASS}
87+
port: ${DB_PORT}
88+
log:
89+
path:
90+
name: ${SUMA_LOG}
91+
admin:
92+
user: ${SUMA_ADMIN_USER}
93+
pass: ${SUMA_ADMIN_PASS}
94+
queryserver:
95+
db:
96+
limit: 10000
97+
98+
development:
99+
_extends: production
100+
sumaserver:
101+
db:
102+
dbname: sumadev
103+
log:
104+
path:
105+
EOC
106+
fi
107+
108+
SUMA_CLIENT_CONFIG_PATH="${SUMA_DIR}/web/config/spaceassessConfig.js"
109+
if [ ! -f "$SUMA_CLIENT_CONFIG_PATH" ]; then
110+
echo "Generating Suma spaceassessConfig.js..."
111+
cp ${SUMA_DIR}/web/config/spaceassessConfig_example.js ${SUMA_CLIENT_CONFIG_PATH}
112+
fi
113+
114+
115+
# Install PHP dependencies if composer.json is present
116+
if [ -f "${SUMA_DIR}/composer.json" ]; then
117+
echo "Installing PHP dependencies via Composer..."
118+
cd "${SUMA_DIR}"
119+
composer install --no-interaction --prefer-dist --optimize-autoloader
120+
chown -R www-data:www-data ${SUMA_DIR}
121+
fi
122+
123+
# create suma.log file
124+
echo "Touching suma.log..."
125+
touch ${SUMA_LOG}
126+
chown www-data:www-data ${SUMA_LOG}
127+
128+
# Chown of files for webserver
129+
chown www-data:www-data ${SUMA_CLIENT_CONFIG_PATH} ${DB_CONFIG_PATH} ${CONFIG_PATH}
130+
131+
# Wait for MySQL to be ready before starting Apache
132+
if [ -n "$DB_HOST" ]; then
133+
echo "Waiting for MySQL at $DB_HOST:$DB_PORT..."
134+
for i in {1..30}; do
135+
if mysqladmin ping -h"$DB_HOST" -P"${DB_PORT:-3306}" --silent; then
136+
echo "MySQL is up!"
137+
break
138+
fi
139+
echo "Waiting for MySQL... retry $i/30"
140+
sleep 2
141+
done
142+
fi
143+
144+
exec "$@"

docs/docs/installation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ For Suma Server Installation:
6161
* Copy contents of `/SUMA_DOWNLOAD_DIR/service/web` to `/YOUR_WEB_DIR/sumaserver`
6262

6363

64+
Suma Software Installation (Docker install)
65+
-------------------------------------------
66+
This is setup to bootstrap, configure and make Suma available at the documented endpoints. Provided are example `.env` and `docker-compose.yml` files that can be used to start the container. It will create a database if one has not already been initalized. The log file is stored in `/var/log/suma.log`.
67+
6468
Apache Configuration
6569
---------------------
6670

0 commit comments

Comments
 (0)