~ salmonella-environment-setup (master) /run-salmonella.sh
Trap1#! /bin/sh
2
3# shellcheck disable=SC3043
4# The `local' declaration is relatively portable
5
6[ -n "$RUN_SALMONELLA_DEBUG" ] && set -x
7
8ROOT_DIR=$(readlink -f "$(dirname "$0")")
9SRC_CONF_DIR=$ROOT_DIR/conf
10HOOKS_DIR=$ROOT_DIR/hooks
11LOCAL_CONF_DIR=$HOME/.salmonella-run-publish
12WORK_DIR=$HOME/salmonella/build
13
14LOCK_DIR=/tmp/salmonella-lock
15LOCK_PID=/tmp/salmonella-lock/pid
16
17# 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-$$.log
20
21
22cleanup() {
23 rm -rf "$LOCK_DIR"
24 rm -f "$TMP_LOG_FILE"
25}
26
27
28trap cleanup EXIT INT TERM
29
30
31usage() {
32 cat <<EOF
33Usage: $(basename "$0") <confs>
34
35<confs>: configuration filenames without the ".conf" extension. E.g.,
36 if you have chicken-5.conf, use "chicken-5" as argument.
37
38Configuration files will be read from:
39
40* $SRC_CONF_DIR
41* $LOCAL_CONF_DIR
42
43in that order. If a $LOCAL_CONF_DIR/site.conf file exists, it will be
44loaded _after_ the configuration file in $SRC_CONF_DIR and _before_
45the configuration file in $LOCAL_CONF_DIR.
46
47If the environment variable CHICKEN_CORE_COMMIT_HASH is set, it is
48going to be passed to salmonella-run-publish.
49
50If the environment variable RUN_SALMONELLA_DEBUG is set, this script
51will be executed in debug mode (set -x).
52EOF
53}
54
55# The CHICKEN major version to be considered when calling tools to run
56# tests (salmonella-run-publish, salmonella-html-report etc)
57CHICKEN_TESTS_MAJOR_VERSION=${CHICKEN_TESTS_MAJOR_VERSION:-5}
58
59# Conf files use these environment variables
60export 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"
65
66if [ "$CHICKEN_TESTS_MAJOR_VERSION" = 4 ]; then
67 CHICKEN_TESTS_PREFIX=$CHICKEN_4_PREFIX
68 OS=$("$CHICKEN_4_PREFIX/bin/csi" -p '(software-version)')
69 ARCH=$("$CHICKEN_4_PREFIX/bin/csi" -p '(machine-type)')
70else
71 CHICKEN_TESTS_PREFIX=$CHICKEN_5_PREFIX
72 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))')
74fi
75
76SETTINGS_FILES="\
77 $SRC_CONF_DIR/shell-settings/${OS}/settings.sh
78 $SRC_CONF_DIR/shell-settings/${OS}-${ARCH}/settings.sh
79 $LOCAL_CONF_DIR/shell-settings/${OS}/settings.sh
80 $LOCAL_CONF_DIR/shell-settings/${OS}-${ARCH}/settings.sh
81"
82
83
84run_hooks() {
85 # Call hooks scripts, passing $OS, $ARCH, CHICKEN_4_PREFIX and
86 # CHICKEN_5_PREFIX as arguments
87 local hook
88 local hook_dir
89
90 mkdir -p "$WORK_DIR"
91 hooks_log_file=${WORK_DIR}/hooks.log
92
93 for hook_dir in "${HOOKS_DIR}/common" "${HOOKS_DIR}/${OS}" "${HOOKS_DIR}/${OS}/${ARCH}"
94 do
95 if [ -d "$hook_dir" ]; then
96 for hook in "${hook_dir}/"*; do
97 if [ -x "$hook" ]; then
98 echo "=== Running hook $hook ===" > "$hooks_log_file"
99 "$hook" "$OS" "$ARCH" "$CHICKEN_4_PREFIX" "$CHICKEN_5_PREFIX" >>"$hooks_log_file" 2>&1
100 fi
101 done
102 fi
103 done
104}
105
106
107run_salmonella() {
108 # $@ => configuration filenames
109
110 mkdir -p "$WORK_DIR"
111
112 local salmonella_run_publish
113 salmonella_run_publish="$CHICKEN_TESTS_PREFIX/bin/salmonella-run-publish"
114
115 local conf_work_dir
116 for conf in "$@"; do
117 conf_work_dir=$WORK_DIR/$conf
118
119 # This must be in sync with the value of `(log-file)' in
120 # salmonella-run-publish.
121 log_file=$conf_work_dir/run-salmonella.log
122
123 # Remove leftovers from previous executions. Once
124 # `$conf_work_dir` is created it is safe to use `info'.
125 rm -rf "$conf_work_dir"
126 mkdir -p "$conf_work_dir"
127
128 # Load settings files
129 local settings_file
130 for settings_file in $SETTINGS_FILES; do
131 if [ -e "$settings_file" ]; then
132 echo "Loading $settings_file" >> "$log_file"
133 # shellcheck disable=SC1090
134 . "$settings_file"
135 fi
136 done
137
138 # The inline egg writes some stuff to the home dir
139 rm -rf ~/.chicken-inline
140
141 run_hooks
142
143 local args
144 args="--work-dir $conf_work_dir $SRC_CONF_DIR/${conf}.conf"
145
146 local site_conf_path
147 site_conf_path=$LOCAL_CONF_DIR/site.conf
148 [ -e "$site_conf_path" ] && args="$args $site_conf_path"
149
150 local local_conf_path
151 local_conf_path=$LOCAL_CONF_DIR/${conf}.conf
152 [ -e "$local_conf_path" ] && args="$args $local_conf_path"
153
154 [ -n "$CHICKEN_CORE_COMMIT_HASH" ] &&
155 args="--commit-hash $CHICKEN_CORE_COMMIT_HASH $args"
156
157 echo "Running $salmonella_run_publish $args" >> "$log_file"
158 # shellcheck disable=SC2086
159 "$salmonella_run_publish" $args >>"$log_file" 2>&1
160
161 done
162}
163
164
165main() {
166 # Check if all conf files exist
167 local conf
168 for conf in "$@"; do
169 if [ ! -e "$SRC_CONF_DIR/${conf}.conf" ]; then
170 echo "No configuration file found for '$conf'. Aborting." >&2
171 exit 1
172 fi
173 done
174
175 while true; do
176 if mkdir "$LOCK_DIR" 2>/dev/null; then
177 echo $$ > "$LOCK_PID"
178 echo "[$(date)] $$ acquired lock" >> "$TMP_LOG_FILE"
179 run_salmonella "$@"
180 cleanup
181 exit
182 else
183 # Check if the lock is stale
184 local pid
185 pid=$(cat "$LOCK_PID")
186 if kill -0 "$pid" 2>/dev/null; then
187 echo "[$(date)] Waiting for $pid" >> "$TMP_LOG_FILE"
188 sleep 60
189 else
190 # Lock is stale. Cleanup and continue.
191 echo "[$(date)] Cleaning up stale lock (pid=$pid)" >> "$TMP_LOG_FILE"
192 cleanup
193 fi
194 fi
195 done
196}
197
198
199if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ]; then
200 usage
201 exit 0
202elif [ "$#" -lt 1 ]; then
203 usage >&2
204 exit 1
205else
206 main "$@"
207fi