Files
tools/import-stx
Scott Little d98a34ad1b import-stx: Avoid divide by zero
Fix a divide by zero error seen when the number_of_users
function returns zero.

At issue is that 'users' only counts logins, and can miss
builds running under automation such as jenkins.

Add code to detect potential automation accounts, just
jenkins so far, and to always return a value greater than
zero.

Also adding a few .gitignore entries for files generated by
stx build related scripts.

Closes-bug: 1959606
Signed-off-by: Scott Little <scott.little@windriver.com>
Change-Id: I1369aa159088a17c24cc996be1ef2d36ecdc9cf6
2022-01-31 13:17:16 -05:00

102 lines
2.6 KiB
Plaintext

# bash
if [ -z "$MINIKUBE_HOME" ];then
MINIKUBE_HOME=$HOME
else
if [ ! -d "$MINIKUBE_HOME" ]; then
echo "The directory defined by \$MINIKUBE_HOME doesn't exist"
return 1
fi
fi
FSTYPE=$(stat -f -L -c %T $MINIKUBE_HOME)
if [ x"$FSTYPE" == x"nfs" ]; then
echo ""
echo "Warning: stx minikube doesn't allow \$MINIKUBE_HOME or \$HOME directory as nfs mount point!!!"
echo " Please set non-nfs MINIKUBE_HOME with the command 'export MINIKUBE_HOME=XXX/YYY'"
echo ""
unset MINIKUBE_HOME
return 1
fi
notice_warn() {
local tty_on tty_off
if [[ -t 2 ]] ; then
tty_on=$'\033[1;33m'
tty_off=$'\033[0m'
fi
echo >&2 "${tty_on}$*${tty_off}"
}
export PRJDIR=$(pwd)
export PATH=$PRJDIR/stx/bin:$PATH
export MINIKUBECPUS=${MINIKUBECPUS:-2}
export MINIKUBEMEMORY=${MINIKUBEMEMORY:-16000}
export MINIKUBENAME=${MINIKUBENAME:-minikube-$USER-upstream}
export KUBECONFIG=$MINIKUBE_HOME/.kube/config
export STX_BUILD_HOME="${STX_BUILD_HOME:-/localdisk/$USER}"
if [ ! -f "stx.conf" ]; then
cp stx.conf.sample stx.conf
fi
number_of_users () {
local count
count=$(users | tr ' ' '\n' | sort --uniq | wc -l)
# Add in non-login users that might trigger a parallel build
# based on a timer, or other trigger.
if getent passwd | grep -q jenkins; then
count=$((count+1))
fi
# Always return at least one. i.e. someone is
# running this script.
if [ $count -le 0 ]; then
count=1
fi
echo $count
}
number_of_cpus () {
/usr/bin/nproc
}
sqrt () {
echo -e "sqrt($1)" | bc -q -i | head -2 | tail -1
}
# Consider many users are just working with code and not actually building.
NUM_USERS=$(sqrt $(number_of_users))
ABSOLUTE_MAX_CPUS=$(($(number_of_cpus)/$NUM_USERS))
MAX_CPUS=$(number_of_cpus)
if [ "$MAX_CPUS" == "" ] || [ "$MAX_CPUS" == "0" ]; then
MAX_CPUS=1
fi
if [ $MAX_CPUS -gt $ABSOLUTE_MAX_CPUS ]; then
MAX_CPUS=$ABSOLUTE_MAX_CPUS
fi
if [ $MINIKUBECPUS -gt $MAX_CPUS ]; then
notice_warn "MINIKUBECPUS setting:$MINIKUBECPUS is more than MAX_CPUS: $MAX_CPUS."
notice_warn "Limit the minikube cluster with MAX_CPUS."
export MINIKUBECPUS=$MAX_CPUS
fi
MAX_MEMORY=`expr $(cat /proc/meminfo |grep MemTotal | awk '{print $2}') / 1024`
if [ "$MAX_MEMORY" == "" ] || [ "$MAX_MEMORY" == "0" ]; then
MAX_MEMORY=2048
fi
if [ $MINIKUBEMEMORY -gt $MAX_MEMORY ]; then
notice_warn "MINIKUBEMEMORY setting:$MINIKUBEMEMORY is more than system MAX_MEMORY: $MAX_MEMORY M."
notice_warn "Limit the minikube cluster with MAX_MEMORY."
export MINIKUBEMEMORY=$MAX_MEMORY
fi