-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotun.sh
executable file
·516 lines (454 loc) · 16.5 KB
/
otun.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
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
#!/usr/bin/env bash
# TODO:
# - Include chmod +x instructions for install.sh (or bash install.sh) and ./install.sh)
# - Make --config and -c file work, check if right keys are there (bot_token and chat_id). Show error and exit 1 if it fails
# - Enable/disable send notification also if no updates via parameter
# - Enable/disable spinner via parameter
# - Time operations, show time per operation plus total, then remove sleeps. (verbose option?)
# - Packages dependencies: util-linux (rev), ncurses (tput), jq, yq, hostname (inetutils)
# - Format the output via terminal (colors, bold) and also via Telegram (Markup/HTML). Check Telegram screenshots for ideas
# - Include option to disable format/colors (just plain text)
# - Show error if curl fails or message is not send (currently it doesn't show anything if it doesn't work)
# - Include systemd, openrc and runit init script / service file and --install and --install-service options
# - Test all scenarios (make it fail and see what happens, multiple parameters, etc.)
# - Include manpage
# - Consistent error messages (format)
# - Comment everything, write consistent variables (upper and lowercase)
# EXTRA:
# - Include updates check for: slackware, pclinuxos, void, easyos, puppy, alpine, solus, termux
# - Try it on debian, ubuntu, redhat, centos, slackware, pclinuxos, void, easyos, puppy, alpine, solus, linux mint via docker
# - Try it on Termux
# - Check updates for flatpak, snap, appimage, etc.?
# - Create release and AUR/GURU package
readonly PROGNAME="otun (On-Telegram Updates Notifier)"
readonly SCRIPTNAME=${0##*/}
readonly VERSION="1.0"
prefix_path=""
config_extension=""
# Define supported distros/families
readonly DISTROS=("arch" "centos" "debian" "fedora" "gentoo" "opensuse" "rhel" "suse")
# Show help/usage multi-line message using a Here Document
help_message() {
# Define the multi-line string
read -r -d '' message <<EOF
${PROGNAME} v${VERSION}
Check for updates and notify them (if any) via Telegram bot.
Usage: ${SCRIPTNAME} [OPTIONS]
Options:
-h, --help Display this help message and exit.
-c, --config Specify the Telegram bot configuration file (default: telegram_config.json/jaml).
-d, --distro Specify the Linux family distro manually, disabling auto-detection (supported: arch, debian, gentoo, rhel, suse).
-p, --prefix Specify the prefix path (location) if you want to run it on your local prefix.
-v, --version Display script version.
EOF
# Pretty print the multi-line string
echo "$message"
}
parse_options() {
update_current_task "Cheking the command-line options..."
# Parse command-line options
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
stop_spinner
help_message
exit 0
;;
-c | --config)
config="$2"
;;
-d | --distro)
auto_distro=false
supported_distro "$2"
shift 2
;;
-p | --prefix)
# It consumes two arguments: option and its value. Otherwise it doesn't work
check_prefix_path "$2"
shift 2
;;
-v | --version)
stop_spinner
display_version
exit 0
;;
*)
stop_spinner
invalid_option_message "$1"
exit 1
;;
esac
shift
done
}
# Trap signals and call show_cursor function
trap show_cursor EXIT
display_version() {
echo "$PROGNAME $VERSION"
}
invalid_option_message() {
echo "$SCRIPTNAME: invalid option '$1'"
echo "Try '$SCRIPTNAME --help' or '$SCRIPTNAME -h' for more information."
}
# Detect distro family
detect_distro() {
update_current_task "Detecting the Linux distro/family..."
# Steps:
# - Filter only ID_LIKE from /etc/os-release
# - Get only the right side after the =
# - Remove double quotes if any
# - In case of multiple results (e.g. manjaro arch or ubuntu debian), keep only the latest
# - Ensure lowercase
# - If no result, try just ID (case for pure Gentoo, Debian, etc.)
distro=$(cat "$prefix_path"/etc/os-release | grep -w ID_LIKE | awk -F= '{print $2}' | tr -d '"' | rev | cut -d' ' -f1 | rev | tr '[:upper:]' '[:lower:]')
# In case of no results, so no ID_LIKE, check for ID (no multiple results)
if [ -z "$distro" ]; then # or [[ $(echo "$distro" | wc -l) -eq 1 ]] since an empty line gives a 1
distro=$(cat "$prefix_path"/etc/os-release | grep -w ID | awk -F= '{print $2}' | tr -d '"' | tr '[:upper:]' '[:lower:]')
fi
if [[ $(echo "$distro" | wc -l) -ne 0 ]]; then
supported_distro "$distro"
else
stop_spinner
unsupported_distro_message "$distro"
exit 1
fi
}
supported_distro() {
# Ensure lowercase
distro=$(echo "$1" | tr '[:upper:]' '[:lower:]')
# Check if the target string is present in the array using the 'in' operator
if [[ "${DISTROS[*]}" =~ $distro ]]; then
add_dependencies "" # empty arg to avoid linter multiple warning (exception adding does not fully work)
else
stop_spinner
unsupported_distro_message "$distro"
exit 1
fi
}
check_prefix_path() {
if [ -e "$1" ]; then
prefix_path=$1
else
stop_spinner
wrong_prefix_path_message "$1"
exit 1
fi
}
# Show unsupported distro multi-line message using a Here Document
wrong_prefix_path_message() {
# Define the multi-line string
echo "$SCRIPTNAME: the prefix specified prefix path '$1' does not exist."
echo "Ensure the specified prefix path exists to be able to run the script."
}
add_dependencies() {
# Include dependencies for each distro to check, also define the udpates check command
pre_check=""
case "$distro" in
arch)
DISTRO_DEP+=(aur checkupdates)
updates_check="checkupdates & pacman -Qm | aur vercmp & wait"
;;
debian)
DISTRO_DEP+=(aptitude)
updates_check="aptitude search '~U' -F '%p %v -> %V' | tr -s ' '"
;;
gentoo)
DISTRO_DEP+=(eix)
pre_check="emerge-webrsync -q >/dev/null 2>&1 && eix-update -q >/dev/null 2>&1"
updates_check="NAMEVERSION=\"<category>/<name> <version>\" INSTFORMAT=\"{last}<version>\" eix --upgrade --format '<installedversions:NAMEVERSION> -> <bestslotupgradeversions:INSTFORMAT>\n' | head -n -1"
;;
rhel | fedora | centos)
pre_check="dnf list --installed > installed_packages.txt && dnf check-update > available_packages.txt"
updates_check="awk 'NR==FNR{a[\$1]=\$2;next} \$1 in a{print \$1, a[\$1], \"->\", \$2}' installed_packages.txt available_packages.txt"
;;
suse | opensuse)
updates_check="zypper list-updates | sed '1,/^Reading installed packages...$/d' | sed '1,2d' | sed -n 's/.*| \([^ ]*\) *| \([^ ]*\) *| \([^ ]*\) *| [^|]*$/\1 \2 -> \3/p'"
;;
esac
DEPENDENCIES+=("${DISTRO_DEP[@]}")
}
# Show unknown distro multi-line message using a Here Document
unknown_distro_message() {
# Define the multi-line string
read -r -d '' message <<EOF
$SCRIPTNAME: unknown Linux distribution/family '$distro'.
Make sure your distro or distro family is specified on /etc/os-release via ID_LIKE or ID variables.
Otherwise, specify your distro family via --distro or -d parameter (possible values: arch, debian, gentoo, rhel, suse).
Alternatively, you can always open an issue or a pull request to include your distro or family distro on https://github.com/mvidaldp/otun"
EOF
# Pretty print the multi-line string
echo "$message"
}
# Show unsupported distro multi-line message using a Here Document
unsupported_distro_message() {
# Define the multi-line string
read -r -d '' message <<EOF
$SCRIPTNAME: the distro '$distro' is not (yet) supported.
The possible distro/family values are arch, debian, gentoo, rhel, suse.
You can always open an issue or a pull request to include your distro or family distro on https://github.com/mvidaldp/otun"
EOF
# Pretty print the multi-line string
echo "$message"
}
# Check for depenencies to run the script, include distro-based dependencies (e.g. eix for Gentoo, aptitude for Debian, etc.)
# Separate distro dependencies than others (e.g. checkupdates for Arch)
DEPENDENCIES=("awk" "curl" "hostname" "jq" "lsb_release" "uname" "yq")
DISTRO_DEP=()
check_dependencies() {
update_current_task "Checking the required dependencies..."
REQUIRED=()
for i in "${DEPENDENCIES[@]}"; do
command -v "$i" >/dev/null 2>&1 || {
REQUIRED+=("$i")
}
done
# Show the missing commands/dependencies found and exit the script if any
if [[ "${#REQUIRED[*]}" -gt 0 ]]; then
stop_spinner
echo "$SCRIPTNAME: to run this script, you need the following commands/dependencies:"
echo "${REQUIRED[@]}"
exit 1
fi
}
read_telegram_config() {
update_current_task "Reading the Telegram bot configuration..."
# Read Telegram bot configuration
command=""
# TODO: ensure the user installs the python version of it (pip install yq)
# https://github.com/kislyuk/yq
if [ "$config_extension" = "yaml" ]; then
command="yq"
else
command="jq"
fi
TELEGRAM_BOT_TOKEN=$($command -r .bot_token telegram_config.$config_extension)
CHAT_ID=$($command -r .chat_id telegram_config.$config_extension)
}
fetch_system_info() {
update_current_task "Fetching the system information..."
# Get current hostname
HOSTNAME=$(hostname)
# Get OS description
os_description=$(lsb_release -s -d | tr -d '\n"')
# Get OS release
os_release=$(lsb_release -s -r | tr -d '\n" ')
# Put description and release together if release is not in description already
if [[ $os_description == *"$os_release"* ]]; then
OS="$os_description"
else
OS="$os_description $os_release"
fi
ARCH=$(uname -m)
# Get current IP address
IP=$(curl -s ifconfig.me)
# Check if the IP is empty
if [ -z "$IP" ]; then
stop_spinner
echo -e "$SCRIPTNAME: unable to fetch the IP address. Check your network connection and try again."
exit 1
fi
# Append message content
MESSAGE="HOSTNAME: $HOSTNAME\n"
MESSAGE+="OS: $OS ($ARCH)\n"
MESSAGE+="IP: $IP\n"
}
pre_updates_error() {
echo -e "$SCRIPTNAME: something went wrong running '$1'.\n"
echo "Ensure this pre-update check command runs to be able to check for updates."
}
check_for_updates() {
update_current_task "Checking for updates..."
# Evaluates if pre-check is an empty string
if [[ -n $pre_check ]]; then
# Execute the pre updates check command
eval "$pre_check"
exit_status=$?
# Evaluates if running the pre-check command failed (non-zero exit status)
if [ $exit_status -ne 0 ] && [ $exit_status -ne 100 ]; then
stop_spinner
pre_updates_error "$pre_check"
exit 1
fi
fi
# Check if updates are available
UPDATES=$(eval "$updates_check")
# Updates states:
# 0: Normal exit condition. => list of results
# 1: Unknown cause of failure. => error
# 2: No updates available. => 0 results
# Updates counter
N_UPDATES=$(echo "$UPDATES" | wc -l)
updates_found=false
# Code block executed if there are updates available:
# [[ ... ]] -> if var not empty
# -n $VAR -> if string length is nonzero
if [[ -n $UPDATES ]]; then
updates_found=true
send_info "$N_UPDATES" "$updates_found"
else
# Append message content
MESSAGE+="\nNo updates were found. This system is up to date."
send_info "$N_UPDATES" "$updates_found"
fi
# Stop the spinner
stop_spinner
printf "\r%s\n\n" "Process completed!"
# Log message via terminal
echo -e "$MESSAGE"
}
# Maximum message length for Telegram is 4096 characters
readonly MAX_MESSAGE_LENGTH=4096
send_info() {
update_current_task "Sending the updates notification via Telegram..."
local n_updates=$1
local updates_found=$2
if "$updates_found"; then
# Use "is" and "update if only 1 udpate, otherwise "are" and "updates"
IS_ARE=$([ "$n_updates" == 1 ] && echo "is" || echo "are")
S=$([ "$IS_ARE" == "is" ] && echo "" || echo "s")
# Append message content
MESSAGE+="\nThere $IS_ARE $n_updates update$S available:\n"
MESSAGE+="$UPDATES"
fi
# Check if the message exceeds the maximum length
if [ ${#MESSAGE} -gt $MAX_MESSAGE_LENGTH ]; then
max_size=4096
current_chunk=""
current_size=0
# Use readarray to populate the array with lines
readarray -t lines <<<"$MESSAGE"
# Iterate over each line
for line in "${lines[@]}"; do
line_size=${#line}
# Check if adding the line exceeds the chunk size
if ((current_size + line_size > max_size - 256)); then
# Remove the last line break
current_chunk=$(echo "$current_chunk" | sed '$s/\\n$//')
# Output the current chunk
chunk=$(echo -e "$current_chunk")
# Send message via telegram bot
curl -s -o /dev/null -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" -d "chat_id=$CHAT_ID" -d "text=$chunk"
# Start a new chunk with the current line
current_chunk="$line\n"
current_size=$line_size
else
# Append the line to the current chunk
current_chunk+="$line\n"
current_size=$((current_size + line_size + 1))
fi
done
# Send last chunk
chunk=$(echo -e "$current_chunk")
# Send message via telegram bot
curl -s -o /dev/null -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" -d "chat_id=$CHAT_ID" -d "text=$chunk"
else
# Include linebreaks
MESSAGE=$(echo -e "$MESSAGE")
# Send message via telegram bot
curl -s -o /dev/null -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" -d "chat_id=$CHAT_ID" -d "text=$MESSAGE"
fi
}
current_task=""
# Global variable to store the spinner process ID
spinner_pid=""
run_spinner() {
local chars="/-\|"
local delay=0.04
local i=0
while true; do
printf "\r%s %s" "${chars:$i:1}" "$current_task"
((i = (i + 1) % ${#chars}))
sleep $delay
done
}
# Stop the spinner
stop_spinner() {
if [[ -n "$spinner_pid" ]]; then
kill "$spinner_pid" &>/dev/null
wait "$spinner_pid" &>/dev/null
tput el
fi
}
# Function to update the current task
update_current_task() {
tput el
echo -ne "$1"\\r
}
# Hide the cursor
hide_cursor() {
tput civis
}
# Show the cursor
show_cursor() {
tput cnorm
}
# Show unsupported distro multi-line message using a Here Document
config_file_not_found() {
# Define the multi-line string
read -r -d '' message <<EOF
$SCRIPTNAME: No config file (telegram_config.yaml/json) was found.
Make sure you have your telegram bot config file on the same folder of this script with the following content:
telegram_config.yaml
====================
bot_token: "yourtoken"
chat_id: "-yourchatid"
telegram_config.json
====================
{
"bot_token": "yourtoken",
"chat_id": "-yourchatid"
}
EOF
# Pretty print the multi-line string
echo "$message"
}
check_config() {
# TODO: include case for custom YAML/JSON
update_current_task "Checking if Telegram config file (YAML/JSON) exists..."
# check if YAML config file exists
if [ -e telegram_config.yaml ]; then
config_extension="yaml"
# check if JSON config file exists
else
if [ -e telegram_config.json ]; then
config_extension="json"
else
stop_spinner
config_file_not_found
exit 1
fi
fi
}
# Script flow entry point
main() {
hide_cursor
# Start the spinner in the background
run_spinner &
# Get the PID of the spinner process
spinner_pid=$!
# Set automatically detect distro boolean to true
auto_distro=true
if [[ $# -ne 0 ]]; then
parse_options "$@"
fi
# case still true, autodetect distro
if "$auto_distro"; then
detect_distro
fi
# sleep only for demonstration purpose
sleep 0.5
check_config
sleep 0.5
check_dependencies
sleep 0.5
read_telegram_config
sleep 0.5
fetch_system_info
sleep 0.5
check_for_updates
}
# Run the script
main "$@"
# Wait for the spinner process to finish (only run if spinner is on)
wait
show_cursor