Build farmOS directly into the container image. Mount /var/www/html/sites.

This commit is contained in:
Michael Stenta 2019-04-20 16:46:10 -04:00
parent bb877d7484
commit a5c5dc75a8
4 changed files with 17 additions and 121 deletions

View File

@ -1,10 +1,8 @@
# Inherit from the Drupal 7 image on Docker Hub.
FROM drupal:7
# Set environment variables.
ENV FARMOS_VERSION 7.x-1.1
ENV FARMOS_DEV_BRANCH 7.x-1.x
ENV FARMOS_DEV false
# Set the farmOS version in an environment variable.
ENV FARMOS_VERSION 7.x-1.x-dev
# Enable Apache rewrite module.
RUN a2enmod rewrite
@ -71,8 +69,17 @@ RUN curl -fsSL -o /usr/local/bin/drush "https://github.com/drush-ops/drush/relea
# Install git and unzip for use by Drush Make.
RUN apt-get update && apt-get install -y git unzip
# Mount a volume at /var/www/html.
VOLUME /var/www/html
# Download the packaged release of farmOS from drupal.org.
RUN curl -SL "http://ftp.drupal.org/files/projects/farm-${FARMOS_VERSION}-core.tar.gz" -o /tmp/farm-${FARMOS_VERSION}-core.tar.gz && \
tar -xvzf /tmp/farm-${FARMOS_VERSION}-core.tar.gz -C /var/www/html/ --strip-components=1 && \
chown -R www-data:www-data /var/www/html
# Create an archive of the sites directory in /tmp/sites.tar.gz, so that it can
# be restored after the volume is mounted via docker-entrypoint.sh.
RUN tar -cvzf /tmp/sites.tar.gz /var/www/html/sites/
# Mount a volume at /var/www/html/sites.
VOLUME /var/www/html/sites
# Set the entrypoint.
COPY docker-entrypoint.sh /usr/local/bin/

View File

@ -20,5 +20,3 @@ services:
- './.data/www:/var/www/html'
ports:
- '80:80'
environment:
FARMOS_DEV: 'true'

View File

@ -3,6 +3,6 @@ services:
www:
image: farmos/farmos:7.x-1.1
volumes:
- './.data/www:/var/www/html'
- './sites:/var/www/html/sites'
ports:
- '80:80'

View File

@ -1,118 +1,9 @@
#!/bin/bash
set -e
# Function for archiving the sites folder.
archive_sites () {
# If the sites folder exists, preserve it by temporarily archiving it.
if [ -e /var/www/html/sites ]; then
echo >&2 "Existing sites folder detected. Archiving temporarily..."
tar -czf /var/www/html/sites.tar.gz /var/www/html/sites
fi
}
# Function for restoring the sites folder.
restore_sites () {
# Restore the sites folder.
if [ -e /var/www/html/sites.tar.gz ]; then
echo >&2 "Restoring sites directory..."
rm -r /var/www/html/sites \
&& tar -xzf /var/www/html/sites.tar.gz -C /var/www/html/ --strip-components=3 \
&& rm /var/www/html/sites.tar.gz
fi
# Change ownership of the sites folder.
chown -R www-data:www-data /var/www/html/sites
}
# Function for deleting the farmOS codebase.
delete_farmos () {
# Remove the existing farmOS codebase, if it exists.
# Exclude sites.tar.gz.
if [ -e /var/www/html/index.php ]; then
echo >&2 "Removing existing farmOS codebase..."
find /var/www/html ! -name 'sites.tar.gz' -mindepth 1 -delete
fi
}
# Function for downloading and unpacking a farmOS release.
build_farmos_release () {
# Download and unpack farmOS release.
echo >&2 "Downloading farmOS $FARMOS_VERSION..."
curl -SL "http://ftp.drupal.org/files/projects/farm-${FARMOS_VERSION}-core.tar.gz" -o /usr/src/farm-${FARMOS_VERSION}-core.tar.gz
echo >&2 "Unpacking farmOS $FARMOS_VERSION..."
tar -xvzf /usr/src/farm-${FARMOS_VERSION}-core.tar.gz -C /var/www/html/ --strip-components=1
}
# Function for building a dev branch of farmOS.
build_farmos_dev () {
# Clone the farmOS installation profile, if it doesn't already exist.
if ! [ -e /var/farmOS/build-farm.make ]; then
git clone --branch $FARMOS_DEV_BRANCH https://git.drupal.org/project/farm.git /var/farmOS
# Update it if it does exist.
else
git -C /var/farmOS pull origin $FARMOS_DEV_BRANCH
fi
# Build farmOS with Drush. Use the --working-copy flag to keep .git folders.
drush make --working-copy --no-gitinfofile /var/farmOS/build-farm.make /tmp/farmOS \
&& cp -r /tmp/farmOS/. /var/www/html \
&& rm -r /tmp/farmOS
}
# Function for building farmOS.
build_farmos () {
# If a development environment is desired, build from dev branch. Otherwise,
# build from official packaged release.
if $FARMOS_DEV; then
build_farmos_dev
else
build_farmos_release
fi
}
# Function for determining whether a rebuild is required.
rebuild_required () {
# If farm.info doesn't exist, a rebuild is required.
if ! [ -e /var/www/html/profiles/farm/farm.info ]; then
echo >&2 "farmOS not detected. Building..."
return 0
fi
# Get the current version of farmOS from the installation profile info file.
CURRENT_VERSION="$(grep 'version' profiles/farm/farm.info | sed 's/version = .\(.*\)./\1/')"
# If this is not a development build, and the current version does not match
# the desired version, then rebuild.
if ! $FARMOS_DEV && [ "$CURRENT_VERSION" != "$FARMOS_VERSION" ]; then
echo >&2 "farmOS $CURRENT_VERSION was detected. Replacing with $FARMOS_VERSION..."
return 0
fi
echo >&2 "An existing farmOS codebase was detected."
echo >&2 "To force a rebuild, delete profiles/farm/farm.info and restart the container."
return 1
}
# Rebuild farmOS, if necessary.
if rebuild_required; then
# Archive the sites folder and delete the farmOS codebase.
archive_sites
delete_farmos
# Build farmOS.
build_farmos
# Restore the sites folder.
restore_sites
# If the sites directory is empty, unpack /tmp/sites.tar.gz.
if ! [ "$(ls -A /var/www/html/sites/)" ]; then
tar -xvzf /tmp/sites.tar.gz -C /var/www/html/sites/ --strip-components=4
fi
# Execute the arguments passed into this script.