-
Notifications
You must be signed in to change notification settings - Fork 1
/
libapt.sh
325 lines (301 loc) · 8.46 KB
/
libapt.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
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
#!/bin/bash
function purge_apt_package {
local ans=0
local package=$1
if dpkg -s "$package">/dev/null 2> /dev/null; then
logexec sudo apt-get --yes --force-yes -q purge "$package"
return 0
fi
return 1
}
function cpu_arch {
local arch=$(uname -m)
if [[ "$arch" == "x86_64" ]]; then
arch=amd64
elif [[ "$arch" == "aarch64" ]]; then
arch=arm64
fi
echo $arch
}
function install_apt_package {
local ans=0
local package=$1
local command=$2
if [ -n "$command" ]; then
if ! which "$command">/dev/null 2> /dev/null; then
do_update
sudo apt-get --yes --force-yes -q install "$package"
return $?
fi
else
if ! dpkg -s "$package">/dev/null 2> /dev/null; then
do_update
sudo apt-get --yes --force-yes -q install "$package"
return $?
fi
fi
return 1
}
function install_apt_packages {
local ans=0
local to_install=""
local packages="$@"
for package in ${packages[@]}; do
if ! dpkg -s "$package">/dev/null 2> /dev/null; then
to_install="${to_install} ${package}"
fi
done
if [[ "${to_install}" != "" ]]; then
do_update
logexec sudo apt-get --yes --force-yes -q install $to_install
return 0
fi
return 1
}
function install_apt_package_file {
local ans=0
local filename=$1
local package=$2
local source="$3"
if ! dpkg -s "$package">/dev/null 2> /dev/null; then
if [ ! -f "${filename}" ]; then
local cfilename=$(get_cached_file $(basename ${filename}) "$source")
if [ ! -f "${cfilename}" ]; then
errcho "Cannot find ${filename}"
return 255
fi
filename="${cfilename}"
fi
logexec sudo dpkg -i "$filename"
logexec sudo apt install -f --yes
return 0
fi
return 1
}
function install_pip3_packages {
local ans=0
local to_install=""
local packages="$@"
for package in ${packages[@]}; do
if ! pip3 list | grep -qF "$package" >/dev/null 2> /dev/null; then
to_install="${to_install} ${package}"
fi
done
if [[ "${to_install}" != "" ]]; then
do_update
install_apt_package pipx pipx
pipx install pipx
pipx ensurepath
purge_apt_package pipx
logexec pipx install $to_install
return 0
fi
return 1
}
function do_update {
refresh_apt_redirections
if [ "$flag_need_apt_update" == "1" ] || [ -n "$1" ]; then
logexec sudo apt update
flag_need_apt_update=0
return 0
fi
return 1
}
function do_upgrade {
do_update
logexec sudo apt upgrade --yes
return 0
}
# example: add_ppa jtaylor/keepass
function add_ppa {
local the_ppa=$1
if ! grep -q "^deb .*$the_ppa" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
install_apt_package software-properties-common add-apt-repository
release=$(get_ubuntu_codename)
if [ "$release" == "xenial" ]; then
logexec sudo add-apt-repository -y ppa:${the_ppa}
else
if ! sudo add-apt-repository --no-update -y ppa:${the_ppa}; then
logexec sudo add-apt-repository -y ppa:${the_ppa}
fi
fi
refresh_apt_redirections
flag_need_apt_update=1
return 0
fi
return 1
}
#Adds apt source. filename must exclude the extension .list.
function add_apt_source_manual {
local filename="$1"
local contents="$2"
local release_key_URI="$3"
local cached_release_key="$4"
local release_key
textfile "/etc/apt/sources.list.d/${filename}.list" "${contents}" root
if [ "$?" == "0" ]; then
flag_need_apt_update=1
refresh_apt_redirections
fi
if [ -n "$release_key_URI" ]; then
if [ -n "$cached_release_key" ]; then
release_key=$(get_cached_file "${cached_release_key}" "${release_key_URI}")
else
release_key=$(get_cached_file /tmp/tmp.key "${release_key_URI}")
fi
fingerpr=$(get_key_fingerprint ${release_key})
if ! apt-key finger | grep -Eo '([0-9A-F]{4} ? ?){10}+' | sed -e "s/\([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\) \([0-9A-F]\{4\}\)/\1\2\3\4\5\6\7\8\9/g" | grep "$fingerpr" > /dev/null; then
logexec sudo cp "${release_key}" /etc/apt/trusted.gpg.d/${filename}.gpg
# logexec sudo apt-key add "${release_key}"
cat "${release_key}" | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/${filename}.gpg
flag_need_apt_update=1
fi
fi
}
function get_apt_proxy {
local pattern1='(#?)Acquire::http::Proxy "https?://(.*):([0-9]+)";$'
local pattern2="^([^:]+):$pattern1"
local myproxy=$(grep -rE "^$pattern1" /etc/apt/apt.conf.d | head -n 1)
if [[ $myproxy =~ $pattern2 ]]; then
echo "$myproxy"
aptproxy_file=${BASH_REMATCH[1]}
aptproxy_enabled=${BASH_REMATCH[2]}
aptproxy_ip=${BASH_REMATCH[3]}
aptproxy_port=${BASH_REMATCH[4]}
else
echo ""
fi
}
function refresh_apt_proxy {
get_apt_proxy
if ping -c 1 -w 1 $aptproxy_ip >/dev/null; then
turn_http_all winehq.org
turn_http_all nodesource.com
turn_http_all slacktechnologies
turn_http_all syncthing.net
turn_http_all gitlab
turn_http_all skype.com
turn_http_all download.jitsi.org
turn_http_all docker
turn_http_all rstudio.com
turn_http_all virtualbox.org
turn_http_all nvidia.github.io
turn_http_all signal.org
turn_http_all bintray.com/zulip
turn_http_all packagecloud.io/AtomEditor
turn_http_all dl.bintray.com/fedarovich/qbittorrent
turn_http_all mkvtoolnix.download
if [ -n "$aptproxy_enabled" ]; then
echo "Acquire::http::Proxy \"http://${aptproxy_ip}:${aptproxy_port}\";" | sudo tee ${aptproxy_file}
fi
else
if [ -z "$aptproxy_enabled" ]; then
echo "#Acquire::http::Proxy \"http://${aptproxy_ip}:${aptproxy_port}\";" | sudo tee ${aptproxy_file}
fi
turn_https_all winehq.org
turn_https_all nodesource.com
turn_https_all slacktechnologies
turn_https_all syncthing.net
turn_https_all gitlab
turn_https_all skype.com
turn_https_all download.jitsi.org
turn_https_all docker
turn_https_all rstudio.com
turn_https_all virtualbox.org
turn_https_all nvidia.github.io
turn_https_all signal.org
turn_https_all bintray.com/zulip
turn_https_all packagecloud.io/AtomEditor
turn_https_all dl.bintray.com/fedarovich/qbittorrent
turn_https_all mkvtoolnix.download
fi
}
function get_key_fingerprint {
local keyfile="$1"
local fingerpr
local pattern
pattern='^\s*(Key fingerprint = )?([0-9A-F]{40})$'
if [ -f "${keyfile}" ]; then
fingerpr=$(gpg ${keyfile} | grep -E "$pattern")
if [[ "$fingerpr" =~ $pattern ]]; then
fingerpr=${BASH_REMATCH[2]}
else
fingerpr="error"
fi
else
fingerpr='missing'
fi
echo "${fingerpr}"
}
function find_apt_list {
local phrase="$1"
files=$(shopt -s nullglob dotglob; echo /etc/apt/sources.list.d/*.list)
if (( ${#files} )); then
grep -l /etc/apt/sources.list.d/*.list -e "${phrase}"
fi
}
function turn_https {
echo "http->https: $1"
# plik="/etc/apt/sources.list.d/$1"
plik="$1"
if [ -f "$plik" ]; then
sudo sed -i 's/http:/https:/g' ${plik}*
fi
}
function turn_http {
echo "https->http: $1"
# plik="/etc/apt/sources.list.d/$1"
plik="$1"
if [ -f "${plik}" ]; then
sudo sed -i 's/https/http/g' ${plik}*
fi
}
function turn_https_all {
find_apt_list "$1" | while read file; do turn_https ${file}; done
}
function turn_http_all {
find_apt_list "$1" | while read file; do turn_http ${file}; done
}
function refresh_apt_redirections {
pattern1='(#?)Acquire::http::Proxy "https?://(.*):([0-9]+)";$'
pattern2="^([^:]+):$pattern1"
myproxy=$(grep -rE "^$pattern1" /etc/apt/apt.conf.d | head -n 1)
if [[ $myproxy =~ $pattern2 ]]; then
aptproxy_file=${BASH_REMATCH[1]}
aptproxy_enabled=${BASH_REMATCH[2]}
aptproxy_ip=${BASH_REMATCH[3]}
aptproxy_port=${BASH_REMATCH[4]}
echo "Found aptproxy: ${aptproxy_ip}:${aptproxy_port} in ${aptproxy_file}"
if ping -c 1 -w 1 $aptproxy_ip >/dev/null; then
turn_http_all winehq.org
turn_http_all nodesource.com
turn_http_all slacktechnologies
turn_http_all syncthing.net
turn_http_all gitlab
turn_http_all skype.com
turn_http_all docker
turn_http_all rstudio.com
turn_http_all virtualbox.org
turn_http_all signal.org
turn_http_all bintray.com/zulip
if [ -n "$aptproxy_enabled" ]; then
echo "Acquire::http::Proxy \"http://${aptproxy_ip}:${aptproxy_port}\";" | sudo tee ${aptproxy_file}
fi
else
if [ -z "$aptproxy_enabled" ]; then
echo "#Acquire::http::Proxy \"http://${aptproxy_ip}:${aptproxy_port}\";" | sudo tee ${aptproxy_file}
fi
turn_https_all winehq.org
turn_https_all nodesource.com
turn_https_all slacktechnologies
turn_https_all syncthing.net
turn_https_all gitlab
turn_https_all skype.com
turn_https_all docker
turn_https_all rstudio.com
turn_https_all virtualbox.org
turn_https_all signal.org
turn_https_all bintray.com/zulip
fi
fi
}