2023-01-05 14:48:22 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC START
|
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
|
|
|
|
source _common.sh
|
2023-01-05 14:48:22 +01:00
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# LOAD SETTINGS
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Loading installation settings..." --weight=1
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? app=$YNH_APP_INSTANCE_NAME
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? domain=$(ynh_app_setting_get --app=$app --key=domain)
|
|
|
|
#REMOVEME? path=$(ynh_app_setting_get --app=$app --key=path)
|
|
|
|
#REMOVEME? #REMOVEME? install_dir=$(ynh_app_setting_get --app=$app --key=install_dir)
|
|
|
|
#REMOVEME? admin=$(ynh_app_setting_get --app=$app --key=admin)
|
|
|
|
#REMOVEME? port=$(ynh_app_setting_get --app=$app --key=port)
|
|
|
|
#REMOVEME? data_dir=$(ynh_app_setting_get --app=$app --key=data_dir)
|
|
|
|
#REMOVEME? db_name=$(ynh_app_setting_get --app=$app --key=db_name)
|
|
|
|
#REMOVEME? db_user=$db_name
|
|
|
|
#REMOVEME? db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
|
|
|
|
#REMOVEME? secret_key=$(ynh_app_setting_get --app=$app --key=secret_key)
|
|
|
|
#REMOVEME? lfs_jwt_secret=$(ynh_app_setting_get --app=$app --key=lfs_jwt_secret)
|
|
|
|
#REMOVEME? internal_token=$(ynh_app_setting_get --app=$app --key=internal_token)
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# CHECK VERSION
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
upgrade_type=$(ynh_check_app_version_changed)
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# STANDARD UPGRADE STEPS
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
|
|
|
# STOP SYSTEMD SERVICE
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
ynh_script_progression --message="Stopping a systemd service..." --weight=2
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Backup the current version of the app
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_backup_before_upgrade
|
|
|
|
#REMOVEME? ynh_clean_setup () {
|
2023-01-11 10:32:49 +01:00
|
|
|
# Restore it if the upgrade fails
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_restore_upgradebackup
|
2023-01-11 10:32:49 +01:00
|
|
|
}
|
2023-01-05 14:48:22 +01:00
|
|
|
# Exit if an error occurs during the execution of the script
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_abort_if_errors
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
|
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
|
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# If db_name doesn't exist, create it
|
|
|
|
if [ -z "$db_name" ]; then
|
|
|
|
db_name=$(ynh_sanitize_dbid --db_name=$app)
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=db_name --value=$db_name
|
2023-01-11 10:32:49 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# If port doesn't exist, create it
|
|
|
|
if [ -z "$port" ]; then
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? port=$(ynh_find_port --port=6000)
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=port --value=$port
|
2023-01-11 10:32:49 +01:00
|
|
|
fi
|
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
# If install_dir doesn't exist, create it
|
|
|
|
if [ -z "$install_dir" ]; then
|
|
|
|
#REMOVEME? install_dir=/opt/$app
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=install_dir --value=$install_dir
|
2023-01-05 14:48:22 +01:00
|
|
|
fi
|
|
|
|
|
2023-01-22 08:10:43 +01:00
|
|
|
# If secret_key doesn't exist, create it
|
|
|
|
if [ -z "$secret_key" ]; then
|
2023-03-10 17:13:54 +01:00
|
|
|
secret_key=$($install_dir/forgejo generate secret SECRET_KEY)
|
2023-01-22 08:10:43 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=secret_key --value=$secret_key
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If lfs_jwt_secret doesn't exist, create it
|
|
|
|
if [ -z "$lfs_jwt_secret" ]; then
|
2023-03-10 17:13:54 +01:00
|
|
|
lfs_jwt_secret=$($install_dir/forgejo generate secret JWT_SECRET)
|
2023-01-22 08:10:43 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=lfs_jwt_secret --value=$lfs_jwt_secret
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If internal_token doesn't exist, create it
|
|
|
|
if [ -z "$internal_token" ]; then
|
2023-03-10 17:13:54 +01:00
|
|
|
internal_token=$($install_dir/forgejo generate secret INTERNAL_TOKEN)
|
2023-01-22 08:10:43 +01:00
|
|
|
ynh_app_setting_set --app=$app --key=internal_token --value=$internal_token
|
|
|
|
fi
|
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
# If data_dir doesn't exist, create it
|
|
|
|
if [ -z "$data_dir" ]; then
|
|
|
|
data_dir=/home/yunohost.app/$app
|
|
|
|
#REMOVEME? ynh_app_setting_set --app=$app --key=data_dir --value=$data_dir
|
|
|
|
mkdir -p $data_dir
|
|
|
|
mkdir -p "$data_dir/data/{repositories,avatars,attachments}"
|
|
|
|
mkdir -p "$data_dir/.ssh"
|
|
|
|
|
|
|
|
chmod 750 "$data_dir"
|
|
|
|
chmod -R o-rwx "$data_dir"
|
|
|
|
chown -R $app:$app "$data_dir"
|
|
|
|
chmod u=rwx,g=,o= "$data_dir/.ssh"
|
2023-01-05 14:48:22 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
# CREATE DEDICATED USER
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Create a dedicated user (if not existing)
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_system_user_create --username=$app --home_dir="$install_dir" --groups ssh.app
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Setting up source files..." --weight=3
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
ynh_setup_source --dest_dir=$install_dir --source_id=$YNH_ARCH
|
|
|
|
chmod 750 "$install_dir"
|
|
|
|
chmod -R o-rwx "$install_dir"
|
|
|
|
chown -R $app:$app "$install_dir"
|
|
|
|
chmod +x "$install_dir/forgejo"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# ADD A CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Adding a configuration file..." --weight=1
|
|
|
|
|
|
|
|
ssh_port=$(grep -P "Port\s+\d+" /etc/ssh/sshd_config | grep -P -o "\d+")
|
2023-03-10 17:13:54 +01:00
|
|
|
ynh_add_config --template="app.ini" --destination="$install_dir/custom/conf/app.ini"
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
chmod 640 "$install_dir/custom/conf/app.ini"
|
|
|
|
chown $app:$app "$install_dir/custom/conf/app.ini"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
# SETUP SYSTEMD
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
|
|
|
|
ynh_add_systemd_config
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# NGINX CONFIGURATION
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2
|
|
|
|
ynh_add_nginx_config
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# UPGRADE DEPENDENCIES
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Upgrading dependencies..." --weight=10
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring log rotation..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Use logrotate to manage application logfile(s)
|
2023-02-25 22:09:33 +01:00
|
|
|
ynh_use_logrotate --logfile "/var/log/$app" --nonappend
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
chown -R $app:$app "/var/log/$app"
|
|
|
|
chmod u=rwX,g=rX,o= "/var/log/$app"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# INTEGRATE SERVICE IN YUNOHOST
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Integrating service in YunoHost..." --weight=2
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
yunohost service add $app --description="Forgejo" --log="/var/log/$app/forgejo.log"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# START SYSTEMD SERVICE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Starting a systemd service..." --weight=1
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
# Start a systemd service
|
2023-01-06 10:12:01 +01:00
|
|
|
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/forgejo.log" --line_match="Starting new Web server: tcp:127.0.0.1:"
|
2023-01-05 14:48:22 +01:00
|
|
|
|
2023-01-11 10:32:49 +01:00
|
|
|
#=================================================
|
|
|
|
# SETUP FAIL2BAN
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring Fail2Ban..." --weight=1
|
|
|
|
|
|
|
|
ynh_add_fail2ban_config --logpath "/var/log/$app/forgejo.log" --failregex ".*Failed authentication attempt for .* from <HOST>" --max_retry 5
|
|
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# RELOAD NGINX
|
|
|
|
#=================================================
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_script_progression --message="Reloading NGINX web server..." --weight=1
|
2023-01-11 10:32:49 +01:00
|
|
|
|
2023-03-10 17:13:54 +01:00
|
|
|
#REMOVEME? ynh_systemd_action --service_name=nginx --action=reload
|
2023-01-05 14:48:22 +01:00
|
|
|
|
|
|
|
#=================================================
|
2023-01-11 10:32:49 +01:00
|
|
|
# END OF SCRIPT
|
2023-01-05 14:48:22 +01:00
|
|
|
#=================================================
|
|
|
|
|
|
|
|
ynh_script_progression --message="Upgrade of $app completed" --last
|