-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsetup-sb2-images.sh
executable file
·416 lines (363 loc) · 12.6 KB
/
setup-sb2-images.sh
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#!/bin/bash
#
# Create installer archives from sb2 tooling/target images in tar.bz2 format
#
# Copyright (C) 2018 Jolla Ltd.
# Contact: Martin Kampas <[email protected]>
# All rights reserved.
#
# You may use this file under the terms of BSD license as follows:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Jolla Ltd nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
. $(dirname $0)/defaults.sh
. $(dirname $0)/utils.sh
set -o nounset
set -o pipefail
synopsis()
{
cat <<END
usage: setup-sb2-images.sh [-c|--compression <num>] [-n|--dry-run]
[-P|--max-procs <limit>] [--no-meta] [--no-shared]
[--shared-path <path>] [-u|--upload <dir>]
[--uhost <hostname>] [--uuser <username>] [--upath <path>]
<image.tar.bz2>...
END
}
short_usage()
{
cat <<END
$(synopsis)
Try 'setup-sb2-images.sh --help' for more information.
END
}
usage()
{
less --quit-if-one-screen <<END
$(synopsis)
Convert the given sb2 tooling/target images in tar.bz2 format to a format
suitable for use with both SDK Control Centre and SDK installer. Create
.md5sum files and unless the '--no-meta' option is used, create the
corresponding .meta files with 'make-archive-meta.sh' as well. The resulting
files will be placed next to the original files and optionally uploaded to the
build host. When uploaded, the targets.json in the destination directory will
be updated, listing all images found in that directory, i.e., including those
uploaded before.
OPTIONS
-c, --compression <num>
Set the 7z compression level from 0 (fast) to 9 (best)
-n, --dry-run
Suppress normal operation, show what would be done
-P, --max-procs
Limit the maximum number of processors to use. Defaults to N-1
available processors or 1 on single processor systems
--no-meta
Suppress creating meta data files with 'make-archive-meta.sh'
--no-shared
See '--upload'.
--shared-path <path>
See '--upload'. Relative <path> will be resolved relatively to
'--upath'. Defaults to [$DEF_SHARED_SB2_IMAGES_PATH]
-u, --upload <dir>
Upload results. <dir> is the root directory for this SDK build,
relative to the global upload path. Files will be uploaded under the
'<dir>/targets' path. If this path does not exists, a symbolic link
will be created using this name, pointing to the shared location for
sb2 images. This can be overriden by '--no-shared', in which case a
directory will be created instead of the symbolic link. The default
shared location can be overriden with '--shared-path'.
--uhost <hostname>
Override the default upload host [$DEF_UPLOAD_HOST]
--uuser <username>
Override the default upload user [$DEF_UPLOAD_USER]
--upath <path>
Override the default global upload path [$DEF_UPLOAD_PATH]
END
}
fatal()
{
echo "setup-sb2-images.sh: fatal: $*" >&2
}
bad_usage()
{
fatal "$*"
short_usage >&2
}
xargs_()
{
xargs --max-procs="$OPT_MAX_PROCS" "$@"
}
recompress()
{
local files=("$@")
recompress_worker_initial_args=("$OPT_LEVEL" "$OPT_NO_META" "$BUILD_TOOLS_SRC" "$OPT_DRY_RUN")
recompress_worker()
{
set -o nounset
set -o pipefail
local level=$1
local no_meta=$2
local build_tools_src=$3
local dry_run=$4
local file=$5
local dirname=
dirname=$(dirname "$file") || return
local basename=
basename=$(basename "$file") || return
local decompressed_basename=${basename%.bz2}
local ok=
recompress_worker_cleanup()
(
trap 'echo cleaning up...' INT TERM HUP
if [[ ! $dry_run ]]; then
rm -f "$dirname/$decompressed_basename"
rm -f "$dirname/$decompressed_basename.meta"
if [[ ! $ok ]]; then
rm -f "$dirname/$decompressed_basename.7z"
rm -f "$dirname/$decompressed_basename.7z.meta"
fi
fi
)
trap 'recompress_worker_cleanup; trap - RETURN' RETURN
trap 'return 1' INT TERM HUP
_ pushd "$dirname" >/dev/null || return
echo "Decompressing '$dirname/$basename'..." >&2
# Pass --force to allow working with input files that are symlinks
_ bunzip2 --keep --force -- "$basename" || return
echo "Finished decompressing '$dirname/$basename'" >&2
basename=$decompressed_basename
echo "Creating meta data file for '$dirname/$basename'" >&2
_ $build_tools_src/make-sb2-image-meta.sh "$basename" || return
echo "Creating archive '$dirname/$basename.7z'..." >&2
_ rm -f -- "$basename.7z" || return
_ 7z a ${level:+-mx="$level"} -- "$basename.7z" "$basename" >/dev/null || return
_ 7z a ${level:+-mx="$level"} -- "$basename.7z" "$basename.meta" >/dev/null || return
_ rm -f -- "$basename" || :
echo "Finished creating archive '$dirname/$basename.7z'" >&2
if [[ ! $no_meta ]]; then
echo "Creating meta data file for '$dirname/$basename.7z" >&2
_ $build_tools_src/make-archive-meta.sh "$basename.7z" || return
fi
md5sum() { command md5sum "$1" > "$2"; }
_ md5sum "$basename.7z"{,.md5sum}
_ popd >/dev/null || return
ok=1
}
export -f recompress_worker || return
printf '%s\n' "${files[@]}" \
|xargs_ -L1 bash -c 'recompress_worker "$@"' bash "${recompress_worker_initial_args[@]}"
}
set_defaults()
{
# Use up to N-1 available processors by default
DEF_MAX_PROCS=
DEF_MAX_PROCS=$(nproc)
let DEF_MAX_PROCS--
if (( DEF_MAX_PROCS <= 0 )); then
DEF_MAX_PROCS=1
fi
OPT_H=
OPT_HELP=
OPT_DRY_RUN=
OPT_IMAGES=()
OPT_LEVEL=
OPT_MAX_PROCS=$DEF_MAX_PROCS
OPT_NO_META=
OPT_NO_SHARED=
OPT_SHARED_PATH=$DEF_SHARED_SB2_IMAGES_PATH
OPT_UPLOAD=
OPT_UPLOAD_DIR=
OPT_UPLOAD_HOST=$DEF_UPLOAD_HOST
OPT_UPLOAD_PATH=$DEF_UPLOAD_PATH
OPT_UPLOAD_USER=$DEF_UPLOAD_USER
TARGETS_SUBDIR=targets
OPT_TARGETS_UPLOAD_PATH=$OPT_UPLOAD_PATH/$OPT_UPLOAD_DIR/$TARGETS_SUBDIR
}
parse_opts()
{
while (( $# > 0 )); do
case $1 in
-h)
OPT_H=1
return
;;
--help)
OPT_HELP=1
return
;;
-c|--compression)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_LEVEL=$2
shift
;;
-n|--dry-run)
OPT_DRY_RUN=1
;;
-P|--max-procs)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_MAX_PROCS=$2
shift
;;
--no-meta)
OPT_NO_META=1
;;
--no-shared)
OPT_NO_SHARED=1
;;
--shared-path)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_SHARED_PATH=$2
shift
;;
-u|--upload)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_UPLOAD=1
OPT_UPLOAD_DIR=$2
shift
;;
--uhost)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_UPLOAD_HOST=$2
shift
;;
--upath)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_UPLOAD_PATH=$2
shift
;;
--uuser)
if [[ ! ${2:-} ]]; then
bad_usage "Argument expected: $1"
return 1
fi
OPT_UPLOAD_USER=$2
shift
;;
--)
shift
break
;;
-*)
bad_usage "Unexpected argument: $1"
return 1
;;
*)
OPT_IMAGES+=("$1")
;;
esac
shift
done
if [[ $OPT_SHARED_PATH && $OPT_SHARED_PATH != /* ]]; then
OPT_SHARED_PATH=$OPT_UPLOAD_PATH/$OPT_SHARED_PATH
fi
OPT_TARGETS_UPLOAD_PATH=$OPT_UPLOAD_PATH/$OPT_UPLOAD_DIR/$TARGETS_SUBDIR
OPT_IMAGES+=("$@")
if (( ${#OPT_IMAGES[@]} == 0 )); then
bad_usage "Argument expected"
return 1
fi
}
setup_dry_run()
{
if [[ $OPT_DRY_RUN ]]; then
OPT_MAX_PROCS=1
_() { printf '%q ' + "$@"; echo; } >&2
else
_() { "$@"; }
fi
export -f _
}
main()
{
set_defaults || return
parse_opts "$@" || return
setup_dry_run || return
if [[ $OPT_H ]]; then
short_usage
return
fi
if [[ $OPT_HELP ]]; then
usage
return
fi
local decompressed_images=()
main_cleanup()
(
trap 'echo cleaning up...' INT TERM HUP
if [[ ! $OPT_DRY_RUN ]]; then
rm -f "${decompressed_images[@]}"
fi
)
trap 'main_cleanup; trap - RETURN' RETURN
trap 'return 1' INT TERM HUP
decompressed_images=("${OPT_IMAGES[@]%.bz2}")
recompress "${OPT_IMAGES[@]}" || return
if [[ $OPT_UPLOAD ]]; then
local results=("${decompressed_images[@]/%/.7z}" "${decompressed_images[@]/%/.7z.md5sum}")
if [[ ! $OPT_NO_META ]]; then
results+=("${decompressed_images[@]/%/.7z.meta}")
fi
echo "Uploading..." >&2
if [[ $OPT_NO_SHARED ]]; then
_ ssh "$OPT_UPLOAD_USER@$OPT_UPLOAD_HOST" "mkdir -p $OPT_TARGETS_UPLOAD_PATH" || return
else
local target=$(realpath --canonicalize-missing \
--relative-to="$OPT_UPLOAD_PATH/$OPT_UPLOAD_DIR" "$OPT_SHARED_PATH")
_ ssh "$OPT_UPLOAD_USER@$OPT_UPLOAD_HOST" "test -e $OPT_TARGETS_UPLOAD_PATH \
|| (mkdir -p $OPT_UPLOAD_PATH/$OPT_UPLOAD_DIR \
&& ln -s $target $OPT_TARGETS_UPLOAD_PATH)" || return
# Remove possibly existing targets.json to avoid being noisy updating it twice
_ ssh "$OPT_UPLOAD_USER@$OPT_UPLOAD_HOST" "rm -f $OPT_TARGETS_UPLOAD_PATH/targets.json"
echo "Looking for possible unused images on the upload host..." >&2
_ "$BUILD_TOOLS_SRC/prune-shared.sh" --uhost "$OPT_UPLOAD_HOST" \
--uuser "$OPT_UPLOAD_USER" --upath "$OPT_UPLOAD_PATH" "$OPT_SHARED_PATH"
fi
_ rsync --ignore-existing --verbose --info=skip "${results[@]}" \
"$OPT_UPLOAD_USER@$OPT_UPLOAD_HOST:$OPT_TARGETS_UPLOAD_PATH/" || return
if [[ ! $OPT_DRY_RUN ]]; then
echo "Updating targets.json..." >&2
update_remote_targets_json "$OPT_TARGETS_UPLOAD_PATH" || return
else
echo "Would update targets.json" >&2
fi
fi
}
main "$@"