portability fixes for adding user/group netdata to the system
Costa Tsaousis committed
Dec 20, 2016 at 21:48 UTC
13ba361b51ccfd89587369181497776ea874a85f
1 file changed
+108
-27
netdata-installer.sh
+108
-27
@@ -548,42 +548,123 @@ do
548
done
549
550
echo >&2 "Fixing permissions ..."
551
-run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -exec chmod 755 {} \;
551
553
-NETDATA_ADDED_TO_DOCKER=0
554
-if [ ${UID} -eq 0 ]
552
+check_cmd() {
553
+ which "${1}" >/dev/null 2>&1 && return 0
554
+ command -v "${1}" >/dev/null 2>&1 && return 0
555
+ return 1
556
+}
557
+
558
+portable_add_user() {
559
+ local username="${1}"
560
+
561
+ getent passwd "${username}" > /dev/null 2>&1
562
+ [ $? -eq 0 ] && return 0
563
+
564
+ echo >&2 "Adding ${username} user account ..."
565
+
566
+ local nologin="$(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false')"
567
+
568
+ # Linux
569
+ if check_cmd useradd
570
then
556
- getent group netdata > /dev/null
557
- if [ $? -ne 0 ]
558
- then
559
- echo >&2 "Adding netdata user group ..."
560
- run groupadd -r netdata
571
+ run useradd -r -g "${username}" -c "${username}" -s "${nologin}" -d / "${username}" && return 0
572
fi
573
563
- getent passwd netdata > /dev/null
564
- if [ $? -ne 0 ]
565
- then
566
- echo >&2 "Adding netdata user account ..."
567
- run useradd -r -g netdata -c netdata -s $(which nologin 2>/dev/null || command -v nologin 2>/dev/null || echo '/bin/false') -d / netdata
574
+ # FreeBSD
575
+ if check_cmd pw
576
+ then
577
+ run pw useradd "${username}" -d / -g "${username}" -s "${nologin}" && return 0
578
fi
579
570
- getent group docker > /dev/null
571
- if [ $? -eq 0 ]
580
+ # BusyBox
581
+ if check_cmd adduser
582
+ then
583
+ run adduser -D -G "${username}" "${username}" && return 0
584
+ fi
585
+
586
+ echo >&2 "Failed to add ${username} user account !"
587
+
588
+ return 1
589
+}
590
+
591
+portable_add_group() {
592
+ local groupname="${1}"
593
+
594
+ getent group "${groupname}" > /dev/null 2>&1
595
+ [ $? -eq 0 ] && return 0
596
+
597
+ echo >&2 "Adding ${groupname} user group ..."
598
+
599
+ # Linux
600
+ if check_cmd groupadd
601
+ then
602
+ run groupadd -r "${groupname}" && return 0
603
+ fi
604
+
605
+ # FreeBSD
606
+ if check_cmd pw
607
+ then
608
+ run pw groupadd "${groupname}" && return 0
609
+ fi
610
+
611
+ # BusyBox
612
+ if check_cmd addgroup
613
+ then
614
+ run addgroup "${groupname}" && return 0
615
+ fi
616
+
617
+ echo >&2 "Failed to add ${groupname} user group !"
618
+ return 1
619
+}
620
+
621
+portable_add_user_to_group() {
622
+ local groupname="${1}" username="${2}"
623
+
624
+ getent group "${groupname}" > /dev/null 2>&1
625
+ [ $? -ne 0 ] && return 1
626
+
627
+ # find the users in the docker group
628
+ local users=$(getent group "${groupname}" | cut -d ':' -f 4)
629
+ if [[ ",${users}," =~ ,${username}, ]]
630
then
573
- # find the users in the docker group
574
- docker=$(getent group docker | cut -d ':' -f 4)
575
- if [[ ",${docker}," =~ ,netdata, ]]
576
- then
577
- # netdata is already there
578
- :
579
- else
580
- # netdata is not in docker group
581
- echo >&2 "Adding netdata user to the docker group (needed to get container names) ..."
582
- run usermod -a -G docker netdata
631
+ # username is already there
632
+ :
633
+ else
634
+ # username is not in group
635
+ echo >&2 "Adding ${username} user to the ${groupname} group ..."
636
+
637
+ # Linux
638
+ if check_cmd usermod
639
+ then
640
+ run usermod -a -G "${groupname}" "${username}" && return 0
641
fi
584
- # let the uninstall script know
585
- NETDATA_ADDED_TO_DOCKER=1
642
+
643
+ # FreeBSD
644
+ if check_cmd pw
645
+ then
646
+ run pw groupmod "${groupname}" -m "${username}" && return 0
647
+ fi
648
+
649
+ # BusyBox
650
+ if check_cmd addgroup
651
+ then
652
+ run addgroup "${username}" "${groupname}" && return 0
653
+ fi
654
+
655
+ echo >&2 "Failed to add user ${username} to group ${groupname} !"
656
+ return 1
657
fi
658
+}
659
+
660
+run find ./system/ -type f -a \! -name \*.in -a \! -name Makefile\* -a \! -name \*.conf -a \! -name \*.service -exec chmod 755 {} \;
661
+
662
+NETDATA_ADDED_TO_DOCKER=0
663
+if [ ${UID} -eq 0 ]
664
+ then
665
+ portable_add_group netdata
666
+ portable_add_user netdata
667
+ portable_add_user_to_group docker netdata && NETDATA_ADDED_TO_DOCKER=1
668
669
if [ -d /etc/logrotate.d -a ! -f /etc/logrotate.d/netdata ]
670
then