~ salmonella-environment-setup (master) /run-salmonella.sh
Trap1#! /bin/sh23# shellcheck disable=SC30434# The `local' declaration is relatively portable56[ -n "$RUN_SALMONELLA_DEBUG" ] && set -x78ROOT_DIR=$(readlink -f "$(dirname "$0")")9SRC_CONF_DIR=$ROOT_DIR/conf10HOOKS_DIR=$ROOT_DIR/hooks11LOCAL_CONF_DIR=$HOME/.salmonella-run-publish12WORK_DIR=$HOME/salmonella/build1314LOCK_DIR=/tmp/salmonella-lock15LOCK_PID=/tmp/salmonella-lock/pid1617# Temporary log file. It won't be published as part of job artifacts.18# It is mostly to log events related to locking.19TMP_LOG_FILE=/tmp/salmonella-$$.log202122cleanup() {23 rm -rf "$LOCK_DIR"24 rm -f "$TMP_LOG_FILE"25}262728trap cleanup EXIT INT TERM293031usage() {32 cat <<EOF33Usage: $(basename "$0") <confs>3435<confs>: configuration filenames without the ".conf" extension. E.g.,36 if you have chicken-5.conf, use "chicken-5" as argument.3738Configuration files will be read from:3940* $SRC_CONF_DIR41* $LOCAL_CONF_DIR4243in that order. If a $LOCAL_CONF_DIR/site.conf file exists, it will be44loaded _after_ the configuration file in $SRC_CONF_DIR and _before_45the configuration file in $LOCAL_CONF_DIR.4647If the environment variable CHICKEN_CORE_COMMIT_HASH is set, it is48going to be passed to salmonella-run-publish.4950If the environment variable RUN_SALMONELLA_DEBUG is set, this script51will be executed in debug mode (set -x).52EOF53}5455# The CHICKEN major version to be considered when calling tools to run56# tests (salmonella-run-publish, salmonella-html-report etc)57CHICKEN_TESTS_MAJOR_VERSION=${CHICKEN_TESTS_MAJOR_VERSION:-5}5859# Conf files use these environment variables60export CHICKEN_4_PREFIX="$HOME/local/chicken-4"61export CHICKEN_5_PREFIX="$HOME/local/chicken-5"62export CHICKEN_6_PREFIX="$HOME/local/chicken-6"63export CHICKEN_5_EGGS_DIR="$HOME/src/chicken-5-eggs"64export SALMONELLA_REPORTS_DIR="$HOME/salmonella/reports"6566if [ "$CHICKEN_TESTS_MAJOR_VERSION" = 4 ]; then67 CHICKEN_TESTS_PREFIX=$CHICKEN_4_PREFIX68 OS=$("$CHICKEN_4_PREFIX/bin/csi" -p '(software-version)')69 ARCH=$("$CHICKEN_4_PREFIX/bin/csi" -p '(machine-type)')70else71 CHICKEN_TESTS_PREFIX=$CHICKEN_5_PREFIX72 OS=$("$CHICKEN_5_PREFIX/bin/csi" -p '(begin (import (chicken platform)) (software-version))')73 ARCH=$("$CHICKEN_5_PREFIX/bin/csi" -p '(begin (import (chicken platform)) (machine-type))')74fi7576SETTINGS_FILES="\77 $SRC_CONF_DIR/shell-settings/${OS}/settings.sh78 $SRC_CONF_DIR/shell-settings/${OS}-${ARCH}/settings.sh79 $LOCAL_CONF_DIR/shell-settings/${OS}/settings.sh80 $LOCAL_CONF_DIR/shell-settings/${OS}-${ARCH}/settings.sh81"828384run_hooks() {85 # Call hooks scripts, passing $OS, $ARCH, CHICKEN_4_PREFIX and86 # CHICKEN_5_PREFIX as arguments87 local hook88 local hook_dir8990 mkdir -p "$WORK_DIR"91 hooks_log_file=${WORK_DIR}/hooks.log9293 for hook_dir in "${HOOKS_DIR}/common" "${HOOKS_DIR}/${OS}" "${HOOKS_DIR}/${OS}/${ARCH}"94 do95 if [ -d "$hook_dir" ]; then96 for hook in "${hook_dir}/"*; do97 if [ -x "$hook" ]; then98 echo "=== Running hook $hook ===" > "$hooks_log_file"99 "$hook" "$OS" "$ARCH" "$CHICKEN_4_PREFIX" "$CHICKEN_5_PREFIX" >>"$hooks_log_file" 2>&1100 fi101 done102 fi103 done104}105106107run_salmonella() {108 # $@ => configuration filenames109110 mkdir -p "$WORK_DIR"111112 local salmonella_run_publish113 salmonella_run_publish="$CHICKEN_TESTS_PREFIX/bin/salmonella-run-publish"114115 local conf_work_dir116 for conf in "$@"; do117 conf_work_dir=$WORK_DIR/$conf118119 # This must be in sync with the value of `(log-file)' in120 # salmonella-run-publish.121 log_file=$conf_work_dir/run-salmonella.log122123 # Remove leftovers from previous executions. Once124 # `$conf_work_dir` is created it is safe to use `info'.125 rm -rf "$conf_work_dir"126 mkdir -p "$conf_work_dir"127128 # Load settings files129 local settings_file130 for settings_file in $SETTINGS_FILES; do131 if [ -e "$settings_file" ]; then132 echo "Loading $settings_file" >> "$log_file"133 # shellcheck disable=SC1090134 . "$settings_file"135 fi136 done137138 # The inline egg writes some stuff to the home dir139 rm -rf ~/.chicken-inline140141 run_hooks142143 local args144 args="--work-dir $conf_work_dir $SRC_CONF_DIR/${conf}.conf"145146 local site_conf_path147 site_conf_path=$LOCAL_CONF_DIR/site.conf148 [ -e "$site_conf_path" ] && args="$args $site_conf_path"149150 local local_conf_path151 local_conf_path=$LOCAL_CONF_DIR/${conf}.conf152 [ -e "$local_conf_path" ] && args="$args $local_conf_path"153154 [ -n "$CHICKEN_CORE_COMMIT_HASH" ] &&155 args="--commit-hash $CHICKEN_CORE_COMMIT_HASH $args"156157 echo "Running $salmonella_run_publish $args" >> "$log_file"158 # shellcheck disable=SC2086159 "$salmonella_run_publish" $args >>"$log_file" 2>&1160161 done162}163164165main() {166 # Check if all conf files exist167 local conf168 for conf in "$@"; do169 if [ ! -e "$SRC_CONF_DIR/${conf}.conf" ]; then170 echo "No configuration file found for '$conf'. Aborting." >&2171 exit 1172 fi173 done174175 while true; do176 if mkdir "$LOCK_DIR" 2>/dev/null; then177 echo $$ > "$LOCK_PID"178 echo "[$(date)] $$ acquired lock" >> "$TMP_LOG_FILE"179 run_salmonella "$@"180 cleanup181 exit182 else183 # Check if the lock is stale184 local pid185 pid=$(cat "$LOCK_PID")186 if kill -0 "$pid" 2>/dev/null; then187 echo "[$(date)] Waiting for $pid" >> "$TMP_LOG_FILE"188 sleep 60189 else190 # Lock is stale. Cleanup and continue.191 echo "[$(date)] Cleaning up stale lock (pid=$pid)" >> "$TMP_LOG_FILE"192 cleanup193 fi194 fi195 done196}197198199if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ]; then200 usage201 exit 0202elif [ "$#" -lt 1 ]; then203 usage >&2204 exit 1205else206 main "$@"207fi