#!/bin/bash CONTAINERS="dev-mysql dev-controller dev-ssh dev-rails dev-django dev-nginx dev-smtpsink dev-registry" die() { echo "error: $*" >&2 exit 1 } # generate the .env file for docker-compose # # DOCKERUSER is set to the uid:gid of the current user # # If the file already exists, it will never be replaced. The function just # prints a warning if it differs from the would-be generated file. generate_env_file() { cat >".env.tmp" </dev/null 2>/dev/null then echo "$name: already pulled" (set -x docker tag -- "$name" "$tag" docker push -- "$tag" docker rmi -- "$name" || true ) else echo "$name:" (set -x docker pull -- "$name" docker tag -- "$name" "$tag" docker push -- "$tag" docker rmi -- "$name" ) fi done } # remove container and its data purge_container() { local name="$1" (set -x ; docker-compose rm -f "$name") # ensure $name is well formatted (to avoid disasters with rm -rf) [[ "$name" =~ ^dev-[a-z][a-z0-9-]*$ ]] || die "bad container name: $name" local data_dir="data/${name/dev-/}" if [ -e "$data_dir" ] ; then echo "warning: data dir '$data_dir' already exists" echo -n "remove it [y/N]? " read confirm [ "$confirm" = "y" ] || die "aborted" (set -x ; rm -rf -- "$data_dir") fi } # init a container init_container() { local name="$1" local data_dir="data/${name/dev-/}" (set -x # create the directory before running the container # so that it is owned by the current user (not by root) mkdir -p -- "$data_dir" # run the /dk/container_init script if present # # FIXME: the "sleep 1" is because of a race condition in # docker-compose (if the command finishes too quickly, then it # is run twice) docker-compose run --rm "$name" sh -e -c \ 'if [ -f /dk/container_init ] ; then sleep 1; /dk/container_init ; fi' # start the container docker-compose up -d "$name" ) } ################################################################## if [ ! -f docker-compose.yml ] || [ ! -f bootstrap ] ; then die "the 'bootstrap' script must be run from the root of the allgo repository" fi if [ "$1" == "-h" ] ; then cat <