1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/peertube_ynh.git synced 2024-09-03 19:56:29 +02:00
peertube_ynh/scripts/upgrade
Éric Gaspar 80a838b735 v2
2023-03-21 23:12:02 +01:00

343 lines
13 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source ynh_redis
source ynh_apps
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
#REMOVEME? ynh_script_progression --message="Loading installation settings..."
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
#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? port=$(ynh_app_setting_get --app=$app --key=port)
#REMOVEME? rtmp_port=$(ynh_app_setting_get --app=$app --key=rtmp_port)
#REMOVEME? db_name=$(ynh_app_setting_get --app=$app --key=db_name)
#REMOVEME? db_user=$(ynh_app_setting_get --app=$app --key=db_user)
#REMOVEME? db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
#REMOVEME? data_dir=$(ynh_app_setting_get --app=$app --key=data_dir)
admin_mail=$(ynh_user_get_info --username=$admin --key="mail")
#REMOVEME? redis_db=$(ynh_app_setting_get --app=$app --key=redis_db)
#REMOVEME? secrets_peertube=$(ynh_app_setting_get --app=$app --key=secrets_peertube)
#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..."
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
#REMOVEME? ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
# Backup the current version of the app
#REMOVEME? ynh_backup_before_upgrade
#REMOVEME? ynh_clean_setup () {
ynh_clean_check_starting
# Restore it if the upgrade fails
#REMOVEME? ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
#REMOVEME? ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..."
ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log"
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
ynh_app_setting_delete --app=$app --key=admin_pass
ynh_app_setting_delete --app=$app --key=admin_mail
# If db_name doesn't exist, create it
if [ -z "$db_name" ]; then
db_name="peertube_${app}"
#REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name
fi
# If db_user doesn't exist, create it
if [ -z "$db_user" ]; then
db_user=$app
#REMOVEME? ynh_app_setting_set --app=$app --key=db_user --value=$db_user
fi
# If db_pwd doesn't exist, create it
if [ -z "$db_pwd" ]; then
#REMOVEME? db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
#REMOVEME? ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
fi
# If redis_db doesn't exist, create it
if [ -z "$redis_db" ]; then
redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app="$app" --key=redis_db --value="$redis_db"
fi
if [ -z "$data_dir" ];
then
data_dir="/home/yunohost.app/${app}/storage"
#REMOVEME? ynh_app_setting_set --app=$app --key=data_dir --value=$data_dir
fi
# Close a port
if yunohost firewall list | grep -q "\- $port$"
then
ynh_script_progression --message="Closing port $port"
ynh_exec_warn_less yunohost firewall disallow TCP $port
fi
# Add PostgreSQL extension for v1.0.0-beta.10.pre.1
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS unaccent;" --database=$db_name
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS pg_trgm;" --database=$db_name
# Remove repository
ynh_secure_remove --file="/etc/apt/sources.list.d/yarn.list"
# Remove not needed checksum
ynh_delete_file_checksum --file="../conf/msg_install"
if [ -z "$rtmp_port" ];
then
rtmp_port=1935
ynh_port_available --port=$rtmp_port || ynh_die --message="Port $rtmp_port is needs to be available for this app"
#REMOVEME? ynh_app_setting_set --app=$app --key=rtmp_port --value=$rtmp_port
# Open the port
ynh_script_progression --message="Configuring firewall..."
ynh_exec_warn_less yunohost firewall allow TCP $rtmp_port
fi
# Cleaning legacy permissions
#REMOVEME? if ynh_legacy_permissions_exists; then
#REMOVEME? ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
# Create a permission if needed
#REMOVEME? if ! ynh_permission_exists --permission="api"; then
#REMOVEME? ynh_permission_create --permission="api" --url="/api" --allowed="visitors" --auth_header="false" --show_tile="false" --protected="true"
fi
# Remove hook
ynh_secure_remove --file="/usr/share/yunohost/hooks/conf_regen/15-nginx_$app"
yunohost tools regen-conf nginx
# Remove old log file
#REMOVEME? ynh_secure_remove --file="$data_dir/logs"
mkdir -p "/var/log/$app"
chown -R $app:$app "/var/log/$app"
#=================================================
# CREATE DEDICATED USER
#=================================================
#REMOVEME? ynh_script_progression --message="Making sure dedicated system user exists..."
# Create a dedicated user (if not existing)
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..."
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --full_replace=1 --dest_dir="$install_dir" \
--keep="config/production.yaml config/local-production.json config/local.yaml"
fi
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
#REMOVEME? ynh_script_progression --message="Upgrading dependencies..."
ynh_exec_warn_less ynh_install_apps --apps="$app_dependencies"
#REMOVEME? ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version
#REMOVEME? ynh_exec_warn_less ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg"
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..."
# Create a dedicated NGINX config
ynh_add_nginx_config
#=================================================
# SPECIFIC UPGRADE
#=================================================
# CREATE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Creating a data directory..."
mkdir -p $data_dir
chmod 750 "$data_dir"
chmod -R o-rwx "$data_dir"
chown -R $app:www-data "$data_dir"
#=================================================
# Generate secrets if they don't exist
#=================================================
if [ -z "$secrets_peertube" ]; then
ynh_print_info --message="Generating and storing PeerTube secrets..."
secrets_peertube=$(ynh_string_random --length=24)
ynh_app_setting_set --app=$app --key=secrets_peertube --value=$secrets_peertube
fi
#=================================================
# BUILD YARN DEPENDENCIES
#=================================================
#REMOVEME? ynh_script_progression --message="Building Yarn dependencies..."
pushd "$install_dir"
ynh_use_nodejs
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH yarn install --production --pure-lockfile
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH yarn cache clean
popd
#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a config file..."
ynh_add_config --template="../conf/production.yaml" --destination="$install_dir/config/production.yaml"
chmod 400 "$install_dir/config/production.yaml"
chown $app:$app "$install_dir/config/production.yaml"
ynh_backup_if_checksum_is_different --file="$install_dir/config/local-production.json"
chmod 600 "$install_dir/config/local-production.json"
chown $app:$app "$install_dir/config/local-production.json"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..."
# Create a dedicated systemd config
ynh_add_systemd_config
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
mkdir -p "/var/log/$app"
chown -R $app:$app "/var/log/$app"
# Start a systemd service
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="HTTP server listening on localhost" --timeout=5400 --length=200
#=================================================
# INSTALL LDAP PLUGIN
#=================================================
ynh_script_progression --message="Installing LDAP plugin..."
pushd "$install_dir"
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH NODE_CONFIG_DIR="$install_dir/config" NODE_ENV=production $ynh_npm run plugin:install -- --npm-name peertube-plugin-auth-ldap
popd
#=================================================
# INSTALL PEERTUBE LIVECHAT PLUGIN
#=================================================
ynh_script_progression --message="Installing PeerTube livechat plugin..."
pushd "$install_dir"
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH NODE_CONFIG_DIR="$install_dir/config" NODE_ENV=production $ynh_npm run plugin:install -- --npm-name peertube-plugin-livechat
popd
#=================================================
# PEERTUBE UPGRADE MIGRATION SCRIPT
#=================================================
if ynh_compare_current_package_version --comparison lt --version 4.0.0~ynh1; then
ynh_script_progression --message="Running Peertube 4.0.0 migration script..."
pushd "$install_dir"
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH NODE_CONFIG_DIR="$install_dir/config" NODE_ENV=production $ynh_node dist/scripts/migrations/peertube-4.0.js
popd
fi
if ynh_compare_current_package_version --comparison lt --version 4.2.0~ynh1; then
ynh_script_progression --message="Running Peertube 4.2.0 migration script..."
pushd "$install_dir"
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH NODE_CONFIG_DIR="$install_dir/config" NODE_ENV=production $ynh_node dist/scripts/migrations/peertube-4.2.js
popd
fi
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..."
# Stop a systemd service
ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log"
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..."
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --description="$app daemon for Peertube" --log="/var/log/$app/$app.log" --needs_exposed_ports $rtmp_port
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" --line_match="HTTP server listening on localhost"
#=================================================
# RELOAD NGINX
#=================================================
#REMOVEME? ynh_script_progression --message="Reloading NGINX web server..."
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed"