-
Notifications
You must be signed in to change notification settings - Fork 19
/
entrypoint.sh
executable file
·76 lines (61 loc) · 2.02 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
function generate_password() {
# We disable exit on error because we close the pipe
# when we have enough characters, which results in a
# non-zero exit status
set +e
tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1 | tr -cd '[:alnum:]'
set -e
}
function kill_devpi() {
_PID=$(pgrep devpi-server)
echo "ENTRYPOINT: Sending SIGTERM to PID $_PID"
kill -SIGTERM "$_PID"
}
if [ "${1:-}" == "bash" ]; then
exec "$@"
fi
DEVPI_ROOT_PASSWORD="${DEVPI_ROOT_PASSWORD:-}"
if [ -f "$DEVPI_SERVER_ROOT/.root_password" ]; then
DEVPI_ROOT_PASSWORD=$(cat "$DEVPI_SERVER_ROOT/.root_password")
elif [ -z "$DEVPI_ROOT_PASSWORD" ]; then
DEVPI_ROOT_PASSWORD=$(generate_password)
fi
if [ ! -d "$DEVPI_SERVER_ROOT" ]; then
echo "ENTRYPOINT: Creating devpi-server root"
mkdir -p "$DEVPI_SERVER_ROOT"
fi
initialize=no
if [ ! -f "$DEVPI_SERVER_ROOT/.serverversion" ]; then
initialize=yes
echo "ENTRYPOINT: Initializing server root $DEVPI_SERVER_ROOT"
devpi-init --serverdir "$DEVPI_SERVER_ROOT"
fi
echo "ENTRYPOINT: Starting devpi-server"
devpi-server --host 0.0.0.0 --port 3141 --serverdir "$DEVPI_SERVER_ROOT" "$@" &
timeout 10 bash -c 'until printf "" 2>>/dev/null >>/dev/tcp/$0/$1; do sleep 1; done' localhost 3141
echo "ENTRYPOINT: Installing signal traps"
trap kill_devpi SIGINT SIGTERM
if [ "$initialize" == "yes" ]; then
echo "ENTRYPOINT: Initializing devpi-server"
devpi use http://localhost:3141
devpi login root --password=''
echo "ENTRYPOINT: Setting root password to $DEVPI_ROOT_PASSWORD"
devpi user -m root "password=$DEVPI_ROOT_PASSWORD"
echo -n "$DEVPI_ROOT_PASSWORD" > "$DEVPI_SERVER_ROOT/.root_password"
devpi logoff
fi
echo "ENTRYPOINT: Watching devpi-server"
PID=$(pgrep devpi-server)
if [ -z "$PID" ]; then
echo "ENTRYPOINT: Could not determine PID of devpi-server!"
exit 1
fi
set +e
while : ; do
kill -0 "$PID" > /dev/null 2>&1 || break
sleep 2s
done
echo "ENTRYPOINT: devpi-server died, exiting..."