-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·273 lines (208 loc) · 6.84 KB
/
run
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#/usr/bin/env sh
# This file is part of reproducible-rust. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/reproducible-rust/master/COPYRIGHT. No part of reproducible-rust, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2021 The developers of reproducible-rust. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/reproducible-rust/master/COPYRIGHT.
set -e
set -u
set -f
_program_path_find()
{
if [ "${0%/*}" = "$0" ]; then
# We've been invoked by the interpreter as, say, bash program
if [ -r "$0" ]; then
pwd -P
# Clutching at straws; probably run via a download, anonymous script, etc, weird execve, etc
else
printf '\n'
fi
else
# We've been invoked with a relative or absolute path (also when invoked via PATH in a shell)
_program_path_find_parentPath()
{
parentPath="${scriptPath%/*}"
if [ -z "$parentPath" ]; then
parentPath='/'
fi
cd "$parentPath" 1>/dev/null
}
# pdksh / mksh have problems with unsetting a variable that was never set...
if [ "${CDPATH+set}" = 'set' ]; then
unset CDPATH
fi
if command -v realpath 1>/dev/null 2>/dev/null; then
(
scriptPath="$(realpath "$0")"
_program_path_find_parentPath
pwd -P
)
elif command -v readlink 1>/dev/null 2>/dev/null; then
(
local recursionDepth=0
_program_path_resolve_symlinks_recursively()
{
local unresolvedPath="$1"
recursionDepth=$((recursionDepth + 1))
if [ $recursionDepth -gt 10 ]; then
printf '%s\n' 'Recursion to depths greater than 10 is not allowed when resolving links.'
return 1
fi
local potentialLinkDestination="$(readlink -- "$unresolvedPath")"
if [ -z "$potentialLinkDestination" ]; then
scriptPath="$unresolvedPath"
return 0
fi
local linkDestination="$potentialLinkDestination"
local parentFolderPath="${unresolvedPath%/*}"
if [ "$parentFolderPath" = "$unresolvedPath" ]; then
_program_path_resolve_symlinks_recursively "$linkDestination"
else
case "$linkDestination" in
/*)
_program_path_resolve_symlinks_recursively "$linkDestination"
;;
*)
_program_path_resolve_symlinks_recursively "$parentFolderPath"/"$linkDestination"
;;
esac
fi
}
scriptPath="$0"
_program_path_resolve_symlinks_recursively "$scriptPath"
_program_path_find_parentPath
pwd -P
)
else
# This approach will fail in corner cases where the script itself is a symlink in a path not parallel with the concrete script
(
scriptPath="$0"
_program_path_find_parentPath
pwd -P
)
fi
fi
}
cd "$(_program_path_find)" 1>/dev/null 2>/dev/null
. functions/common.sh
. functions/common.outside-docker.sh
. functions/docker.sh
guard_arguments()
{
local message="Specify an ARCHITECTURE and CONTAINER to build, eg ./build.sh x86_64 base; ARCHITECTURE is aarch64, powerpc64le, riscv64, s390x or x86_64 (not a Docker platform name, which is derived)"
case $# in
0|2)
exit_usage_message "$message"
;;
1)
case "$1" in
-h|-help|--help|help)
exit_help_message "$message"
;;
*)
exit_usage_message "$message"
;;
esac
;;
*)
architecture="$1"
image="$2"
container="$3"
validate_architecture "$architecture"
arguments_shift=3
if [ $# -gt 3 ]; then
if [ "$4" = '--' ]; then
arguments_shift=4
fi
fi
;;
esac
}
depends head
get_image_identifier_value()
{
image_identifier_value="$(head -n 1 "$output_image_identifier_file_path")"
}
depends rm mkdir
set_container_output_paths()
{
local container_output_folder_path="$output_folder_path"/container/"$image"/"$container"
rm -rf "$container_output_folder_path"
mkdir -m 0700 -p "$container_output_folder_path"
container_identifier_file_path="$output_folder_path"/container/"$image"/"$container"/container_identifier.value
if [ -e "$container_identifier_file_path" ]; then
exit_can_not_create_message "Container $container is already running or did not stop cleanly"
fi
clean_folder "$container_output_folder_path"
}
set_container_configuration_paths()
{
container_configuration_folder_path="$source_folder_path"/container/"$image"/"$container"
exit_if_folder_missing "$container_configuration_folder_path"
docker_container_environment_variables_file_path="$container_configuration_folder_path"/environment-variables.list
exit_if_configuration_file_missing "$docker_container_environment_variables_file_path"
docker_options_file_path="$container_configuration_folder_path"/options.sh
exit_if_configuration_file_missing "$docker_options_file_path"
}
depends sh docker rm
run_using_docker()
{
# Prefixes '$@' with options generated by the container's `options.sh`.
{
local options_output_file_path="$TMPDIR"/options-output
sh "$docker_options_file_path" "$architecture" "$image" "$container" "$container_configuration_folder_path" >"$options_output_file_path"
command_tac "$options_output_file_path" >"$options_output_file_path".reversed
set -- "$image_identifier_value" "$@"
local option_a option_b
while IFS=' ' read -r option_a option_b in
do
case "$option_a" in
'')
continue
;;
'#'*)
continue
;;
*)
:
;;
esac
if [ -n "$option_b" ]; then
set -- "$option_a" "$option_b" "$@"
else
set -- "$option_a" "$@"
fi
done <"$options_output_file_path".reversed
rm "$options_output_file_path" "$options_output_file_path".reversed
}
docker run --rm --isolation default --platform "$docker_platform" --cidfile "$container_identifier_file_path" --env-file "$docker_container_environment_variables_file_path" "$@"
printf '\n' >>"$container_identifier_file_path"
# If `--rm` is not specified in docker run command above we can remove this line.
rm "$container_identifier_file_path"
}
main()
{
local arguments_shift
local docker_platform
local architecture
local container
guard_arguments "$@"
shift $arguments_shift
./build "$architecture" "$image"
local output_folder_path
local source_folder_path
local temporary_folder_path
set_paths
make_temporary_folder
local image_output_folder_path
local output_image_identifier_file_path
local git_describe_file_path
set_image_output_paths
local image_identifier_value
get_image_identifier_value
local container_identifier_file_path
set_container_output_paths
local container_configuration_folder_path
local docker_container_environment_variables_file_path
local docker_options_file_path
set_container_configuration_paths
run_using_docker "$@"
}
main "$@"