-
Notifications
You must be signed in to change notification settings - Fork 14
/
sdk-manage
executable file
·2067 lines (1782 loc) · 58.7 KB
/
sdk-manage
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# This is a helper script for the Mer SDK to manage sb2 target and
# toolchain installation
set -o nounset
shopt -s extglob
# Make sure normal users can use any dirs we make
umask 022
SELF=$(basename "$0")
____=${SELF//?/ }
usage() {
cat >&2 <<EOF
Try '$SELF --help' for more information.
EOF
}
help() {
cat >&2 <<EOF
usage:
$SELF tooling list [-l|--long]
$SELF tooling upgradable <name>
$SELF tooling install <name> <url>
$SELF tooling remove <name>
$SELF tooling refresh {--all | <name>...}
$SELF tooling update <name>
$SELF tooling register [--user <user> [--password <password>]] [--force]
$____ {--all | <name>...}
$SELF tooling uuidgen {--all | <name>...}
$SELF target list [-l|--long] [--tooling <name>] [--snapshots-of <name>]
$____ [--check-snapshots]
$SELF target upgradable <name>
$SELF target install <name> <url> [--tooling <name>
$____ [--tooling-url <url>]] [--toolchain <name>]
$SELF target snapshot [-r|--reset[={outdated|force}]] <original> <snapshot>
$SELF target reserve <original> <snapshot-template> <lock-file> <pool-size>
$SELF target remove <name>
$SELF target refresh {--all | <name>...}
$SELF target update <name>
$SELF target sync <name>
$SELF target import <name>
$SELF target register [--user <user> [--password <password>]] [--force]
$____ {--all | <name>...}
$SELF target uuidgen {--all | <name>...}
$SELF toolchain list <tooling>
$SELF toolchain install <tooling> <name>...
$SELF toolchain remove <tooling> <name>...
$SELF develpkg list <target> [<search string>]
$SELF develpkg install <target> <name>...
$SELF develpkg remove <target> <name>...
$SELF sdk version
$SELF sdk refresh
$SELF sdk upgradable
$SELF sdk upgrade
$SELF sdk status
$SELF sdk register [--user <user> [--password <password>]] [--force]
$SELF refresh-all
$SELF register-all [--user <user> [--password <password>]] [--force]
$SELF --version
Usage notes
$SELF target list
The '--long' format uses the following columns:
TARGET NAME
TOOLING NAME
- or '-' if the target does not use a tooling
MODE
- see '--mode'
ORIGINAL TARGET NAME
- or '-' if this is not a snapshot
- immediatelly followed with '*' if the snapshot is outdated and
checking snapshots was requested with '--check-snapshots'
$SELF target install
When tooling selection is forced with '--tooling' without passing
'--tooling-url', the selected tooling MUST exist. If '--tooling-url' is
passed as well, the tooling MAY NOT exist, in which case it will be
installed automatically prior to target installation.
$SELF target snapshot
Snapshot targets are like normal targets in most respects. When the
'--reset[={outdated|force}]' option is used, it is not an error when
the target named <snapshot> already exists, provided that it is a
snapshot target that was taken from the specified <original> target.
This defaults to the 'outdated' behavior, in which case the snapshot
will be reset only when the original target was updated (RPM database
changed) after the snapshot was taken. A snapshot can be reset
unconditionally by passing 'force' instead. Resetting a snapshot has
the same effect as removing the snapshot and creating it again, except
that it preserves it's internal state and is more efficient.
$SELF target reserve
Reserve a pooled snapshot of the target identified with <original>.
The name for the snapshot will be printed on stdout. It will be
determined using <snapshot-template>, which is expected to contain at
least 3 consecutive 'X's in its last dot-separated component. The
snapshot will be reserved for as long as the <lock-file> remains
locked. <pool-size> serves also as a limit for the number of
snapshots that can be reserved. Only snapshots using the given
<snapshot-template> are considered for this purpose. The
least-recently-used approach is applied to choose from pooled
snapshots unless a snapshot exists with the same <lock-file> recently
used.
Global options
-m, --mode <mode>
Accepts one of 'user' or 'installer'. By default the user mode is
active. Targets and toolings installed in the installer mode cannot be
removed in the user mode. This option is not meant to be used by
regular users.
For more information see https://sailfishos.org/wiki/Platform_SDK
EOF
}
if [[ $EUID -eq 0 ]]; then
echo >&2 "WARNING: Invoking $0 as root is deprecated."
if [[ -n ${SUDO_USER:-} ]]; then
exec sudo -i -u $SUDO_USER $0 "$@"
echo >&2 "sudo as '$SUDO_USER' failed"
exit 1
else
echo >&2 "Cannot determine Mer SDK user. Invoke $0 as non-root to fix this."
exit 1
fi
fi
################################################################'
# Common utilities
# When sb2 is used outside of home directory CWD mapping may not be possible.
# This would break e.g. `sdk-manage target install <pkg>` when invoked outside
# of home directory - zypper would fail. The `cd` is intentionally
# unconditional, so that eventual issues are discovered ASAP.
sb2() ( cd; command sb2 "$@" )
print_array() {
local array=("$@")
declare -p array |sed 's/^[^=]*=//'
}
# Unpack result of `print_array` to variables passed as second and following args
unpack() {
(eval local -a array=$1) 2>/dev/null || return
eval local -a array=$1
shift
local i=0
while [[ $# -gt 0 ]]; do
if [[ $(declare -p "$1") == "declare -a "* ]]; then
eval local -a array_item=${array[i++]}
eval $1=\(\${array_item[@]:+"\${array_item[@]}"}\)
else
eval $1=\${array[i++]}
fi
shift
done
}
# Suppress command's stderr unless it exits with non-zero
silent() {
local stderr=
{ stderr=$("$@" 3>&1 1>&2 2>&3 3>&-); } 2>&1
local rc=$?
[[ $rc -eq 0 ]] || printf >&2 "%s\n" "$stderr"
return $rc
}
assert_name_valid() {
local name=$1
if ! [[ $name ]]; then
echo >&2 "Name cannot be an empty string"
return 1
fi
if ! [[ $name =~ ^[[:alnum:]_.-]*$ ]]; then
echo >&2 "Name cannot consist of non-alphanumeric characters other than '_-.': '$name'"
return 1
fi
if ! [[ $name =~ ^[[:alnum:]] ]]; then
echo >&2 "Name must start with an alphanumeric character: '$name'"
return 1
fi
}
# Downloads file at ${url} into ${tmp_dir} using ${local_file_hint} as file name, unless ${url}
# points to a local file already, in which case the file is to be used directly.
#
# On success prints array with following items:
# - local file path
# - non-empty if the file was actually downloaded
#
# Returns non-zero on failure.
download() {
local url=$1 tmp_dir=$2 local_file_hint=$3
local local_file= downloaded=
url=${url#file://}
local_file_hint+=.tar.${url##*.}
if ! [[ $url =~ ^(http|ftp)s?:// ]]; then
local_file=$(readlink -f "$url") || return
print_array "$local_file" "$downloaded"
return
fi
local_file=$tmp_dir/$local_file_hint
local orig_downloaded_file=$tmp_dir/${url##*/}
local md5file="$orig_downloaded_file.md5sum"
local md5sum_failed=
local suceeded=
download_cleanup() (
trap 'echo cleaning up...' INT TERM HUP
rm -f "$md5file"
if [[ ! $succeeded ]]; then
rm -f "$orig_downloaded_file"
rm -f "$local_file"
fi
)
trap 'download_cleanup; trap - RETURN' RETURN
trap 'return 1' INT TERM HUP
echo >&2 "Downloading '${url##*/}'"
curl "$url" -o "$orig_downloaded_file"
curl_status=$?
if [[ $curl_status -eq 0 ]] ; then
# check if md5sum file exists
if curl --output /dev/null --silent --head --fail "$url.md5sum"; then
# md5sum exists, let's check it
curl --silent "$url.md5sum" > "$md5file"
if ! (cd "$tmp_dir" && md5sum --status -c "$md5file"); then
echo >&2 "ERROR: md5sum check failed for file '$orig_downloaded_file'!"
md5sum_failed=1
else
echo >&2 "INFO: md5sum matches - download ok"
fi
else
echo >&2 "WARNING: No md5sum file found - can not verify file integrity."
fi
fi
if [[ $curl_status -ne 0 || ! -f $orig_downloaded_file || $md5sum_failed ||
$(stat -c %s "$orig_downloaded_file") -lt 10000 ]]; then
echo >&2 "Download failed!"
return 1
fi
if ! [[ $orig_downloaded_file -ef $local_file ]]; then
mv -f "$orig_downloaded_file" "$local_file" || return
fi
downloaded=1
print_array "$local_file" "$downloaded"
succeeded=1
}
maybe_decompress_tarball() {
local file=$1
local mime=
mime=$(file --brief --mime-type "$file")
if [[ $mime == application/x-tar ]]; then
cat "$file"
else
7z x -so "$file"
fi
}
# Extends standard ssu CLI with '--root <dir>' option and 'arch' command
ssu() (
while [[ $# -gt 0 ]]; do
case $1 in
--root)
shift
export SSU_SANDBOX_DIR=$1
export LD_PRELOAD=/usr/lib/ssu/libsandboxhook.so
;;
arch)
sed -n 's/^arch=//; T; p' ${SSU_SANDBOX_DIR:-}/etc/ssu/ssu.ini
return
;;
*)
break
;;
esac
shift
done
silent command ssu "$@"
)
disk_full() [[ $(stat --file-system --format '%a*%s/1024' "$1") -lt 10000 ]]
# Replace 'shell' field in `getent passwd` output with /bin/bash
fix_shell() {
cut -d: -f1-6 |sed 's,$,:/bin/bash,'
}
manage_user_targets_repository() {
$USER_TARGETS_REPOSITORY/manage.sh --repository "$USER_TARGETS_REPOSITORY" "$@"
}
sudo_sync_trees() {
local src=$1 dst=$2
local dst_existed=$([[ ! -e "$dst" ]] && echo 1)
# TODO Currently our 'cp' does not support --reflink, so right now we will
# just act here as if the clonefile(2) call was not supported by the filesystem.
local can_reflink=
if [[ $can_reflink ]]; then
sudo cp -a --reflink=always --no-target-directory "$src" "$dst" || return
fi
if [[ $dst_existed || ! $can_reflink ]]; then
sudo rsync -a --delete "$src/" "$dst" || return
fi
}
assert_template_valid() {
local template=$1
if ! [[ $template =~ \.XXXX*$ ]]; then
echo >&2 "Expected name with at least 3 consecutive 'X's in its last component." \
"Got '$template'"
return 1
fi
}
name_matches_template() {
local name=$1 template=$2
assert_template_valid "$template" || return
local prefix=${template%.*}
local suffix=${template##*.}
[[ $name =~ ^"$prefix."${suffix//?/.}$ ]]
}
random_name() {
local template=$1 existing_names=$(cat)
assert_template_valid "$template" || return
local prefix=${template%.*}
local suffix_length=$((${#template} - ${#prefix} - 1))
local suffix= name=
while true; do
suffix=$(LC_ALL=C tr -dc A-Z0-9 </dev/urandom \
|dd bs=1 count="$suffix_length" 2>/dev/null) || return
name=$prefix.$suffix
if ! grep -q -F --line-regexp "$name" <<<"$existing_names"; then
break
fi
done
echo "$name"
}
is_locked() {
local lock_file=$1
[[ -e $lock_file ]] && ! flock --nonblock 0 <"$lock_file"
}
################################################################'
# Polymorphic utilities for common operations on targets, toolings and the SDK itself.
#
# Notice the use of the terms 'object' and 'type'.
get_all_of_type() {
local type=$1
get_all_of_type_$type
}
get_all_or_selected_of_type() {
local type=$1
shift
local objects= object= get_all=
if [[ ${1:-} == "--all" ]]; then
objects=($(get_all_of_type "$type")) || return
get_all=1
shift
else
objects=("$@")
for object in "${objects[@]}"; do
assert_object_exists "$type:$object" || return
done
fi
# Fail here if --all was not used and no objects were given
[[ ${#objects[*]} -ne 0 || $get_all ]] || { usage; return 1; }
echo -n "${objects[@]}"
}
print_object() {
local type=${1%:*} object=${1#*:}
case $type in
sdk) echo -n "SDK";;
*) echo -n "$type '$object'";;
esac
}
assert_object_exists() {
local type=${1%:*} object=${1#*:}
assert_${type}_exists "$object"
}
enter() {
local type=${1%:*} object=${1#*:} cmd=("${@:2}")
if [[ $type == sdk ]]; then
sudo "${cmd[@]}"
else
enter_$type "$object" "${cmd[@]}"
fi
}
object_config_file()
{
local type=${1%:*} object=${1#*:}
${type}_config_file "$object"
}
object_config_get()
{
local object=$1 key=$2 default=${3:-}
local config_file=$(object_config_file "$object")
local value=
if [[ -e "$config_file" ]]; then
value=$(sed -n "s/^$key=//p" "$config_file") || return
fi
echo "${value:-$default}"
}
object_config_set()
{
local type=${1%:*} object=${1#*:} key=$2 value=$3
local config_file=$(object_config_file "$type:$object")
local maybe_sudo=
if [[ $type != target ]]; then
maybe_sudo=sudo
fi
# The following sed command needs at least one line
$maybe_sudo tee -a "$config_file" <<<'' >/dev/null || return
$maybe_sudo sed -i -e "1i $key=$value" -e "/^$key=/d" -e '/^$/d' "$config_file"
}
get_object_mode()
{
local object=$1
local mode=
object_config_get "$object" "$MODE_KEY" "$MODE_USER"
}
set_object_mode()
{
local object=$1 value=$2
object_config_set "$object" "$MODE_KEY" "$value"
}
object_zypper() {
local object=$1 args=("${@:2}")
enter "$object" zypper --non-interactive "${args[@]}"
}
register_object() {
local type=$1
shift
local credentials= username= password= other=()
credentials=$(get_register_credentials "$@") || return
unpack "$credentials" username password other
local force= i=
for ((i=0; i < ${#other[@]}; ++i)); do
if [[ ${other[i]} == --force ]]; then
force=1
unset other[i]
break
fi
done
set -- ${other[@]:+"${other[@]}"}
local objects=
if [[ $type == sdk ]]; then
[[ $# -eq 0 ]] || { usage; return 1; }
objects=('')
else
objects=($(get_all_or_selected_of_type "$type" "$@")) || return
fi
local object= domain=
for object in "${objects[@]}"; do
object=$type:$object
domain=$(enter "$object" sdk-register -d) || return
if [[ $domain != jolla && ! $force ]]; then
echo >&2 "Registration not needed for $(print_object "$object") (domain: ${domain:-empty})"
continue
fi
echo >&2 -n "$(print_object "$object"): "
enter "$object" sdk-register -u "$username" -p "$password" || return
done
}
register_all() {
local credentials= username= password= other=()
credentials=$(get_register_credentials "$@") || return
unpack "$credentials" username password other
set -- ${other[@]:+"${other[@]}"}
local force=
while [[ $# -gt 0 ]]; do
case $1 in
--force)
force=1
;;
*)
echo >&2 "unrecognized option '$1'"
usage
return 1
;;
esac
shift
done
set -- --user "$username" --password "$password" ${force:+--force}
manage_sdk register "$@" || return
manage_toolings register "$@" --all || return
manage_targets register "$@" --all
}
refresh_objects() {
local type=$1
shift
local objects=
if [[ $type == sdk ]]; then
objects=('')
else
objects=($(get_all_or_selected_of_type "$type" "$@")) || return
fi
local object=
for object in "${objects[@]}"; do
object_zypper "$type:$object" refresh -f
done
}
object_upgradable() {
local type=$1 object=${2:-}
if [[ $type != sdk ]]; then
assert_object_exists "$type:$object" || return
fi
object_zypper "$type:$object" --quiet --no-refresh list-updates
}
upgrade_object() {
local type=$1 object=${2:-}
if [[ $type != sdk ]]; then
assert_object_exists "$type:$object" || return
fi
# Rebuild RPM database prior to and after upgrading a target to prevent "Database environment
# version mismatch" errors e.g. after upgrading RPM
if [[ $type == target ]]; then
# Ignore possible errors here in hope that they will disappear after the upgrade
enter_target "$object" rpm --rebuilddb
fi
object_zypper "$type:$object" dup || return
if [[ $type == target ]]; then
enter_target "$object" rpm --rebuilddb || return
fi
}
init_machine_id() {
local type=$1 object=$2
if [[ $type == sdk ]]; then
echo >&2 "init_machine_id: not intented to be used on sdk"
return 1
fi
echo >&2 "Initializing machine ID for $type '$object'"
if enter "$type:$object" test -s /etc/machine-id; then
echo >&2 "NOTICE: removing unexpected non-empty /etc/machine-id in the '$object' $type"
enter "$type:$object" rm -f /etc/machine-id || return
fi
if enter "$type:$object" test -s /var/lib/dbus/machine-id; then
echo >&2 "NOTICE: removing unexpected non-empty /var/lib/dbus/machine-id in the '$object' $type"
enter "$type:$object" rm -f /var/lib/dbus/machine-id || return
fi
enter "$type:$object" systemd-machine-id-setup || return
enter "$type:$object" dbus-uuidgen --ensure || return
}
reset_machine_id() {
local type=$1
shift
if [[ $type == sdk ]]; then
echo >&2 "reset_machine_id: not intented to be used on sdk"
return 1
fi
local objects=
objects=($(get_all_or_selected_of_type "$type" "$@")) || return
local object=
for object in "${objects[@]}"; do
echo >&2 "Resetting machine ID for $type '$object'"
enter "$type:$object" rm -f /etc/machine-id || return
enter "$type:$object" rm -f /var/lib/dbus/machine-id || return
enter "$type:$object" systemd-machine-id-setup || return
enter "$type:$object" dbus-uuidgen --ensure || return
done
}
################################################################'
# tooling management
tooling_exists() [[ -e $MER_TOOLINGS/$1 ]]
# specialization of object_config_file()
tooling_config_file()
{
local tooling=$1
echo "$MER_TOOLINGS/$tooling/$CONFIG_FILE"
}
# specialization of assert_object_exists()
assert_tooling_exists() {
local tooling=$1
assert_name_valid "$tooling" || return
if ! tooling_exists "$tooling"; then
echo >&2 "Unknown tooling: $tooling"
return 1
fi
}
# specialization of enter()
enter_tooling() {
local tooling=$1 cmd=("${@:2}")
assert_tooling_exists "$tooling" || return
sudo "$MER_TOOLINGS/$tooling/mer-tooling-chroot" "${cmd[@]}"
}
# specialization of get_all_of_type()
get_all_of_type_tooling() {
local tooling
while read tooling; do
if ! assert_name_valid "$tooling" 2>/dev/null; then
echo >&2 "Ignoring tooling with invalid name '$tooling'"
continue
fi
echo "$tooling"
done < <(ls --literal "$MER_TOOLINGS")
}
list_toolings() {
local long=
while [[ $# -gt 0 ]]; do
case $1 in
-l|--long)
long=1
;;
*)
echo >&2 "unrecognized option '$1'"
usage
return 1
;;
esac
shift
done
if [[ ! $long ]]; then
get_all_of_type_tooling
return
fi
local tooling=
for tooling in $(get_all_of_type_tooling); do
echo "$tooling" "$(get_object_mode "tooling:$tooling")"
done |column -t
}
# Our toolchains are available through patterns-sailfish-sb2-* meta-packages
get_toolchains() {
local tooling=$1
object_zypper "tooling:$tooling" --no-refresh search "patterns-sailfish-sb2-*" | \
grep patterns-sailfish-sb2 |grep -v sb2-common | while IFS='| ' read installed pkg dummy; do
echo "${pkg},${installed}"
done
return ${PIPESTATUS[0]}
}
ensure_installed() {
local tooling=$1 toolchain=$2
# Do not require building zypper caches as this is not always possible
enter_tooling "$tooling" rpm -qa --queryformat='%{name}\n' |grep -q -F --line-regexp "$toolchain"
}
ensure_installable() {
local tooling=$1 toolchain=$2
get_toolchains "$tooling" |grep -q -F --line-regexp "$toolchain,"
}
install_toolchain() {
local tooling=$1 pkg=$2
if ! ensure_installable "$tooling" "$pkg"; then
echo >&2 "Toolchain '$pkg' doesn't exist or is already installed - can not install."
return 1
fi
object_zypper "tooling:$tooling" --no-refresh install "$pkg"
}
install_tooling() {
local name=$1 url=$2
assert_name_valid "$name" || return
if tooling_exists "$name"; then
echo >&2 "Name already in use: $name"
return 1
fi
sudo mkdir -p "$MER_TOOLINGS" || return
local download_result= local_file= downloaded=
local succeeded=
install_tooling_cleanup() (
trap 'echo cleaning up...' INT TERM HUP
if [[ $download_result ]]; then
local local_file= downloaded=
if unpack "$download_result" local_file downloaded && [[ $downloaded ]]; then
rm -f "$local_file"
fi
fi
if [[ ! $succeeded ]]; then
sudo rm -rf "$MER_TOOLINGS/$name"
fi
)
trap 'install_tooling_cleanup; trap - RETURN' RETURN
trap 'return 1' INT TERM HUP
download_result=$(download "$url" "$TMPDIR_DOWNLOADS" "$name") || return
unpack "$download_result" local_file downloaded
sudo rm -rf "$MER_TOOLINGS/$name" || return
sudo mkdir -p "$MER_TOOLINGS/$name" || return
echo >&2 "Unpacking tooling ..."
if ! maybe_decompress_tarball "$local_file" | sudo tar -C "$MER_TOOLINGS/$name" -x; then
if disk_full "$MER_TOOLINGS"; then
echo >&2 "Not enough disk space to unpack tooling image"
else
echo >&2 "Could not unpack tooling image"
fi
return 1
fi
if [[ $MODE == "$MODE_USER" ]]; then
if [[ $INSIDE_VBOX ]]; then
local size=$(sudo du --summarize --bytes "$MER_TOOLINGS/$name" |cut -f1)
manage_user_targets_repository touch tooling "$name" "${size:-0}"
fi
else
set_object_mode "tooling:$name" "$MODE"
fi
init_machine_id tooling "$name" || return
echo >&2 "Tooling '$name' now setup"
succeeded=1
}
remove_tooling() {
local name=$1
assert_name_valid "$name" || return
local mode=$(get_object_mode "tooling:$name")
if [[ $MODE != "$mode" ]]; then
echo >&2 "The tooling '$name' can only be removed in the '$mode' mode."
return 1
fi
local targets=$(get_targets_using_tooling "$name")
if [[ $targets ]]; then
targets=$(echo $targets |sed 's/ /, /')
echo >&2 "Cannot remove tooling '$name': Tooling used by following targets: '$targets'"
return 1
fi
sudo rm -rf "$MER_TOOLINGS/$name"
if [[ $INSIDE_VBOX && $mode == "$MODE_USER" ]]; then
manage_user_targets_repository rm tooling "$name"
fi
}
manage_toolings() {
if ! [[ ${1:-} ]]; then
usage
return 1
fi
case $1 in
?(--)list ) shift
list_toolings "$@"
;;
?(--)upgradable ) shift
object_upgradable tooling "$@"
;;
?(--)install ) shift
install_tooling "$@"
;;
?(--)remove ) shift
remove_tooling "$@"
;;
?(--)refresh ) shift
refresh_objects tooling "$@"
;;
?(--)update ) shift
upgrade_object tooling "$@"
;;
?(--)register ) shift
register_object tooling "$@"
;;
?(--)uuidgen ) shift
reset_machine_id tooling "$@"
;;
* )
echo >&2 "unrecognized option '$1'"
usage
return 1
;;
esac
}
################################################################'
# Toolchains
manage_toolchains() {
if ! [[ {$1:-} ]]; then
usage
return 1
fi
case $1 in
?(--)list ) shift
local tooling=${1:-}
assert_tooling_exists "$tooling" || return
get_toolchains "$tooling"
;;
?(--)install ) shift
local tooling=${1:-}
assert_tooling_exists "$tooling" || return
shift
object_zypper "tooling:$tooling" --no-refresh install "$@" || return
;;
?(--)remove ) shift
local tooling=${1:-}
assert_tooling_exists "$tooling" || return
shift
object_zypper "tooling:$tooling" --no-refresh remove "$@" || return
;;
* )
echo >&2 "unrecognized option '$1'"
usage
return 1
;;
esac
}
################################################################'
# Devel packages
# Our devel packages are available through zypper as -devel packages
# specialization of assert_object_exists()
assert_target_exists() {
local target=$1
assert_name_valid "$target" || return
if ! [[ -f $SBOX2DIR/$target/sb2.config ]]; then
echo >&2 "Target '$target' is not a valid Scratchbox2 target"
return 1
fi
if ! check_target_visible "$target"; then
echo >&2 "Target '$target' is not accessible"
return 1
fi
}
# specialization of enter()
enter_target() {
local target=$1
shift
sb2 -t "$target" -m sdk-install -R "$@"
}
get_develpkgs() {
local target=$1 search=$2
# do not include sourcepackages to the list (-t package -t pattern)
object_zypper "target:$target" --no-refresh search -t package -t pattern -- ${search:+"$search"} 2>&1 | \
sed '0,/---------/d' | while IFS='| ' read installed pkg dummy; do
echo "${pkg},${installed}"
done
return ${PIPESTATUS[0]}
}
manage_develpkgs() {
if ! [[ {$1:-} ]]; then
usage
return 1
fi
case $1 in
?(--)list ) shift
local target=${1:-} search=${2:-}
assert_target_exists "$target" || return
get_develpkgs "$target" "$search"
;;
?(--)install ) shift
local target=${1:-}
assert_target_exists "$target" || return
shift
object_zypper "target:$target" --no-refresh install "$@" || return
if [[ $INSIDE_VBOX ]] && ! target_is_snapshot "$target"; then
synchronise_target "$target"
fi
;;
?(--)remove ) shift
local target=${1:-}
assert_target_exists "$target" || return
shift
object_zypper "target:$target" --no-refresh remove "$@" || return
if [[ $INSIDE_VBOX ]] && ! target_is_snapshot "$target"; then
synchronise_target "$target"
fi
;;
* )
echo >&2 "unrecognized option '$1'"
usage
return 1
;;
esac
}
################################################################
# Targets
# specialization of object_config_file()
target_config_file()
{
local target=$1
echo "$MER_TARGETS/$target/$CONFIG_FILE"
}
target_exists() {
local target=$1
# sb2-config is verbose when no target exists
sb2-config -f 2>/dev/null |grep -q -F --line-regexp "$target"
}