Indicate what root privileges are needed for in kickstart.sh. (#14314)
* Indicate what root privileges are needed for in kickstart.sh. Instead of just blindly prompting the user for root privileges when they are needed in the kickstart script, print a message about _why_ we need root privileges. This improves transparency for users about why they are being prompted. * Fix shellcheck issues. * Fix message formatting. * Fix handling of root privilege messages. We should be printing one per command, not one per word of the command.
Austin S. Hemmelgarn committed
Feb 6, 2023 at 11:14 UTC
a52d9ef14a6e968365cd8bc6851bebb309258099
1 file changed
+44
-38
packaging/installer/kickstart.sh
+44
-38
@@ -406,7 +406,7 @@ success_banner() {
406
cleanup() {
407
if [ -z "${NO_CLEANUP}" ] && [ -n "${tmpdir}" ]; then
408
cd || true
409
- ${ROOTCMD} rm -rf "${tmpdir}"
409
+ run_as_root rm -rf "${tmpdir}"
410
fi
411
}
412
@@ -497,6 +497,16 @@ run() {
497
return ${ret}
498
}
499
500
+run_as_root() {
501
+ confirm_root_support
502
+
503
+ if [ "$(id -u)" -ne "0" ]; then
504
+ printf >&2 "Root privileges required to run %s\n" "${*}"
505
+ fi
506
+
507
+ run ${ROOTCMD} "${@}"
508
+}
509
+
510
run_script() {
511
set_tmpdir
512
@@ -800,6 +810,7 @@ uninstall() {
810
return 0
811
else
812
progress "Found existing netdata-uninstaller. Running it.."
813
+ # shellcheck disable=SC2086
814
if ! run_script "${uninstaller}" ${FLAGS}; then
815
warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
816
fi
@@ -813,6 +824,7 @@ uninstall() {
824
progress "Downloading netdata-uninstaller ..."
825
download "${uninstaller_url}" "${tmpdir}/netdata-uninstaller.sh"
826
chmod +x "${tmpdir}/netdata-uninstaller.sh"
827
+ # shellcheck disable=SC2086
828
if ! run_script "${tmpdir}/netdata-uninstaller.sh" ${FLAGS}; then
829
warning "Uninstaller failed. Some parts of Netdata may still be present on the system."
830
fi
@@ -858,7 +870,7 @@ detect_existing_install() {
870
if [ -n "${ndprefix}" ]; then
871
typefile="${ndprefix}/etc/netdata/.install-type"
872
if [ -r "${typefile}" ]; then
861
- ${ROOTCMD} sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
873
+ run_as_root sh -c "cat \"${typefile}\" > \"${tmpdir}/install-type\""
874
# shellcheck disable=SC1090,SC1091
875
. "${tmpdir}/install-type"
876
else
@@ -868,7 +880,7 @@ detect_existing_install() {
880
envfile="${ndprefix}/etc/netdata/.environment"
881
if [ "${INSTALL_TYPE}" = "unknown" ] || [ "${INSTALL_TYPE}" = "custom" ]; then
882
if [ -r "${envfile}" ]; then
871
- ${ROOTCMD} sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
883
+ run_as_root sh -c "cat \"${envfile}\" > \"${tmpdir}/environment\""
884
# shellcheck disable=SC1091
885
. "${tmpdir}/environment"
886
if [ -n "${NETDATA_IS_STATIC_INSTALL}" ]; then
@@ -1022,30 +1034,30 @@ soft_disable_cloud() {
1034
1035
cloud_prefix="${INSTALL_PREFIX}/var/lib/netdata/cloud.d"
1036
1025
- run ${ROOTCMD} mkdir -p "${cloud_prefix}"
1037
+ run_as_root mkdir -p "${cloud_prefix}"
1038
1039
cat > "${tmpdir}/cloud.conf" << EOF
1040
[global]
1041
enabled = no
1042
EOF
1043
1032
- run ${ROOTCMD} cp "${tmpdir}/cloud.conf" "${cloud_prefix}/cloud.conf"
1044
+ run_as_root cp "${tmpdir}/cloud.conf" "${cloud_prefix}/cloud.conf"
1045
1046
if [ -z "${NETDATA_NO_START}" ]; then
1047
case "${SYSTYPE}" in
1036
- Darwin) run ${ROOTCMD} launchctl kickstart -k com.github.netdata ;;
1037
- FreeBSD) run ${ROOTCMD} service netdata restart ;;
1048
+ Darwin) run_as_root launchctl kickstart -k com.github.netdata ;;
1049
+ FreeBSD) run_as_root service netdata restart ;;
1050
Linux)
1039
- initpath="$(${ROOTCMD} readlink /proc/1/exe)"
1051
+ initpath="$(run_as_root readlink /proc/1/exe)"
1052
1053
if command -v service > /dev/null 2>&1; then
1042
- run ${ROOTCMD} service netdata restart
1054
+ run_as_root service netdata restart
1055
elif command -v rc-service > /dev/null 2>&1; then
1044
- run ${ROOTCMD} rc-service netdata restart
1056
+ run_as_root rc-service netdata restart
1057
elif [ "$(basename "${initpath}" 2> /dev/null)" = "systemd" ]; then
1046
- run ${ROOTCMD} systemctl restart netdata
1058
+ run_as_root systemctl restart netdata
1059
elif [ -f /etc/init.d/netdata ]; then
1048
- run ${ROOTCMD} /etc/init.d/netdata restart
1060
+ run_as_root /etc/init.d/netdata restart
1061
fi
1062
;;
1063
esac
@@ -1144,7 +1156,7 @@ claim() {
1156
fi
1157
1158
# shellcheck disable=SC2086
1147
- run ${ROOTCMD} "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
1159
+ run_as_root "${NETDATA_CLAIM_PATH}" -token="${NETDATA_CLAIM_TOKEN}" -rooms="${NETDATA_CLAIM_ROOMS}" -url="${NETDATA_CLAIM_URL}" ${NETDATA_CLAIM_EXTRA}
1160
case $? in
1161
0)
1162
progress "Successfully claimed node"
@@ -1224,16 +1236,16 @@ set_auto_updates() {
1236
if [ "${DRY_RUN}" -eq 1 ]; then
1237
progress "Would have attempted to enable automatic updates."
1238
# This first case is for catching using a new kickstart script with an old build. It can be safely removed after v1.34.0 is released.
1227
- elif ! grep -q '\-\-enable-auto-updates' ${updater}; then
1239
+ elif ! grep -q '\-\-enable-auto-updates' "${updater}"; then
1240
echo
1229
- elif ! ${ROOTCMD} ${updater} --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
1241
+ elif ! run_as_root "${updater}" --enable-auto-updates "${NETDATA_AUTO_UPDATE_TYPE}"; then
1242
warning "Failed to enable auto updates. Netdata will still work, but you will need to update manually."
1243
fi
1244
else
1245
if [ "${DRY_RUN}" -eq 1 ]; then
1246
progress "Would have attempted to disable automatic updates."
1247
else
1236
- ${ROOTCMD} ${updater} --disable-auto-updates
1248
+ run_as_root "${updater}" --disable-auto-updates
1249
fi
1250
fi
1251
}
@@ -1323,7 +1335,7 @@ check_special_native_deps() {
1335
progress "EPEL is available, attempting to install so that required dependencies are available."
1336
1337
# shellcheck disable=SC2086
1326
- if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} epel-release; then
1338
+ if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} epel-release; then
1339
warning "Failed to install EPEL, even though it is required to install native packages on this system."
1340
return 1
1341
fi
@@ -1503,21 +1515,21 @@ try_package_install() {
1515
1516
if [ -n "${needs_early_refresh}" ]; then
1517
# shellcheck disable=SC2086
1506
- if ! run ${ROOTCMD} env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
1518
+ if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
1519
warning "${failed_refresh_msg}"
1520
return 2
1521
fi
1522
fi
1523
1524
# shellcheck disable=SC2086
1513
- if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
1525
+ if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} "${tmpdir}/${repoconfig_file}"; then
1526
warning "Failed to install repository configuration package."
1527
return 2
1528
fi
1529
1530
if [ -n "${repo_subcmd}" ]; then
1531
# shellcheck disable=SC2086
1520
- if ! run ${ROOTCMD} env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
1532
+ if ! run_as_root env ${env} ${pm_cmd} ${repo_subcmd} ${repo_update_opts}; then
1533
fatal "${failed_refresh_msg}" F0205
1534
fi
1535
fi
@@ -1538,7 +1550,7 @@ try_package_install() {
1550
if [ -z "${NO_CLEANUP}" ]; then
1551
progress "Attempting to uninstall repository configuration package."
1552
# shellcheck disable=SC2086
1541
- run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
1553
+ run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
1554
fi
1555
return 2
1556
fi
@@ -1548,23 +1560,23 @@ try_package_install() {
1560
if [ -z "${NO_CLEANUP}" ]; then
1561
progress "Attempting to uninstall repository configuration package."
1562
# shellcheck disable=SC2086
1551
- run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
1563
+ run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
1564
fi
1565
return 2
1566
fi
1567
1568
if [ "${NETDATA_DISABLE_TELEMETRY}" -eq 1 ]; then
1557
- run ${ROOTCMD} mkdir -p "/etc/netdata"
1558
- run ${ROOTCMD} touch "/etc/netdata/.opt-out-from-anonymous-statistics"
1569
+ run_as_root mkdir -p "/etc/netdata"
1570
+ run_as_root touch "/etc/netdata/.opt-out-from-anonymous-statistics"
1571
fi
1572
1573
# shellcheck disable=SC2086
1562
- if ! run ${ROOTCMD} env ${env} ${pm_cmd} install ${pkg_install_opts} "netdata${NATIVE_VERSION}"; then
1574
+ if ! run_as_root env ${env} ${pm_cmd} install ${pkg_install_opts} "netdata${NATIVE_VERSION}"; then
1575
warning "Failed to install Netdata package."
1576
if [ -z "${NO_CLEANUP}" ]; then
1577
progress "Attempting to uninstall repository configuration package."
1578
# shellcheck disable=SC2086
1567
- run ${ROOTCMD} env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
1579
+ run_as_root env ${env} ${pm_cmd} ${uninstall_subcmd} ${pkg_install_opts} "${repoconfig_name}"
1580
fi
1581
return 2
1582
fi
@@ -1657,7 +1669,7 @@ try_static_install() {
1669
1670
progress "Installing netdata"
1671
# shellcheck disable=SC2086
1660
- if ! run ${ROOTCMD} sh "${tmpdir}/${netdata_agent}" ${opts} -- ${NETDATA_INSTALLER_OPTIONS}; then
1672
+ if ! run_as_root sh "${tmpdir}/${netdata_agent}" ${opts} -- ${NETDATA_INSTALLER_OPTIONS}; then
1673
warning "Failed to install static build of Netdata on ${SYSARCH}."
1674
run rm -rf /opt/netdata
1675
return 2
@@ -1666,16 +1678,16 @@ try_static_install() {
1678
if [ "${DRY_RUN}" -ne 1 ]; then
1679
install_type_file="/opt/netdata/etc/netdata/.install-type"
1680
if [ -f "${install_type_file}" ]; then
1669
- ${ROOTCMD} sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
1670
- ${ROOTCMD} chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
1681
+ run_as_root sh -c "cat \"${install_type_file}\" > \"${tmpdir}/install-type\""
1682
+ run_as_root chown "$(id -u)":"$(id -g)" "${tmpdir}/install-type"
1683
# shellcheck disable=SC1090,SC1091
1684
. "${tmpdir}/install-type"
1685
cat > "${tmpdir}/install-type" <<- EOF
1686
INSTALL_TYPE='kickstart-static'
1687
PREBUILT_ARCH='${PREBUILT_ARCH}'
1688
EOF
1677
- ${ROOTCMD} chown netdata:netdata "${tmpdir}/install-type"
1678
- ${ROOTCMD} cp "${tmpdir}/install-type" "${install_type_file}"
1689
+ run_as_root chown netdata:netdata "${tmpdir}/install-type"
1690
+ run_as_root cp "${tmpdir}/install-type" "${install_type_file}"
1691
fi
1692
fi
1693
}
@@ -1728,14 +1740,8 @@ install_local_build_dependencies() {
1740
opts="--dont-wait --non-interactive"
1741
fi
1742
1731
- if [ "${SYSTYPE}" = "Darwin" ]; then
1732
- sudo=""
1733
- else
1734
- sudo="${ROOTCMD}"
1735
- fi
1736
-
1743
# shellcheck disable=SC2086
1738
- if ! run ${sudo} "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
1744
+ if ! run_as_root "${bash}" "${tmpdir}/install-required-packages.sh" ${opts} netdata; then
1745
warning "Failed to install all required packages, but installation might still be possible."
1746
fi
1747
}