-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-header.sh
160 lines (144 loc) · 3.04 KB
/
common-header.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
#!/bin/bash
# goto /tmp
cd /tmp || exit 1
# https://unix.stackexchange.com/questions/1496/why-doesnt-my-bash-script-recognize-aliases
# shopt -s expand_aliases
# alias curl="curl --retry-all-errors --retry 5 --silent --show-error --location"
# alias wget="wget --tries=5 --quiet"
# Ideally, we should use functions instead of aliases
curl (){
command curl --retry-all-errors --retry 5 --silent --show-error --location "$@"
}
wget (){
# retry forever
command wget --retry-connrefused --waitretry=1 --tries=5 --quiet "$@"
}
# https://unix.stackexchange.com/questions/351557/on-what-linux-distributions-can-i-rely-on-the-presence-of-etc-os-release
# https://github.com/which-distro/os-release
. /etc/os-release
KERNEL_VERSION=$(for file in /boot/vmlinuz-*; do echo "${file#/boot/vmlinuz-}"; done | sort -V | tail -n 1)
# https://superuser.com/questions/1017959/how-to-know-if-i-am-using-systemd-on-linux
# ps fail at first chroot because /proc is not mounted
INIT=$(ps --no-headers -o comm 1) || true
check_and_exec() {
if declare -f "$1" >/dev/null; then
"$1"
else
echo "Warning: $1 is not supported"
return 1
fi
}
##########
# github #
##########
# https://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8
get_github_url() {
# check if jq is installed
if ! command -v jq >/dev/null; then
install_pkg jq
fi
local repo=$1
local match=$2
set -o pipefail
local url
local counter=20
# if url is empty, retry 20 times until it's not empty
while [ -z "$url" ] && [ $counter -gt 0 ]; do
url=$(curl "https://api.github.com/repos/$repo/releases" |
jq -r "$match" |
head -n 1)
counter=$((counter - 1))
sleep 1
done
set +o pipefail
echo "$url"
}
get_asset_from_github() {
local repo=$1
local match=$2
local output=$3
local url
url=$(get_github_url "$repo" ".[].assets[] | select(.name|$match) | .browser_download_url")
wget -O "$output" "$url"
}
get_tarball_from_github() {
local repo=$1
local output=$2
local url
url=$(get_github_url "$repo" ".[].tarball_url")
wget -O "$output" "$url"
}
########
# pkgs #
########
case $ID in
ubuntu | debian)
PKG_FORMAT=deb
;;
openEuler)
PKG_FORMAT=rpm
;;
arch)
PKG_FORMAT=.tar.zst
;;
*)
echo "Unknown distribution: $ID"
exit 1
;;
esac
install_pkg() {
case $ID in
ubuntu | debian)
apt-get update
apt-get install "$@"
;;
openEuler)
dnf install "$@"
;;
arch)
pacman -S "$@"
;;
*)
echo "Unknown distribution: $ID"
exit 1
;;
esac
}
install_pkg_local() {
case $ID in
ubuntu | debian)
dpkg -i "$@" || apt-get --fix-broken --fix-missing install
;;
openEuler)
rpm -i "$@"
# dnf install -y
;;
arch)
pacman -U "$@"
;;
*)
echo "Unknown distribution: $ID"
exit 1
;;
esac
}
install_pkg_from_url() {
local url=$1
local tmpfile
tmpfile=$(mktemp)
if ! wget -O "$tmpfile" "$url"; then
echo "Failed to download $url"
exit 1
fi
install_pkg_local "$tmpfile"
rm "$tmpfile"
}
install_pkg_from_github() {
local repo=$1
local match=$2
local tmpfile
tmpfile=$(mktemp)
get_asset_from_github "$repo" "$match" "$tmpfile"
install_pkg_local "$tmpfile"
rm "$tmpfile"
}