2018-02-25 21:45:24 +01:00
#!/bin/bash
2023-09-09 11:17:52 +02:00
source _common.sh
source /usr/share/yunohost/helpers
2022-07-24 11:39:51 +02:00
2022-07-22 23:51:16 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2024-08-30 22:57:28 +02:00
ynh_script_progression 'Ensuring downward compatibility...'
2022-07-22 23:51:16 +02:00
2024-08-30 22:57:28 +02:00
if ynh_app_upgrading_from_version_before 1.6.4~ynh1; then
2023-11-29 10:58:11 +01:00
message="Upgrade from $YNH_APP_CURRENT_VERSION was dropped. You need to run this, then upgrade to the latest version:
sudo yunohost app upgrade $app -u https://github.com/YunoHost-Apps/gitea_ynh/commit/a9ceb157032ae2290e944c1d0a255451ff2d133d"
ynh_print_info "$message"
2024-08-30 22:57:28 +02:00
ynh_die "$message" --ret_code=1
2022-07-22 23:51:16 +02:00
fi
2023-11-29 10:58:11 +01:00
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
2024-08-30 22:57:28 +02:00
ynh_script_progression 'Stopping $app's systemd service...'
2023-11-29 10:58:11 +01:00
2024-08-30 22:57:28 +02:00
ynh_systemctl --service="$app" --action=stop
2023-11-29 10:58:11 +01:00
#=================================================
# MIGRATION STEP 1 (migrate path from packagin v1)
#=================================================
2023-09-09 11:17:52 +02:00
# This is not handled by the core because the previous package did not define final_path...
if [ -d "/opt/$app" ]; then
# This is the package version of the manifestv2
2024-02-02 15:29:47 +01:00
mv -t "$install_dir/" "/opt/$app/"*
2024-08-30 22:57:28 +02:00
ynh_safe_rm "/opt/$app"
2023-09-09 11:17:52 +02:00
fi
2018-02-25 21:45:24 +01:00
2023-09-09 11:17:52 +02:00
# Move data directory
2023-12-06 11:56:38 +01:00
if [ -d /home"/$app" ]; then
mv /home"/$app/" "$data_dir"
2023-09-09 11:17:52 +02:00
fi
2019-02-15 11:33:29 +01:00
2023-09-09 11:17:52 +02:00
# Ensure the user has the correct home dir
if [ ~"$app" != "$data_dir" ]; then
usermod --home "$data_dir" "$app"
fi
2019-02-15 11:33:29 +01:00
2024-08-30 22:57:28 +02:00
# FIXME: this is still supported but the recommendation is now to *always* re-setup the app sources wether or not the upstream sources changed
if ynh_app_upstream_version_changed; then
ynh_setup_source --dest_dir="$install_dir" --source_id=main --full_replace --keep=custom
2024-04-09 22:21:11 +02:00
fi
2019-03-02 00:16:46 +01:00
2023-09-09 11:17:52 +02:00
#=================================================
2023-11-29 10:58:11 +01:00
# MIGRATION STEP 2 (Set undefined Vars)
2023-09-09 11:17:52 +02:00
#=================================================
2022-09-05 22:43:19 +02:00
2024-08-30 22:57:28 +02:00
ynh_script_progression 'Migrating missing settings...'
2024-02-07 12:06:46 +01:00
# Must set permission before to call gitea command
2024-08-30 22:57:28 +02:00
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chown -R "$app:$app" "$install_dir"
#REMOVEME? Assuming the install dir is setup using ynh_setup_source, the proper chmod/chowns are now already applied and it shouldn't be necessary to tweak perms | chmod -R u=rwX,g=rX,o= "$install_dir"
2024-02-07 12:06:46 +01:00
chmod +x "$install_dir/gitea"
2024-08-30 22:57:28 +02:00
# FIXMEhelpers2.1: maybe replace with: ynh_app_setting_set_default --key=internal_token --value="$(ynh_exec_as_app "$install_dir/gitea" generate secret INTERNAL_TOKEN)"
2023-11-29 10:58:11 +01:00
if [[ -z "${internal_token:-}" ]]; then
2024-08-30 22:57:28 +02:00
internal_token="$(ynh_exec_as_app "$install_dir/gitea" generate secret INTERNAL_TOKEN)"
ynh_app_setting_set --key=internal_token --value="$internal_token"
2023-11-29 10:58:11 +01:00
fi
2023-09-09 11:17:52 +02:00
2024-08-30 22:57:28 +02:00
# FIXMEhelpers2.1: maybe replace with: ynh_app_setting_set_default --key=secret_key --value="$(ynh_exec_as_app "$install_dir/gitea" generate secret SECRET_KEY)"
2023-11-29 10:58:11 +01:00
if [[ -z "${secret_key:-}" ]]; then
2024-08-30 22:57:28 +02:00
secret_key="$(ynh_exec_as_app "$install_dir/gitea" generate secret SECRET_KEY)"
ynh_app_setting_set --key=secret_key --value="$secret_key"
2023-11-29 10:58:11 +01:00
fi
2023-09-09 11:17:52 +02:00
2023-11-29 10:58:11 +01:00
if [[ -n "${lfs_key:-}" ]]; then
lfs_jwt_secret="$lfs_key"
2024-08-30 22:57:28 +02:00
ynh_app_setting_delete --key=lfs_key
ynh_app_setting_set --key=lfs_jwt_secret --value="$lfs_jwt_secret"
2023-11-29 10:58:11 +01:00
fi
2023-09-09 11:17:52 +02:00
2024-08-30 22:57:28 +02:00
# FIXMEhelpers2.1: maybe replace with: ynh_app_setting_set_default --key=lfs_jwt_secret --value="$(ynh_exec_as_app "$install_dir/gitea" generate secret JWT_SECRET)"
2023-11-29 10:58:11 +01:00
if [[ -z "${lfs_jwt_secret:-}" ]]; then
2024-08-30 22:57:28 +02:00
lfs_jwt_secret="$(ynh_exec_as_app "$install_dir/gitea" generate secret JWT_SECRET)"
ynh_app_setting_set --key=lfs_jwt_secret --value="$lfs_jwt_secret"
2023-11-29 10:58:11 +01:00
fi
2022-04-23 19:40:27 +02:00
2024-08-30 22:57:28 +02:00
# FIXMEhelpers2.1: maybe replace with: ynh_app_setting_set_default --key=jwt_secret --value="$(ynh_exec_as_app "$install_dir/gitea" generate secret JWT_SECRET)"
2023-12-13 10:38:10 +01:00
if [[ -z "${jwt_secret:-}" ]]; then
2024-08-30 22:57:28 +02:00
jwt_secret="$(ynh_exec_as_app "$install_dir/gitea" generate secret JWT_SECRET)"
ynh_app_setting_set --key=jwt_secret --value="$jwt_secret"
2023-12-13 10:38:10 +01:00
fi
2024-02-05 19:12:58 +01:00
#=================================================
# MIGRATION STEP Fix regression linked to 77c3678 and #76
#=================================================
2024-04-27 00:07:37 +02:00
list_param_sql="$(yunohost user list --output-as json | jq -c '.users | keys' | sed 's|\[|\(|' | sed 's|\]|\)|')"
2024-08-30 22:57:28 +02:00
ynh_replace --match=__APP__ --replace="$app" --file=./fix_user_db_for_local_users.sql
ynh_replace --match=__USER_LIST__ --replace="$list_param_sql" --file=./fix_user_db_for_local_users.sql
ynh_mysql_db_shell < ./fix_user_db_for_local_users.sql
2024-02-05 19:12:58 +01:00
2023-09-09 11:17:52 +02:00
#=================================================
# UPDATE A CONFIG FILE
2018-02-25 21:45:24 +01:00
#=================================================
2024-08-30 22:57:28 +02:00
ynh_script_progression 'Updating configuration files...'
2019-09-10 22:43:45 +02:00
2018-02-25 21:45:24 +01:00
# Clean template to fix issue : https://github.com/gogits/gogs/issues/4585
2024-08-30 22:57:28 +02:00
ynh_safe_rm "$install_dir/templates"
2018-02-25 21:45:24 +01:00
2018-06-15 22:08:34 +02:00
# Configure gitea with app.ini file
2024-08-30 22:57:28 +02:00
ynh_config_add --template=app.ini --destination="$install_dir/custom/conf/app.ini"
2024-02-07 12:06:46 +01:00
2024-08-30 22:57:28 +02:00
ynh_script_progression 'Setting permissions...'
2023-11-29 10:58:11 +01:00
_set_permissions
2018-02-25 21:45:24 +01:00
2024-01-24 22:58:29 +01:00
# Update ldap config
2024-02-05 22:45:24 +01:00
# Note that the 'GroupTeamMap' depends of the user need so we can't apply a generic values for all instances
# So to avoid to override the value after each update we retrive and apply the user value
group_team_map_config=$(mysql --user="$db_user" --password="$db_pwd" --batch --raw "$db_name" <<< 'SELECT `cfg` FROM `'$app'`.`login_source` WHERE `id`=1;' \
| tail -n+2 | jq '.GroupTeamMap')
sed -i 's|__GROUP_TEAM_MAP__|'"${group_team_map_config//\\/\\\\\\\\}"'|g' ./login_source.sql
2024-08-30 22:57:28 +02:00
ynh_replace --match=__APP__ --replace="$app" --file=./login_source.sql
ynh_mysql_db_shell < ./login_source.sql
2024-01-24 22:58:29 +01:00
2019-02-15 11:33:29 +01:00
#=================================================
2023-11-29 10:58:11 +01:00
# REAPPLY SYSTEM CONFIGURATIONS
2019-02-15 11:33:29 +01:00
#=================================================
2024-08-30 22:57:28 +02:00
ynh_script_progression "Upgrading system configurations related to $app..."
2019-09-10 22:43:45 +02:00
2024-08-30 22:57:28 +02:00
ynh_config_add_nginx
2019-02-15 11:33:29 +01:00
2024-08-30 22:57:28 +02:00
ynh_config_add_systemd
2019-02-15 11:33:29 +01:00
2023-11-29 10:58:11 +01:00
yunohost service add "$app" --log="/var/log/$app/gitea.log"
2019-02-03 15:05:38 +01:00
2024-08-30 22:57:28 +02:00
ynh_config_add_logrotate
2020-03-25 16:33:19 +01:00
2024-08-30 22:57:28 +02:00
ynh_config_add_fail2ban --logpath="/var/log/$app/gitea.log" --failregex=".*Failed authentication attempt for .* from <HOST>"
2019-02-15 11:33:29 +01:00
#=================================================
2023-09-09 11:17:52 +02:00
# START SYSTEMD SERVICE
2019-02-15 11:33:29 +01:00
#=================================================
2024-08-30 22:57:28 +02:00
ynh_script_progression 'Starting $app's systemd service...'
2019-02-15 11:33:29 +01:00
2024-08-30 22:57:28 +02:00
ynh_systemctl --service="$app" --action=start --log_path="/var/log/$app/gitea.log" --wait_until="$systemd_match_start_line"
2019-02-15 11:33:29 +01:00
2023-09-09 11:17:52 +02:00
#=================================================
# END OF SCRIPT
#=================================================
2019-10-27 14:54:35 +01:00
2024-08-30 22:57:28 +02:00
ynh_script_progression "Upgrade of $app completed"