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