Browse Source

[cleanup crusade] linting shell scripts for docker, tests and python (#4162)

* shell linting various scripts

* remove python-modules-installer.sh.in
Paweł Krupa 6 years ago
parent
commit
0426209ead
7 changed files with 17 additions and 177 deletions
  1. 4 4
      coverity-scan.sh
  2. 1 1
      cppcheck.sh
  3. 1 1
      docker/build.sh
  4. 0 1
      packaging/tar-compare
  5. 0 2
      python.d/Makefile.am
  6. 0 159
      python.d/python-modules-installer.sh.in
  7. 11 9
      tests/stress.sh

+ 4 - 4
coverity-scan.sh

@@ -1,10 +1,10 @@
 #!/usr/bin/env bash
 
-cpus=$(grep ^processor </proc/cpuinfo| wc -l)
+cpus=$(grep -c ^processor </proc/cpuinfo)
 [ -z "${cpus}" ] && cpus=1
 
 token="${COVERITY_SCAN_TOKEN}"
-[ -z "${token}" -a -f .coverity-token ] && token="$(<.coverity-token)"
+([ -z "${token}" ] && [ -f .coverity-token ]) && token="$(<.coverity-token)"
 [ -z "${token}" ] && \
 	echo >&2 "Save the coverity token to .coverity-token or export it as COVERITY_SCAN_TOKEN." && \
 	exit 1
@@ -12,7 +12,7 @@ token="${COVERITY_SCAN_TOKEN}"
 # echo >&2 "Coverity token: ${token}"
 
 covbuild="$(which cov-build 2>/dev/null || command -v cov-build 2>/dev/null)"
-[ -z "${covbuild}" -a -f .coverity-build ] && covbuild="$(<.coverity-build)"
+([ -z "${covbuild}" ] && [ -f .coverity-build ]) && covbuild="$(<.coverity-build)"
 [ -z "${covbuild}" ] && \
 	echo "Save command the full filename of cov-build in .coverity-build" && \
 	exit 1
@@ -21,7 +21,7 @@ covbuild="$(which cov-build 2>/dev/null || command -v cov-build 2>/dev/null)"
 	echo "The command ${covbuild} is not executable. Save command the full filename of cov-build in .coverity-build" && \
 	exit 1
 
-version="$(cat config.h | grep "^#define PACKAGE_VERSION" | cut -d '"' -f 2)"
+version="$(grep "^#define PACKAGE_VERSION" config.h | cut -d '"' -f 2)"
 echo >&2 "Working on netdata version: ${version}"
 
 echo >&2 "Cleaning up old builds..."

+ 1 - 1
cppcheck.sh

@@ -17,7 +17,7 @@ cd "${base}/src" || exit 1
 
 file="${1}"
 shift
-[ "${file}" = "${base}" -o -z "${file}" ] && file="${base}/src"
+([ "${file}" = "${base}" ] || [ -z "${file}" ]) && file="${base}/src"
 
 "${cppcheck}" \
 	-j ${processors} \

+ 1 - 1
docker/build.sh

@@ -14,7 +14,7 @@ fi
 docker run --rm --privileged multiarch/qemu-user-static:register --reset
 
 if [ -f Dockerfile ]; then
-    cd ../
+    cd ../ || exit 1
 fi
 
 for ARCH in i386 armhf aarch64 amd64; do

+ 0 - 1
packaging/tar-compare

@@ -59,7 +59,6 @@ diff -r "$1" $MYTMP/unpack/* | grep "^Only" | sed \
 	-e '/: .*\.o$/d' \
 	-e '/: CMakeLists.txt/d' \
 	-e '/: tests$/d' \
-	-e '/python.d: python-modules-installer\.sh\.in$/d' \
 	-e '/: .travis.yml/d' > $MYTMP/out
 
 cat $MYTMP/out

+ 0 - 2
python.d/Makefile.am

@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: GPL-3.0+
 MAINTAINERCLEANFILES= $(srcdir)/Makefile.in
 CLEANFILES = \
-    python-modules-installer.sh \
     $(NULL)
 
 include $(top_srcdir)/build/subst.inc
@@ -9,7 +8,6 @@ include $(top_srcdir)/build/subst.inc
 SUFFIXES = .in
 
 dist_python_SCRIPTS = \
-    python-modules-installer.sh \
     $(NULL)
 
 dist_python_DATA = \

+ 0 - 159
python.d/python-modules-installer.sh.in

@@ -1,159 +0,0 @@
-#!/usr/bin/env bash
-# SPDX-License-Identifier: GPL-3.0+
-
-umask 022
-
-dir="@pythondir_POST@"
-target="${dir}/python_modules"
-pv="$(python -V 2>&1)"
-
-# parse parameters
-while [ ! -z "${1}" ]
-do
-    case "${1}" in
-    -p|--python)
-        pv="Python ${2}"
-        shift 2
-        ;;
-
-    -d|--dir)
-        dir="${2}"
-        target="${dir}/python_modules"
-        echo >&2 "Will install python modules in: '${target}'"
-        shift 2
-        ;;
-
-    -s|--system)
-        target=
-        echo >&2 "Will install python modules system-wide"
-        shift
-        ;;
-
-    -h|--help)
-        echo "${0} [--dir netdata-python.d-path] [--system]"
-        echo "Please make sure you have installed packages: python-pip (or python3-pip) python-dev libyaml-dev libmysqlclient-dev"
-        exit 0
-        ;;
-
-    *)
-        echo >&2 "Cannot understand parameter: ${1}"
-        exit 1
-        ;;
-    esac
-done
-
-
-if [ ! -z "${target}" -a ! -d "${target}" ]
-then
-    echo >&2 "Cannot find directory: '${target}'"
-    exit 1
-fi
-
-if [[ "${pv}" =~ ^Python\ 2.* ]]
-then
-    pv=2
-    pip="$(which pip2 2>/dev/null)"
-elif [[ "${pv}" =~ ^Python\ 3.* ]]
-then
-    pv=3
-    pip="$(which pip3 2>/dev/null)"
-else
-    echo >&2 "Cannot detect python version. Is python installed?"
-    exit 1
-fi
-
-[ -z "${pip}" ] && pip="$(which pip 2>/dev/null)"
-if [ -z "${pip}" ]
-then
-    echo >&2 "pip command is required to install python v${pv} modules."
-    [ "${pv}" = "2" ] && echo >&2 "Please install python-pip."
-    [ "${pv}" = "3" ] && echo >&2 "Please install python3-pip."
-    exit 1
-fi
-
-echo >&2 "Working for python version ${pv} (pip command: '${pip}')"
-echo >&2 "Installing netdata python modules in: '${target}'"
-
-run() {
-    printf "Running command:\n# "
-    printf "%q " "${@}"
-    printf "\n"
-    "${@}"
-}
-
-# try to install all the python modules given as parameters
-# until the first that succeeds
-failed=""
-installed=""
-errors=0
-pip_install() {
-    local ret x msg="${1}"
-    shift
-
-    echo >&2
-    echo >&2
-    echo >&2 "Installing one of: ${*}"
-
-    for x in "${@}"
-    do
-        echo >&2
-        echo >&2 "attempting to install: ${x}"
-        if [ ! -z "${target}" ]
-        then
-            run "${pip}" install --target "${target}" "${x}"
-            ret=$?
-        else
-            run "${pip}" install "${x}"
-            ret=$?
-        fi
-        [ ${ret} -eq 0 ] && break
-        echo >&2 "failed to install: ${x}. ${msg}"
-    done
-
-    if [ ${ret} -ne 0 ]
-    then
-        echo >&2
-        echo >&2
-        echo >&2 "FAILED: could not install any of: ${*}. ${msg}"
-        echo >&2
-        echo >&2
-        errors=$(( errors + 1 ))
-        failed="${failed}|${*}"
-    else
-        echo >&2
-        echo >&2
-        echo >&2 "SUCCESS: we have: ${x}"
-        echo >&2
-        echo >&2
-        installed="${installed} ${x}"
-    fi
-    return ${ret}
-}
-
-if [ "${pv}" = "2" ]
-then
-    pip_install "is libyaml-dev and python-dev installed?" pyyaml
-    pip_install "is libmysqlclient-dev and python-dev installed?" mysqlclient mysql-python pymysql
-else
-    pip_install "is libyaml-dev and python-dev installed?" pyyaml
-    pip_install "is libmysqlclient-dev and python-dev installed?" mysql-python mysqlclient pymysql
-fi
-
-echo >&2
-echo >&2
-if [ ${errors} -ne 0 ]
-then
-    echo >&2 "Failed to install ${errors} modules: ${failed}"
-    if [ ! -z "${target}" ]
-    then
-		echo >&2
-		echo >&2 "If you are getting errors during cleanup from pip, there is a known bug"
-		echo >&2 "in certain versions of pip that prevents installing packages local to an"
-		echo >&2 "application. To install them system-wide please run:"
-		echo >&2 "$0 --system"
-	fi
-    exit 1
-else
-    echo >&2 "All done. We have: ${installed}"
-    exit 0
-fi

+ 11 - 9
tests/stress.sh

@@ -3,16 +3,17 @@
 
 # set the host to connect to
 if [ ! -z "$1" ]
-	then
+then
 	host="$1"
 else
 	host="http://127.0.0.1:19999"
 fi
 echo "using netdata server at: $host"
 
+# shellcheck disable=SC2207 disable=SC1117
 charts=($(curl "$host/netdata.conf" 2>/dev/null | grep "^\[" | cut -d '[' -f 2 | cut -d ']' -f 1 | grep -v ^global$ | grep -v "^plugin" | sort -u))
 if [ "${#charts[@]}" -eq 0 ]
-	then
+then
 	echo "Cannot download charts from server: $host"
 	exit 1
 fi
@@ -27,7 +28,7 @@ entries="$(curl "$host/netdata.conf" 2>/dev/null | grep "history = " | head -n 1
 [ $entries -gt 3600 ] && entries=3600
 
 if [ $entries -ne 3600 ]
-	then
+then
 	echo >&2 "You are running a test for a history of $entries entries."
 fi
 
@@ -42,19 +43,20 @@ duration=$((now - first))
 file="$(mktemp /tmp/netdata-stress-XXXXXXXX)"
 cleanup() {
 	echo "cleanup"
-	[ -f $file ] && rm $file
+	[ -f "$file" ] && rm "$file"
 }
 trap cleanup EXIT
 
-while [ 1 = 1 ]
+while true
 do
-	echo "curl --compressed --keepalive-time 120 --header \"Connection: keep-alive\" \\" >$file
+	echo "curl --compressed --keepalive-time 120 --header \"Connection: keep-alive\" \\" >"$file"
+	# shellcheck disable=SC2034
 	for x in {1..100}
 	do
 		dt=$((RANDOM * duration / 32767))
 		st=$((RANDOM * duration / 32767))
 		et=$(( st + dt ))
-		[ $et -gt $now ] && st=$(( now - dt ))
+		[ $et -gt "$now" ] && st=$(( now - dt ))
 
 		points=$((RANDOM * 2000 / 32767 + 2))
 		st=$((first + st))
@@ -70,6 +72,6 @@ do
 		format="${formats[$format]}"
 
 		echo "--url \"$host/api/v1/data?chart=$chart&mode=$mode&format=$format&options=$options&after=$st&before=$et&points=$points\" \\"
-	done >>$file
-	bash $file >/dev/null
+	done >>"$file"
+	bash "$file" >/dev/null
 done