Update script using best practices

This commit is contained in:
adrien 2022-04-19 00:19:01 +02:00
parent 48dc55d769
commit b0ab525d02
Signed by: adrien
GPG Key ID: 4F17BEA67707AC21
2 changed files with 39 additions and 41 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -1,75 +1,72 @@
#! /usr/bin/env bash
# all executed commands are printed to stdout
# All executed commands are printed to stdout
set -x
# redirect stdout (and stderr to stdout) to a file
exec 1> /var/log/updater/vaultwarden-updater/vaultwarden-updater-$(date +%F).log 2>&1
# Redirect stdout (and stderr to stdout) to a file
exec 1>/var/log/updater/vaultwarden-updater/vaultwarden-updater-"$(date +%F)".log 2>&1
# abort on nonzero exitstatus
# Abort on nonzero exit status
set -o errexit
# abort on unbound variable
# Abort on unbound variable
set -o nounset
# don't hide errors within pipes
# Don't hide errors within pipes
set -o pipefail
# set your API key here
# Set your API key here
key=
# set your chat id here
# Set your chat id here
chat_id=
# set your username
# Set your username
username=
function send_message() {
if [ $# -eq 0 ]
then
echo "No argument supplied. Please specify the message to send."
if [ $# -eq 0 ]; then
echo "No argument supplied, please specify the message to send"
else
curl --silent --show-error --fail --request POST https://api.telegram.org/${key}/sendMessage --data chat_id=${chat_id} --data text="${1}" --output /dev/null
curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
fi
}
function update_vaultwarden {
rm --recursive --force /tmp/vaultwarden
# clone the latest release
su --login ${username} --command "git clone https://github.com/dani-garcia/vaultwarden.git /tmp/vaultwarden"
# Clone the latest release
su --login "$username" --command "git clone https://github.com/dani-garcia/vaultwarden.git /tmp/vaultwarden"
# compile the binary
su --login ${username} --command "/home/${username}/.cargo/bin/cargo build --quiet --features sqlite --release --manifest-path=/tmp/vaultwarden/Cargo.toml"
# Compile the binary
su --login "$username" --command "/home/$username/.cargo/bin/cargo build --quiet --features sqlite --release --manifest-path=/tmp/vaultwarden/Cargo.toml"
systemctl stop vaultwarden.service
# move the binary to the right place
# Move the binary to the right place
mv /tmp/vaultwarden/target/release/vaultwarden /usr/local/bin/vaultwarden
# set the correct permissions
chown root:vaultwarden /usr/local/bin/vaultwarden
chmod 750 /usr/local/bin/vaultwarden
# Set the correct permissions
/usr/bin/chown root:vaultwarden /usr/local/bin/vaultwarden
/usr/bin/chmod 750 /usr/local/bin/vaultwarden
/usr/sbin/restorecon /usr/local/bin/vaultwarden
if systemctl start vaultwarden.service
then
send_message "[Vaultwarden] - Vaultwarden has just been updated."
if systemctl start vaultwarden.service; then
send_message "[Vaultwarden] - Vaultwarden has just been updated"
else
send_message "[Vaultwarden] - Vaultwarden service did not start correctly. Please log in as soon as possible and see what went wrong."
send_message "[Vaultwarden] - Vaultwarden service did not start correctly. Please log in as soon as possible and see what went wrong"
fi
}
# retrieve local release
# Retrieve local release
local_release=$(/usr/local/bin/vaultwarden --version | awk --field-separator '-' '{print $2}')
# retrieve the latest release
# Retrieve the latest release
latest_release=$(git ls-remote https://github.com/dani-garcia/vaultwarden.git HEAD | awk '{print substr($1, 1, length($1) - 32)}')
# compare these two versions
if [ ${latest_release} == ${local_release} ]
then
send_message "[Vaultwarden] - Vaultwarden is up to date."
# Compare these two versions
if [ "$latest_release" == "$local_release" ]; then
send_message "[Vaultwarden] - Vaultwarden is up to date"
else
send_message "[Vaultwarden] - Vaultwarden is not up to date (https://github.com/dani-garcia/vaultwarden/commit/${latest_release})."
send_message "[Vaultwarden] - Vaultwarden is not up to date (https://github.com/dani-garcia/vaultwarden/commit/$latest_release)"
update_vaultwarden
fi