xref: /unit/pkg/docker/docker-entrypoint.sh (revision 2021:7255f494cf48)
11219Sthresh@nginx.com#!/usr/bin/env bash
21219Sthresh@nginx.com
31219Sthresh@nginx.comset -e
41219Sthresh@nginx.com
51219Sthresh@nginx.comcurl_put()
61219Sthresh@nginx.com{
71219Sthresh@nginx.com    RET=`/usr/bin/curl -s -w '%{http_code}' -X PUT --data-binary @$1 --unix-socket /var/run/control.unit.sock http://localhost/$2`
81219Sthresh@nginx.com    RET_BODY=${RET::-3}
91219Sthresh@nginx.com    RET_STATUS=$(echo $RET | /usr/bin/tail -c 4)
101219Sthresh@nginx.com    if [ "$RET_STATUS" -ne "200" ]; then
111219Sthresh@nginx.com        echo "$0: Error: HTTP response status code is '$RET_STATUS'"
121219Sthresh@nginx.com        echo "$RET_BODY"
131219Sthresh@nginx.com        return 1
141219Sthresh@nginx.com    else
151219Sthresh@nginx.com        echo "$0: OK: HTTP response status code is '$RET_STATUS'"
161219Sthresh@nginx.com        echo "$RET_BODY"
171219Sthresh@nginx.com    fi
181219Sthresh@nginx.com    return 0
191219Sthresh@nginx.com}
201219Sthresh@nginx.com
211511Sthresh@nginx.comif [ "$1" = "unitd" -o "$1" = "unitd-debug" ]; then
221219Sthresh@nginx.com    if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
231219Sthresh@nginx.com        echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..."
241219Sthresh@nginx.com    else
251219Sthresh@nginx.com        if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
261219Sthresh@nginx.com            echo "$0: /docker-entrypoint.d/ is not empty, launching Unit daemon to perform initial configuration..."
27*2021Sthresh@nginx.com            /usr/sbin/$1 --control unix:/var/run/control.unit.sock
281219Sthresh@nginx.com
291219Sthresh@nginx.com            while [ ! -S /var/run/control.unit.sock ]; do echo "$0: Waiting for control socket to be created..."; /bin/sleep 0.1; done
301219Sthresh@nginx.com            # even when the control socket exists, it does not mean unit has finished initialisation
311219Sthresh@nginx.com            # this curl call will get a reply once unit is fully launched
321219Sthresh@nginx.com            /usr/bin/curl -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
331219Sthresh@nginx.com
341219Sthresh@nginx.com            echo "$0: Looking for certificate bundles in /docker-entrypoint.d/..."
351219Sthresh@nginx.com            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.pem"); do
361219Sthresh@nginx.com                echo "$0: Uploading certificates bundle: $f"
371219Sthresh@nginx.com                curl_put $f "certificates/$(basename $f .pem)"
381219Sthresh@nginx.com            done
391219Sthresh@nginx.com
401219Sthresh@nginx.com            echo "$0: Looking for configuration snippets in /docker-entrypoint.d/..."
411219Sthresh@nginx.com            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.json"); do
421219Sthresh@nginx.com                echo "$0: Applying configuration $f";
431219Sthresh@nginx.com                curl_put $f "config"
441219Sthresh@nginx.com            done
451219Sthresh@nginx.com
461219Sthresh@nginx.com            echo "$0: Looking for shell scripts in /docker-entrypoint.d/..."
471219Sthresh@nginx.com            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.sh"); do
481219Sthresh@nginx.com                echo "$0: Launching $f";
491219Sthresh@nginx.com                "$f"
501219Sthresh@nginx.com            done
511219Sthresh@nginx.com
521219Sthresh@nginx.com            # warn on filetypes we don't know what to do with
531219Sthresh@nginx.com            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -not -name "*.sh" -not -name "*.json" -not -name "*.pem"); do
541219Sthresh@nginx.com                echo "$0: Ignoring $f";
551219Sthresh@nginx.com            done
561219Sthresh@nginx.com
571219Sthresh@nginx.com            echo "$0: Stopping Unit daemon after initial configuration..."
581219Sthresh@nginx.com            kill -TERM `/bin/cat /var/run/unit.pid`
591219Sthresh@nginx.com
601219Sthresh@nginx.com            while [ -S /var/run/control.unit.sock ]; do echo "$0: Waiting for control socket to be removed..."; /bin/sleep 0.1; done
611219Sthresh@nginx.com
621219Sthresh@nginx.com            echo
631219Sthresh@nginx.com            echo "$0: Unit initial configuration complete; ready for start up..."
641219Sthresh@nginx.com            echo
651219Sthresh@nginx.com        else
661219Sthresh@nginx.com            echo "$0: /docker-entrypoint.d/ is empty, skipping initial configuration..."
671219Sthresh@nginx.com        fi
681219Sthresh@nginx.com    fi
691219Sthresh@nginx.comfi
701219Sthresh@nginx.com
711219Sthresh@nginx.comexec "$@"
72