Improve script logging

Signed-off-by: Martin Heide <martin.heide@faro.com>
This commit is contained in:
Martin Heide 2020-07-13 15:40:56 +00:00
parent ce337661b9
commit b4d22bf1b2

View file

@ -10,41 +10,47 @@
set -eu
cd -- "$(dirname "$0")/.."
run_cmd() {
echo ">" "$@" >&2
"$@"
}
keep_running=
if [ $# -gt 0 ] && [ "$1" = "--keep" ]; then
keep_running=1
keep_running=1
fi
if [ -z "$keep_running" ] || [ "$(docker inspect --format="{{.State.Running}}" ldap 2> /dev/null)" != "true" ]; then
echo "LDAP container not running, or running and --keep not specified."
echo "Removing old LDAP container (if any)..."
docker rm --force ldap || true
echo "Starting LDAP container..."
# Currently the most popular OpenLDAP image on Docker Hub. Comes with the latest version OpenLDAP 2.4.50.
docker run -p 389:389 -p 636:636 -v $PWD:$PWD --name ldap --detach osixia/openldap:1.4.0
echo "LDAP container not running, or running and --keep not specified."
echo "Removing old LDAP container (if any)..."
run_cmd docker rm --force ldap || true
echo "Starting LDAP container..."
# Currently the most popular OpenLDAP image on Docker Hub. Comes with the latest version OpenLDAP 2.4.50.
run_cmd docker run -p 389:389 -p 636:636 -v $PWD:$PWD --name ldap --detach osixia/openldap:1.4.0
tries=1
max_tries=10
echo "Waiting for LDAP container ($tries/$max_tries)..."
# Wait until expected line "structuralObjectClass: organization" shows up.
# Seems to work more reliably than waiting for exit code 0. That would be:
# while ! docker exec ldap slapcat -b "dc=example,dc=org" > /dev/null 2>&1; do
while [[ ! "$(docker exec ldap slapcat -b "dc=example,dc=org" 2>/dev/null)" =~ organization ]]; do
((++tries))
if [ "$tries" -gt "$max_tries" ]; then
echo "ERROR: Timeout waiting for LDAP container."
exit 1
fi
sleep 1
echo "Waiting for LDAP container ($tries/$max_tries)..."
done
tries=1
max_tries=10
echo "Waiting for LDAP container ($tries/$max_tries)..."
# Wait until expected line "structuralObjectClass: organization" shows up.
# Seems to work more reliably than waiting for exit code 0. That would be:
# while ! docker exec ldap slapcat -b "dc=example,dc=org" > /dev/null 2>&1; do
while [[ ! "$(docker exec ldap slapcat -b "dc=example,dc=org" 2>/dev/null)" =~ organization ]]; do
((++tries))
if [ "$tries" -gt "$max_tries" ]; then
echo "ERROR: Timeout waiting for LDAP container."
exit 1
fi
sleep 1
echo "Waiting for LDAP container ($tries/$max_tries)..."
done
fi
echo "Adding example entries to directory..."
set -x
docker exec ldap ldapadd \
-x \
-D "cn=admin,dc=example,dc=org" \
-w admin \
-H ldap://localhost:389/ \
-f $PWD/examples/config-ldap.ldif
run_cmd docker exec ldap ldapadd \
-x \
-D "cn=admin,dc=example,dc=org" \
-w admin \
-H ldap://localhost:389/ \
-f $PWD/examples/config-ldap.ldif
echo "OK."