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

278 lines
10 KiB
Text
Raw Normal View History

2019-08-05 01:29:15 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
2022-09-02 22:17:22 +02:00
source ynh_docker_image_extract
2021-05-09 11:06:05 +02:00
source ynh_handle_app_migration
2019-08-05 01:29:15 +02:00
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_script_progression --message="Loading installation settings..."
2019-08-05 01:29:15 +02:00
2023-06-22 09:09:24 +02:00
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
2019-08-05 01:29:15 +02:00
2023-06-22 09:09:24 +02:00
#REMOVEME? domain=$(ynh_app_setting_get --app=$app --key=domain)
#REMOVEME? path=$(ynh_app_setting_get --app=$app --key=path)
#REMOVEME? admin=$(ynh_app_setting_get --app=$app --key=admin)
#REMOVEME? #REMOVEME? install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
#REMOVEME? data_dir=$(ynh_app_setting_get --app=$app --key=data_dir)
2019-08-05 01:29:15 +02:00
2023-06-22 09:09:24 +02:00
#REMOVEME? admin_token=$(ynh_app_setting_get --app=$app --key=admin_token)
#REMOVEME? rocket_port=$(ynh_app_setting_get --app=$app --key=rocket_port)
#REMOVEME? 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)
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2023-06-22 09:09:24 +02:00
#REMOVEME? 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
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_backup_before_upgrade
#REMOVEME? ynh_clean_setup () {
2019-08-05 01:29:15 +02:00
ynh_clean_check_starting
2021-04-10 01:38:20 +02:00
# Restore it if the upgrade fails
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_restore_upgradebackup
2019-08-05 01:29:15 +02:00
}
# Exit if an error occurs during the execution of the script
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_abort_if_errors
2019-08-05 01:29:15 +02:00
#=================================================
# 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
2021-05-09 11:06:05 +02:00
ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd" --line_match="Stopped"
#=================================================
# HANDLE MIGRATION FROM BITWARDEN
#=================================================
ynh_handle_app_migration --migration_id="bitwarden" --migration_list="bitwarden_migration"
2021-05-09 20:55:30 +02:00
2021-05-09 11:06:05 +02:00
if [ $migration_process -eq 1 ]
then
# If a migration has been perform
# Reload some values changed by the migration process
2023-06-22 09:09:24 +02:00
#REMOVEME? #REMOVEME? install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
#REMOVEME? db_name=$(ynh_app_setting_get --app=$app --key=db_name)
2021-05-09 11:06:05 +02:00
2021-05-11 01:40:02 +02:00
# Move config file
2023-06-22 09:09:24 +02:00
mv $install_dir/live/bitwarden_rs.env $install_dir/live/.env
2021-05-11 01:40:02 +02:00
ynh_delete_file_checksum --file="/var/www/$old_app/live/bitwarden_rs.env"
2023-06-22 09:09:24 +02:00
ynh_store_file_checksum --file="$install_dir/live/.env"
#REMOVEME? ynh_secure_remove --file="$install_dir/live/bitwarden_rs"
2021-05-11 01:40:02 +02:00
# Manage permissions
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_permission_update --permission="main" --add="all_users"
#REMOVEME? ynh_permission_update --permission="main" --add="visitors"
2021-05-11 01:40:02 +02:00
# Remove the service from the list of services known by YunoHost (added from `yunohost service add`)
if ynh_exec_warn_less yunohost service status $old_app >/dev/null
then
ynh_script_progression --message="Removing $old_app service integration..."
yunohost service remove $old_app
fi
2021-05-09 11:06:05 +02:00
fi
2019-08-05 01:29:15 +02:00
2021-05-09 20:55:30 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
# Cleaning legacy permissions
2023-06-22 09:09:24 +02:00
#REMOVEME? if ynh_legacy_permissions_exists; then
#REMOVEME? ynh_legacy_permissions_delete_all
2021-05-09 20:55:30 +02:00
ynh_app_setting_delete --app=$app --key=is_public
fi
2023-06-22 09:09:24 +02:00
#REMOVEME? if ! ynh_permission_exists --permission="admin"; then
2021-05-09 20:55:30 +02:00
# Create the required permissions
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_permission_create --permission="admin" --url="/admin" --allowed=$admin --show_tile="false"
2021-05-09 20:55:30 +02:00
fi
# Create a permission if needed
2023-06-22 09:09:24 +02:00
#REMOVEME? if ! ynh_permission_exists --permission="api"; then
#REMOVEME? ynh_permission_create --permission="api" --url="/api" --additional_urls="/identity/connect/token" --allowed="visitors" --auth_header="false" --show_tile="false" --protected="true"
2021-05-09 20:55:30 +02:00
fi
2023-06-22 09:09:24 +02:00
# If data_dir doesn't exist, create it
if [ -z $data_dir ]; then
2022-07-17 05:57:09 +02:00
ynh_script_progression --message="Making sure data directory exists..."
2023-06-22 09:09:24 +02:00
data_dir=/home/yunohost.app/$app
#REMOVEME? ynh_app_setting_set --app=$app --key=data_dir --value=$data_dir
mkdir -p $data_dir
rsync -arz "$install_dir/live/data/" "$data_dir/" --delete-after --remove-source-files
#REMOVEME? ynh_secure_remove --file="$install_dir/live/data"
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir"
chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
chown -R $app:$app "$data_dir"
fi
2022-07-17 05:57:09 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_script_progression --message="Making sure dedicated system user exists..."
2022-07-17 05:57:09 +02:00
# Create a dedicated user (if not existing)
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir"
2022-07-17 05:57:09 +02:00
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
2021-04-30 13:49:31 +02:00
# Download, check integrity, uncompress the source of vaultwarden from app.src to his build directory
2022-10-19 23:05:00 +02:00
docker_arg=""
if [ $YNH_ARCH == "armhf" ]
then
2022-10-20 15:08:16 +02:00
docker_arg="--os_arch_variant=linux/arm/v7"
2022-10-19 23:05:00 +02:00
fi
2023-06-22 09:09:24 +02:00
ynh_docker_image_extract --dest_dir="$install_dir/build/" --image_spec="$pkg_image:$(ynh_app_upstream_version)" $docker_arg
mkdir -p "$install_dir/live/"
2019-08-05 01:29:15 +02:00
fi
2023-06-22 09:09:24 +02:00
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:$app "$install_dir"
2021-04-10 01:38:20 +02:00
2019-08-05 01:29:15 +02:00
#=================================================
2022-07-17 05:57:09 +02:00
# UPGRADE DEPENDENCIES
2019-08-05 01:29:15 +02:00
#=================================================
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_script_progression --message="Upgrading dependencies..."
2019-08-05 01:29:15 +02:00
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_install_app_dependencies $pkg_dependencies
2019-08-05 01:29:15 +02:00
#=================================================
2022-07-17 05:57:09 +02:00
# NGINX CONFIGURATION
2019-08-05 01:29:15 +02:00
#=================================================
2022-07-17 05:57:09 +02:00
ynh_script_progression --message="Upgrading NGINX web server configuration..."
2019-08-05 01:29:15 +02:00
2022-07-17 05:57:09 +02:00
# Create a dedicated NGINX config
ynh_add_nginx_config
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
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2023-06-22 09:09:24 +02:00
mv -f "$install_dir/build/vaultwarden" "$install_dir/live/vaultwarden"
#REMOVEME? ynh_secure_remove --file="$install_dir/live/web-vault/"
rsync -a "$install_dir/build/web-vault/" "$install_dir/live/web-vault/"
#REMOVEME? ynh_secure_remove --file="$install_dir/build"
2019-08-05 01:29:15 +02:00
fi
2023-06-22 09:09:24 +02:00
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:$app "$install_dir"
2022-07-18 21:17:17 +02:00
2020-12-01 22:04:05 +01:00
#=================================================
2021-04-10 23:52:18 +02:00
# UPDATE A CONFIG FILE
2020-12-01 22:04:05 +01:00
#=================================================
2021-07-29 20:05:36 +02:00
ynh_script_progression --message="Updating a configuration file..."
2020-12-01 22:04:05 +01:00
2023-06-22 09:09:24 +02:00
ynh_add_config --template="../conf/vaultwarden.env" --destination="$install_dir/live/.env"
2021-04-10 23:52:18 +02:00
2023-06-22 09:09:24 +02:00
chmod 400 "$install_dir/live/.env"
chown $app:$app "$install_dir/live/.env"
2020-12-01 22:04:05 +01:00
2019-08-05 01:29:15 +02:00
#=================================================
2021-04-10 23:52:18 +02:00
# SETUP SYSTEMD
2019-08-05 01:29:15 +02:00
#=================================================
2021-04-10 23:52:18 +02:00
ynh_script_progression --message="Upgrading systemd configuration..."
2019-08-05 01:29:15 +02:00
2021-04-10 23:52:18 +02:00
# Create a dedicated systemd config
ynh_add_systemd_config
2019-08-05 01:29:15 +02:00
2020-12-01 22:04:05 +01:00
#=================================================
# GENERIC FINALIZATION
2022-07-17 05:57:09 +02:00
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..."
mkdir -p "/var/log/$app"
chown -R $app:$app "/var/log/$app"
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
2019-08-05 01:29:15 +02:00
#=================================================
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
2021-04-30 13:49:31 +02:00
yunohost service add $app --description="$app daemon for vaultwarden" --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
2021-05-09 11:06:05 +02:00
mkdir -p "/var/log/$app"
2022-07-04 20:10:51 +02:00
touch "/var/log/$app/$app.log"
chown -R $app:$app "/var/log/$app"
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
#=================================================
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_script_progression --message="Reloading NGINX web server..."
2019-08-05 01:29:15 +02:00
2023-06-22 09:09:24 +02:00
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
2019-08-05 01:29:15 +02:00
2021-05-09 11:06:05 +02:00
#=================================================
# FINISH MIGRATION PROCESS
#=================================================
if [ $migration_process -eq 1 ]
then
ynh_script_progression --message="Bitwarden has been successfully migrated to Vaultwarden! \
A last scheduled operation will run in a couple of minutes to finish the \
migration in YunoHost side. Do not proceed any application operation while \
you don't see Vaultwarden as installed."
script_post_migration=bitwarden_post_migration.sh
cp ../conf/$script_post_migration /tmp
ynh_replace_string --match_string="__OLD_APP__" --replace_string="$old_app" --target_file=/tmp/$script_post_migration
ynh_replace_string --match_string="__NEW_APP__" --replace_string="$app" --target_file=/tmp/$script_post_migration
chmod +x /tmp/$script_post_migration
(cd /tmp; echo "/tmp/$script_post_migration > /tmp/$script_post_migration.log 2>&1" | at now + 2 minutes)
fi
2019-08-05 01:29:15 +02:00
#=================================================
# END OF SCRIPT
#=================================================
2020-12-01 22:04:05 +01:00
ynh_script_progression --message="Upgrade of $app completed"