2020-11-10 15:23:07 +01:00
#!/bin/bash
UNIT_SOCKET = "/opt/unit/unit.sock"
2022-06-23 18:10:43 +02:00
put_config( ) {
RET = $(
curl \
--silent \
--write-out '%{http_code}' \
--request PUT \
--data-binary " @ $1 " \
--unix-socket $UNIT_SOCKET \
http://localhost/$2
)
RET_BODY = ${ RET : :- 3 }
RET_STATUS = $( echo $RET | tail -c 4)
echo $RET
if [ " $RET_STATUS " -ne "200" ] ; then
echo " ⚠️ Error: Failed to load configuration from $1 "
( echo " HTTP response status code is ' $RET_STATUS ' "
echo " $RET_BODY "
) | sed 's/^/ /'
kill " $( cat /opt/unit/unit.pid) "
return 1
fi
return 0
}
2020-11-10 15:23:07 +01:00
load_configuration( ) {
MAX_WAIT = 10
WAIT_COUNT = 0
while [ ! -S $UNIT_SOCKET ] ; do
2021-01-31 16:39:45 +01:00
if [ $WAIT_COUNT -ge $MAX_WAIT ] ; then
2020-11-10 15:23:07 +01:00
echo "⚠️ No control socket found; configuration will not be loaded."
return 1
fi
WAIT_COUNT = $(( WAIT_COUNT + 1 ))
echo " ⏳ Waiting for control socket to be created... ( ${ WAIT_COUNT } / ${ MAX_WAIT } ) "
sleep 1
done
# even when the control socket exists, it does not mean unit has finished initialisation
# this curl call will get a reply once unit is fully launched
curl --silent --output /dev/null --request GET --unix-socket $UNIT_SOCKET http://localhost/
2022-06-23 18:10:43 +02:00
if [ [ -n " $UNIT_CONFIG " ] ] && [ [ -s " $UNIT_CONFIG " ] ] ; then
echo "⚠️ The UNIT_CONFIG environment variable is deprecated. All *.pem and *.json files in /etc/unit will be loaded automatically when UNIT_CONFIG is undefined."
echo " ⚙️ Applying configuration from UNIT_CONFIG environment variable: $UNIT_CONFIG "
put_config $UNIT_CONFIG "config" || return 1
else
echo "🔍 Looking for certificate bundles in /etc/unit/..."
for f in $( find /etc/unit/ -type f -name "*.pem" ) ; do
echo " ⚙️ Uploading certificates bundle: $f "
put_config $f " certificates/ $( basename $f .pem) " || return 1
done
2021-02-08 12:16:04 +01:00
2022-06-23 18:10:43 +02:00
echo "🔍 Looking for configuration snippets in /etc/unit/..."
for f in $( find /etc/unit/ -type f -name "*.json" ) ; do
echo " ⚙️ Applying configuration $f " ;
put_config $f "config" || return 1
done
# warn on filetypes we don't know what to do with
for f in $( find /etc/unit/ -type f -not -name "*.json" -not -name "*.pem" ) ; do
echo " ↩️ Ignoring $f " ;
done
2020-11-10 15:23:07 +01:00
fi
echo "✅ Unit configuration loaded successfully"
}
2022-06-23 18:10:43 +02:00
if [ -n " $( ls -A /opt/unit/state) " ] ; then
echo "💣 Clearing previous unit state from /opt/unit/state"
rm -r /opt/unit/state/*
fi
2020-11-10 15:23:07 +01:00
load_configuration &
exec unitd \
2021-02-08 12:16:04 +01:00
--no-daemon \
--control unix:$UNIT_SOCKET \
--pid /opt/unit/unit.pid \
--log /dev/stdout \
--state /opt/unit/state/ \
2021-07-30 21:42:05 +02:00
--tmp /opt/unit/tmp/ \
2021-09-24 08:16:07 +02:00
--user unit \
--group root