-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathlib.sh
176 lines (154 loc) · 3.75 KB
/
lib.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
#!/usr/bin/env bash
# shellcheck disable=SC2294
purge_list=()
set_centos_ulimit() {
# this is a bug affecting buildkit with yum when ulimit is unlimited
# https://github.com/docker/buildx/issues/379#issuecomment-1196517905
ulimit -n 1024000
}
install_packages() {
if grep -i ubuntu /etc/os-release; then
apt-get update
for pkg in "${@}"; do
if ! dpkg -L "${pkg}" >/dev/null 2>/dev/null; then
apt-get install --assume-yes --no-install-recommends "${pkg}"
purge_list+=( "${pkg}" )
fi
done
else
set_centos_ulimit
for pkg in "${@}"; do
if ! yum list installed "${pkg}" >/dev/null 2>/dev/null; then
yum install -y "${pkg}"
purge_list+=( "${pkg}" )
fi
done
fi
}
purge_packages() {
if (( ${#purge_list[@]} )); then
if grep -i ubuntu /etc/os-release; then
apt-get purge --assume-yes --auto-remove "${purge_list[@]}"
else
yum remove -y "${purge_list[@]}"
fi
fi
}
if_centos() {
if grep -q -i centos /etc/os-release; then
eval "${@}"
fi
}
if_ubuntu() {
if grep -q -i ubuntu /etc/os-release; then
eval "${@}"
fi
}
if_ubuntu_ge() {
if grep -q -i ubuntu /etc/os-release; then
local ver
ver="$(source /etc/os-release; echo $VERSION_ID)"
if dpkg --compare-versions "$ver" "ge" "$1"; then
shift
eval "${@}"
fi
fi
}
GNU_MIRRORS=(
"https://ftp.gnu.org/gnu/"
"https://ftpmirror.gnu.org/"
)
download_mirrors() {
local relpath="${1}"
shift
local filename="${1}"
shift
for mirror in "${@}"; do
if curl --retry 3 -sSfL "${mirror}/${relpath}/${filename}" -O; then
break
fi
done
if [[ ! -f "${filename}" ]]; then
echo "Unable to download ${filename}" >&2
exit 1
fi
}
download_binutils() {
local mirror
local version="${1}"
local ext="${2}"
local filename="binutils-${version}.tar.${ext}"
download_mirrors "binutils" "${filename}" "${GNU_MIRRORS[@]}"
}
download_gcc() {
local mirror
local version="${1}"
local ext="${2}"
local filename="gcc-${version}.tar.${ext}"
download_mirrors "gcc/gcc-${version}" "${filename}" "${GNU_MIRRORS[@]}"
}
docker_to_qemu_arch() {
local arch="${1}"
case "${arch}" in
arm64)
echo "aarch64"
;;
386)
echo "i386"
;;
amd64)
echo "x86_64"
;;
arm|ppc64le|riscv64|s390x)
echo "${arch}"
;;
*)
echo "Unknown Docker image architecture, got \"${arch}\"." >&2
exit 1
;;
esac
}
docker_to_linux_arch() {
# variant may not be provided
local oldstate
oldstate="$(set +o)"
set +u
local arch="${1}"
local variant="${2}"
case "${arch}" in
arm64)
echo "aarch64"
;;
386)
echo "i686"
;;
amd64)
echo "x86_64"
;;
ppc64le)
echo "powerpc64le"
;;
arm)
case "${variant}" in
v6)
echo "arm"
;;
""|v7)
echo "armv7"
;;
*)
echo "Unknown Docker image variant, got \"${variant}\"." >&2
exit 1
;;
esac
;;
riscv64|s390x)
echo "${arch}"
;;
*)
echo "Unknown Docker image architecture, got \"${arch}\"." >&2
exit 1
;;
esac
eval "${oldstate}"
}