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
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --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)
|
|
|
|
is_public=$(ynh_app_setting_get --app=$app --key=is_public)
|
|
|
|
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
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
upgrade_type=$(ynh_check_app_version_changed)
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Ensuring downward compatibility..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
# Fix is_public as a boolean value
|
|
|
|
if [ "$is_public" = "Yes" ]; then
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=is_public --value=1
|
2019-08-05 01:29:15 +02:00
|
|
|
is_public=1
|
|
|
|
elif [ "$is_public" = "No" ]; then
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=is_public --value=0
|
2019-08-05 01:29:15 +02:00
|
|
|
is_public=0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If final_path doesn't exist, create it
|
|
|
|
if [ -z "$final_path" ]; then
|
|
|
|
final_path=/var/www/$app
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
2019-08-05 01:29:15 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --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
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Stopping a systemd service..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
2019-12-29 23:23:32 +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
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Upgrading source files..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
if [ "$upgrade_type" == "UPGRADE_APP" ]
|
|
|
|
then
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Upgrading source files..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
2019-08-22 10:38:11 +02:00
|
|
|
# Download, check integrity, uncompress the source of bitwarden_rs from app.src to his build directory
|
2019-08-05 01:29:15 +02:00
|
|
|
ynh_setup_source --dest_dir="$final_path/build/" --source_id=app
|
2019-08-22 10:38:11 +02:00
|
|
|
|
|
|
|
# Download, check integrity, uncompress and patch the source from web.src
|
2019-08-05 01:29:15 +02:00
|
|
|
ynh_setup_source --dest_dir="$final_path/live/web-vault/" --source_id=web
|
|
|
|
fi
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# NGINX CONFIGURATION
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Upgrading nginx web server configuration..."
|
2019-08-05 01:29:15 +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
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --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
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --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
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --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
|
2019-08-22 10:53:56 +02:00
|
|
|
# Install rustup with the toolchain needed by bitwarden_rs
|
2020-01-10 00:42:40 +01:00
|
|
|
pushd "$final_path"
|
2019-12-19 17:34:40 +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=$(cat build/rust-toolchain)'
|
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"
|
|
|
|
|
2019-08-22 10:38:11 +02:00
|
|
|
# Compile bitwarden_rs
|
2020-01-10 00:42:40 +01:00
|
|
|
pushd "$final_path"/build
|
2019-12-19 17:34:40 +01:00
|
|
|
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
|
|
|
|
2019-08-22 10:38:11 +02:00
|
|
|
# Install bitwarden_rs
|
2019-12-19 17:34:40 +01:00
|
|
|
cp -a "$final_path"/build/target/release/. "$final_path"/live/.
|
2019-08-05 01:29:15 +02:00
|
|
|
|
2019-08-22 10:38:11 +02: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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# MODIFY A CONFIG FILE
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Modifying a config file..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
config="$final_path/live/bitwarden_rs.env"
|
|
|
|
|
|
|
|
ynh_backup_if_checksum_is_different --file="$config"
|
|
|
|
|
|
|
|
cp -f ../conf/bitwarden_rs.env "$config"
|
|
|
|
|
|
|
|
ynh_replace_string --match_string="__ADMIN_TOKEN__" --replace_string="$admin_token" --target_file="$config"
|
|
|
|
ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$config"
|
|
|
|
ynh_replace_string --match_string="__WEBSOCKET_PORT__" --replace_string="$websocket_port" --target_file="$config"
|
|
|
|
ynh_replace_string --match_string="__ROCKET_PORT__" --replace_string="$rocket_port" --target_file="$config"
|
2019-08-08 20:47:26 +02:00
|
|
|
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="$config"
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STORE THE CONFIG FILE CHECKSUM
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Storing the config file checksum..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
# Recalculate and store the checksum of the file for the next upgrade.
|
|
|
|
ynh_store_file_checksum --file="$config"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --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
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP SYSTEMD
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Upgrading systemd configuration..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
# Create a dedicated systemd config
|
|
|
|
ynh_add_systemd_config
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# UPGRADE FAIL2BAN
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Reconfiguring fail2ban..."
|
2019-08-05 01:29:15 +02: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:.*$"
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SECURE FILES AND DIRECTORIES
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Securing files and directories..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
# Set permissions on app files
|
2019-12-19 17:34:40 +01:00
|
|
|
chown -R "$app":"$app" "$final_path"
|
2019-08-05 01:29:15 +02:00
|
|
|
mkdir -p "/var/log/$app"
|
2019-12-19 17:34:40 +01:00
|
|
|
chown -R "$app":"$app" /var/log/"$app"
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP SSOWAT
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Upgrading SSOwat configuration..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
# Make app public if necessary
|
|
|
|
if [ $is_public -eq 1 ]
|
|
|
|
then
|
|
|
|
# unprotected_uris allows SSO credentials to be passed anyway
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
|
2019-08-05 01:29:15 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# START SYSTEMD SERVICE
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Starting a systemd service..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
2020-01-10 00:42:40 +01:00
|
|
|
ynh_systemd_action --service_name=$app --action="start" --log_path=systemd --line_match="Rocket has launched from" --length=100
|
2020-01-14 22:47:01 +01:00
|
|
|
if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then
|
|
|
|
ynh_systemd_action --service_name=$app --action="restart" --log_path="systemd" --line_match="Rocket has launched from" --length=100
|
2020-01-21 20:39:25 +01:00
|
|
|
sleep 60
|
2020-01-14 22:47:01 +01:00
|
|
|
fi
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# RELOAD NGINX
|
|
|
|
#=================================================
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Reloading nginx web server..."
|
2019-08-05 01:29:15 +02:00
|
|
|
|
|
|
|
ynh_systemd_action --service_name=nginx --action=reload
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# END OF SCRIPT
|
|
|
|
#=================================================
|
|
|
|
|
2019-12-29 23:23:32 +01:00
|
|
|
ynh_print_info --message="Upgrade of $app completed"
|