xref: /unit/pkg/docker/docker-entrypoint.sh (revision 1511:0b1c33be02c3)
1#!/usr/bin/env bash
2
3set -e
4
5curl_put()
6{
7    RET=`/usr/bin/curl -s -w '%{http_code}' -X PUT --data-binary @$1 --unix-socket /var/run/control.unit.sock http://localhost/$2`
8    RET_BODY=${RET::-3}
9    RET_STATUS=$(echo $RET | /usr/bin/tail -c 4)
10    if [ "$RET_STATUS" -ne "200" ]; then
11        echo "$0: Error: HTTP response status code is '$RET_STATUS'"
12        echo "$RET_BODY"
13        return 1
14    else
15        echo "$0: OK: HTTP response status code is '$RET_STATUS'"
16        echo "$RET_BODY"
17    fi
18    return 0
19}
20
21if [ "$1" = "unitd" -o "$1" = "unitd-debug" ]; then
22    if /usr/bin/find "/var/lib/unit/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
23        echo "$0: /var/lib/unit/ is not empty, skipping initial configuration..."
24    else
25        if /usr/bin/find "/docker-entrypoint.d/" -mindepth 1 -print -quit 2>/dev/null | /bin/grep -q .; then
26            echo "$0: /docker-entrypoint.d/ is not empty, launching Unit daemon to perform initial configuration..."
27            /usr/sbin/unitd --control unix:/var/run/control.unit.sock
28
29            while [ ! -S /var/run/control.unit.sock ]; do echo "$0: Waiting for control socket to be created..."; /bin/sleep 0.1; done
30            # even when the control socket exists, it does not mean unit has finished initialisation
31            # this curl call will get a reply once unit is fully launched
32            /usr/bin/curl -s -X GET --unix-socket /var/run/control.unit.sock http://localhost/
33
34            echo "$0: Looking for certificate bundles in /docker-entrypoint.d/..."
35            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.pem"); do
36                echo "$0: Uploading certificates bundle: $f"
37                curl_put $f "certificates/$(basename $f .pem)"
38            done
39
40            echo "$0: Looking for configuration snippets in /docker-entrypoint.d/..."
41            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.json"); do
42                echo "$0: Applying configuration $f";
43                curl_put $f "config"
44            done
45
46            echo "$0: Looking for shell scripts in /docker-entrypoint.d/..."
47            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -name "*.sh"); do
48                echo "$0: Launching $f";
49                "$f"
50            done
51
52            # warn on filetypes we don't know what to do with
53            for f in $(/usr/bin/find /docker-entrypoint.d/ -type f -not -name "*.sh" -not -name "*.json" -not -name "*.pem"); do
54                echo "$0: Ignoring $f";
55            done
56
57            echo "$0: Stopping Unit daemon after initial configuration..."
58            kill -TERM `/bin/cat /var/run/unit.pid`
59
60            while [ -S /var/run/control.unit.sock ]; do echo "$0: Waiting for control socket to be removed..."; /bin/sleep 0.1; done
61
62            echo
63            echo "$0: Unit initial configuration complete; ready for start up..."
64            echo
65        else
66            echo "$0: /docker-entrypoint.d/ is empty, skipping initial configuration..."
67        fi
68    fi
69fi
70
71exec "$@"
72