-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-functions.sh
156 lines (146 loc) · 3.62 KB
/
common-functions.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
#!/bin/bash
# https://stackoverflow.com/questions/29926773/run-shell-command-in-jenkins-as-root-user
if [ "$EUID" -ne 0 ]; then
echo "Please run as root."
exit 1
fi
PREREQUISITES=(psmisc rsync)
case "$INIT" in
systemd)
PREREQUISITES+=(systemd-container systemd-resolved)
;;
esac
INIT=$(ps --no-headers -o comm 1)
ARCH=$(dpkg --print-architecture)
# choose chroot method
case $INIT in
systemd)
# use systemd-nspawn
CHROOT_METHOD=systemd
;;
tini)
CHROOT_METHOD=chroot
;;
*)
echo "Unsupported init system."
exit 1
;;
esac
common_init() {
if ! dpkg -l "${PREREQUISITES[@]}" &>/dev/null; then
apt-get -qq update
apt-get -qq install "${PREREQUISITES[@]}"
case "$INIT" in
systemd)
systemctl restart systemd-resolved
;;
esac
fi
# some sensible default
export LC_ALL=C.UTF-8
export LC_CTYPE=$LC_ALL
export LANG=$LC_ALL
echo "===================================================================================="
echo "$(date) - running $0 on $(hostname), called using \"$*\" as arguments."
echo "$(date) - actually running \"$(basename "$0")\" (md5sum $(md5sum "$0" | cut -d ' ' -f1))"
echo
export MIRROR=$MIRROR
# do not enable proxy by default
if [ -n "$PROXY" ]; then
export http_proxy=$PROXY
export https_proxy=$PROXY
fi
# be more verbose, maybe
if $DEBUG; then
export
set -x
fi
}
cleanup_all() {
echo "Doing cleanup now."
echo "Debug: Removing trap."
trap - INT TERM EXIT
# debug: preserve content of /tmp
# if [ "$1" != "fine" ]; then
# tar -czf /tmp /root/tmp.tar.gz
# fi
set -x
case $CHROOT_METHOD in
chroot)
for mount in tmp run dev/pts dev sys proc /; do
if mountpoint -q "$CHROOT_TARGET/$mount"; then
# why we need lazy umount?
# https://groups.google.com/g/linux.debian.user/c/ei2Guc_ZnXg?pli=1
umount -l "$CHROOT_TARGET/$mount" ||
fuser -mv "$CHROOT_TARGET/$mount"
fi
done
;;
esac
echo "\$1 = $1"
rm -f "$TMPLOG"
if [ "$1" != "fine" ]; then
# rename to .error
rm -rf --one-file-system "$CHROOT_BASE".error ||
fuser -mv "$CHROOT_BASE".error
mv "$CHROOT_TARGET" "$CHROOT_BASE".error ||
fuser -mv "$CHROOT_TARGET" "$CHROOT_BASE".error
echo "Something went wrong, $CHROOT_TARGET is moved to $CHROOT_BASE.error for further inspection."
exit 1
else
# remove .error
rm -rf --one-file-system "$CHROOT_BASE".error ||
fuser -mv "$CHROOT_BASE".error
rm -rf --one-file-system "$CHROOT_BASE".incr ||
fuser -mv "$CHROOT_BASE".incr
# rename to .latest
rm -rf --one-file-system "$CHROOT_BASE".latest ||
fuser -mv "$CHROOT_BASE".latest
echo "$CHROOT_TARGET succeeded."
mv "$CHROOT_TARGET" "$CHROOT_BASE".latest ||
fuser -mv "$CHROOT_TARGET" "$CHROOT_BASE".latest
echo "$CHROOT_BASE.latest can now be released."
fi
}
prepare_module() {
# /tmp is mount to tmpfs in systemd-nspawn
TMPFILE=/root/$TIMESTAMP.sh
CTMPFILE=$CHROOT_TARGET/$TMPFILE
TMPLOG=/tmp/$DISTRO-$RELEASE-$TIMESTAMP.log
# https://www.shellcheck.net/wiki/SC2129
{
echo "$MODULE_HEADER"
cat distro/"$DISTRO"-header.sh
cat common-header.sh
cat "$1"
echo "
"
} >"$CTMPFILE"
}
execute_module() {
echo "echo xxxxxSUCCESSxxxxx" >>"$CTMPFILE"
set -x
chmod +x "$CTMPFILE"
set -o pipefail # see eg http://petereisentraut.blogspot.com/2010/11/pipefail.html
case $CHROOT_METHOD in
systemd)
(systemd-nspawn --resolv-conf=replace-stub \
-D "$CHROOT_TARGET" "$TMPFILE" 2>&1 |
tee "$TMPLOG") ||
true
;;
chroot)
(chroot "$CHROOT_TARGET" "$TMPFILE" 2>&1 |
tee "$TMPLOG") ||
true
;;
esac
RESULT=$(grep "xxxxxSUCCESSxxxxx" "$TMPLOG" || true)
if [ -z "$RESULT" ]; then
echo "Failed to run $TMPFILE in $CHROOT_TARGET."
exit 1
fi
rm "$CTMPFILE"
set +o pipefail
set +x
}