Skip to content

Commit

Permalink
smaller improvements to setup.sh (#153)
Browse files Browse the repository at this point in the history
* replace bash function to list docker tags with https://github.com/genuinetools/reg/
  * this depends on new release in genuinetools/reg#186 to fully function
* update docker version
* fix version.sh to also show versions from repos defined in .env
* add script to list available tags and update values in .env
* define default values for all version vars
* add previously missing containers
* use command instead of hash in update env script
* use a custom select function instead shell builtin (since it does not handle default values)
* add setup-update-tag in test.exp
  • Loading branch information
fbartels authored May 31, 2019
1 parent f19cf27 commit 3a0ad6d
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 38 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
SHELL := /bin/bash # Use bash syntax

# if not run in travis, get docker_login and _pwd from file
ifndef TRAVIS
docker_repo := zokradonh
Expand Down Expand Up @@ -174,6 +176,7 @@ tag-all: build-all ## Helper target to create tags for all images.

tag-container: component ?= base
tag-container: ## Helper target to tag a given image. Defaults to the base image.
# TODO how to tag additional releases. e.g. also tag 8.7.80.1035 as 8.7.80?
@echo 'create tag $($(component)_version)'
docker tag $(docker_repo)/kopano_$(component) $(docker_repo)/kopano_$(component):${$(component)_version}
@echo $(docker_repo)/kopano_$(component):${$(component)_version} >> $(TAG_FILE)
Expand Down
8 changes: 7 additions & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
FROM docker:18.09.1
FROM docker:18.09.6
ENV COMPOSE_VERSION "1.23.2"
ENV REG_VERSION "0.16.0"
RUN apk add --no-cache bash curl expect make nano jq py-pip
RUN pip install --no-cache-dir docker-compose==${COMPOSE_VERSION}
# the 0.16.0 release of reg has a bug that breaks loading tags from the docker hub.
# issue is fixed in master, but not in a release.
# rel https://github.com/genuinetools/reg/issues/186
RUN curl -fSL "https://github.com/genuinetools/reg/releases/download/v$REG_VERSION/reg-linux-amd64" -o "/usr/local/bin/reg" \
&& chmod a+x "/usr/local/bin/reg"
WORKDIR /kopano-docker
CMD ["bash"]
118 changes: 118 additions & 0 deletions setup-update-tag.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

set -euo pipefail
IFS=$'\n\t'

if ! command -v reg > /dev/null; then
echo "Please install reg in order to run this script."
exit 1
fi

if [ ! -e ./.env ]; then
echo "please run setup.sh first"
exit 1
fi

# this is a kind of ugly hack to be able to source the env file
# this is sadly needed since postfix in https://github.com/tomav/docker-mailserver/ cannot deal with quoted values
tmpfile=$(mktemp /tmp/kopano-docker-env.XXXXXX)
cp ./.env "$tmpfile"
sed -i '/LDAP_QUERY_FILTER/s/^/#/g' "$tmpfile"
sed -i '/SASLAUTHD_LDAP_FILTER/s/^/#/g' "$tmpfile"
# shellcheck disable=SC1090
source "$tmpfile"

# define a default docker_repo in case its not in .env
docker_repo=${docker_repo:-zokradonh}

docker_tag_search () {
image="$1"
results=$(reg tags "$image" 2> /dev/null)
echo "$results" | xargs -n1 | sort -ru
}

# function from https://stackoverflow.com/a/42790579/4754613
selectWithDefault() {

local item i=0 numItems=$#

# Print numbered menu items, based on the arguments passed.
for item; do # Short for: for item in "$@"; do
printf '%s\n' "$((++i))) $item"
done >&2 # Print to stderr, as `select` does.

# Prompt the user for the index of the desired item.
while :; do
printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
read -r index
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $index ]] && break # empty input
(( index >= 1 && index <= numItems )) 2>/dev/null || { echo "Invalid selection. Please try again." >&2; continue; }
break
done

# Output the selected item, if any.
[[ -n $index ]] && printf %s "${@: index:1}"
}

update_env_file () {
varname="$1"
varvalue="$2"
if ! grep -q "$varname" ./.env; then
echo "$varname=$varvalue" >> ./.env
else
sed -i "/$varname/c $varname=$varvalue" ./.env
fi
}

tag_question () {
containername="$1"
value_default="$2"
description="$3"
echo "Which tag do you want to use for $description? [$value_default]"
echo "Available tags in $docker_repo/$containername/: "
set +e # do not exit when new_value is empty
# shellcheck disable=SC2046
new_value=$(selectWithDefault $(docker_tag_search "$docker_repo/$containername"))
set -e
return_value=${new_value:-$value_default}
}

echo "Please be aware that downgrading to an older version could result in failure to start!"

tag_question kopano_core "${CORE_VERSION:-latest}" "Kopano Core components"
update_env_file CORE_VERSION "$return_value"

tag_question kopano_webapp "${WEBAPP_VERSION:-latest}" "Kopano WebApp"
update_env_file WEBAPP_VERSION "$return_value"

tag_question kopano_web "${KWEB_VERSION:-latest}" "reverse proxy"
update_env_file KWEB_VERSION "$return_value"

tag_question kopano_zpush "${ZPUSH_VERSION:-latest}" "Z-Push"
update_env_file ZPUSH_VERSION "$return_value"

tag_question kopano_kdav "${KDAV_VERSION:-latest}" "kDAV"
update_env_file KDAV_VERSION "$return_value"

tag_question kopano_konnect "${KONNECT_VERSION:-latest}" "Kopano Konnect"
update_env_file KONNECT_VERSION "$return_value"

tag_question kopano_kwmserver "${KWM_VERSION:-latest}" "Kopano Kwmserver"
update_env_file KWM_VERSION "$return_value"

tag_question kopano_meet "${MEET_VERSION:-latest}" "Kopano Meet"
update_env_file MEET_VERSION "$return_value"

tag_question kopano_scheduler "${SCHEDULER_VERSION:-latest}" "Scheduler"
update_env_file SCHEDULER_VERSION "$return_value"

tag_question kopano_ssl "${SSL_VERSION:-latest}" "SSL helper container"
update_env_file SSL_VERSION "$return_value"

tag_question kopano_ldap "${LDAP_VERSION:-latest}" "LDAP container"
update_env_file LDAP_VERSION "$return_value"

if [ -e "$tmpfile" ]; then
rm "$tmpfile"
fi
45 changes: 9 additions & 36 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,8 @@ plugin_menu() {
}

docker_tag_search () {
set +e
# Display help
if [[ "${1}" == "" ]]; then
echo "Usage: docker tags repo/image"
echo " docker tags image"
return
fi

# Full repo/image was supplied
if [[ $1 == *"/"* ]]; then
name=$1

# Only image was supplied, default to library/image
else
name=library/${1}
fi
#printf "Searching tags for ${name}"

# Fetch all pages, because the only endpoint supporting pagination params
# appears to be tags/lists, but that needs authorization
results=""
i=0
has_more=0
while [ $has_more -eq 0 ]; do
i=$((i+1))
result=$(curl "https://registry.hub.docker.com/v2/repositories/${name}/tags/?page=${i}" 2>/dev/null | jq -r '."results"[]["name"]' 2>/dev/null)
has_more=$?
if [[ -n "${result// }" ]]; then results="${results} ${result}"; fi
#printf "."
done

image="$1"
results=$(reg tags "$image" 2> /dev/null)
echo "$results" | xargs -n1 | sort -ru | xargs
}

Expand All @@ -73,28 +44,30 @@ if [ ! -e ./.env ]; then
PRINT_SETUP_SUCCESS=""

echo "Creating an .env file for you"
if command -v jq > /dev/null; then
echo "Available tags in https://hub.docker.com/r/zokradonh/kopano_core/: $(docker_tag_search zokradonh/kopano_core)"

# if the optional https://github.com/genuinetools/reg is installed this will list available tags
if command -v reg > /dev/null; then
echo "Available tags in zokradonh/kopano_core/: $(docker_tag_search zokradonh/kopano_core)"
fi
value_default=latest
read -r -p "Which tag do you want to use for Kopano Core components? [$value_default]: " new_value
CORE_VERSION=${new_value:-$value_default}

if command -v jq > /dev/null; then
if command -v reg > /dev/null; then
echo "Available tags in https://hub.docker.com/r/zokradonh/kopano_webapp/: $(docker_tag_search zokradonh/kopano_webapp)"
fi
value_default=latest
read -r -p "Which tag do you want to use for Kopano WebApp? [$value_default]: " new_value
WEBAPP_VERSION=${new_value:-$value_default}

if command -v jq > /dev/null; then
if command -v reg > /dev/null; then
echo "Available tags in https://hub.docker.com/r/zokradonh/kopano_zpush/: $(docker_tag_search zokradonh/kopano_zpush)"
fi
value_default=latest
read -r -p "Which tag do you want to use for Z-Push? [$value_default]: " new_value
ZPUSH_VERSION=${new_value:-$value_default}

if command -v jq > /dev/null; then
if command -v reg > /dev/null; then
echo "Available tags in https://hub.docker.com/r/zokradonh/kopano_konnect/: $(docker_tag_search zokradonh/kopano_konnect)"
fi
value_default=latest
Expand Down
10 changes: 10 additions & 0 deletions test.exp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ expect {
}
eof
}

spawn "./setup-update-tag.sh"

expect {
"#?" {
send "\r"
exp_continue
}
eof
}
3 changes: 2 additions & 1 deletion version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ distribution=${2:-Debian_9.0}

if [ -e ./.env ]; then
# this is a kind of ugly hack to be able to source the env file
# this is sadly needed since postfix in https://github.com/tomav/docker-mailserver/ cannot deal with quotes values
# this is sadly needed since postfix in https://github.com/tomav/docker-mailserver/ cannot deal with quoted values
tmpfile=$(mktemp /tmp/kopano-docker-env.XXXXXX)
cp ./.env "$tmpfile"
sed -i '/LDAP_QUERY_FILTER/s/^/#/g' "$tmpfile"
sed -i '/SASLAUTHD_LDAP_FILTER/s/^/#/g' "$tmpfile"
# shellcheck disable=SC1090
Expand Down

0 comments on commit 3a0ad6d

Please sign in to comment.