shellcheck (#12258)

* remove unneeded semi-colons at end of line

* fix SC2206: Quote to prevent word splitting/globbing

* fix SC2164: Use 'cd ... || exit' in case cd fails

* fix SC2004: $/${} is unnecessary on arithmetic variables.

* fix SC2155: Declare and assign separately to avoid masking return values.

* fix SC2124: Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

* fix SC2059: Don't use variables in the printf format string. Use printf "..%s.." "$foo".

* fix SC2076: Don't quote rhs of =~, it'll match literally rather than as a regex.

* fix SC1090: Can't follow non-constant source. Use a directive to specify location.

* fix SC2166: Prefer [ p ] || [ q ] as [ p -o q ] is not well defined.

* SC2223: This default assignment may cause DoS due to globbing. Quote it.

* fix SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

* fix SC1117: Backslash is literal in "\n". Prefer explicit escaping: "\\n"

* better handling of branch comapre

* add missing local ver_73 from merge

* remove duplicate definition
This commit is contained in:
RobJE 2020-11-11 23:21:15 +01:00 committed by GitHub
parent 565dc3c445
commit e1497ecf0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 60 deletions

2
cronic
View File

@ -23,7 +23,7 @@ else
ERR=$TRACE ERR=$TRACE
fi fi
if [ $RESULT -ne 0 -o -s "$ERR" ] if [ $RESULT -ne 0 ] || [ -s "$ERR" ]
then then
echo "Cronic detected failure or error output for the command:" echo "Cronic detected failure or error output for the command:"
echo "$@" echo "$@"

121
daily.sh
View File

@ -30,6 +30,7 @@ COMPOSER="php ${LIBRENMS_DIR}/scripts/composer_wrapper.php --no-interaction"
LOG_DIR=$(php -r "@include '${LIBRENMS_DIR}/config.php'; echo isset(\$config['log_dir']) ? \$config['log_dir'] : '${LIBRENMS_DIR}/logs';") LOG_DIR=$(php -r "@include '${LIBRENMS_DIR}/config.php'; echo isset(\$config['log_dir']) ? \$config['log_dir'] : '${LIBRENMS_DIR}/logs';")
# get the librenms user # get the librenms user
# shellcheck source=/opt/librenms/.env
source "${LIBRENMS_DIR}/.env" source "${LIBRENMS_DIR}/.env"
LIBRENMS_USER="${LIBRENMS_USER:-librenms}" LIBRENMS_USER="${LIBRENMS_USER:-librenms}"
LIBRENMS_USER_ID=$(id -u "$LIBRENMS_USER") LIBRENMS_USER_ID=$(id -u "$LIBRENMS_USER")
@ -46,31 +47,30 @@ LIBRENMS_USER_ID=$(id -u "$LIBRENMS_USER")
####################################### #######################################
status_run() { status_run() {
# Explicitly define our arguments # Explicitly define our arguments
local args="$@"; local args arg_text arg_command arg_option log_file exit_code tmp log_file
local arg_text=$1;
local arg_command=$2; args=("$@")
local arg_option=$3; arg_text=$1
local log_file; arg_command=$2
local exit_code; arg_option=$3
local tmp; log_file=${LOG_DIR}/daily.log
local log_file=${LOG_DIR}/daily.log;
# set log_file, using librenms $config['log_dir'], if set # set log_file, using librenms $config['log_dir'], if set
# otherwise we default to ./logs/daily.log # otherwise we default to ./logs/daily.log
printf "%-50s" "${arg_text}"; printf "%-50s" "${arg_text}"
echo "${arg_text}" >> ${log_file} echo "${arg_text}" >> "${log_file}"
tmp=$(bash -c "${arg_command}" 2>&1); tmp=$(bash -c "${arg_command}" 2>&1)
exit_code=$? exit_code=$?
echo "${tmp}" >> ${log_file} echo "${tmp}" >> "${log_file}"
echo "Returned: ${exit_code}" >> ${log_file} echo "Returned: ${exit_code}" >> "${log_file}"
# print OK if the command ran successfully # print OK if the command ran successfully
# or FAIL otherwise (non-zero exit code) # or FAIL otherwise (non-zero exit code)
if [[ "${exit_code}" == "0" ]]; then if [[ "${exit_code}" == "0" ]]; then
printf " \033[0;32mOK\033[0m\n"; printf " \\033[0;32mOK\\033[0m\\n"
else else
printf " \033[0;31mFAIL\033[0m\n"; printf " \\033[0;31mFAIL\\033[0m\\n"
if [[ "${arg_option}" == "update" ]]; then if [[ "${arg_option}" == "update" ]]; then
php "${LIBRENMS_DIR}/daily.php" -f notify -o "${tmp}" php "${LIBRENMS_DIR}/daily.php" -f notify -o "${tmp}"
fi fi
@ -94,10 +94,12 @@ status_run() {
# Exit-Code of Command # Exit-Code of Command
####################################### #######################################
call_daily_php() { call_daily_php() {
local args=( "$@" ); local args
args=("$@")
for arg in "${args[@]}"; do for arg in "${args[@]}"; do
php "${LIBRENMS_DIR}/daily.php" -f "${arg}"; php "${LIBRENMS_DIR}/daily.php" -f "${arg}"
done done
} }
@ -113,11 +115,13 @@ call_daily_php() {
# Exit-Code of Command # Exit-Code of Command
####################################### #######################################
set_notifiable_result() { set_notifiable_result() {
local args="$@"; local args arg_type arg_result
local arg_type=$1;
local arg_result=$2;
php "${LIBRENMS_DIR}/daily.php" -f handle_notifiable -t ${arg_type} -r ${arg_result}; args=("$@")
arg_type=$1
arg_result=$2
php "${LIBRENMS_DIR}/daily.php" -f handle_notifiable -t "${arg_type}" -r "${arg_result}"
} }
####################################### #######################################
@ -126,20 +130,22 @@ set_notifiable_result() {
# Exit-Code: 0 >= min ver, 1 < min ver # Exit-Code: 0 >= min ver, 1 < min ver
####################################### #######################################
check_dependencies() { check_dependencies() {
local branch=$(git rev-parse --abbrev-ref HEAD) local branch ver_56 ver_71 ver_72 ver_73 python3 python_deps phpver pythonver old_branches msg
branch=$(git rev-parse --abbrev-ref HEAD)
scripts/check_requirements.py > /dev/null 2>&1 || pip3 install -r requirements.txt > /dev/null 2>&1 scripts/check_requirements.py > /dev/null 2>&1 || pip3 install -r requirements.txt > /dev/null 2>&1
local ver_56=$(php -r "echo (int)version_compare(PHP_VERSION, '5.6.4', '<');") ver_56=$(php -r "echo (int)version_compare(PHP_VERSION, '5.6.4', '<');")
local ver_71=$(php -r "echo (int)version_compare(PHP_VERSION, '7.1.3', '<');") ver_71=$(php -r "echo (int)version_compare(PHP_VERSION, '7.1.3', '<');")
local ver_72=$(php -r "echo (int)version_compare(PHP_VERSION, '7.2.5', '<');") ver_72=$(php -r "echo (int)version_compare(PHP_VERSION, '7.2.5', '<');")
local ver_73=$(php -r "echo (int)version_compare(PHP_VERSION, '7.3', '<');") ver_73=$(php -r "echo (int)version_compare(PHP_VERSION, '7.3', '<');")
local python3=$(python3 -c "import sys;print(int(sys.version_info < (3, 4)))" 2> /dev/null) python3=$(python3 -c "import sys;print(int(sys.version_info < (3, 4)))" 2> /dev/null)
local python_deps=$("${LIBRENMS_DIR}/scripts/check_requirements.py" > /dev/null 2>&1; echo $?) python_deps=$("${LIBRENMS_DIR}/scripts/check_requirements.py" > /dev/null 2>&1; echo $?)
local phpver="master" phpver="master"
local pythonver="master" pythonver="master"
local old_branches="php53 php56 php71-python2 php72"
if [[ " $old_branches " =~ " $branch " ]] && [[ "$ver_73" == "0" && "$python3" == "0" && "$python_deps" == "0" ]]; then old_branches="^(php53|php56|php71-python2|php72)$"
if [[ $branch =~ $old_branches ]] && [[ "$ver_73" == "0" && "$python3" == "0" && "$python_deps" == "0" ]]; then
status_run "Supported PHP and Python version, switched back to master branch." 'git checkout master' status_run "Supported PHP and Python version, switched back to master branch." 'git checkout master'
elif [[ "$ver_56" != "0" ]]; then elif [[ "$ver_56" != "0" ]]; then
phpver="php53" phpver="php53"
@ -152,7 +158,7 @@ check_dependencies() {
status_run "Unsupported PHP version, switched to php56 branch." 'git checkout php56' status_run "Unsupported PHP version, switched to php56 branch." 'git checkout php56'
fi fi
elif [[ "$ver_72" != "0" || "$python3" != "0" || "$python_deps" != "0" ]]; then elif [[ "$ver_72" != "0" || "$python3" != "0" || "$python_deps" != "0" ]]; then
local msg="" msg=""
if [[ "$ver_72" != "0" ]]; then if [[ "$ver_72" != "0" ]]; then
msg="Unsupported PHP version, $msg" msg="Unsupported PHP version, $msg"
phpver="php71" phpver="php71"
@ -179,9 +185,9 @@ check_dependencies() {
set_notifiable_result pythonver ${pythonver} set_notifiable_result pythonver ${pythonver}
if [[ "$phpver" == "master" && "$pythonver" == "master" ]]; then if [[ "$phpver" == "master" && "$pythonver" == "master" ]]; then
return 0; return 0
fi fi
return 1; return 1
} }
####################################### #######################################
@ -195,13 +201,16 @@ check_dependencies() {
# Exit-Code: 0: if equal 1: if 1 > 2 2: if 1 < 2 # Exit-Code: 0: if equal 1: if 1 > 2 2: if 1 < 2
####################################### #######################################
version_compare () { version_compare () {
local IFS i ver1 ver2 parts1 parts2
if [[ "$1" == "$2" ]]; then if [[ "$1" == "$2" ]]; then
return 0 return 0
fi fi
local IFS=. IFS=.
local i ver1=($1) ver2=($2) ver1=("$1")
ver2=("$2")
local parts2=${#ver2[@]} parts2=${#ver2[@]}
[[ -n $3 ]] && parts2=$3 [[ -n $3 ]] && parts2=$3
# fill empty fields in ver1 with zeros # fill empty fields in ver1 with zeros
@ -209,7 +218,7 @@ version_compare () {
ver1[i]=0 ver1[i]=0
done done
local parts1=${#ver1[@]} parts1=${#ver1[@]}
[[ -n $3 ]] && parts1=$3 [[ -n $3 ]] && parts1=$3
for ((i=0; i<parts1; i++)); do for ((i=0; i<parts1; i++)); do
@ -238,12 +247,14 @@ version_compare () {
# Exit-Code of Command # Exit-Code of Command
####################################### #######################################
main () { main () {
local arg="$1"; local arg old_version new_version branch options
local old_version="$2";
local new_version="$3";
local old_version="${old_version:=unset}" # if $1 is unset, make it mismatch for pre-update daily.sh
cd ${LIBRENMS_DIR}; arg="$1"
old_version="$2"
new_version="$3"
old_version="${old_version:=unset}" # if $1 is unset, make it mismatch for pre-update daily.sh
cd "${LIBRENMS_DIR}" || exit 1
# if not running as $LIBRENMS_USER (unless $LIBRENMS_USER = root), relaunch # if not running as $LIBRENMS_USER (unless $LIBRENMS_USER = root), relaunch
if [[ "$LIBRENMS_USER" != "root" ]]; then if [[ "$LIBRENMS_USER" != "root" ]]; then
@ -251,11 +262,11 @@ main () {
if [[ "$EUID" -eq 0 ]]; then if [[ "$EUID" -eq 0 ]]; then
echo "Re-running ${DAILY_SCRIPT} as ${LIBRENMS_USER} user" echo "Re-running ${DAILY_SCRIPT} as ${LIBRENMS_USER} user"
sudo -u "$LIBRENMS_USER" "$DAILY_SCRIPT" "$@" sudo -u "$LIBRENMS_USER" "$DAILY_SCRIPT" "$@"
exit; exit
fi fi
if [[ "$EUID" -ne "$LIBRENMS_USER_ID" ]]; then if [[ "$EUID" -ne "$LIBRENMS_USER_ID" ]]; then
printf "\033[0;93mWARNING\033[0m: You should run this script as ${LIBRENMS_USER}\n"; printf "\\033[0;93mWARNING\\033[0m: You should run this script as %s\\n" "${LIBRENMS_USER}"
fi fi
fi fi
@ -281,7 +292,7 @@ main () {
update_res=0 update_res=0
if [[ "$up" == "1" ]] || [[ "$php_ver_ret" == "1" ]]; then if [[ "$up" == "1" ]] || [[ "$php_ver_ret" == "1" ]]; then
# Update current branch to latest # Update current branch to latest
local branch=$(git rev-parse --abbrev-ref HEAD) branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$branch" == "HEAD" ]]; then if [[ "$branch" == "HEAD" ]]; then
# if the branch is HEAD, then we are not on a branch, checkout master # if the branch is HEAD, then we are not on a branch, checkout master
git checkout master git checkout master
@ -317,7 +328,7 @@ main () {
fi fi
fi fi
if (( $update_res > 0 )); then if (( update_res > 0 )); then
set_notifiable_result update 0 set_notifiable_result update 0
fi fi
@ -361,7 +372,7 @@ main () {
;; ;;
cleanup) cleanup)
# Cleanups # Cleanups
local options=("refresh_alert_rules" options=("refresh_alert_rules"
"refresh_os_cache" "refresh_os_cache"
"refresh_device_groups" "refresh_device_groups"
"recalculate_device_dependencies" "recalculate_device_dependencies"
@ -377,8 +388,8 @@ main () {
"rrd_purge" "rrd_purge"
"ports_fdb" "ports_fdb"
"route" "route"
"ports_purge"); "ports_purge")
call_daily_php "${options[@]}"; call_daily_php "${options[@]}"
;; ;;
submodules) submodules)
# Init+Update our submodules # Init+Update our submodules
@ -387,12 +398,12 @@ main () {
;; ;;
notifications) notifications)
# Get notifications # Get notifications
local options=("notifications"); options=("notifications")
call_daily_php "${options[@]}"; call_daily_php "${options[@]}"
;; ;;
peeringdb) peeringdb)
local options=("peeringdb"); options=("peeringdb")
call_daily_php "${options[@]}"; call_daily_php "${options[@]}"
;; ;;
esac esac
fi fi

View File

@ -24,7 +24,7 @@ set -e
PROGRAM=${0##*/} PROGRAM=${0##*/}
# can be overriden from env # can be overriden from env
: ${PASTE_URL='https://p.libren.ms/api/create'} : "${PASTE_URL='https://p.libren.ms/api/create'}"
# paste. take input from stdin # paste. take input from stdin
pastebin() { pastebin() {

View File

@ -37,7 +37,7 @@ EOF
mkdir -p out mkdir -p out
cd out cd out || exit 1
git init git init
git remote add origin "$FULL_REPO" git remote add origin "$FULL_REPO"
@ -46,13 +46,13 @@ git config user.name "librenms-docs"
git config user.email "travis@librenms.org" git config user.email "travis@librenms.org"
git checkout master git checkout master
cd ../ cd ../ || exit 1
mkdocs build --clean mkdocs build --clean
build_result=$? build_result=$?
# Only deploy after merging to master # Only deploy after merging to master
if [ "$build_result" == "0" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then if [ "$build_result" == "0" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then
cd out/ cd out/ || exit 1
touch . touch .
git add -A . git add -A .
git commit -m "GH-Pages update by travis after $TRAVIS_COMMIT" git commit -m "GH-Pages update by travis after $TRAVIS_COMMIT"