From 9e9faec1f9d07cdeff69d8650c326a5665ffff43 Mon Sep 17 00:00:00 2001 From: Samuel Cooper Date: Sun, 15 Apr 2018 00:15:16 +0200 Subject: [PATCH] Add setup, clean and status to docker-compose wrapper --- Dockerfile.diaspora | 17 +++---- docker-compose.build.yml | 11 ++++ docker-compose.yml | 52 +++++++++++-------- start.sh | 18 ------- wrapper | 105 +++++++++++++++++++++++++-------------- 5 files changed, 117 insertions(+), 86 deletions(-) create mode 100644 docker-compose.build.yml delete mode 100755 start.sh diff --git a/Dockerfile.diaspora b/Dockerfile.diaspora index 806cabe..a665e50 100644 --- a/Dockerfile.diaspora +++ b/Dockerfile.diaspora @@ -1,7 +1,7 @@ FROM ruby:2.4.4-slim-stretch -ARG DEBIAN_FRONTEND=noninteractive -RUN apt-get update \ +RUN DEBIAN_FRONTEND=noninteractive \ + apt-get update \ && apt-get install -y -qq \ build-essential \ libssl-dev \ @@ -21,8 +21,8 @@ RUN apt-get update \ ARG EXT_UID ARG EXT_GID -RUN addgroup --system --gid ${EXT_GID} diaspora -RUN adduser --system --uid ${EXT_UID} --gid ${EXT_GID} diaspora +RUN addgroup --system --gid ${EXT_GID} diaspora \ + && adduser --system --uid ${EXT_UID} --gid ${EXT_GID} diaspora USER diaspora ENV DIASPORA_HOME /home/diaspora/diaspora @@ -31,11 +31,8 @@ WORKDIR $DIASPORA_HOME ENV GEM_HOME $DIASPORA_HOME/vendor/bundle ENV BUNDLE_PATH="$GEM_HOME" \ - BUNDLE_BIN="$GEM_HOME/bin" \ - BUNDLE_APP_CONFIG="$DIASPORA_HOME/.bundle" + BUNDLE_BIN="$GEM_HOME/bin" \ + BUNDLE_APP_CONFIG="$DIASPORA_HOME/.bundle" ENV PATH $BUNDLE_BIN:$PATH -# Finish setup -COPY ./start.sh ../start.sh - -CMD ["../start.sh"] +CMD ["./script/server"] diff --git a/docker-compose.build.yml b/docker-compose.build.yml new file mode 100644 index 0000000..f150d0e --- /dev/null +++ b/docker-compose.build.yml @@ -0,0 +1,11 @@ +version: "3.4" + +services: + diaspora: + build: + context: . + dockerfile: Dockerfile.diaspora + args: + EXT_UID: "${EXT_UID}" + EXT_GID: "${EXT_GID}" + image: diaspora:dev-latest diff --git a/docker-compose.yml b/docker-compose.yml index 2cde805..2a6c406 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,36 +1,44 @@ -version: "3" +version: "3.4" -volumes: - postgres_db: +x-diaspora-base: &diaspora-base + image: diaspora:dev-latest + volumes: + - "${DIASPORA_PATH}:/home/diaspora/diaspora:rw" + +x-diaspora-db: &diaspora-db + depends_on: + - "${DIASPORA_DOCKER_DB}" +volumes: + postgresql_data: + mysql_data: services: + diaspora-bundle: + << : *diaspora-base + command: /bin/sh -c "script/configure_bundler && bin/bundle install --full-index" + + diaspora-migrations: + << : *diaspora-base + << : *diaspora-db + command: /bin/sh -c "bin/rake db:create db:migrate" diaspora: - build: - context: . - dockerfile: Dockerfile.diaspora - args: - EXT_UID: "${EXT_UID}" - EXT_GID: "${EXT_GID}" - image: diaspora:dev-latest - environment: - DIASPORA_DOCKER_DEV_INSTALL_GEMS: "${DIASPORA_DOCKER_DEV_INSTALL_GEMS}" - DIASPORA_DOCKER_DEV_DB_MIGRATE: "${DIASPORA_DOCKER_DEV_DB_MIGRATE}" + << : *diaspora-base + << : *diaspora-db ports: - 8080:3000 - volumes: - - ./diaspora.docker:/home/diaspora/diaspora:rw - depends_on: - - postgres -# - redis - postgres: + postgresql: image: postgres:9.6 ports: - 55432:5432 volumes: - - postgres_db:/var/lib/postgresql + - postgresql_data:/var/lib/postgresql -# redis: -# image: redis:latest + mysql: + image: mysql:5.7 + ports: + - 53306:3306 + volumes: + - mysql_data:/var/lib/mysql diff --git a/start.sh b/start.sh deleted file mode 100755 index d34f6c4..0000000 --- a/start.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash --login - -action_install_gems() { - script/configure_bundler - bin/bundle install --full-index -} - -action_db_migrate() { - bin/rake db:create db:migrate -} - -[ -z $DIASPORA_DOCKER_DEV_INSTALL_GEMS ] && action_install_gems - -[ -z $DIASPORA_DOCKER_DEV_DB_MIGRATE ] && action_db_migrate - -[ ! -f public/source.tar.gz ] && touch public/source.tar.gz - -exec ./script/server diff --git a/wrapper b/wrapper index 4cf5f65..5cf6ebf 100755 --- a/wrapper +++ b/wrapper @@ -3,70 +3,88 @@ print_usage() { echo "$SCRIPT_NAME COMMAND" echo - echo "COMMAND" - echo " build" -# echo " bundle" -# echo " migrate" -# echo " setup" - echo " start" - echo " stop" -# echo " clean" + echo "Commands:" + echo " build Rebuild image" + echo " bundle Install gems using bundle" + echo " migrate Execute pending migrations (incl. database setup)" + echo " setup Build, bundle and migrate" + echo " start Start diaspora\* incl. database" + echo " stop Stop all diaspora\*-related containers" + echo " clean Delete all diaspora\*-related containers and volumes" + echo " status Show currently running containers and related images" # echo " rspec" } +dia_build() { + if [ -z "$UID" -o -z "$GID" ]; then + # $UID is not builtin, determine using a program + if ! type "id" 2>&1 >/dev/null; then + echo "Fatal: Cannot determine UID or GID." + exit 1 + fi + EXT_UID=$(id -u) + EXT_GID=$(id -g) + else + EXT_UID=$UID + EXT_GID=$GID + fi + docker-compose rm -v diaspora + EXT_UID=$EXT_UID EXT_GID=$EXT_GID docker-compose -f docker-compose.build.yml build diaspora +} + +dia_bundle() { + docker-compose up $option diaspora-bundle + docker-compose rm --stop -v --force diaspora-bundle +} + +dia_migrate() { + docker-compose up $option diaspora-migrations +} + dia_setup() { dia_build # This is just a temporary (dev-only) solution [ ! -f $DIASPORA_PATH/public/source.tar.gz ] && touch $DIASPORA_PATH/public/source.tar.gz - # do bundle install - # start db container - # do db migrate - # start dia container -} - -dia_build() { - docker-compose build diaspora + dia_bundle + dia_migrate } dia_start() { - docker-compose up + docker-compose up $option diaspora } dia_stop() { docker-compose stop } +dia_clean() { + docker-compose down +} + +dia_status() { + docker-compose ps + docker-compose images +} + SCRIPT_NAME=$(basename $0) SCRIPT_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) -DIASPORA_PATH= - -if [ -z "$UID" -o -z "$GID" ]; then - # $UID is not builtin, determine using a program - if ! type "id" 2>&1 >/dev/null; then - echo "Fatal: Cannot determine UID or GID." - exit 1 - fi - EXT_UID=$(id -u) - EXT_GID=$(id -g) -else - EXT_UID=$UID - EXT_GID=$GID -fi +export DIASPORA_PATH=./diaspora.docker +export DIASPORA_DOCKER_DB=postgresql -export EXT_UID -export EXT_GID -export DIASPORA_PATH if [ $# -lt 1 ]; then print_usage exit 1 fi -command=$1 +dia_command=$1 shift +if [ $# -eq 1 -a "$1" -eq "-d" ]; then + export option="-d" +fi -case $command in +case $dia_command in --help|-h) print_usage exit 0 @@ -74,12 +92,27 @@ case $command in build) dia_build ;; + bundle) + dia_bundle + ;; + migrate) + dia_migrate + ;; + setup) + dia_setup + ;; start) dia_start ;; stop) dia_stop ;; + clean) + dia_clean + ;; + status) + dia_status + ;; *) print_usage exit 1