yunohost-gitea_ynh/scripts/upgrade

304 lines
10 KiB
Plaintext
Raw Normal View History

2018-02-25 21:45:24 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
2020-11-17 23:33:50 +01:00
# Load common variables and helpers
source ./experimental_helper.sh
source ./_common.sh
2018-02-25 21:45:24 +01:00
# IMPORT GENERIC HELPERS
source /usr/share/yunohost/helpers
2022-07-22 23:51:16 +02:00
#=================================================
# LOAD SETTINGS
#=================================================
2019-09-10 22:43:45 +02:00
ynh_script_progression --message="Loading installation settings..."
2022-07-22 23:51:16 +02:00
2022-07-22 23:53:41 +02:00
domain=$(ynh_app_setting_get --app=$app --key=domain)
2019-10-01 21:34:41 +02:00
path_url=$(ynh_normalize_url_path --path_url $(ynh_app_setting_get --app $app --key path))
2022-07-22 23:53:41 +02:00
db_password=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
admin=$(ynh_app_setting_get --app=$app --key=adminusername)
key=$(ynh_app_setting_get --app=$app --key=secret_key)
lfs_key=$(ynh_app_setting_get --app=$app --key=lfs_key)
port=$(ynh_app_setting_get --app=$app --key=web_port)
upstream_version=$(ynh_app_setting_get --app=$app --key=upstream_version)
2018-02-25 21:45:24 +01:00
2022-07-24 11:39:51 +02:00
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=1
# We stop the service before to set ynh_clean_setup
ynh_systemd_action --service_name=$app --action="stop"
2020-12-07 15:11:16 +01:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2022-07-22 23:51:16 +02:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
2020-12-07 15:11:16 +01:00
if [ "0$(ynh_app_setting_get --app=$app --key=disable_backup_before_upgrade)" -ne 1 ]
then
ynh_backup_before_upgrade
ynh_clean_setup () {
# Clean installation remainings that are not handled by the remove script.
ynh_clean_check_starting
ynh_restore_upgradebackup
}
fi
2018-02-25 21:45:24 +01:00
2022-07-22 23:51:16 +02:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
# If lfs_key doesn't exist, create it
if [ -z "$lfs_key" ]; then
lfs_key=$(ynh_string_random)
ynh_app_setting_set --app=$app --key=lfs_key --value=$lfs_key
fi
2018-02-25 21:45:24 +01:00
#=================================================
2019-02-15 11:33:29 +01:00
# MIGRATION FROM GOGS
2018-02-25 21:45:24 +01:00
#=================================================
2019-02-15 11:33:29 +01:00
[[ $YNH_APP_ID == "gogs" ]] \
&& [[ "$(cat "/opt/$app/templates/.VERSION")" != 0.11.79.1211 ]] \
2022-07-24 20:41:48 +02:00
&& ynh_die --message "It look like that you have an old Gogs install. You need first upgrade Gogs instance (id: $gogs_migrate_id) and after migrate to Gitea."
2020-11-17 23:33:50 +01:00
ynh_handle_app_migration --migration_id=gogs --migration_list=gogs_migrations
2019-02-15 11:33:29 +01:00
if [[ $migration_process -eq 1 ]]; then
# Reload variables
dbname=$app
2022-04-01 22:54:46 +02:00
db_user=$app
2019-02-15 11:33:29 +01:00
final_path="/opt/$app"
2022-04-23 19:40:27 +02:00
datadir="/home/""$app"
repos_path="$datadir/repositories"
data_path="$datadir/data"
2019-02-15 11:33:29 +01:00
2019-03-02 00:00:55 +01:00
# Replace the user
ynh_system_user_delete $old_app
test getent passwd "$app" &>/dev/null || \
2022-04-23 19:40:27 +02:00
useradd -d "$datadir" --system --user-group "$app" --shell /bin/bash || \
2019-08-01 14:01:59 +02:00
ynh_die --message "Unable to create $app system account"
2019-03-02 00:00:55 +01:00
2019-02-15 11:33:29 +01:00
# Clean old binary
2019-06-09 21:05:14 +02:00
ynh_secure_remove --file=$final_path/gogs
ynh_secure_remove --file=$final_path/custom/conf/auth.d
2019-02-15 11:33:29 +01:00
# Restore authentication from SQL database
2019-09-10 21:26:06 +02:00
ynh_replace_string --match_string __APP__ --replace_string "$app" --target_file ../conf/login_source.sql
2022-04-23 19:40:27 +02:00
ynh_mysql_connect_as "$db_user" "$db_password" "$dbname" < ../conf/login_source.sql
2019-02-15 11:33:29 +01:00
2019-03-02 00:16:46 +01:00
# Fix hooks
2022-04-23 19:40:27 +02:00
if [[ -e $repos_path ]];then
ls $repos_path/*/*.git/hooks/pre-receive | while read p; do
2019-06-09 21:05:14 +02:00
ynh_secure_remove --file=$p
2019-03-02 00:16:46 +01:00
done
2022-04-23 19:40:27 +02:00
ls $repos_path/*/*.git/hooks/post-receive | while read p; do
2019-06-09 21:05:14 +02:00
ynh_secure_remove --file=$p
2019-03-02 00:16:46 +01:00
done
fi
2019-02-15 11:33:29 +01:00
upstream_version="0.0.1"
2018-02-25 21:45:24 +01:00
fi
2022-04-23 19:40:27 +02:00
# Move data directory
if [ -e "/home/""$app" ] && [ ! -e $datadir ]; then
mv "/home/""$app" "$datadir"
fi
2018-02-25 21:45:24 +01:00
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
2019-09-10 22:43:45 +02:00
ynh_script_progression --message="Configuring application..."
2018-02-25 21:45:24 +01:00
# Clean template to fix issue : https://github.com/gogits/gogs/issues/4585
2019-06-09 21:05:14 +02:00
ynh_secure_remove --file="/opt/$app/templates"
2018-02-25 21:45:24 +01:00
2018-06-15 22:08:34 +02:00
# Configure gitea with app.ini file
config_gitea
2018-02-25 21:45:24 +01:00
# Configure init script
2019-09-10 22:43:45 +02:00
ynh_script_progression --message="Updating systemd units..."
2018-02-25 21:45:24 +01:00
ynh_add_systemd_config
2022-07-24 16:35:00 +02:00
# Modify Nginx configuration file and copy it to NGINX conf directory
ynh_script_progression --message="Configuring NGINX..." --weight=1
2018-02-25 21:45:24 +01:00
config_nginx
2019-02-15 11:33:29 +01:00
#=================================================
# DB migration
#=================================================
2019-09-10 22:43:45 +02:00
ynh_script_progression --message="Upgrading database and sources..." --weight=6
# Before the version 1.7 the upstream version was not stored
# The way to find the version for the install < 1.7 is to parse the binary file to find which version is installed
if [ -z ${upstream_version:-} ]; then
for version in "0.0." "1.0." "1.1." "1.2." "1.3." "1.4." "1.5." "1.6." "1.7."; do
if strings $final_path/gitea | grep -P "^${version//./\\.}\d"; then
upstream_version="${version}0"
break
fi
done
fi
2019-02-15 11:33:29 +01:00
restart_gitea() {
# Set permissions
set_permission
2022-07-24 20:41:48 +02:00
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/gitea.log" --line_match="Starting new Web server: tcp:127.0.0.1:"
2019-02-15 11:33:29 +01:00
# Leave the time to update the database schema
sleep 5
systemctl stop $app
}
case $upstream_version in
"0.0."* )
2019-02-15 11:33:29 +01:00
ynh_setup_source $final_path source/${architecture}_1.0
set_permission
systemctl start $app
sleep 20
systemctl stop $app
;&
"1.0."* )
ynh_setup_source $final_path source/${architecture}_1.1
restart_gitea
;&
"1.1."* )
ynh_setup_source $final_path source/${architecture}_1.2
restart_gitea
;&
"1.2."* )
ynh_setup_source $final_path source/${architecture}_1.3
restart_gitea
;&
"1.3."* )
ynh_setup_source $final_path source/${architecture}_1.4
restart_gitea
;&
"1.4."* )
ynh_setup_source $final_path source/${architecture}_1.5
restart_gitea
;&
"1.5."* )
ynh_setup_source $final_path source/${architecture}_1.6
restart_gitea
;&
2019-05-05 12:02:02 +02:00
"1.6."* )
ynh_setup_source $final_path source/${architecture}_1.7
restart_gitea
;&
2019-08-01 14:17:11 +02:00
"1.7."* )
ynh_setup_source $final_path source/${architecture}_1.8
restart_gitea
;&
2019-12-13 00:20:45 +01:00
"1.8."* )
ynh_setup_source $final_path source/${architecture}_1.9
restart_gitea
;&
2020-02-10 22:03:25 +01:00
"1.9."* )
ynh_setup_source $final_path source/${architecture}_1.10
restart_gitea
;&
2020-06-18 22:03:28 +02:00
"1.10."* )
ynh_setup_source $final_path source/${architecture}_1.11
restart_gitea
;&
2020-12-02 22:03:22 +01:00
"1.11."* )
ynh_setup_source $final_path source/${architecture}_1.12
restart_gitea
;&
2021-04-11 22:03:17 +02:00
"1.12."* )
ynh_setup_source $final_path source/${architecture}_1.13
restart_gitea
;&
2021-08-22 22:03:20 +02:00
"1.13."* )
ynh_setup_source $final_path source/${architecture}_1.14
restart_gitea
;&
2022-02-01 22:05:20 +01:00
"1.14."* )
ynh_setup_source $final_path source/${architecture}_1.15
restart_gitea
;&
2022-07-31 22:03:23 +02:00
"1.15."* )
ynh_setup_source $final_path source/${architecture}_1.16
restart_gitea
;&
"1.16."* )
ynh_setup_source $final_path source/${architecture}_1.16
restart_gitea
;&
2019-02-15 11:33:29 +01:00
esac
2019-08-01 15:06:08 +02:00
# Install gitea source
2019-02-15 11:33:29 +01:00
ynh_setup_source $final_path source/$architecture
restart_gitea
2019-02-15 11:33:29 +01:00
2019-02-03 15:05:38 +01:00
# SETUP FAIL2BAN
2022-07-24 16:35:00 +02:00
ynh_script_progression --message="Configuring Fail2Ban..."
ynh_add_fail2ban_config --logpath="/var/log/$app/gitea.log" --failregex=".*Failed authentication attempt for .* from <HOST>" --max_retry 5
2019-02-03 15:05:38 +01:00
2018-02-25 21:45:24 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2020-03-25 16:33:19 +01:00
# Set all permissions
ynh_script_progression --message="Update permission..."
if ! ynh_permission_exists --permission admin; then
ynh_app_setting_delete --app $app --key unprotected_uris
2022-07-24 16:35:00 +02:00
ynh_permission_create --permission="admin" --allowed="$admin"
2020-03-25 16:33:19 +01:00
# Update ldap config
2022-07-24 16:35:00 +02:00
ynh_replace_string --match_string="__APP__" --replace_string="$app" --target_file="../conf/login_source.sql"
2022-04-23 19:40:27 +02:00
ynh_mysql_connect_as "$db_user" "$db_password" "$dbname" < ../conf/login_source.sql
2020-03-25 16:33:19 +01:00
fi
2020-11-17 23:33:50 +01:00
# Add gitea to YunoHost's monitored services
2022-07-24 16:35:00 +02:00
ynh_script_progression --message="Register Gitea service..."
yunohost service add "$app" --log="/var/log/$app/gitea.log"
2020-11-17 23:33:50 +01:00
# Add ssh permission for gitea user
adduser $app ssh.app
2018-02-25 21:45:24 +01:00
# Set permissions
2019-09-10 22:43:45 +02:00
ynh_script_progression --message="Protecting directory"
2018-02-25 21:45:24 +01:00
set_permission
2019-02-15 11:33:29 +01:00
# Save Version
2022-07-24 16:35:00 +02:00
ynh_app_setting_set --app=$app --key=upstream_version --value=$(ynh_app_upstream_version)
2019-02-15 11:33:29 +01:00
2018-02-25 21:45:24 +01:00
# Reload services
2022-07-24 16:35:00 +02:00
ynh_script_progression --message="Starting Gitea services..." --weight=3
#ynh_systemd_action -l "Starting new Web server: tcp:127.0.0.1:" -p "/var/log/$app/gitea.log" -t 10
#sleep 1
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/gitea.log" --line_match="Starting new Web server: tcp:127.0.0.1:"
# Store the checksum with the 'INTERNAL_TOKEN' value.
# Should be removed when the issue https://github.com/go-gitea/gitea/issues/3246 is fixed
2022-07-24 16:35:00 +02:00
ynh_store_file_checksum --file="$final_path/custom/conf/app.ini"
2019-02-15 11:33:29 +01:00
#=================================================
# FINISH MIGRATION PROCESS
#=================================================
if [[ $migration_process -eq 1 ]]; then
2022-07-24 16:35:00 +02:00
echo "Gogs has been successfully migrated to Gitea! \
2019-02-15 11:33:29 +01:00
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 Gogs as installed." >&2
# Execute a post migration script after the end of this upgrade.
# Mainly for some cleaning
script_post_migration=gogs_post_migration.sh
2019-09-10 21:26:06 +02:00
ynh_replace_string --match_string __OLD_APP__ --replace_string "$old_app" --target_file ../conf/$script_post_migration
ynh_replace_string --match_string __NEW_APP__ --replace_string "$app" --target_file ../conf/$script_post_migration
2019-02-15 11:33:29 +01:00
cp ../conf/$script_post_migration /tmp
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-10-27 14:54:35 +01:00
ynh_script_progression --message="Upgrade of $app completed" --last