Skip to content

Commit 42ce9af

Browse files
committed
hw-probe: run.sh: modular CLI runner (local/docker, install/update, extract)
Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 01ae7c9 commit 42ce9af

File tree

1 file changed

+251
-0
lines changed

1 file changed

+251
-0
lines changed

Runner/tools/hw-probe/run.sh

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
#!/bin/sh
2+
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
3+
# SPDX-License-Identifier: BSD-3-Clause-Clear
4+
# hw-probe orchestrator for Debian/Ubuntu
5+
# POSIX-only, modular, with local and Docker modes.
6+
# Uses functestlib.sh for logging & dependency checks.
7+
8+
# --------- Robustly source init_env and functestlib.sh ----------
9+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
10+
INIT_ENV=""
11+
SEARCH="$SCRIPT_DIR"
12+
while [ "$SEARCH" != "/" ]; do
13+
if [ -f "$SEARCH/init_env" ]; then
14+
INIT_ENV="$SEARCH/init_env"
15+
break
16+
fi
17+
SEARCH=$(dirname "$SEARCH")
18+
done
19+
20+
if [ -z "$INIT_ENV" ]; then
21+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
22+
exit 1
23+
fi
24+
25+
if [ -z "$__INIT_ENV_LOADED" ]; then
26+
# shellcheck disable=SC1090
27+
. "$INIT_ENV"
28+
fi
29+
# shellcheck disable=SC1090,SC1091
30+
. "$TOOLS/functestlib.sh"
31+
# --- source our libs (they rely on functestlib logging) ---
32+
# shellcheck disable=SC1091
33+
. "$TOOLS/lib_common.sh"
34+
# shellcheck disable=SC1091
35+
. "$TOOLS/lib_apt.sh"
36+
# shellcheck disable=SC1091
37+
. "$TOOLS/lib_docker.sh"
38+
# shellcheck disable=SC1091
39+
. "$TOOLS/lib_hwprobe.sh"
40+
41+
usage() {
42+
cat <<EOF
43+
Usage: $0 [OPTIONS]
44+
45+
Modes:
46+
--mode local|docker Run hw-probe locally or via docker (default: local)
47+
--upload yes|no Upload results to linux-hardware.org (default: no)
48+
--out DIR Directory to save reports/logs (default: ./hw-probe_out)
49+
--extract yes|no Auto-extract saved report into OUT/extracted-<ts> (default: no)
50+
51+
Install / Update:
52+
--install Install hw-probe (latest) if not present
53+
--version VER Install a specific version (implies --install)
54+
--update Update hw-probe to latest available
55+
--deps-only Install only dependencies (no hw-probe)
56+
57+
Cleanup (optional):
58+
--uninstall yes|no After run, uninstall what we installed in this run (hw-probe and/or docker). Default: no
59+
60+
Extra:
61+
--probe-args "ARGS" Extra args passed to hw-probe (both local/docker)
62+
--dry-run Show planned actions without executing installs/runs
63+
--verbose Verbose logging
64+
-h|--help Show help
65+
EOF
66+
}
67+
68+
# Defaults
69+
MODE="local"
70+
UPLOAD="no"
71+
OUT_DIR="./hw-probe_out"
72+
EXTRACT="no"
73+
DO_INSTALL=0
74+
DO_UPDATE=0
75+
LIST_VERS=0
76+
DEPS_ONLY=0
77+
VERSION=""
78+
PROBE_ARGS=""
79+
DRY=0
80+
VERBOSE=0
81+
UNINSTALL="no"
82+
83+
# Parse args
84+
while [ $# -gt 0 ]; do
85+
case "$1" in
86+
--mode) shift; MODE="$1" ;;
87+
--upload) shift; UPLOAD="$1" ;;
88+
--out) shift; OUT_DIR="$1" ;;
89+
--extract) shift; EXTRACT="$1" ;;
90+
--install) DO_INSTALL=1 ;;
91+
--version) shift; VERSION="$1"; DO_INSTALL=1 ;;
92+
--update) DO_UPDATE=1 ;;
93+
--list-versions) LIST_VERS=1 ;;
94+
--deps-only) DEPS_ONLY=1 ;;
95+
--uninstall) shift; UNINSTALL="$1" ;;
96+
--probe-args) shift; PROBE_ARGS="$1" ;;
97+
--dry-run) DRY=1 ;;
98+
--verbose) VERBOSE=1 ;;
99+
-h|--help) usage; exit 0 ;;
100+
*) log_warn "Unknown option: $1"; usage; exit 1 ;;
101+
esac
102+
shift
103+
done
104+
105+
# --- Verbose handling (pre-run): use maximal hw-probe logging if user didn't set it ---
106+
if [ "$VERBOSE" = "1" ]; then
107+
case " $PROBE_ARGS " in
108+
*" -log-level "*|*" -minimal "*|*" -min "*|*" -maximal "*|*" -max "*)
109+
:
110+
;;
111+
*)
112+
PROBE_ARGS="${PROBE_ARGS}${PROBE_ARGS:+ }-log-level maximal"
113+
log_info "Verbose enabled: adding '-log-level maximal' to hw-probe"
114+
;;
115+
esac
116+
fi
117+
118+
# Quick base dependencies
119+
check_dependencies grep sed awk tee || { log_skip "Missing basic tools (grep/sed/awk/tee)"; echo "hw-probe SKIP" > ./hw-probe.res 2>/dev/null; exit 2; }
120+
if [ "$EXTRACT" = "yes" ]; then
121+
check_dependencies tar || { log_fail "tar is required for --extract"; echo "hw-probe FAIL" > ./hw-probe.res 2>/dev/null; exit 1; }
122+
fi
123+
124+
# Sanity: OS
125+
if ! is_debianish; then
126+
log_fail "This tool supports Debian/Ubuntu only."
127+
echo "hw-probe FAIL" > ./hw-probe.res 2>/dev/null
128+
exit 1
129+
fi
130+
131+
# -------- Early network gate (as requested) --------
132+
ONLINE=0
133+
if network_is_ok; then ONLINE=1; fi
134+
log_info "Network: $( [ "$ONLINE" -eq 1 ] && echo online || echo offline )"
135+
136+
# Docker requires network (unless preloaded), but per patch we hard-gate to online only
137+
if [ "$MODE" = "docker" ] && [ "$ONLINE" -ne 1 ]; then
138+
log_skip "Docker mode requires network (to pull image unless preloaded)."
139+
echo "hw-probe SKIP" > ./hw-probe.res
140+
exit 2
141+
fi
142+
143+
# Local offline allowed only if hw-probe already present
144+
if [ "$MODE" = "local" ] && [ "$ONLINE" -ne 1 ] && ! hwprobe_installed; then
145+
log_skip "Offline and hw-probe not installed locally; skipping."
146+
echo "hw-probe SKIP" > ./hw-probe.res
147+
exit 2
148+
fi
149+
150+
# Dry-run wrapper
151+
maybe_run() {
152+
if [ "$DRY" -eq 1 ]; then
153+
log_info "[dry-run] $*"
154+
return 0
155+
fi
156+
sh -c "$*"
157+
}
158+
159+
# Actions
160+
if [ "$LIST_VERS" -eq 1 ]; then
161+
apt_list_versions hw-probe
162+
if [ "$DO_INSTALL" -eq 0 ] && [ "$DO_UPDATE" -eq 0 ]; then
163+
echo "hw-probe PASS" > ./hw-probe.res 2>/dev/null
164+
exit 0
165+
fi
166+
fi
167+
168+
if [ "$DEPS_ONLY" -eq 1 ]; then
169+
log_info "Installing dependencies only..."
170+
if [ "$DRY" -eq 1 ]; then
171+
log_info "[dry-run] would install deps: $HWPROBE_DEPS"
172+
else
173+
apt_ensure_deps "$HWPROBE_DEPS" || true
174+
fi
175+
if [ "$DO_INSTALL" -eq 0 ] && [ "$DO_UPDATE" -eq 0 ] && [ "$MODE" = "local" ]; then
176+
echo "hw-probe PASS" > ./hw-probe.res 2>/dev/null
177+
exit 0
178+
fi
179+
fi
180+
181+
# Install / Update logic (local package path)
182+
if [ "$DO_INSTALL" -eq 1 ]; then
183+
if [ -n "$VERSION" ]; then
184+
if [ "$DRY" -eq 1 ]; then
185+
log_info "[dry-run] would install hw-probe version: $VERSION"
186+
else
187+
hwprobe_install_version "$VERSION" || true
188+
fi
189+
else
190+
if [ "$DRY" -eq 1 ]; then
191+
log_info "[dry-run] would install hw-probe latest"
192+
else
193+
hwprobe_install_latest || true
194+
fi
195+
fi
196+
fi
197+
if [ "$DO_UPDATE" -eq 1 ]; then
198+
if [ "$DRY" -eq 1 ]; then
199+
log_info "[dry-run] would update hw-probe to latest"
200+
else
201+
hwprobe_update || true
202+
fi
203+
fi
204+
205+
# Run
206+
log_info "Mode=$MODE | Upload=$UPLOAD | Out=$OUT_DIR | Extract=$EXTRACT | Online=$ONLINE"
207+
208+
RC=0
209+
if [ "$DRY" -eq 1 ]; then
210+
log_info "[dry-run] would run hw-probe now"
211+
RC=0
212+
else
213+
case "$MODE" in
214+
local)
215+
hwprobe_run_local "$UPLOAD" "$OUT_DIR" "$PROBE_ARGS" "$EXTRACT"
216+
RC=$?
217+
;;
218+
docker)
219+
hwprobe_run_docker "$UPLOAD" "$OUT_DIR" "$PROBE_ARGS" "$EXTRACT"
220+
RC=$?
221+
;;
222+
*)
223+
log_fail "Unknown mode: $MODE"
224+
RC=1
225+
;;
226+
esac
227+
fi
228+
229+
# ----- Post-run uninstall/cleanup as per patch -----
230+
if [ "$MODE" = "docker" ] && [ "$UNINSTALL" = "yes" ]; then
231+
docker_image_prune_hwprobe || true
232+
fi
233+
234+
if [ "$UNINSTALL" = "yes" ] && [ "$MODE" = "local" ]; then
235+
hwprobe_uninstall || true
236+
fi
237+
238+
# (Existing result handling retained)
239+
if [ "$RC" -eq 0 ]; then
240+
log_pass "hw-probe PASS"
241+
echo "hw-probe PASS" > ./hw-probe.res 2>/dev/null
242+
exit 0
243+
elif [ "$RC" -eq 2 ]; then
244+
log_skip "hw-probe SKIP"
245+
echo "hw-probe SKIP" > ./hw-probe.res 2>/dev/null
246+
exit 2
247+
else
248+
log_fail "hw-probe FAIL"
249+
echo "hw-probe FAIL" > ./hw-probe.res 2>/dev/null
250+
exit 1
251+
fi

0 commit comments

Comments
 (0)