netdata/packaging: Fix netdata/netdata docker image failure, when users passing PGID that already exists on the system (#6259)
* netdata/packaging: Adjust group creation, make failure a soft error and allow failing over to alternative group name if given group id exist on the image * netdata/packaging: make exception handling a bit softer, dont run commands when required data are missing and provide enough visibility for the admin * netdata/packaging: Stronger pattern matching. 999 may match to 23999 if left as it was -- wow
Paul Emm. Katsoulakis committed
Jun 11, 2019 at 11:25 UTC
7ee5dc37ae6bca9945db518b2b0cc7d69ce36ae0
1 file changed
+26
-8
packaging/docker/run.sh
+26
-8
@@ -13,19 +13,37 @@ if [ ${RESCRAMBLE+x} ]; then
13
apk upgrade --update-cache --available
14
fi
15
16
+create_group_and_assign_to_user() {
17
+ local local_DOCKER_GROUP="$1"
18
+ local local_DOCKER_GID="$2"
19
+ local local_DOCKER_USR="$3"
20
+
21
+ echo >&2 "Adding group with ID ${local_DOCKER_GID} and name '${local_DOCKER_GROUP}'"
22
+ addgroup -g "${local_DOCKER_GID}" "${local_DOCKER_GROUP}" || echo >&2 "Could not add group ${local_DOCKER_GROUP} with ID ${local_DOCKER_GID}, its already there probably"
23
+
24
+ echo >&2 "Adding user '${local_DOCKER_USR}' to group '${local_DOCKER_GROUP}/${local_DOCKER_GID}'"
25
+ sed -i "s/:${local_DOCKER_GID}:$/:${local_DOCKER_GID}:${local_DOCKER_USR}/g" /etc/group
26
+
27
+ # Make sure we use the right docker group
28
+ GRP_TO_ASSIGN="$(grep ":x:${local_DOCKER_GID}:" /etc/group | cut -d':' -f1)"
29
+ if [ -z "${GRP_TO_ASSIGN}" ]; then
30
+ echo >&2 "Could not find group ID ${local_DOCKER_GID} in /etc/group. Check your logs and report it if this is an unrecovereable error"
31
+ else
32
+ echo >&2 "Group creation and assignment completed, netdata was assigned to group ${GRP_TO_ASSIGN}/${local_DOCKER_GID}"
33
+ echo "${GRP_TO_ASSIGN}"
34
+ fi
35
+}
36
+
37
DOCKER_USR="netdata"
38
DOCKER_SOCKET="/var/run/docker.sock"
39
DOCKER_GROUP="docker"
40
41
if [ -S "${DOCKER_SOCKET}" ] && [ -n "${PGID}" ]; then
21
- echo "Adding group with ID ${PGID} and name '${DOCKER_GROUP}'"
22
- addgroup -g "${PGID}" "${DOCKER_GROUP}"
23
-
24
- echo "Adding user '${DOCKER_USR}' to group '${DOCKER_GROUP}'"
25
- sed -i "s/${DOCKER_GID}:$/${DOCKER_GID}:${DOCKER_USR}/g" /etc/group
26
-
27
- echo "Adjusting ownership of mapped docker socket '${DOCKER_SOCKET}'"
28
- chown "root:${DOCKER_GROUP}" "${DOCKER_SOCKET}"
42
+ GRP=$(create_group_and_assign_to_user "${DOCKER_GROUP}" "${PGID}" "${DOCKER_USR}")
43
+ if [ -n "${GRP}" ]; then
44
+ echo "Adjusting ownership of mapped docker socket '${DOCKER_SOCKET}' to root:${GRP}"
45
+ chown "root:${GRP}" "${DOCKER_SOCKET}"
46
+ fi
47
fi
48
49
exec /usr/sbin/netdata -u "${DOCKER_USR}" -D -s /host -p "${NETDATA_PORT}" "$@"