-
Notifications
You must be signed in to change notification settings - Fork 4
/
distrocheck
executable file
·1635 lines (1421 loc) · 41.8 KB
/
distrocheck
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
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
#
# MIT License
#
# Copyright (c) 2021 Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
_args="$*"
container_global_flags=""
container_ip=""
container_name=""
container_user=""
ephemeral=0
_gid=0
_home="/root"
package_manager=""
packman=""
prefix="true"
tag=""
_uid=0
_user=
use_ssh=0
function _echo {
if [[ ${OVE_DISTROCHECK_STEPS} == *verbose* ]]; then
ove-echo stderr "$*"
fi
return 0
}
function container_command {
if command -v incus > /dev/null; then
echo "incus"
elif command -v lxc > /dev/null; then
echo "lxc"
else
return 1
fi
return 0
}
function init {
local c
local s
if ! c=$(container_command); then
echo "error: no supported container runtime found" 1>&2
exit 1
elif [ $# -ne 2 ]; then
echo "usage: $(basename "$0") file|project|unittest distro"
exit 1
fi
if [ "$1" = "unittest" ]; then
unittest=1
OVE_DISTROCHECK_STEPS="ove verbose user"
if ! command -v shunit2 > /dev/null; then
echo "error: command 'shunit2' not found" 1>&2
exit 1
fi
else
unittest=0
distcheck="$1"
fi
bash_opt=""
distro="$2"
if [[ ${OVE_DISTROCHECK_STEPS} == *debug* ]]; then
set -x
fi
if [[ ${OVE_DISTROCHECK_STEPS} == *X* ]]; then
if ! command -v xpra > /dev/null; then
echo "error: command 'xpra' not found" 1>&2
exit 1
fi
OVE_DISTROCHECK_STEPS+=" ssh"
OVE_DISTROCHECK_STEPS+=" ssh_config"
if [ "${OVE_DISTROCHECK_XPRA_DE}" = "" ]; then
OVE_DISTROCHECK_XPRA_DE="xfce"
OVE_DISTROCHECK_XPRA_DE_CMD="xfce4-session"
elif [ "${OVE_DISTROCHECK_XPRA_DE_CMD}" = "" ]; then
echo "error: specify how to start '${OVE_DISTROCHECK_XPRA_DE}' in OVE_DISTROCHECK_XPRA_DE_CMD" 1>&2
exit 1
fi
fi
if [ "${OVE_DISTROCHECK_CONTAINER_USER}" != "" ]; then
container_user="${OVE_DISTROCHECK_CONTAINER_USER}"
else
container_user="${OVE_USER}"
fi
if [ ! -v OVE_DISTROCHECK_STEPS ]; then
OVE_DISTROCHECK_STEPS=""
fi
container_global_flags=""
if [[ ${OVE_DISTROCHECK_STEPS} == *verbose* ]]; then
for s in ${OVE_DISTROCHECK_STEPS//:/ };do
echo "${s}"
done | LC_ALL=C sort -u
elif "${c}" -h | grep -q '\-q'; then
container_global_flags+=" -q"
fi
if [[ ${OVE_DISTROCHECK_STEPS} == *ssh* ]]; then
if ! command -v sshpass > /dev/null; then
echo "error: command 'sshpass' not found" 1>&2
exit 1
fi
ssh_opts=""
ssh_opts+=" -o GlobalKnownHostsFile=/dev/null"
ssh_opts+=" -o StrictHostKeyChecking=no"
ssh_opts+=" -o UserKnownHostsFile=/dev/null"
ssh_copy_id_opts="${ssh_opts}"
if [[ ${OVE_DISTROCHECK_STEPS} == *debug* ]]; then
ssh_opts+=" -v"
fi
fi
}
function run {
local sleep_s
_echo "[${distro}]$ $*"
while true; do
if eval "$*" 2> "${OVE_TMP}/${tag:?}.err"; then
return 0
fi
_echo "error: '$*' failed for distro '${distro}'"
if [ ! -s "${OVE_TMP}/${tag}.err" ]; then
if [ "${NO_EXIT}" = "1" ]; then
return 1
else
exit 1
fi
fi
sleep_s=$((RANDOM%10))
if grep "^Error:.*i/o timeout" "${OVE_TMP}/${tag}.err"; then
_echo "$(container_command): i/o timeout. Retry in ${sleep_s} sec"
sleep ${sleep_s}
continue
elif grep "^Error: websocket:" "${OVE_TMP}/${tag}.err"; then
_echo "$(container_command): websocket error. Retry in ${sleep_s} sec"
sleep ${sleep_s}
continue
elif grep "^Error: Missing event connection with target cluster member" "${OVE_TMP}/${tag}.err"; then
_echo "$(container_command): cluster error. Retry in ${sleep_s} sec"
sleep ${sleep_s}
continue
elif grep "^Error: Operation not found" "${OVE_TMP}/${tag}.err"; then
_echo "$(container_command): operation not found error. Retry in ${sleep_s} sec"
sleep ${sleep_s}
continue
elif grep -q "^Error: Command not found" "${OVE_TMP}/${tag}.err"; then
true
else
cat "${OVE_TMP}/${tag}.err"
fi
if [ "${NO_EXIT}" = "1" ]; then
return 1
else
exit 1
fi
done
}
function run_no_exit {
if ! NO_EXIT=1 run "$@"; then
return 1
fi
}
function ssh_exec {
local prefix
_echo "ssh -F /dev/null -t -q ${ssh_opts:?} ${_user:?}@${container_ip:?} $*"
# shellcheck disable=SC2086
if ! ssh \
-F /dev/null \
-t \
-q \
${ssh_opts} \
${_user}@${container_ip} "$@"; then
return 1
fi
return 0
}
function container_exec {
local e
local container_exec_options
if [ "${use_ssh}" -eq 1 ]; then
if ! ssh_exec "$@"; then
return 1
fi
return 0
fi
container_exec_options=""
if [[ ${package_manager} == apt-get* ]]; then
container_exec_options+=" --env DEBIAN_FRONTEND=noninteractive"
fi
for e in ftp_proxy http_proxy https_proxy; do
if [ "${!e}" = "" ]; then
continue
fi
container_exec_options+=" --env ${e}=${!e}"
done
if [ "${CONTAINER_EXEC_EXTRA}" != "" ]; then
container_exec_options+=" ${CONTAINER_EXEC_EXTRA}"
fi
run "$(container_command)${container_global_flags} exec${container_exec_options} ${container_name} -- $*"
}
function container_exec_as_root {
CONTAINER_EXEC_EXTRA="--user 0 --group 0" \
use_ssh=0 \
_user=root \
container_exec "$@"
}
function container_exec_as_user {
CONTAINER_EXEC_EXTRA="--user ${_uid:?} --group ${_gid:?} --env HOME=${HOME:?}" \
use_ssh=0 \
container_exec "$@"
}
function container_exec_no_exit {
local e
local container_exec_options
if [ "${use_ssh}" -eq 1 ]; then
if ! ssh_exec "$@"; then
return 1
fi
return 0
fi
container_exec_options=""
if [[ ${package_manager} == apt-get* ]]; then
container_exec_options+=" --env DEBIAN_FRONTEND=noninteractive"
fi
for e in ftp_proxy http_proxy https_proxy; do
if [ "${!e}" = "" ]; then
continue
fi
container_exec_options+=" --env ${e}=${!e}"
done
if [ "${CONTAINER_EXEC_EXTRA}" != "" ]; then
container_exec_options+=" ${CONTAINER_EXEC_EXTRA}"
fi
if ! run_no_exit "$(container_command)${container_global_flags} exec${container_exec_options} ${container_name} -- $*"; then
return 1
fi
}
function container_exec_no_exit_as_root {
CONTAINER_EXEC_EXTRA="--user 0 --group 0" \
use_ssh=0 \
_user=root \
container_exec_no_exit "$@"
}
function package_manager_noconfirm {
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_INSTALL_PKG 1'"
if [[ ${package_manager} == apt-get* ]]; then
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_OS_PACKAGE_MANAGER_ARGS -y -qq -o=Dpkg::Progress=0 -o=Dpkg::Progress-Fancy=false install'"
elif [[ ${package_manager} == pacman* ]]; then
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_OS_PACKAGE_MANAGER_ARGS -S --noconfirm --noprogressbar'"
elif [[ ${package_manager} == xbps-install* ]]; then
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_OS_PACKAGE_MANAGER_ARGS -y'"
elif [[ ${package_manager} == zypper* ]]; then
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_OS_PACKAGE_MANAGER_ARGS install -y'"
elif [[ ${package_manager} == dnf* ]]; then
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_OS_PACKAGE_MANAGER_ARGS install -y'"
elif [[ ${package_manager} == apk* ]]; then
container_exec "bash ${bash_opt} -c '${prefix}; ove-add-config \$HOME/.oveconfig OVE_OS_PACKAGE_MANAGER_ARGS add --no-progress -q'"
fi
}
function remove_tmp {
if [[ ${OVE_DISTROCHECK_STEPS:?} != *debug* ]]; then
find "${OVE_TMP:?}" -maxdepth 1 -type f -name "${tag:?}*" -exec rm {} \;
fi
return 0
}
function cleanup {
local f
if [ -s "${HOME}/.ssh/config" ] && \
[[ ${OVE_DISTROCHECK_STEPS} = *ove* ]] && \
[[ ${OVE_DISTROCHECK_STEPS} = *user* ]] && \
[[ ${OVE_DISTROCHECK_STEPS} = *ssh_config* ]]; then
# enable RemoteCommand
sed \
-i \
-e "s,^\t#\(RemoteCommand cd $OVE_BASE_DIR.*$\),\t\1,g" \
"${HOME}/.ssh/config"
fi
f="${OVE_TMP}/${tag}-$(container_command)-config-exit"
if ! save_instance_config "$f"; then
exit 1
elif [[ ${OVE_DISTROCHECK_STEPS:?} == *running* ]]; then
remove_tmp
return
fi
run_no_exit "$(container_command)${container_global_flags} stop ${container_name:?}"
if [[ ${OVE_DISTROCHECK_STEPS:?} == *stopped* ]]; then
remove_tmp
return
fi
run_no_exit "$(container_command)${container_global_flags} delete ${container_name:?} --force"
remove_tmp
}
function setup_package_manager {
run "$(container_command)${container_global_flags} file pull ${container_name}/root/${tag}-packman ${OVE_TMP}/${tag}-packman"
if [ ! -s "${OVE_TMP}/${tag}-packman" ]; then
echo "error: could not determine package manager for ${distro}" 1>&2
exit 1
fi
packman=$(cat "${OVE_TMP}/${tag}-packman")
if [ "${packman}" = "apk" ]; then
package_manager="apk add --no-progress -q"
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
if ! timeout 10 apk update > /dev/null 2>&1; then
sed -i 's,https,http,g' /etc/apk/repositories
if ! timeout 10 apk update > /dev/null 2>&1; then
echo "error: apk update failed"
exit 1
fi
fi
EOF
elif [ "${packman}" = "pacman" ]; then
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
pacman -Syu --noconfirm -q --noprogressbar
EOF
package_manager="pacman -S --noconfirm -q --noprogressbar"
elif [ "${packman}" = "apt-get" ]; then
package_manager="apt-get -y -qq -o=Dpkg::Progress=0 -o=Dpkg::Progress-Fancy=false install"
if [ -s "/etc/apt/apt.conf" ]; then
cp -a "/etc/apt/apt.conf" "${OVE_TMP}/${tag}-apt.conf"
container_file_push_root \
"${OVE_TMP}/${tag}-apt.conf" \
"${container_name}/etc/apt/apt.conf"
fi
echo "apt-get update >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
elif [ "${packman}" = "xbps" ]; then
package_manager="xbps-install -y"
elif [ "${packman}" = "dnf" ]; then
package_manager="dnf install -y"
elif [ "${packman}" = "zypper" ]; then
package_manager="zypper install -y"
elif [ "${packman}" = "opkg" ]; then
package_manager="opkg install"
else
echo "error: unknown package manager for '${distro}'" 1>&2
exit 1
fi
cat > "${OVE_TMP}/${tag}-packman" <<EOF
#!/usr/bin/env sh
${package_manager} "\$@"
EOF
container_file_push_root \
"${OVE_TMP}/${tag}-packman" \
"${container_name}/sbin/packman" \
"--mode 0755"
}
function is_cluster {
# shellcheck disable=SC2086
if "$(container_command)"${container_global_flags} cluster list &> /dev/null; then
return 0
else
return 1
fi
}
function _setup_sshd {
echo "${package_manager:?} openssh-server >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
echo "${package_manager} openssh >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
if [[ ${distro} == *opensuse* ]]; then
echo "cp -a /usr/etc/ssh/sshd_config /etc/ssh/" >> "${OVE_TMP}/${tag}-services.sh"
elif [[ ${distro} == *void* ]]; then
echo "ln -s /etc/sv/sshd /var/service" >> "${OVE_TMP}/${tag}-services.sh"
fi
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
if [ ! -s /etc/ssh/sshd_config ]; then
echo 'error: ${container_name:?}: could not find /etc/ssh/sshd_config' >&2
exit 1
fi
if ! sed -i \
-e 's,.*PermitRootLogin.*,PermitRootLogin yes,g' \
-e 's,.*PermitUserEnvironment.*,PermitUserEnvironment yes,g' \
-e 's,.*AllowAgentForwarding.*,AllowAgentForwarding yes,g' \
-e 's,.*PasswordAuthentication.*,PasswordAuthentication yes,g' /etc/ssh/sshd_config; then
exit 1
fi
EOF
if [[ ${distro} == *alpine* ]]; then
echo "rc-update add sshd >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
echo "/etc/init.d/sshd start >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
elif [[ ${distro} == *devuan* ]]; then
echo "service ssh restart" >> "${OVE_TMP}/${tag}-services.sh"
elif [[ ${distro} == *openwrt* ]]; then
{
echo "uci set dropbear.@dropbear[0].Port=2222"
echo "uci commit dropbear"
echo "/etc/init.d/dropbear restart"
echo "/etc/init.d/firewall stop"
echo "/etc/init.d/firewall disable"
echo "/etc/init.d/sshd enable"
echo "/etc/init.d/sshd start"
} >> "${OVE_TMP}/${tag}-services.sh"
else
for s in ssh sshd; do
echo "systemctl -q restart $s >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
echo "systemctl -q enable $s >/dev/null 2>&1" >> "${OVE_TMP}/${tag}-services.sh"
done
fi
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
i=0
while true; do
i=\$((i+1))
if [ \$i -gt 100 ]; then
echo "error: sshd did not start"
exit 1
elif pgrep -f sshd >/dev/null 2>&1; then
sleep 1
break
fi
EOF
if [[ ${OVE_DISTROCHECK_STEPS} == *verbose* ]]; then
echo -e "\techo \"waiting for sshd \$i\"" >> "${OVE_TMP}/${tag}-services.sh"
fi
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
sleep 0.1
done
EOF
if [[ ${OVE_DISTROCHECK_STEPS} == *verbose* ]]; then
sed -i -e "1iset -x" "${OVE_TMP}/${tag}-services.sh"
fi
sed -i -e "1i#!/usr/bin/env sh" "${OVE_TMP}/${tag}-services.sh"
}
function setup_X_apk {
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
# install Xpra
packman xpra
# install Desktop Environment
setup-desktop ${OVE_DISTROCHECK_XPRA_DE}
EOF
}
function setup_X_deb {
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
# install wget
packman wget
# xpra keyring
wget -q -O /usr/share/keyrings/xpra.asc https://xpra.org/xpra.asc
# xpra.sources
codename=\$(. /etc/os-release; echo \${VERSION_CODENAME})
wget -q -O /etc/apt/sources.list.d/xpra.sources https://raw.githubusercontent.com/Xpra-org/xpra/master/packaging/repos/\${codename:?}/xpra.sources
# refresh package manager
apt-get update
# install packages
packman cron xfce4-session xfce4-terminal xpra
EOF
}
function setup_X_rpm {
cat >> "${OVE_TMP}/${tag}-services.sh" <<EOF
# install wget
packman wget
# xpra.sources
id=\$(. /etc/os-release; echo \${ID})
echo "id=\$id"
if [ "\${id,,}" = "fedora" ]; then
id="Fedora"
elif [[ "\${id,,}" == *centos* ]]; then
id="CentOS-Stream"
elif [[ "\${id,,}" == *almalinux* ]]; then
id="almalinux"
elif [[ "\${id,,}" == *rocky* ]]; then
id="rockylinux"
else
id="almalinux"
fi
wget -O /etc/yum.repos.d/xpra.repo https://raw.githubusercontent.com/Xpra-org/xpra/master/packaging/repos/\${id}/xpra.repo
# install config-manager plugin
packman dnf-plugins-core
# enable CRB
dnf config-manager --set-enabled crb
# EPEL
packman epel-release
# refresh package manager
dnf update -y
# packages
packs=""
packs+=" cronie"
packs+=" xpra"
# install packages
packman \${packs}
# install xfce
dnf groupinstall -y xfce
EOF
}
function ssh_config_entry {
cat << EOF
Host ${container_name:?}
HostName ${container_ip:?}
RequestTTY yes
StrictHostKeyChecking no
User ${_user:?}
EOF
if [[ ${OVE_DISTROCHECK_STEPS} = *ove* ]] && \
[[ ${OVE_DISTROCHECK_STEPS} = *user* ]]; then
echo -e "\t#RemoteCommand cd ${OVE_BASE_DIR:?}; exec \$SHELL"
fi
}
function update_ssh_config {
local n
local _user
if [[ ${OVE_DISTROCHECK_STEPS} != *ssh* ]]; then
return 0
elif [[ ${OVE_DISTROCHECK_STEPS} != *ssh_config* ]]; then
return 0
fi
if command -v flock > /dev/null; then
exec {fd}>"${OVE_TMP}"/distrocheck.lock
flock ${fd}
fi
if [[ ${OVE_DISTROCHECK_STEPS} == *user* ]] && [ ${EUID} -ne 0 ]; then
_user="${container_user}"
else
_user="root"
fi
if ! mkdir -p "${HOME}/.ssh"; then
echo "error: mkdir ${HOME}/.ssh failed" 1>&2
exit 1
elif [ -s "${HOME}/.ssh/config" ] && \
grep -q "^Host ${container_name}$" "${HOME}/.ssh/config"; then
# insert on top of existing host
n=$(grep -m 1 -n "^Host ${container_name}$" "${HOME}/.ssh/config" | cut -d: -f1)
{
echo -n "${n}i"
ssh_config_entry | sed -e 's,$,\\,g'
} > "${OVE_TMP}/${tag}-sed-script"
if ! sed -i -f "${OVE_TMP}/${tag}-sed-script" "${HOME}/.ssh/config"; then
echo "error: not possible to update ${HOME}/.ssh/config" 1>&2
exit 1
fi
else
# append
ssh_config_entry >> "${HOME}/.ssh/config"
fi
if command -v flock > /dev/null; then
flock -u "${fd}"
fi
}
# $1: user
function setup_ssh {
local pass="${RANDOM}${RANDOM}${RANDOM}"
local _home
_user="$1"
if [ "${_user}" = "root" ]; then
_home="/root"
if [ "${OVE_DISTROCHECK_CONTAINER_ROOT_PASS}" != "" ]; then
pass="${OVE_DISTROCHECK_CONTAINER_ROOT_PASS}"
fi
else
_home="${HOME}"
if [ "${OVE_DISTROCHECK_CONTAINER_USER_PASS}" != "" ]; then
pass="${OVE_DISTROCHECK_CONTAINER_USER_PASS}"
fi
fi
if [[ ${distro} == *alpine* ]]; then
cat > "${OVE_TMP}/${tag}-passwd-${_user}" <<EOF
echo -e "${pass}\n${pass}" | passwd ${_user} &> /dev/null
EOF
container_file_push_root \
"${OVE_TMP}/${tag}-passwd-${_user}" \
"${container_name}/root/${tag}-passwd-${_user}"
# shellcheck disable=SC2097,SC2098
container_exec_as_root "sh /root/${tag}-passwd-${_user}"
else
if [[ ${distro} == *openwrt* ]]; then
container_exec_as_root "${package_manager} shadow-usermod shadow-useradd" > /dev/null
fi
# shellcheck disable=SC2097,SC2098
container_exec_as_root "usermod -p '$(openssl passwd -1 "${pass}")' ${_user}"
fi
container_exec_as_user "mkdir -p .ssh"
container_exec_as_user "chmod 700 .ssh"
_echo "copy public key to container for user: ${_user}"
i=0
while true; do
((i++))
if [ ${i} -gt 100 ]; then
echo "error: not possible to copy public key to ${container_name} ${container_ip}" 1>&2
exit 1
fi
# shellcheck disable=SC2086
if ! sshpass -p${pass} \
ssh-copy-id \
-f \
${ssh_copy_id_opts} \
"${_user}"@"${container_ip}" &> /dev/null; then
_echo "copy public key to ${container_name} ${container_ip} failed - retry in 1 sec"
sleep 1
continue
fi
break
done
# from now on use ssh instead of exec
use_ssh=1
true > "${OVE_TMP}/${tag}-sshenv"
for e in ftp_proxy http_proxy https_proxy; do
if [ "${!e}" = "" ]; then
continue
fi
echo "${e}=${!e}" >> "${OVE_TMP}/${tag}-sshenv"
done
if [ -s "${OVE_TMP}/${tag}-sshenv" ]; then
# shellcheck disable=SC2086
scp ${ssh_opts} -p -q "${OVE_TMP}/${tag}-sshenv" "${_user}"@"${container_ip}":.ssh/environment
fi
}
# $1: uid or gid
function get_id {
local i
local _id
local sleep_s
i=0
while true; do
((i++))
if [ $i -ge 30 ]; then
echo "error: could not retreive $1 for ${container_user} for ${container_name}" 1>&2
return 1
fi
_id=$(container_exec "id -${1::1} ${container_user}")
if [ "${_id}" = "" ]; then
sleep_s=$((RANDOM%10))
echo "warning: empty $1, retry in ${sleep_s}s ($i)" 1>&2
sleep ${sleep_s}
continue
fi
break
done
_id=${_id//[$'\t\r\n']/}
if [[ ! "${_id}" =~ ^[0-9]+$ ]]; then
echo "error: weird $1 '${_id}'" 1>&2
return 1
fi
echo "$_id"
}
function container_file_push_helper {
local _cmd
_cmd="$(container_command)${container_global_flags}"
_cmd+=" file"
_cmd+=" push"
if [ $# -eq 3 ]; then
_cmd+=" $3"
fi
if [[ "${FUNCNAME[1]}" == *_root ]]; then
_cmd+=" --uid 0"
_cmd+=" --gid 0"
elif [[ "${FUNCNAME[1]}" == *_user ]]; then
_cmd+=" --uid ${_uid:?}"
_cmd+=" --gid ${_gid:?}"
else
echo "fatal: container_file_push_helper" 1>&2
exit 1
fi
_cmd+=" $1"
_cmd+=" $2"
run "${_cmd}"
}
function container_file_push_root {
container_file_push_helper "$@"
}
function container_file_push_user {
container_file_push_helper "$@"
}
function mkdir_var_tmp {
local i
i=0
while true; do
((i++))
if [ $i -ge 100 ]; then
echo "error: could not create /var/tmp for ${container_name}" 1>&2
exit 1
elif ! container_exec_no_exit "mkdir -p -v /var/tmp"; then
sleep 0.1
continue
fi
break
done
}
function _uniq {
if [ $# -eq 1 ]; then
echo "${1}"
return
fi
declare -A a_hash
while [ $# -ne 0 ]; do
if test "${a_hash["${1}"]+isset}"; then
shift
continue
fi
a_hash["${1}"]=1
echo "${1}"
done
}
function print_vars {
local v
for v in $(compgen -A variable OVE_DISTROCHECK_); do
if [ "${!v}" != "" ]; then
# shellcheck disable=SC2086
echo "${v/OVE_DISTROCHECK_}=$(_uniq ${!v})"
fi
done | xargs
}
# $1: file
function save_instance_config {
# shellcheck disable=SC2086
if ! "$(container_command)"${container_global_flags} config show ${container_name} > "$1"; then
return 1
fi
}
# $1: file
function set_instance_config {
# shellcheck disable=SC2086
if ! "$(container_command)"${container_global_flags} config edit ${container_name} < "$1"; then
return 1
fi
}
function set_instance_description {
local f
f="${OVE_TMP}/${tag}-$(container_command)-config-init"
g="${OVE_TMP}/${tag}-$(container_command)-config-desc"
if ! save_instance_config "$f"; then
exit 1
elif ! cp -a "$f" "$g"; then
exit 1
elif ! sed -i "s,^description:.*,description: \"${_args:?} $(print_vars)\"," "$g"; then
exit 1
elif ! set_instance_config "$g"; then
exit 1
fi
rm "$g"
}
function launch_container {
_echo "$(container_command)${container_global_flags} launch images:${distro} ${container_name} ${OVE_DISTROCHECK_LAUNCH_EXTRA_ARGS//\#/ }"
# shellcheck disable=SC2086
if ! "$(container_command)"${container_global_flags} launch images:${distro} ${container_name} ${OVE_DISTROCHECK_LAUNCH_EXTRA_ARGS//\#/ } > /dev/null; then
exit 1
fi
trap cleanup EXIT
# use instance description for args and env vars
set_instance_description
if ! container_exec_no_exit "test -d /var/tmp"; then
mkdir_var_tmp
fi
if [[ ${OVE_DISTROCHECK_STEPS} == *verbose* ]] && is_cluster; then
# shellcheck disable=SC2086
_echo "location:$("$(container_command)"${container_global_flags} list --format csv -cL "${container_name}")"
fi
}
function prepare_bootcheck {
cat > "${OVE_TMP}/${tag}-bootcheck.sh" <<EOF
#!/usr/bin/env sh
if [ ! -s /root/${tag}-packman ]; then
if command -v apk > /dev/null; then
packman="apk"
elif command -v pacman > /dev/null; then
packman="pacman"
elif command -v apt-get > /dev/null; then
packman="apt-get"
elif command -v dnf > /dev/null; then
packman="dnf"
elif command -v zypper > /dev/null; then
packman="zypper"
elif command -v xbps-install > /dev/null; then
packman="xbps"
elif command -v opkg > /dev/null; then
packman="opkg"
else
echo "bootcheck: unknown package manager for ${distro}"
packman="unknown"
fi
echo "\$packman" > /root/${tag}-packman
fi
if command -v systemctl > /dev/null; then
cmd="systemctl is-system-running"
exp="running"
elif command -v rc-status > /dev/null; then
cmd="rc-status -r"
exp="default"
elif command -v sv > /dev/null; then
cmd="readlink -f /etc/runit/runsvdir/current"
exp="/etc/runit/runsvdir/default"
elif command -v runlevel > /dev/null; then
cmd="runlevel"
exp="N 2"
elif command -v procd > /dev/null; then
cmd="sleep 1"
exp=""
else
echo "bootcheck: unknown init system for ${distro}"
sleep 1
exit 0
fi
i=0
while true; do
i=\$((i+1))
if [ \$i -ge 100 ]; then
exit 0
fi
s=\$(\$cmd 2> /dev/null);
if [ "\$s" = "\$exp" ]; then
break
fi
sleep 0.1
done
exit 0
EOF
if [[ ${OVE_DISTROCHECK_STEPS} == *verbose* ]]; then
sed -i -e "2iset -x" "${OVE_TMP}/${tag}-bootcheck.sh"
fi
container_file_push_root \
"${OVE_TMP}/${tag}-bootcheck.sh" \
"${container_name}/root/${tag}-bootcheck.sh"
}
function run_bootcheck {
container_exec_no_exit_as_root "sh /root/${tag}-bootcheck.sh"
wait_for_ip
}
function prepare_package_manager {
if [[ ( ${OVE_DISTROCHECK_STEPS} == *user* && ${EUID} -ne 0 ) || \
( ${OVE_DISTROCHECK_STEPS} == *ssh* ) || \
( ${OVE_DISTROCHECK_STEPS} == *ove* ) ]]; then
setup_package_manager
fi
}
function wait_for_ip {
local i
if [[ ${distro} == *openwrt* ]]; then
_echo "openwrt: sleep 5: wait for real IPv4 address"
sleep 5
fi
i=0
while true; do
((i++))
if [ $i -ge 100 ]; then
echo "error: no IPv4 address for ${container_name}" 1>&2
exit 1
fi
# shellcheck disable=SC2086
container_ip=$("$(container_command)"${container_global_flags} list -c4 --format csv ${container_name})
container_ip=${container_ip% *}
if [ "${container_ip}" = "" ]; then
_echo "waiting for IPv4 address for ${container_name} ($i)"
sleep 1
continue
fi
_echo "${container_name}=${container_ip}"
break
done
}
function run_X {
if [[ ${OVE_DISTROCHECK_STEPS} != *X* ]]; then
return 0
elif [ "${OVE_DISTROCHECK_XPRA_SERVER_ARGS}" = "" ]; then
OVE_DISTROCHECK_XPRA_SERVER_ARGS=""
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" start-desktop"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" :100"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --exit-with-children"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --microphone=no"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --opengl=no"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --printing=no"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --resize-display=FHD"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --speaker=no"
OVE_DISTROCHECK_XPRA_SERVER_ARGS+=" --webcam=no"