From 8ce7f2abdc847d129aa426909f7f39c1fd332c93 Mon Sep 17 00:00:00 2001 From: Andre Zoledziowski Date: Sat, 13 Oct 2018 14:30:28 +0200 Subject: [PATCH] Created another image for shared scripts. --- build.sh | 54 +++++++++++++++----------------- common/Dockerfile | 3 ++ common/common.sh | 49 +++++++++++++++++++++++++++++ core/Dockerfile | 38 +++++++++++++++-------- core/download_community.sh | 29 ----------------- core/start-service.sh | 0 download_community.sh | 29 ----------------- webapp/Dockerfile | 60 +++++++++++++++++++----------------- webapp/download_community.sh | 29 ----------------- webapp/start.sh | 0 10 files changed, 134 insertions(+), 157 deletions(-) create mode 100644 common/Dockerfile create mode 100755 common/common.sh delete mode 100755 core/download_community.sh mode change 100644 => 100755 core/start-service.sh delete mode 100755 download_community.sh delete mode 100755 webapp/download_community.sh mode change 100644 => 100755 webapp/start.sh diff --git a/build.sh b/build.sh index 300f8f9..ddf4b07 100755 --- a/build.sh +++ b/build.sh @@ -9,7 +9,7 @@ serial="" component="" nocache="" -function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; } +. ./common/common.sh function _usage() { @@ -23,7 +23,7 @@ function _usage() echo "-a You can specify custom build args via e.g. -a ADDITIONAL_KOPANO_PACKAGES=kopano-migration-imap" } -while getopts ":s:c:b:p:n:a:i" opt; do +while getopts ":s:c:b:p:a:i" opt; do case $opt in s) serial=$OPTARG @@ -81,12 +81,9 @@ then # start build of supported kopano # get current version to brand and tag the image correctly - currentVersion=$(curl -s -S -L https://serial:$serial@download.kopano.io/supported/$component:/$branch/Debian_9.0/Packages.gz |\ + currentVersion=$(curl -s -S -L "https://serial:$serial@download.kopano.io/supported/$component:/$branch/Debian_9.0/Packages.gz" |\ gzip -d | grep -A 8 "^Package: $mainpackage$" | awk '/Version/ { print $2 }') - currentVersionDocker=$(echo $currentVersion | sed 's/+/plus/') - - # webapp also needs core repository if [ "$component" == "webapp" ] then @@ -94,49 +91,48 @@ then fi echo "Start building supported kopano $component image version ($currentVersion)..." - + set -x # build it - docker build --build-arg KOPANO_${component^^}_REPOSITORY_URL=https://serial:$serial@download.kopano.io/supported/$component:/$branch/Debian_9.0 \ + if docker build \ + --pull \ + --build-arg "KOPANO_${component^^}_REPOSITORY_URL=https://serial:$serial@download.kopano.io/supported/$component:/$branch/Debian_9.0" \ --build-arg RELEASE_KEY_DOWNLOAD=1 \ - --build-arg DOWNLOAD_COMMUNITY_PACKAGES=0 \ - --build-arg KOPANO_${component^^}_VERSION=$currentVersion \ - -t zokradonh/kopano_$component:$currentVersionDocker \ - -t zokradonh/kopano_$component:latest-$branch \ + --build-arg "DOWNLOAD_COMMUNITY_PACKAGES=0" \ + --build-arg "KOPANO_${component^^}_VERSION=$currentVersion" \ + -t "zokradonh/kopano_$component:${currentVersion//+/plus}" \ + -t "zokradonh/kopano_$component:latest-$branch" \ $nocache \ $customBuildString \ - ${buildcontext_base}${component} - if [ $? -eq 0 ] + "${buildcontext_base}${component}" then + set +x echo "Please note that this image does include your serial. If you publish this image then your serial is exposed to public." fi else # start build of community kopano - hash jq > /dev/null - if [ $? -ne 0 ] + + if ! hash jq then echo "Please install jq in order to run this build script." exit 1 fi # query community server by h5ai API - filename=$(curl -s -S -L -d "action=get&items%5Bhref%5D=%2Fcommunity%2F$component%3A%2F&items%5Bwhat%5D=1" -H \ - "Accept: application/json" https://download.kopano.io/community/ | jq '.items[].href' | \ - grep Debian_9.0-a | sed 's#"##g' | sed "s#/community/$component:/##") + filename=$(h5ai_query "$component") - filename=$(urldecode $filename) - - currentVersion=$(echo $filename | sed -r 's#[a-z]+-([0-9_.+]+)-.*#\1#') - currentVersionDocker=$(echo $currentVersion | sed 's/+/plus/') + currentVersion=$(version_from_filename "$filename") echo "Start building community kopano $component image version ($currentVersion)..." - + set -x # build it - docker build -t zokradonh/kopano_$component:$currentVersionDocker \ - -t zokradonh/kopano_$component:latest-$branch \ - -t zokradonh/kopano_$component:latest \ - --build-arg KOPANO_${component^^}_VERSION=$currentVersion \ + docker build --pull \ + -t "zokradonh/kopano_$component:${currentVersion//+/plus}" \ + -t "zokradonh/kopano_$component:latest-$branch" \ + -t "zokradonh/kopano_$component:latest" \ + --build-arg "KOPANO_${component^^}_VERSION=$currentVersion" \ $nocache \ $customBuildString \ - ${buildcontext_base}${component} + "${buildcontext_base}${component}" + set +x fi \ No newline at end of file diff --git a/common/Dockerfile b/common/Dockerfile new file mode 100644 index 0000000..04e633b --- /dev/null +++ b/common/Dockerfile @@ -0,0 +1,3 @@ +FROM scratch + +COPY common.sh / \ No newline at end of file diff --git a/common/common.sh b/common/common.sh new file mode 100755 index 0000000..f2557ef --- /dev/null +++ b/common/common.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +set -eu + +function urldecode { : "${*//+/ }"; echo -e "${_//%/\\x}"; } + +function version_from_filename { + echo "$1" | sed -r 's#[a-z]+-([0-9_.+]+)-.*#\1#' +} + +function h5ai_query { + filename=$(curl -s -S -L -d "action=get&items%5Bhref%5D=%2Fcommunity%2F$1%3A%2F&items%5Bwhat%5D=1" -H \ + "Accept: application/json" https://download.kopano.io/community/ | jq '.items[].href' | \ + grep 'Debian_9.0-all\|Debian_9.0-amd64' | sed 's#"##g' | sed "s#/community/$1:/##") + + if [ -z "${filename// }" ]; then + echo "unknown component" + exit 1 + fi + + filename=$(urldecode "$filename") + echo "$filename" +} + +function dl_and_package_community { + # take component as first argument and fallback to core if none given + component=${1:-core} + + # query community server by h5ai API + filename=$(h5ai_query "$component") + + # download & extract packages + curl -s -S -L -o "$filename" https://download.kopano.io/community/"$component":/"${filename}" + tar xf "$filename" + + # save buildversion + currentVersion=$(version_from_filename "$filename") + echo "$component-$currentVersion" >> /kopano/buildversion + + # save disk space + rm "$filename" + + mv "${filename%.tar.gz}" "$component" + + # prepare directory to be apt source + cd "$component" + apt-ftparchive packages . | gzip -9c > Packages.gz + cd ".." +} \ No newline at end of file diff --git a/core/Dockerfile b/core/Dockerfile index b7e0816..471834e 100644 --- a/core/Dockerfile +++ b/core/Dockerfile @@ -1,9 +1,11 @@ +FROM zokradonh/kopano_common AS common + FROM debian:stretch LABEL maintainer=az@zok.xyz \ version="2.0" -RUN mkdir -p /kopano/repo /kopano/data +RUN mkdir -p /kopano/repo /kopano/data /kopano/helper WORKDIR /kopano/repo ARG DEBIAN_FRONTEND=noninteractive @@ -29,29 +31,40 @@ RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \ update-locale LANG=en_US.UTF-8 ARG KOPANO_CORE_VERSION=newest -ARG KOPANO_CORE_REPOSITORY_URL="file:/kopano/repo" +ARG KOPANO_CORE_REPOSITORY_URL="file:/kopano/repo/core" ARG KOPANO_REPOSITORY_FLAGS="trusted=yes" ARG DOWNLOAD_COMMUNITY_PACKAGES=1 ARG RELEASE_KEY_DOWNLOAD=0 -ARG ADDITIONAL_KOPANO_PACKAGES +ARG ADDITIONAL_KOPANO_PACKAGES="" -# community build -COPY download_community.sh /kopano/download_community.sh +# get common utilities +COPY --from=common /common.sh /kopano/helper/ + +SHELL [ "/bin/bash", "-c"] RUN \ - # community download - [ ${DOWNLOAD_COMMUNITY_PACKAGES} -eq 1 ] && \ - /kopano/download_community.sh core && \ + # community download and package as apt source repository + . /kopano/helper/common.sh && \ + if [ ${DOWNLOAD_COMMUNITY_PACKAGES} -eq 1 ]; then \ + dl_and_package_community "core"; \ + fi; \ + echo "deb [${KOPANO_REPOSITORY_FLAGS}] ${KOPANO_CORE_REPOSITORY_URL} ./" > /etc/apt/sources.list.d/kopano.list; \ + # save kopano version if supported kopano + if [ -z /kopano/buildversion ]; then \ + echo "core-${KOPANO_CORE_VERSION}" > /kopano/buildversion; \ + fi; \ + # install apt key if supported kopano + if [ ${RELEASE_KEY_DOWNLOAD} -eq 1 ]; then \ + curl -s -S -o - "${KOPANO_CORE_REPOSITORY_URL}/Release.key" | apt-key add -; \ + fi; \ # install - set -x && \ - echo ${KOPANO_CORE_VERSION} > /kopano/buildversion && \ - echo "deb [${KOPANO_REPOSITORY_FLAGS}] ${KOPANO_CORE_REPOSITORY_URL} ./" > /etc/apt/sources.list.d/kopano-core.list; \ - [ ${RELEASE_KEY_DOWNLOAD} -eq 1 ] && curl -s -S -o - "${KOPANO_CORE_REPOSITORY_URL}/Release.key" | apt-key add -; \ apt-get update && \ + set -x && \ apt-get install --no-install-recommends -y \ kopano-server-packages \ ${ADDITIONAL_KOPANO_PACKAGES} \ php7.0-cli && \ + set +x && \ rm -rf /var/cache/apt /var/lib/apt/lists && \ cp /usr/share/doc/kopano/example-config/*.cfg /etc/kopano/ && \ cp /usr/share/doc/kopano/example-config/*.cfg.gz /etc/kopano/ && \ @@ -64,7 +77,6 @@ ENV LANG=en_US.UTF-8 ENV SERVICE_TO_START=server COPY kcconf.py defaultconfigs/ start-service.sh /kopano/ -RUN chmod a+x /kopano/start-service.sh ENTRYPOINT ["/usr/bin/dumb-init", "--"] diff --git a/core/download_community.sh b/core/download_community.sh deleted file mode 100755 index 3da5280..0000000 --- a/core/download_community.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# take component as first argument and fallback to core if none given -component=${1:-core} - -function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; } - -# query community server by h5ai API -filename=$(curl -s -S -L -d "action=get&items%5Bhref%5D=%2Fcommunity%2F$component%3A%2F&items%5Bwhat%5D=1" -H \ - "Accept: application/json" https://download.kopano.io/community/ | jq '.items[].href' | \ - grep 'Debian_9.0-all\|Debian_9.0-amd64' | sed 's#"##g' | sed "s#/community/$component:/##") - -if [ -z "${filename// }" ]; then - echo "unknown component" - exit 1 -fi - -filename=$(urldecode "$filename") - -# download & extract packages -curl -s -S -L -o "$filename" https://download.kopano.io/community/"$component":/"${filename}" - -tar xf "$filename" - -# save disk space -rm "$filename" - -# prepare directory to be apt source -apt-ftparchive packages "${filename%.tar.gz}" | gzip -9c > Packages.gz diff --git a/core/start-service.sh b/core/start-service.sh old mode 100644 new mode 100755 diff --git a/download_community.sh b/download_community.sh deleted file mode 100755 index 3da5280..0000000 --- a/download_community.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# take component as first argument and fallback to core if none given -component=${1:-core} - -function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; } - -# query community server by h5ai API -filename=$(curl -s -S -L -d "action=get&items%5Bhref%5D=%2Fcommunity%2F$component%3A%2F&items%5Bwhat%5D=1" -H \ - "Accept: application/json" https://download.kopano.io/community/ | jq '.items[].href' | \ - grep 'Debian_9.0-all\|Debian_9.0-amd64' | sed 's#"##g' | sed "s#/community/$component:/##") - -if [ -z "${filename// }" ]; then - echo "unknown component" - exit 1 -fi - -filename=$(urldecode "$filename") - -# download & extract packages -curl -s -S -L -o "$filename" https://download.kopano.io/community/"$component":/"${filename}" - -tar xf "$filename" - -# save disk space -rm "$filename" - -# prepare directory to be apt source -apt-ftparchive packages "${filename%.tar.gz}" | gzip -9c > Packages.gz diff --git a/webapp/Dockerfile b/webapp/Dockerfile index 1b9a418..b9ca21f 100644 --- a/webapp/Dockerfile +++ b/webapp/Dockerfile @@ -1,12 +1,14 @@ +FROM zokradonh/kopano_common AS common + FROM debian:stretch LABEL maintainer=az@zok.xyz \ version="2.0" -RUN mkdir -p /kopano/repo /kopano/data +RUN mkdir -p /kopano/repo /kopano/data /kopano/helper WORKDIR /kopano/repo -ENV DEBIAN_FRONTEND noninteractive +ARG DEBIAN_FRONTEND=noninteractive # install basics RUN apt-get update && \ @@ -25,40 +27,44 @@ RUN apt-get update && \ && \ rm -rf /var/cache/apt /var/lib/apt/lists -# If you have active Kopano subscription you can change KOPANO_WEBAPP_REPOSITORY_URL to -# https://serial:@download.kopano.io/supported/webapp:/final/Debian_9.0 -# and replace with your serial. You can also use pre-final or any other repository branch. -# docker build --build-arg KOPANO_WEBAPP_REPOSITORY_URL=https://serial:@download.kopano.io/supported/webapp:/final/Debian_9.0 https://github.com/zokradonh/kopano-docker.git#:webapp -# Do the same with KOPANO_CORE_REPOSITORY as webapp needs some packages from core. -# If you want to use community version please use images from hub.docker.com. -# Changing KOPANO_WEBAPP_VERSION does not really change the resulting image. ARG KOPANO_WEBAPP_VERSION=newest -ARG KOPANO_WEBAPP_REPOSITORY_URL="file:/kopano/repo" -ARG KOPANO_CORE_REPOSITORY_URL="file:/kopano/repo" +ARG KOPANO_WEBAPP_REPOSITORY_URL="file:/kopano/repo/webapp" +ARG KOPANO_CORE_REPOSITORY_URL="file:/kopano/repo/core" ARG KOPANO_REPOSITORY_FLAGS="trusted=yes" ARG DOWNLOAD_COMMUNITY_PACKAGES=1 ARG RELEASE_KEY_DOWNLOAD=0 -ARG ADDITIONAL_KOPANO_PACKAGES +ARG ADDITIONAL_KOPANO_PACKAGES="" -# community build -COPY download_community.sh /kopano/download_community.sh +# get common utilities +COPY --from=common /common.sh /kopano/helper/ + +SHELL [ "/bin/bash", "-c"] # install kopano web app and refresh ca-certificates RUN \ - # community download - [ ${DOWNLOAD_COMMUNITY_PACKAGES} -eq 1 ] && \ - /kopano/download_community.sh core && \ - /kopano/download_community.sh webapp && \ - # install - set -x && \ - echo ${KOPANO_CORE_VERSION} > /kopano/buildversion && \ - echo ${KOPANO_WEBAPP_VERSION} >> /kopano/buildversion && \ - echo "deb [${KOPANO_REPOSITORY_FLAGS}] ${KOPANO_CORE_REPOSITORY_URL} ./" > /etc/apt/sources.list.d/kopano-core.list; \ - [ ${DOWNLOAD_COMMUNITY_PACKAGES} -eq 0 ] && echo "deb [${KOPANO_REPOSITORY_FLAGS}] ${KOPANO_WEBAPP_REPOSITORY_URL} ./" > /etc/apt/sources.list.d/kopano-webapp.list; \ - [ ${RELEASE_KEY_DOWNLOAD} -eq 1 ] && curl -s -S -o - "${KOPANO_CORE_REPOSITORY_URL}/Release.key" | apt-key add -; \ - [ ${RELEASE_KEY_DOWNLOAD} -eq 1 ] && curl -s -S -o - "${KOPANO_WEBAPP_REPOSITORY_URL}/Release.key" | apt-key add -; \ + # community download and package as apt source repository + . /kopano/helper/common.sh && \ + if [ ${DOWNLOAD_COMMUNITY_PACKAGES} -eq 1 ]; then \ + dl_and_package_community "core"; \ + dl_and_package_community "webapp"; \ + fi; \ + echo "deb [${KOPANO_REPOSITORY_FLAGS}] ${KOPANO_CORE_REPOSITORY_URL} ./" > /etc/apt/sources.list.d/kopano.list; \ + echo "deb [${KOPANO_REPOSITORY_FLAGS}] ${KOPANO_WEBAPP_REPOSITORY_URL} ./" >> /etc/apt/sources.list.d/kopano.list; \ + # save kopano version if supported kopano + if [ -z /kopano/buildversion ]; then \ + echo "core-${KOPANO_CORE_VERSION}" > /kopano/buildversion; \ + echo "webapp-${KOPANO_WEBAPP_VERSION}" >> /kopano/buildversion; \ + fi; \ + # install apt keys if supported kopano + if [ ${RELEASE_KEY_DOWNLOAD} -eq 1 ]; then \ + curl -s -S -o - "${KOPANO_CORE_REPOSITORY_URL}/Release.key" | apt-key add -; \ + curl -s -S -o - "${KOPANO_WEBAPP_REPOSITORY_URL}/Release.key" | apt-key add -; \ + fi; \ + # prepare z-push installation echo "deb http://repo.z-hub.io/z-push:/final/Debian_9.0/ /" > /etc/apt/sources.list.d/zpush.list && \ curl -s -S -o - "http://repo.z-hub.io/z-push:/final/Debian_9.0/Release.key" | apt-key add - && \ + # install + set -x && \ apt-get update && apt-get install -y --no-install-recommends \ apache2 \ libapache2-mod-php7.0 \ @@ -131,8 +137,6 @@ EXPOSE 80/tcp COPY start.sh /kopano/start.sh -RUN chmod a+x /kopano/start.sh - ENV LANG en_US.UTF-8 ENTRYPOINT ["/usr/bin/dumb-init", "--"] diff --git a/webapp/download_community.sh b/webapp/download_community.sh deleted file mode 100755 index 3da5280..0000000 --- a/webapp/download_community.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# take component as first argument and fallback to core if none given -component=${1:-core} - -function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; } - -# query community server by h5ai API -filename=$(curl -s -S -L -d "action=get&items%5Bhref%5D=%2Fcommunity%2F$component%3A%2F&items%5Bwhat%5D=1" -H \ - "Accept: application/json" https://download.kopano.io/community/ | jq '.items[].href' | \ - grep 'Debian_9.0-all\|Debian_9.0-amd64' | sed 's#"##g' | sed "s#/community/$component:/##") - -if [ -z "${filename// }" ]; then - echo "unknown component" - exit 1 -fi - -filename=$(urldecode "$filename") - -# download & extract packages -curl -s -S -L -o "$filename" https://download.kopano.io/community/"$component":/"${filename}" - -tar xf "$filename" - -# save disk space -rm "$filename" - -# prepare directory to be apt source -apt-ftparchive packages "${filename%.tar.gz}" | gzip -9c > Packages.gz diff --git a/webapp/start.sh b/webapp/start.sh old mode 100644 new mode 100755