1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/vaultwarden_ynh.git synced 2024-09-03 18:26:31 +02:00
vaultwarden_ynh/scripts/upgrade

230 lines
8.3 KiB
Text
Raw Normal View History

2019-08-05 01:29:15 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Loading installation settings..."
2019-08-05 01:29:15 +02:00
app=$YNH_APP_INSTANCE_NAME
2019-12-29 23:23:32 +01:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
2019-08-05 01:29:15 +02:00
2019-12-29 23:23:32 +01:00
admin_token=$(ynh_app_setting_get --app=$app --key=admin_token)
rocket_port=$(ynh_app_setting_get --app=$app --key=rocket_port)
websocket_port=$(ynh_app_setting_get --app=$app --key=websocket_port)
2019-08-05 01:29:15 +02:00
#=================================================
# CHECK VERSION
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Checking version..."
2019-08-05 01:29:15 +02:00
upgrade_type=$(ynh_check_app_version_changed)
2021-01-12 12:31:23 +01:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
2021-01-12 12:31:23 +01:00
# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
2021-02-28 12:24:08 +01:00
if ! ynh_permission_exists --permission="admin"; then
# Create the required permissions
ynh_permission_create --permission="admin" --url="/admin" --allowed="$admin" --show_tile="false"
fi
2021-01-12 12:31:23 +01:00
# Create a permission if needed
if ! ynh_permission_exists --permission="api"; then
ynh_permission_create --permission="api" --url="/api" --additional_urls="/identity/connect/token" --allowed="visitors" --auth_header="false" --show_tile="false" --protected="true"
fi
2019-08-05 01:29:15 +02:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
2019-08-05 01:29:15 +02:00
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
ynh_clean_check_starting
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Stopping a systemd service..."
2019-08-05 01:29:15 +02:00
2020-01-21 23:03:55 +01:00
ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd" --line_match="Stopped Bitwarden Server"
2019-08-05 01:29:15 +02:00
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Upgrading source files..."
2019-08-05 01:29:15 +02:00
# Download, check integrity, uncompress the source of bitwarden_rs from app.src to his build directory
2020-04-07 02:15:19 +02:00
ynh_setup_source --dest_dir="$final_path/build/" --source_id="app"
# Download, check integrity, uncompress and patch the source from web.src
2020-04-07 02:15:19 +02:00
ynh_setup_source --dest_dir="$final_path/live/web-vault/" --source_id="web"
2019-08-05 01:29:15 +02:00
fi
#=================================================
# NGINX CONFIGURATION
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Upgrading NGINX web server configuration..."
2019-08-05 01:29:15 +02:00
2020-09-21 09:02:06 +02:00
# Create a dedicated NGINX config
2019-08-06 00:44:05 +02:00
ynh_add_nginx_config "websocket_port rocket_port"
2019-08-05 01:29:15 +02:00
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Upgrading dependencies..."
2019-08-05 01:29:15 +02:00
2019-12-29 23:23:32 +01:00
ynh_install_app_dependencies $pkg_dependencies
2019-08-05 01:29:15 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Making sure dedicated system user exists..."
2019-08-05 01:29:15 +02:00
# Create a dedicated user (if not existing)
2019-12-29 23:23:32 +01:00
ynh_system_user_create --username=$app --home_dir="$final_path"
2019-08-05 01:29:15 +02:00
#=================================================
# SPECIFIC UPGRADE
#=================================================
2019-08-12 22:16:54 +02:00
# MAKE UPGRADE
2019-08-05 01:29:15 +02:00
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Making upgrade..."
2019-08-05 01:29:15 +02:00
# Set right permissions
2019-12-19 17:34:40 +01:00
chown -R "$app":"$app" "$final_path"
2019-08-05 01:29:15 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
# Install rustup with the toolchain needed by bitwarden_rs
2020-01-10 00:42:40 +01:00
pushd "$final_path"
2021-01-05 21:57:10 +01:00
sudo -u "$app" RUSTUP_HOME="$final_path"/.rustup CARGO_HOME="$final_path"/.cargo bash -c 'curl -sSf -L https://static.rust-lang.org/rustup.sh | sh -s -- -y --default-toolchain nightly'
2020-01-10 00:42:40 +01:00
popd
2019-08-05 01:29:15 +02:00
export PATH="$PATH:$final_path/.cargo/bin:$final_path/.local/bin:/usr/local/sbin"
# Compile bitwarden_rs
2020-01-10 00:42:40 +01:00
pushd "$final_path"/build
2021-03-19 22:10:49 +01:00
ynh_exec_warn_less sudo -u "$app" env PATH="$PATH" cargo build --features sqlite --release
2020-01-10 00:42:40 +01:00
popd
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
# Remove old generated files before copying the new ones
2021-02-21 06:38:40 +01:00
ynh_secure_remove --file="$final_path/live/.fingerprint"
2020-04-23 16:36:27 +02:00
ynh_secure_remove --file="$final_path/live/build"
ynh_secure_remove --file="$final_path/live/deps"
2021-02-21 06:38:40 +01:00
ynh_secure_remove --file="$final_path/live/examples"
ynh_secure_remove --file="$final_path/live/incremental"
ynh_secure_remove --file="$final_path/live/.cargo-lock"
ynh_secure_remove --file="$final_path/live/bitwarden_rs.d"
# Install bitwarden_rs
2021-02-28 12:24:08 +01:00
cp -af "$final_path/build/target/release/bitwarden_rs" "$final_path/live/bitwarden_rs"
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
# Remove build files and rustup
2020-01-12 21:42:15 +01:00
ynh_secure_remove --file="$final_path/build"
ynh_secure_remove --file="$final_path/.cargo"
ynh_secure_remove --file="$final_path/.rustup"
2019-08-05 01:29:15 +02:00
fi
2020-12-01 22:04:05 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config
2019-08-05 01:29:15 +02:00
#=================================================
# MODIFY A CONFIG FILE
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Modifying a config file..."
2019-08-05 01:29:15 +02:00
2021-02-28 12:24:08 +01:00
ynh_add_config --template="../conf/bitwarden_rs.env" --destination="$final_path/live/bitwarden_rs.env"
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
ynh_script_progression --message="Securing files and directories..."
# Set permissions on app files
chown -R "$app":"$app" "$final_path"
mkdir -p "/var/log/$app"
chown -R "$app":"$app" /var/log/"$app"
2019-08-05 01:29:15 +02:00
#=================================================
# SETUP LOGROTATE
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Upgrading logrotate configuration..."
2019-08-05 01:29:15 +02:00
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
2020-12-01 22:04:05 +01:00
# INTEGRATE SERVICE IN YUNOHOST
2019-08-05 01:29:15 +02:00
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Integrating service in YunoHost..."
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
yunohost service add $app --description "$app daemon for Bitwarden" --log "/var/log/$app/$app.log"
2019-08-05 01:29:15 +02:00
#=================================================
2020-12-01 22:04:05 +01:00
# START SYSTEMD SERVICE
2019-08-05 01:29:15 +02:00
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Starting a systemd service..."
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Rocket has launched from" --length=100
2019-08-05 01:29:15 +02:00
#=================================================
2020-12-01 22:04:05 +01:00
# UPGRADE FAIL2BAN
2019-08-05 01:29:15 +02:00
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Reconfiguring Fail2Ban..."
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
# Create a dedicated Fail2Ban config
ynh_add_fail2ban_config --logpath="/var/log/$app/$app.log" --failregex="^.*Username or password is incorrect\. Try again\. IP: <HOST>\. Username:.*$"
2019-08-05 01:29:15 +02:00
#=================================================
# RELOAD NGINX
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Reloading NGINX web server..."
2019-08-05 01:29:15 +02:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Upgrade of $app completed"