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 #! /usr/bin/env bash
# all executed commands are printed to stdout # All executed commands are printed to stdout
set -x set -x
# redirect stdout (and stderr to stdout) to a file # Redirect stdout (and stderr to stdout) to a file
exec 1> /var/log/updater/vaultwarden-updater/vaultwarden-updater-$(date +%F).log 2>&1 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 set -o errexit
# abort on unbound variable # Abort on unbound variable
set -o nounset set -o nounset
# don't hide errors within pipes # Don't hide errors within pipes
set -o pipefail set -o pipefail
# set your API key here # Set your API key here
key= key=
# set your chat id here # Set your chat id here
chat_id= chat_id=
# set your username # Set your username
username= username=
function send_message() { function send_message() {
if [ $# -eq 0 ] if [ $# -eq 0 ]; then
then echo "No argument supplied, please specify the message to send"
echo "No argument supplied. Please specify the message to send."
else 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 fi
} }
function update_vaultwarden { function update_vaultwarden {
rm --recursive --force /tmp/vaultwarden rm --recursive --force /tmp/vaultwarden
# clone the latest release # Clone the latest release
su --login ${username} --command "git clone https://github.com/dani-garcia/vaultwarden.git /tmp/vaultwarden" su --login "$username" --command "git clone https://github.com/dani-garcia/vaultwarden.git /tmp/vaultwarden"
# compile the binary # Compile the binary
su --login ${username} --command "/home/${username}/.cargo/bin/cargo build --quiet --features sqlite --release --manifest-path=/tmp/vaultwarden/Cargo.toml" su --login "$username" --command "/home/$username/.cargo/bin/cargo build --quiet --features sqlite --release --manifest-path=/tmp/vaultwarden/Cargo.toml"
systemctl stop vaultwarden.service 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 mv /tmp/vaultwarden/target/release/vaultwarden /usr/local/bin/vaultwarden
# set the correct permissions # Set the correct permissions
chown root:vaultwarden /usr/local/bin/vaultwarden /usr/bin/chown root:vaultwarden /usr/local/bin/vaultwarden
chmod 750 /usr/local/bin/vaultwarden /usr/bin/chmod 750 /usr/local/bin/vaultwarden
/usr/sbin/restorecon /usr/local/bin/vaultwarden /usr/sbin/restorecon /usr/local/bin/vaultwarden
if systemctl start vaultwarden.service if systemctl start vaultwarden.service; then
then send_message "[Vaultwarden] - Vaultwarden has just been updated"
send_message "[Vaultwarden] - Vaultwarden has just been updated."
else 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 fi
} }
# retrieve local release # Retrieve local release
local_release=$(/usr/local/bin/vaultwarden --version | awk --field-separator '-' '{print $2}') 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)}') 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 # Compare these two versions
if [ ${latest_release} == ${local_release} ] if [ "$latest_release" == "$local_release" ]; then
then send_message "[Vaultwarden] - Vaultwarden is up to date"
send_message "[Vaultwarden] - Vaultwarden is up to date."
else 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 update_vaultwarden
fi fi