forked from simonrupf/docker-chronyd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.sh
66 lines (59 loc) · 2.19 KB
/
run.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
#!/bin/bash
# grab global variables
source vars
DOCKER=$(which docker)
# function to check if container is running
function check_container() {
$DOCKER ps --filter "name=${CONTAINER_NAME}" --format "{{.ID}}"
}
# function to start new docker container
function start_container() {
if [ "${ENABLE_PTP:-false}" = true ]; then
echo "PTP requested..."
if [ -e /dev/ptp0 ]; then
echo "PTP device found: /dev/ptp0, passing through..."
DOCKER_OPTS="${DOCKER_OPTS} --device=/dev/ptp0"
else
echo "PTP device not found: /dev/ptp0"
fi
fi
if [ "${ENABLE_SYSCLK:-false}" = true ]; then
echo "SYSCLK requested, adding SYS_TIME capability..."
DOCKER_OPTS="${DOCKER_OPTS} --cap-add=SYS_TIME"
fi
$DOCKER run --name=${CONTAINER_NAME} \
--detach=true \
--restart=always \
--publish=123:123/udp \
--env=NTP_SERVERS=${NTP_SERVERS} \
--env=ENABLE_NTS=${ENABLE_NTS} \
--env=ENABLE_SYSCLK=${ENABLE_SYSCLK} \
--env=NOCLIENTLOG=${NOCLIENTLOG} \
--env=LOG_LEVEL=${LOG_LEVEL} \
--read-only=true \
--tmpfs=/etc/chrony:rw,mode=1750 \
--tmpfs=/run/chrony:rw,mode=1750 \
--tmpfs=/var/lib/chrony:rw,mode=1750 \
${DOCKER_OPTS} \
${IMAGE_NAME}:latest > /dev/null
}
# check if docker container with same name is already running.
if [ "$(check_container)" != "" ]; then
# container found...
# 1) rename existing container
$DOCKER rename ${CONTAINER_NAME} "${CONTAINER_NAME}_orig" > /dev/null 2>&1
# 2) stop exiting container
$DOCKER stop "${CONTAINER_NAME}_orig" > /dev/null 2>&1
# 3) start new container
start_container
# 4) remover existing container
if [ "$(check_container)" != "" ]; then
$DOCKER rm "${CONTAINER_NAME}_orig" > /dev/null 2>&1
fi
# finally, lets clean up old docker images
# shellcheck disable=SC2046
$DOCKER rmi $($DOCKER images -q ${IMAGE_NAME}) > /dev/null 2>&1
# no docker container found. start a new one.
else
start_container
fi