Move kickstart argument parsing code to a function. (#12805)
Move kickstart argument arsing code to a function. This will make it slightly more maintainable in the long term, and slightly easier for newcomers to understand.
Austin S. Hemmelgarn committed
May 17, 2022 at 12:46 UTC
ae87d84b016b242a392695d5c9043aa35ff3063b
1 file changed
+213
-209
packaging/installer/kickstart.sh
+213
-209
@@ -67,6 +67,9 @@ else
67
INTERACTIVE=1
68
fi
69
70
+# ======================================================================
71
+# Core program logic
72
+
73
main() {
74
case "${ACTION}" in
75
uninstall)
@@ -1880,234 +1883,235 @@ install_on_freebsd() {
1883
}
1884
1885
# ======================================================================
1883
-# Main program
1886
+# Argument parsing code
1887
1885
-setup_terminal || echo > /dev/null
1888
+validate_args() {
1889
+ check_claim_opts
1890
1887
-if [ -n "${NETDATA_INSTALLER_OPTIONS}" ]; then
1888
- warning "Explicitly specifying additional installer options with NETDATA_INSTALLER_OPTIONS is deprecated. Please instead pass the options to the script using either --local-build-options or --static-install-options as appropriate."
1889
-fi
1890
-
1891
-while [ -n "${1}" ]; do
1892
- case "${1}" in
1893
- "--help")
1894
- usage
1895
- cleanup
1896
- trap - EXIT
1897
- exit 0
1898
- ;;
1899
- "--no-cleanup") NO_CLEANUP=1 ;;
1900
- "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
1901
- "--interactive") INTERACTIVE=1 ;;
1902
- "--dry-run") DRY_RUN=1 ;;
1903
- "--release-channel")
1904
- RELEASE_CHANNEL="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
1905
- case "${RELEASE_CHANNEL}" in
1906
- nightly|stable|default)
1907
- shift 1
1908
- ;;
1909
- *)
1910
- echo "Unrecognized value for --release-channel. Valid release channels are: stable, nightly, default"
1911
- exit 1
1912
- ;;
1913
- esac
1914
- ;;
1915
- "--stable-channel") RELEASE_CHANNEL="stable" ;;
1916
- "--nightly-channel") RELEASE_CHANNEL="nightly" ;;
1917
- "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
1918
- "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
1919
- "--auto-update-method")
1920
- NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
1921
- case "${NETDATA_AUTO_UPDATE_TYPE}" in
1922
- systemd|interval|crontab)
1923
- shift 1
1924
- ;;
1925
- *)
1926
- echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
1927
- exit 1
1928
- ;;
1929
- esac
1930
- ;;
1931
- "--reinstall") NETDATA_REINSTALL=1 ;;
1932
- "--reinstall-even-if-unsafe") NETDATA_UNSAFE_REINSTALL=1 ;;
1933
- "--claim-only") NETDATA_CLAIM_ONLY=1 ;;
1934
- "--disable-cloud")
1935
- NETDATA_DISABLE_CLOUD=1
1936
- NETDATA_REQUIRE_CLOUD=0
1937
- ;;
1938
- "--require-cloud")
1939
- NETDATA_DISABLE_CLOUD=0
1940
- NETDATA_REQUIRE_CLOUD=1
1941
- ;;
1942
- "--dont-start-it")
1943
- NETDATA_NO_START=1
1944
- NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
1945
- ;;
1946
- "--disable-telemetry")
1947
- NETDATA_DISABLE_TELEMETRY="1"
1948
- NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
1949
- ;;
1950
- "--install")
1951
- warning "--install flag is deprecated and will be removed in a future version. Please use --install-prefix instead."
1952
- INSTALL_PREFIX="${2}"
1953
- shift 1
1954
- ;;
1955
- "--install-prefix")
1956
- INSTALL_PREFIX="${2}"
1957
- shift 1
1958
- ;;
1959
- "--install-version")
1960
- INSTALL_VERSION="${2}"
1961
- shift 1
1962
- ;;
1963
- "--old-install-prefix")
1964
- OLD_INSTALL_PREFIX="${2}"
1965
- shift 1
1966
- ;;
1967
- "--uninstall")
1968
- ACTION="uninstall"
1969
- ;;
1970
- "--reinstall-clean")
1971
- ACTION="reinstall-clean"
1972
- ;;
1973
- "--repositories-only")
1974
- REPO_ACTION="repositories-only"
1975
- ;;
1976
- "--native-only")
1977
- NETDATA_ONLY_NATIVE=1
1978
- NETDATA_ONLY_STATIC=0
1979
- NETDATA_ONLY_BUILD=0
1980
- SELECTED_INSTALL_METHOD="native"
1981
- ;;
1982
- "--static-only")
1983
- NETDATA_ONLY_STATIC=1
1984
- NETDATA_ONLY_NATIVE=0
1985
- NETDATA_ONLY_BUILD=0
1986
- SELECTED_INSTALL_METHOD="static"
1987
- ;;
1988
- "--build-only")
1989
- NETDATA_ONLY_BUILD=1
1990
- NETDATA_ONLY_NATIVE=0
1991
- NETDATA_ONLY_STATIC=0
1992
- SELECTED_INSTALL_METHOD="build"
1993
- ;;
1994
- "--claim-token")
1995
- NETDATA_CLAIM_TOKEN="${2}"
1996
- shift 1
1997
- ;;
1998
- "--claim-rooms")
1999
- NETDATA_CLAIM_ROOMS="${2}"
2000
- shift 1
2001
- ;;
2002
- "--claim-url")
2003
- NETDATA_CLAIM_URL="${2}"
2004
- shift 1
2005
- ;;
2006
- "--claim-"*)
2007
- optname="$(echo "${1}" | cut -d '-' -f 4-)"
2008
- case "${optname}" in
2009
- id|proxy|user|hostname)
2010
- NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
2011
- shift 1
2012
- ;;
2013
- verbose|insecure|noproxy|noreload|daemon-not-running)
2014
- NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}"
2015
- ;;
2016
- *)
2017
- warning "Ignoring unrecognized claiming option ${optname}"
2018
- ;;
2019
- esac
2020
- ;;
2021
- "--local-build-options")
2022
- LOCAL_BUILD_OPTIONS="${2}"
2023
- shift 1
2024
- ;;
2025
- "--static-install-options")
2026
- STATIC_INSTALL_OPTIONS="${2}"
2027
- shift 1
2028
- ;;
2029
- "--prepare-offline-install-source")
2030
- if [ -n "${2}" ]; then
2031
- ACTION="prepare-offline"
2032
- OFFLINE_TARGET="${2}"
2033
- shift 1
2034
- else
2035
- fatal "A target directory must be specified with the --prepare-offline-install-source option." F0500
2036
- fi
2037
- ;;
2038
- "--offline-install-source")
2039
- if [ -d "${2}" ]; then
2040
- NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
2041
- shift 1
2042
- else
2043
- fatal "A source directory must be specified with the --offline-install-source option." F0501
2044
- fi
2045
- ;;
2046
- *)
2047
- warning "Passing unrecognized option '${1}' to installer script. This behavior is deprecated and will be removed in the near future. If you intended to pass this option to the installer code, please use either --local-build-options or --static-install-options to specify it instead."
2048
- NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${1}"
2049
- ;;
2050
- esac
2051
- shift 1
2052
-done
2053
-
2054
-if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
2055
- if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ] || [ "${NETDATA_ONLY_BUILD}" -eq 1 ]; then
2056
- fatal "Offline installs are only supported for static builds currently." F0502
1891
+ if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
1892
+ if [ "${NETDATA_ONLY_NATIVE}" -eq 1 ] || [ "${NETDATA_ONLY_BUILD}" -eq 1 ]; then
1893
+ fatal "Offline installs are only supported for static builds currently." F0502
1894
+ fi
1895
fi
2058
-fi
1896
2060
-if [ -n "${LOCAL_BUILD_OPTIONS}" ]; then
2061
- if [ "${NETDATA_ONLY_BUILD}" -eq 1 ]; then
2062
- NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${LOCAL_BUILD_OPTIONS}"
2063
- else
2064
- fatal "Specifying local build options is only supported when the --build-only option is also specified." F0401
1897
+ if [ -n "${LOCAL_BUILD_OPTIONS}" ]; then
1898
+ if [ "${NETDATA_ONLY_BUILD}" -eq 1 ]; then
1899
+ NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${LOCAL_BUILD_OPTIONS}"
1900
+ else
1901
+ fatal "Specifying local build options is only supported when the --build-only option is also specified." F0401
1902
+ fi
1903
fi
2066
-fi
1904
2068
-if [ -n "${STATIC_INSTALL_OPTIONS}" ]; then
2069
- if [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
2070
- NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${STATIC_INSTALL_OPTIONS}"
2071
- else
2072
- fatal "Specifying installer options options is only supported when the --static-only option is also specified." F0402
1905
+ if [ -n "${STATIC_INSTALL_OPTIONS}" ]; then
1906
+ if [ "${NETDATA_ONLY_STATIC}" -eq 1 ]; then
1907
+ NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${STATIC_INSTALL_OPTIONS}"
1908
+ else
1909
+ fatal "Specifying installer options options is only supported when the --static-only option is also specified." F0402
1910
+ fi
1911
fi
2074
-fi
1912
2076
-if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ -n "${INSTALL_VERSION}" ]; then
2077
- fatal "Specifying an install version alongside an offline install source is not supported." F050A
2078
-fi
1913
+ if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ -n "${INSTALL_VERSION}" ]; then
1914
+ fatal "Specifying an install version alongside an offline install source is not supported." F050A
1915
+ fi
1916
2080
-if [ "${NETDATA_AUTO_UPDATES}" = "default" ]; then
2081
- if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] || [ -n "${INSTALL_VERSION}" ]; then
2082
- AUTO_UPDATE=0
2083
- else
1917
+ if [ "${NETDATA_AUTO_UPDATES}" = "default" ]; then
1918
+ if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] || [ -n "${INSTALL_VERSION}" ]; then
1919
+ AUTO_UPDATE=0
1920
+ else
1921
+ AUTO_UPDATE=1
1922
+ fi
1923
+ elif [ "${NETDATA_AUTO_UPDATES}" = 1 ]; then
1924
AUTO_UPDATE=1
1925
+ else
1926
+ AUTO_UPDATE=0
1927
fi
2086
-elif [ "${NETDATA_AUTO_UPDATES}" = 1 ]; then
2087
- AUTO_UPDATE=1
2088
-else
2089
- AUTO_UPDATE=0
2090
-fi
1928
2092
-if [ "${RELEASE_CHANNEL}" = "default" ]; then
2093
- if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
2094
- SELECTED_RELEASE_CHANNEL="$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")"
1929
+ if [ "${RELEASE_CHANNEL}" = "default" ]; then
1930
+ if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ]; then
1931
+ SELECTED_RELEASE_CHANNEL="$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")"
1932
2096
- if [ -z "${SELECTED_RELEASE_CHANNEL}" ]; then
2097
- fatal "Could not find a release channel indicator in ${NETDATA_OFFLINE_INSTALL_SOURCE}." F0508
1933
+ if [ -z "${SELECTED_RELEASE_CHANNEL}" ]; then
1934
+ fatal "Could not find a release channel indicator in ${NETDATA_OFFLINE_INSTALL_SOURCE}." F0508
1935
+ fi
1936
+ else
1937
+ SELECTED_RELEASE_CHANNEL="${DEFAULT_RELEASE_CHANNEL}"
1938
fi
1939
else
2100
- SELECTED_RELEASE_CHANNEL="${DEFAULT_RELEASE_CHANNEL}"
1940
+ if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ "${RELEASE_CHANNEL}" != "$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")" ]; then
1941
+ fatal "Release channal '${RELEASE_CHANNEL}' requested, but indicated offline installation source release channel is '$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")'." F0509
1942
+ fi
1943
+
1944
+ SELECTED_RELEASE_CHANNEL="${RELEASE_CHANNEL}"
1945
fi
2102
-else
2103
- if [ -n "${NETDATA_OFFLINE_INSTALL_SOURCE}" ] && [ "${RELEASE_CHANNEL}" != "$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")" ]; then
2104
- fatal "Release channal '${RELEASE_CHANNEL}' requested, but indicated offline installation source release channel is '$(cat "${NETDATA_OFFLINE_INSTALL_SOURCE}/channel")'." F0509
1946
+}
1947
+
1948
+parse_args() {
1949
+ if [ -n "${NETDATA_INSTALLER_OPTIONS}" ]; then
1950
+ warning "Explicitly specifying additional installer options with NETDATA_INSTALLER_OPTIONS is deprecated. Please instead pass the options to the script using either --local-build-options or --static-install-options as appropriate."
1951
fi
1952
2107
- SELECTED_RELEASE_CHANNEL="${RELEASE_CHANNEL}"
2108
-fi
1953
+ while [ -n "${1}" ]; do
1954
+ case "${1}" in
1955
+ "--help")
1956
+ usage
1957
+ cleanup
1958
+ trap - EXIT
1959
+ exit 0
1960
+ ;;
1961
+ "--no-cleanup") NO_CLEANUP=1 ;;
1962
+ "--dont-wait"|"--non-interactive") INTERACTIVE=0 ;;
1963
+ "--interactive") INTERACTIVE=1 ;;
1964
+ "--dry-run") DRY_RUN=1 ;;
1965
+ "--stable-channel") RELEASE_CHANNEL="stable" ;;
1966
+ "--no-updates") NETDATA_AUTO_UPDATES=0 ;;
1967
+ "--auto-update") NETDATA_AUTO_UPDATES="1" ;;
1968
+ "--auto-update-method")
1969
+ NETDATA_AUTO_UPDATE_TYPE="$(echo "${2}" | tr '[:upper:]' '[:lower:]')"
1970
+ case "${NETDATA_AUTO_UPDATE_TYPE}" in
1971
+ systemd|interval|crontab)
1972
+ shift 1
1973
+ ;;
1974
+ *)
1975
+ echo "Unrecognized value for --auto-update-type. Valid values are: systemd, interval, crontab"
1976
+ exit 1
1977
+ ;;
1978
+ esac
1979
+ ;;
1980
+ "--reinstall") NETDATA_REINSTALL=1 ;;
1981
+ "--reinstall-even-if-unsafe") NETDATA_UNSAFE_REINSTALL=1 ;;
1982
+ "--claim-only") NETDATA_CLAIM_ONLY=1 ;;
1983
+ "--disable-cloud")
1984
+ NETDATA_DISABLE_CLOUD=1
1985
+ NETDATA_REQUIRE_CLOUD=0
1986
+ ;;
1987
+ "--require-cloud")
1988
+ NETDATA_DISABLE_CLOUD=0
1989
+ NETDATA_REQUIRE_CLOUD=1
1990
+ ;;
1991
+ "--dont-start-it")
1992
+ NETDATA_NO_START=1
1993
+ NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --dont-start-it"
1994
+ ;;
1995
+ "--disable-telemetry")
1996
+ NETDATA_DISABLE_TELEMETRY="1"
1997
+ NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} --disable-telemetry"
1998
+ ;;
1999
+ "--install")
2000
+ warning "--install flag is deprecated and will be removed in a future version. Please use --install-prefix instead."
2001
+ INSTALL_PREFIX="${2}"
2002
+ shift 1
2003
+ ;;
2004
+ "--install-prefix")
2005
+ INSTALL_PREFIX="${2}"
2006
+ shift 1
2007
+ ;;
2008
+ "--old-install-prefix")
2009
+ OLD_INSTALL_PREFIX="${2}"
2010
+ shift 1
2011
+ ;;
2012
+ "--install-version")
2013
+ INSTALL_VERSION="${2}"
2014
+ AUTO_UPDATE=0
2015
+ shift 1
2016
+ ;;
2017
+ "--uninstall")
2018
+ ACTION="uninstall"
2019
+ ;;
2020
+ "--reinstall-clean")
2021
+ ACTION="reinstall-clean"
2022
+ ;;
2023
+ "--repositories-only")
2024
+ REPO_ACTION="repositories-only"
2025
+ ;;
2026
+ "--native-only")
2027
+ NETDATA_ONLY_NATIVE=1
2028
+ NETDATA_ONLY_STATIC=0
2029
+ NETDATA_ONLY_BUILD=0
2030
+ SELECTED_INSTALL_METHOD="native"
2031
+ ;;
2032
+ "--static-only")
2033
+ NETDATA_ONLY_STATIC=1
2034
+ NETDATA_ONLY_NATIVE=0
2035
+ NETDATA_ONLY_BUILD=0
2036
+ SELECTED_INSTALL_METHOD="static"
2037
+ ;;
2038
+ "--build-only")
2039
+ NETDATA_ONLY_BUILD=1
2040
+ NETDATA_ONLY_NATIVE=0
2041
+ NETDATA_ONLY_STATIC=0
2042
+ SELECTED_INSTALL_METHOD="build"
2043
+ ;;
2044
+ "--claim-token")
2045
+ NETDATA_CLAIM_TOKEN="${2}"
2046
+ shift 1
2047
+ ;;
2048
+ "--claim-rooms")
2049
+ NETDATA_CLAIM_ROOMS="${2}"
2050
+ shift 1
2051
+ ;;
2052
+ "--claim-url")
2053
+ NETDATA_CLAIM_URL="${2}"
2054
+ shift 1
2055
+ ;;
2056
+ "--claim-"*)
2057
+ optname="$(echo "${1}" | cut -d '-' -f 4-)"
2058
+ case "${optname}" in
2059
+ id|proxy|user|hostname)
2060
+ NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}=${2}"
2061
+ shift 1
2062
+ ;;
2063
+ verbose|insecure|noproxy|noreload|daemon-not-running)
2064
+ NETDATA_CLAIM_EXTRA="${NETDATA_CLAIM_EXTRA} -${optname}"
2065
+ ;;
2066
+ *)
2067
+ warning "Ignoring unrecognized claiming option ${optname}"
2068
+ ;;
2069
+ esac
2070
+ ;;
2071
+ "--local-build-options")
2072
+ LOCAL_BUILD_OPTIONS="${2}"
2073
+ shift 1
2074
+ ;;
2075
+ "--static-install-options")
2076
+ STATIC_INSTALL_OPTIONS="${2}"
2077
+ shift 1
2078
+ ;;
2079
+ "--prepare-offline-install-source")
2080
+ if [ -n "${2}" ]; then
2081
+ ACTION="prepare-offline"
2082
+ OFFLINE_TARGET="${2}"
2083
+ shift 1
2084
+ else
2085
+ fatal "A target directory must be specified with the --prepare-offline-install-source option." F0500
2086
+ fi
2087
+ ;;
2088
+ "--offline-install-source")
2089
+ if [ -d "${2}" ]; then
2090
+ NETDATA_OFFLINE_INSTALL_SOURCE="${2}"
2091
+ shift 1
2092
+ else
2093
+ fatal "A source directory must be specified with the --offline-install-source option." F0501
2094
+ fi
2095
+ ;;
2096
+ *)
2097
+ warning "Passing unrecognized option '${1}' to installer script. This behavior is deprecated and will be removed in the near future. If you intended to pass this option to the installer code, please use either --local-build-options or --static-install-options to specify it instead."
2098
+ NETDATA_INSTALLER_OPTIONS="${NETDATA_INSTALLER_OPTIONS} ${1}"
2099
+ ;;
2100
+ esac
2101
+ shift 1
2102
+ done
2103
+
2104
+ validate_args
2105
+}
2106
+
2107
+# ======================================================================
2108
+# Main program
2109
+
2110
+setup_terminal || echo > /dev/null
2111
+
2112
+# shellcheck disable=SC2068
2113
+parse_args $@
2114
2110
-check_claim_opts
2115
confirm_root_support
2116
get_system_info
2117
confirm_install_prefix