From 418480bff571abc6a57929a8a065c93642f63f73 Mon Sep 17 00:00:00 2001 From: Gernot Schulz Date: Fri, 14 Aug 2020 16:21:21 +0200 Subject: [PATCH] build.sh: Make build script more useful build.sh replaces docker-compose as an image build tool. Instead, all OpenSlides services can be built using this script which offers various important options such as tagging and configurable defaults. The now-redundant build instructions have been removed from the YAML templates. The almost identical server and client build scripts have been made fully identical. --- README.rst | 5 +- client/build.sh | 10 +--- client/docker/Dockerfile | 2 +- docker/build.sh | 104 +++++++++++++++++++++++++++++++++-- docker/docker-compose.yml.m4 | 4 -- docker/docker-stack.yml.m4 | 4 -- server/build.sh | 21 +++++-- 7 files changed, 119 insertions(+), 31 deletions(-) mode change 100755 => 120000 client/build.sh diff --git a/README.rst b/README.rst index 8abc3eccf..99e432b27 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,7 @@ First, you have to clone this repository:: You need to build the Docker images for the client and server with this script:: - $ ./build.sh + $ ./build.sh all You must define a Django secret key in ``secrets/django.env``, for example:: @@ -46,8 +46,7 @@ Afterwards, generate the configuration file:: EXTERNAL_HTTP_PORT=8000 m4 docker-compose.yml.m4 > docker-compose.yml -Once the server and client have been built, you can use ``docker-compose`` as -usual (except for the ``build`` method):: +Finally, you can start the instance using ``docker-compose``:: $ docker-compose up $ # or: diff --git a/client/build.sh b/client/build.sh deleted file mode 100755 index 3836ae214..000000000 --- a/client/build.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -cd "$(dirname "$0")" - -printf "Client built on %s:\n\nBranch: %s\n\n%s\n" \ - "$(date)" \ - "$(git rev-parse --abbrev-ref HEAD)" \ - "$(git show -s --format=raw)" > client-version.txt -docker build -f docker/Dockerfile . $@ diff --git a/client/build.sh b/client/build.sh new file mode 120000 index 000000000..b2207a97d --- /dev/null +++ b/client/build.sh @@ -0,0 +1 @@ +../server/build.sh \ No newline at end of file diff --git a/client/docker/Dockerfile b/client/docker/Dockerfile index 96f8f4f88..c12783299 100644 --- a/client/docker/Dockerfile +++ b/client/docker/Dockerfile @@ -16,7 +16,7 @@ COPY browserslist *.json ./ COPY src ./src RUN npm run build -COPY client-version.txt static/ +COPY docker/client-version.txt static/ FROM nginx COPY --from=nodejs /build/app/static /usr/share/nginx/html diff --git a/docker/build.sh b/docker/build.sh index cd60a72bb..c52ac54a3 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -1,7 +1,103 @@ #!/bin/bash -../server/build.sh -t openslides/openslides-server:latest -../client/build.sh -t openslides/openslides-client:latest -[[ -f docker-compose.yml ]] || m4 < docker-compose.yml.m4 > docker-compose.yml +set -e -docker-compose build +declare -A TARGETS +TARGETS=( + [client]="$(dirname "${BASH_SOURCE[0]}")/../client/docker/" + [server]="$(dirname "${BASH_SOURCE[0]}")/../server/docker/" + [media-service]="https://github.com/OpenSlides/openslides-media-service.git" + [pgbouncer]="https://github.com/OpenSlides/openslides-docker-compose.git#:pgbouncer" + [postfix]="https://github.com/OpenSlides/openslides-docker-compose.git#:postfix" + [repmgr]="https://github.com/OpenSlides/openslides-docker-compose.git#:repmgr" +) + +DOCKER_REPOSITORY="openslides" +DOCKER_TAG="latest" +CONFIG="/etc/osinstancectl" +OPTIONS=() +BUILT_IMAGES=() +DEFAULT_TARGETS=(server client) + +usage() { + cat << EOF +Usage: $(basename ${BASH_SOURCE[0]}) [] ... + +Options: + -D, --docker-repo Specify a Docker repository + (default: unspecified, i.e., system default) + -t, --tag Tag the Docker image (default: $DOCKER_TAG) + --no-cache Pass --no-cache to docker-build +EOF +} + +# Config file +if [[ -f "$CONFIG" ]]; then + echo "Found ${CONFIG} file." + source "$CONFIG" +fi + +shortopt="hr:D:t:" +longopt="help,docker-repo:,tag:,no-cache" +ARGS=$(getopt -o "$shortopt" -l "$longopt" -n "$ME" -- "$@") +if [ $? -ne 0 ]; then usage; exit 1; fi +eval set -- "$ARGS"; +unset ARGS + +# Parse options +while true; do + case "$1" in + -D|--docker-repo) + DOCKER_REPOSITORY="$2" + shift 2 + ;; + -t|--tag) + DOCKER_TAG="$2" + shift 2 + ;; + --no-cache) + OPTIONS+="--no-cache" + shift 1 + ;; + -h|--help) usage; exit 0 ;; + --) shift ; break ;; + *) usage; exit 1 ;; + esac +done + +SELECTED_TARGETS=($@) +[[ "${#SELECTED_TARGETS[@]}" -ge 1 ]] || SELECTED_TARGETS=("${DEFAULT_TARGETS[@]}") +[[ "${SELECTED_TARGETS[@]}" != "all" ]] || SELECTED_TARGETS=("${!TARGETS[@]}") + +for i in "${SELECTED_TARGETS[@]}"; do + + loc="${TARGETS[$i]}" + [[ -n "$loc" ]] || { + echo "ERROR: Cannot build ${i}: not configured." + continue + } + + img_name="openslides-${i}" + img="${img_name}:${DOCKER_TAG}" + if [[ -n "$DOCKER_REPOSITORY" ]]; then + img="${DOCKER_REPOSITORY}/${img}" + fi + + echo "Building $img..." + # Special instructions for local services + build_script="$(dirname "${BASH_SOURCE[0]}")/../${i}/build.sh" + if [[ -f "$build_script" ]]; then + . "$build_script" + else + docker build --tag "$img" --pull "${OPTIONS[@]}" "$loc" + fi + BUILT_IMAGES+=("$img") +done + +for img in "${BUILT_IMAGES[@]}"; do + read -p "Push image '$img' to repository? [y/N] " REPL + case "$REPL" in + Y|y|Yes|yes|YES) + docker push "$img" ;; + esac +done diff --git a/docker/docker-compose.yml.m4 b/docker/docker-compose.yml.m4 index db126094e..1d001d5a2 100644 --- a/docker/docker-compose.yml.m4 +++ b/docker/docker-compose.yml.m4 @@ -48,7 +48,6 @@ x-osserver-env: &default-osserver-env REDIS_REPLICAS: ifenvelse(`REDIS_RO_SERVICE_REPLICAS', 1) x-pgnode: &default-pgnode image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-repmgr:latest - build: https://github.com/OpenSlides/openslides-docker-compose.git#:repmgr networks: - dbnet labels: @@ -134,7 +133,6 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' environment: - PG_NODE_LIST=pgnode1`'PGBOUNCER_NODELIST image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-pgbouncer:latest - build: https://github.com/OpenSlides/openslides-docker-compose.git#:pgbouncer restart: always networks: back: @@ -144,7 +142,6 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' dbnet: postfix: image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-postfix:latest - build: https://github.com/OpenSlides/openslides-docker-compose.git#:postfix restart: always environment: MYHOSTNAME: "ifenvelse(`POSTFIX_MYHOSTNAME', localhost)" @@ -177,7 +174,6 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' back: media: image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-media-service:latest - build: https://github.com/OpenSlides/openslides-media-service.git environment: - CHECK_REQUEST_URL=server:8000/check-media/ restart: always diff --git a/docker/docker-stack.yml.m4 b/docker/docker-stack.yml.m4 index e5b900c20..191e8aacf 100644 --- a/docker/docker-stack.yml.m4 +++ b/docker/docker-stack.yml.m4 @@ -47,7 +47,6 @@ x-osserver-env: &default-osserver-env REDIS_REPLICAS: ifenvelse(`REDIS_RO_SERVICE_REPLICAS', 3) x-pgnode: &default-pgnode image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-repmgr:latest - build: https://github.com/OpenSlides/openslides-docker-compose.git#:repmgr networks: - dbnet labels: @@ -141,7 +140,6 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' environment: - PG_NODE_LIST=pgnode1`'PGBOUNCER_NODELIST image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-pgbouncer:latest - build: https://github.com/OpenSlides/openslides-docker-compose.git#:pgbouncer networks: back: aliases: @@ -156,7 +154,6 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' constraints: ifenvelse(`PGBOUNCER_PLACEMENT_CONSTR', [node.role == manager]) postfix: image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-postfix:latest - build: https://github.com/OpenSlides/openslides-docker-compose.git#:postfix environment: MYHOSTNAME: "ifenvelse(`POSTFIX_MYHOSTNAME', localhost)" RELAYHOST: "ifenvelse(`POSTFIX_RELAYHOST', localhost)" @@ -203,7 +200,6 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' delay: 5s media: image: ifenvelse(`DEFAULT_DOCKER_REGISTRY', openslides)/openslides-media-service:latest - build: https://github.com/OpenSlides/openslides-media-service.git environment: - CHECK_REQUEST_URL=server:8000/check-media/ deploy: diff --git a/server/build.sh b/server/build.sh index 8ce7ef779..8d4439dc2 100755 --- a/server/build.sh +++ b/server/build.sh @@ -1,10 +1,19 @@ #!/bin/bash -cd "$(dirname "$0")" +cd "$(dirname "${BASH_SOURCE[0]}")" +service_name="$(basename "$PWD")" +version_file="docker/${service_name}-version.txt" -printf "Server built on %s:\n\nBranch: %s\n\n%s\n" \ - "$(date)" \ - "$(git rev-parse --abbrev-ref HEAD)" \ - "$(git show -s --format=raw)" > docker/server-version.txt +{ + printf "${service_name^} built on %s:\n\n" "$(date)" + printf "Branch: %s\n" "$(git rev-parse --abbrev-ref HEAD)" + printf '\n' + git show -s --format=raw +} > "$version_file" -docker build -f docker/Dockerfile . $@ +docker build --tag "${img:-openslides/openslides-${service_name}:latest}" \ + --pull "${OPTIONS[@]}" -f docker/Dockerfile . + +rm "$version_file" || true +unset version_file +unset service_name