forked from PlaceOS/drivers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathharness
More file actions
executable file
·176 lines (151 loc) · 4.13 KB
/
harness
File metadata and controls
executable file
·176 lines (151 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#! /usr/bin/env bash
# `e`: fail script if a command's exitcode is non-zero
# `u`: fail script if a variable is unset (unitialized)
set -eu
# Holds the path to the saved .git worktree pointer while the harness runs.
_WORKTREE_BACKUP=""
# Restore the original git worktree .git file if we replaced it.
# Safe to call even when no replacement was made.
restore_git_worktree() {
if [ -n "${_WORKTREE_BACKUP}" ] && [ -f "${_WORKTREE_BACKUP}" ]; then
rm -rf "${PWD}/.git"
mv "${_WORKTREE_BACKUP}" "${PWD}/.git"
_WORKTREE_BACKUP=""
fi
}
# The test-harness needs a real git repo to read the current commit hash.
# When running inside a git worktree the .git entry is a file (not a directory)
# that points to an absolute path the container cannot see. Detect this and
# temporarily replace it with a self-contained repo for the duration of the run.
setup_git_for_harness() {
if [ -f "${PWD}/.git" ]; then
echo '░░░ Git worktree detected, creating temporary repo for harness...'
_WORKTREE_BACKUP="${PWD}/.git.worktree-bak"
cp "${PWD}/.git" "${_WORKTREE_BACKUP}"
rm "${PWD}/.git"
git -C "${PWD}" init -q
git -C "${PWD}" add -A
git -C "${PWD}" commit -q -m "temp: harness run"
fi
}
# Always restore the worktree pointer on exit (covers errors and Ctrl-C).
trap restore_git_worktree EXIT
say_done() {
printf "░░░ Done.\n"
}
fail() {
echo "${@}" >&2
exit 1
}
# Called when Ctrl-C is sent
function trap_ctrlc ()
{
echo ">~"
echo "░░░ Cleaning up..."
down
exit 2
}
trap "trap_ctrlc" 2
up() {
echo '░░░ PlaceOS Driver Harness'
echo '░░░ -> Pulling latest code...'
git pull
echo '░░░ -> Pulling latest images...'
docker compose pull
echo '░░░ -> Starting environment...'
docker compose up -d
printf "░░░ The harness can be found at http://localhost:8085/index.html\n"
echo '░░░ Stop the harness with `harness down`'
say_done
}
down() {
echo '░░░ Stopping PlaceOS Driver Harness...'
docker compose down --remove-orphans &> /dev/null
say_done
}
format() {
echo '░░░ Running `crystal tool format` over `drivers` and `repositories`'
docker compose run \
-v "${PWD}/drivers:/wd/drivers" \
-v "${PWD}/repositories:/wd/repositories" \
--no-deps \
--rm \
install-shards \
crystal tool format
say_done
}
report() {
setup_git_for_harness
echo '░░░ PlaceOS Driver Compilation Report'
echo '░░░ Pulling images...'
docker compose pull &> /dev/null
# Ensure shards are satisfied before running the report
echo '░░░ Installing shards...'
docker compose run \
--rm \
install-shards > /dev/null
echo '░░░ Starting environment...'
docker compose up -d &> /dev/null
exit_code=0
echo '░░░ Starting report...'
docker exec placeos-drivers report $@ || exit_code=$?
down
exit ${exit_code}
}
build() {
docker compose run \
--rm \
--no-deps \
-v "${PWD}/repositories:/app/repositories" \
-v "${PWD}/drivers:/app/repositories/drivers" \
--entrypoint="/app/scripts/entrypoint.sh" \
build build $@
}
usage() {
cat <<EOF
Usage: harness [-h|--help] [command]
Helper script for interfacing with the PlaceOS Driver spec runner
Command:
report check all drivers' compilation status
up starts the harness
down stops the harness
build builds drivers and uploads them to S3
format formats driver code
help display this message
EOF
}
if [ $# -eq 0 ]
then
usage
exit 1
fi
command="$1"
shift
case $command in
report)
report $@
;;
build)
build $@
;;
up)
up
;;
down)
down
;;
format)
format
;;
-h|--help|help)
usage
;;
*)
if [ -n "$command" ]; then
fail "Unknown command: $command"
else
usage
exit 1
fi
;;
esac