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
|
|
|
|
#=================================================
|
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)
|
2022-01-14 21:31:33 +01:00
|
|
|
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
|
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)
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# 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
|
2021-04-10 01:38:20 +02:00
|
|
|
# Restore it if the upgrade fails
|
2019-08-05 01:29:15 +02:00
|
|
|
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
|
|
|
|
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
|
|
|
|
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
|
|
|
|
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
|
|
|
|
|
2021-05-11 01:40:02 +02:00
|
|
|
# Move config file
|
2022-01-23 19:54:02 +01:00
|
|
|
mv $final_path/live/bitwarden_rs.env $final_path/live/.env
|
2021-05-11 01:40:02 +02:00
|
|
|
ynh_delete_file_checksum --file="/var/www/$old_app/live/bitwarden_rs.env"
|
2022-01-23 19:54:02 +01:00
|
|
|
ynh_store_file_checksum --file="$final_path/live/.env"
|
2021-05-09 11:06:05 +02:00
|
|
|
ynh_secure_remove --file="$final_path/live/bitwarden_rs"
|
2021-05-11 01:40:02 +02:00
|
|
|
|
|
|
|
# Manage permissions
|
|
|
|
ynh_permission_update --permission="main" --add="all_users"
|
|
|
|
ynh_permission_update --permission="main" --add="visitors"
|
|
|
|
|
|
|
|
# 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
|
|
|
|
if ynh_legacy_permissions_exists; then
|
|
|
|
ynh_legacy_permissions_delete_all
|
|
|
|
|
|
|
|
ynh_app_setting_delete --app=$app --key=is_public
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! ynh_permission_exists --permission="admin"; then
|
|
|
|
# Create the required permissions
|
|
|
|
ynh_permission_create --permission="admin" --url="/admin" --allowed=$admin --show_tile="false"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2022-01-24 22:21:08 +01:00
|
|
|
# If datadir doesn't exist, create it
|
|
|
|
if [ -z $datadir ]; then
|
2022-07-17 05:57:09 +02:00
|
|
|
ynh_script_progression --message="Making sure data directory exists..."
|
2022-01-24 22:21:08 +01:00
|
|
|
datadir=/home/yunohost.app/$app
|
|
|
|
ynh_app_setting_set --app=$app --key=datadir --value=$datadir
|
|
|
|
mkdir -p $datadir
|
|
|
|
rsync -arz "$final_path/live/data/" "$datadir/" --delete-after --remove-source-files
|
|
|
|
ynh_secure_remove --file="$final_path/live/data"
|
2022-08-30 20:31:43 +02:00
|
|
|
ynh_system_user_create --username=$app --home_dir="$final_path"
|
2022-01-24 22:21:08 +01:00
|
|
|
chmod 750 "$datadir"
|
|
|
|
chmod -R o-rwx "$datadir"
|
|
|
|
chown -R $app:$app "$datadir"
|
|
|
|
fi
|
|
|
|
|
2022-07-17 05:57:09 +02:00
|
|
|
#=================================================
|
|
|
|
# CREATE DEDICATED USER
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Making sure dedicated system user exists..."
|
|
|
|
|
|
|
|
# Create a dedicated user (if not existing)
|
|
|
|
ynh_system_user_create --username=$app --home_dir="$final_path"
|
|
|
|
|
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-09-07 07:40:00 +02:00
|
|
|
ynh_setup_source_docker --dest_dir="$final_path/build"
|
2022-07-18 21:17:17 +02:00
|
|
|
mkdir -p "$final_path/live/"
|
2019-08-05 01:29:15 +02:00
|
|
|
fi
|
|
|
|
|
2021-04-17 18:30:49 +02:00
|
|
|
chmod 750 "$final_path"
|
2021-04-10 01:38:20 +02:00
|
|
|
chmod -R o-rwx "$final_path"
|
2021-04-11 20:32:45 +02:00
|
|
|
chown -R $app:$app "$final_path"
|
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
|
|
|
#=================================================
|
2022-07-17 05:57:09 +02:00
|
|
|
ynh_script_progression --message="Upgrading dependencies..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
2022-07-17 05:57:09 +02:00
|
|
|
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
|
2022-09-02 22:17:22 +02:00
|
|
|
mv -f "$final_path/build/vaultwarden" "$final_path/live/vaultwarden"
|
2022-07-18 21:17:17 +02:00
|
|
|
ynh_secure_remove --file="$final_path/live/web-vault/"
|
2022-09-02 22:17:22 +02:00
|
|
|
rsync -a "$final_path/build/web-vault/" "$final_path/live/web-vault/"
|
2020-01-12 21:42:15 +01:00
|
|
|
ynh_secure_remove --file="$final_path/build"
|
2019-08-05 01:29:15 +02:00
|
|
|
fi
|
|
|
|
|
2022-07-18 21:17:17 +02:00
|
|
|
chmod 750 "$final_path"
|
|
|
|
chmod -R o-rwx "$final_path"
|
|
|
|
chown -R $app:$app "$final_path"
|
|
|
|
|
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
|
|
|
|
2022-01-23 19:54:02 +01:00
|
|
|
ynh_add_config --template="../conf/vaultwarden.env" --destination="$final_path/live/.env"
|
2021-04-10 23:52:18 +02:00
|
|
|
|
2022-01-23 19:54:02 +01:00
|
|
|
chmod 400 "$final_path/live/.env"
|
|
|
|
chown $app:$app "$final_path/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
|
|
|
|
#=================================================
|
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
|
|
|
|
|
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"
|