Skip to content

Commit

Permalink
Replace Ansible with Shell script
Browse files Browse the repository at this point in the history
* Remove Ansible and all Python packages
* Reduce image size by 452 MB (689 MB => 237 MB uncompressed)
  • Loading branch information
ngosang committed Oct 29, 2022
1 parent ae9214e commit 4599b78
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 265 deletions.
24 changes: 13 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
FROM golang:alpine3.16 as builder

# envsubst from gettext can not replace env vars with default values
# this package is not available for ARM32 and we have to build it from source code
RUN go install -v github.com/a8m/envsubst/cmd/[email protected]

FROM alpine:3.16

LABEL maintainer "Marvin Steadfast <[email protected]>"
COPY --from=builder /go/bin/envsubst /usr/bin/envsubst

ARG WALLABAG_VERSION=2.5.2

RUN set -ex \
&& apk update \
&& apk upgrade --available \
&& apk add \
ansible \
&& apk add --no-cache \
curl \
libwebp \
mariadb-client \
nginx \
pcre \
php8 \
Expand Down Expand Up @@ -40,9 +42,8 @@ RUN set -ex \
php8-xmlreader \
php8-tidy \
php8-intl \
py3-mysqlclient \
py3-psycopg2 \
py-simplejson \
mariadb-client \
postgresql14-client \
rabbitmq-c \
s6 \
tar \
Expand All @@ -59,13 +60,14 @@ RUN set -ex \
COPY root /

RUN set -ex \
&& mv /var/www/wallabag/app /tmp/app \
&& curl -L -o /tmp/wallabag.tar.gz https://github.com/wallabag/wallabag/archive/$WALLABAG_VERSION.tar.gz \
&& tar xvf /tmp/wallabag.tar.gz -C /tmp \
&& mkdir /var/www/wallabag \
&& mv /tmp/wallabag-*/* /var/www/wallabag/ \
&& rm -rf /tmp/wallabag* \
&& mv /tmp/app/config/parameters.yml /var/www/wallabag/app/config/parameters.yml \
&& mv /etc/wallabag/* /var/www/wallabag/app/config/ \
&& cd /var/www/wallabag \
&& mkdir data/assets \
&& SYMFONY_ENV=prod composer install --no-dev -o --prefer-dist --no-progress \
&& rm -rf /root/.composer/* /var/www/wallabag/var/cache/* /var/www/wallabag/var/logs/* /var/www/wallabag/var/sessions/* \
&& chown -R nobody:nobody /var/www/wallabag
Expand Down
60 changes: 49 additions & 11 deletions root/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,65 @@
#!/bin/sh

COMMAND_ARG1="$1"
COMMAND_ARG2="$2"

cd /var/www/wallabag || exit

provisioner () {
echo "Starting provisioner..."
if ! out=`ansible-playbook -i /etc/ansible/hosts /etc/ansible/entrypoint.yml -c local "$@"`;then
echo $out;
SYMFONY__ENV__DATABASE_DRIVER=${SYMFONY__ENV__DATABASE_DRIVER:-pdo_sqlite}
POPULATE_DATABASE=${POPULATE_DATABASE:-True}

envsubst < app/config/parameters.template.yml > app/config/parameters.yml

if [ "$SYMFONY__ENV__DATABASE_DRIVER" = "pdo_sqlite" ]; then
if [ ! -f "/var/www/wallabag/data/db/wallabag.sqlite" ]; then
su -c "php bin/console wallabag:install --env=prod -n" -s /bin/sh nobody
fi
fi

if [ "$POPULATE_DATABASE" = "True" ] && [ "$SYMFONY__ENV__DATABASE_DRIVER" = "pdo_mysql" ]; then
timeout 60s /bin/sh -c "until echo 'Waiting for MariaDB ...' && mysql -h ${SYMFONY__ENV__DATABASE_HOST} --port ${SYMFONY__ENV__DATABASE_PORT} -uroot -p${MYSQL_ROOT_PASSWORD} -e 'show databases;' > /dev/null 2>&1 ; do sleep 1 ; done"
mysql -h "${SYMFONY__ENV__DATABASE_HOST}" --port "${SYMFONY__ENV__DATABASE_PORT}" -uroot -p"${MYSQL_ROOT_PASSWORD}" \
-e "CREATE DATABASE ${SYMFONY__ENV__DATABASE_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
if [ "$SYMFONY__ENV__DATABASE_USER" != "root" ]; then
mysql -h "${SYMFONY__ENV__DATABASE_HOST}" --port "${SYMFONY__ENV__DATABASE_PORT}" -uroot -p"${MYSQL_ROOT_PASSWORD}" \
-e "CREATE USER '${SYMFONY__ENV__DATABASE_USER}'@'%' IDENTIFIED BY '${SYMFONY__ENV__DATABASE_PASSWORD}';"
mysql -h "${SYMFONY__ENV__DATABASE_HOST}" --port "${SYMFONY__ENV__DATABASE_PORT}" -uroot -p"${MYSQL_ROOT_PASSWORD}" \
-e "GRANT ALL PRIVILEGES ON ${SYMFONY__ENV__DATABASE_NAME}.* TO '${SYMFONY__ENV__DATABASE_USER}'@'%';"
fi
su -c "php bin/console wallabag:install --env=prod -n" -s /bin/sh nobody
fi

if [ "$POPULATE_DATABASE" = "True" ] && [ "$SYMFONY__ENV__DATABASE_DRIVER" = "pdo_pgsql" ]; then
export PGPASSWORD="${POSTGRES_PASSWORD}"
timeout 60s /bin/sh -c "until echo 'Waiting for Postgres ...' && pg_isready -h ${SYMFONY__ENV__DATABASE_HOST} -p ${SYMFONY__ENV__DATABASE_PORT} -U ${POSTGRES_USER} > /dev/null 2>&1 ; do sleep 1 ; done"
psql -q -h "${SYMFONY__ENV__DATABASE_HOST}" -p "${SYMFONY__ENV__DATABASE_PORT}" -U "${POSTGRES_USER}" \
-c "CREATE DATABASE ${SYMFONY__ENV__DATABASE_NAME};"
if [ "$SYMFONY__ENV__DATABASE_USER" != "postgres" ]; then
psql -q -h "${SYMFONY__ENV__DATABASE_HOST}" -p "${SYMFONY__ENV__DATABASE_PORT}" -U "${POSTGRES_USER}" \
-c "CREATE ROLE ${SYMFONY__ENV__DATABASE_USER} with PASSWORD '${SYMFONY__ENV__DATABASE_PASSWORD}' LOGIN;"
fi
su -c "php bin/console wallabag:install --env=prod -n" -s /bin/sh nobody
fi
echo "Provisioner finished."

rm -f -r /var/www/wallabag/var/cache
su -c "SYMFONY_ENV=prod composer install --no-dev -o --prefer-dist" -s /bin/sh nobody
}

if [ "$1" = "wallabag" ];then
if [ "$COMMAND_ARG1" = "wallabag" ]; then
echo "Starting Wallabag..."
provisioner
echo "Wallabag is ready!"
exec s6-svscan /etc/s6/
fi

if [ "$1" = "import" ];then
provisioner --skip-tags=firstrun
cd /var/www/wallabag/
exec su -c "bin/console wallabag:import:redis-worker --env=prod $2 -vv" -s /bin/sh nobody
if [ "$COMMAND_ARG1" = "import" ]; then
provisioner
exec su -c "bin/console wallabag:import:redis-worker --env=prod $COMMAND_ARG2 -vv" -s /bin/sh nobody
fi

if [ "$1" = "migrate" ];then
if [ "$COMMAND_ARG1" = "migrate" ]; then
provisioner
cd /var/www/wallabag/
exec su -c "bin/console doctrine:migrations:migrate --env=prod --no-interaction" -s /bin/sh nobody
fi

Expand Down
171 changes: 0 additions & 171 deletions root/etc/ansible/entrypoint.yml

This file was deleted.

2 changes: 0 additions & 2 deletions root/etc/ansible/hosts

This file was deleted.

63 changes: 0 additions & 63 deletions root/etc/ansible/templates/parameters.yml.j2

This file was deleted.

Loading

0 comments on commit 4599b78

Please sign in to comment.