1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/gitlab_ynh.git synced 2024-09-03 18:36:35 +02:00
gitlab_ynh/scripts/restore
2019-05-11 01:42:05 +02:00

174 lines
7.3 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
ynh_exec_warn_less ynh_secure_remove --file="$tempdir"
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading settings..." --weight=1
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
config_path=$(ynh_app_setting_get --app=$app --key=config_path)
port=$(ynh_app_setting_get --app="$app" --key=web_port)
portUnicorn=$(ynh_app_setting_get --app="$app" --key=unicorn_port)
architecture=$(ynh_app_setting_get --app="$app" --key=architecture)
#=================================================
# CHECK IF THE APP CAN BE RESTORED
#=================================================
ynh_script_progression --message="Validating restoration parameters..." --weight=1
ynh_webpath_available --domain=$domain --path_url=$path_url \
|| ynh_die --message="Path not available: ${domain}${path_url}"
test ! -d $final_path \
|| ynh_die --message="There is already a directory: $final_path "
#=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Reinstalling dependencies..." --weight=5
# Define and install dependencies
ynh_install_app_dependencies $pkg_dependencies
#=================================================
# ADD SWAP IF NEEDED
#=================================================
total_memory=$(ynh_check_ram)
total_swap=$(ynh_check_ram --only_swap)
swap_needed=0
# https://docs.gitlab.com/ee/install/requirements.html#memory
if [ $total_memory -lt 8192 ]; then
# Need a minimum of 8Go of memory
swap_needed=$((8192 - $total_memory))
fi
# Need at least 2Go of swap
if [ $(($total_swap + $swap_needed)) -lt 2048 ]; then
swap_needed=$((2048 - $total_swap))
fi
ynh_script_progression --message="Adding $swap_needed Mo to swap..." --weight=1
ynh_add_swap --size=$swap_needed
#=================================================
# RESTORE CONF FILES
#=================================================
ynh_script_progression --message="Restoring configuration files of Gitlab..." --weight=1
ynh_restore_file --origin_path="$config_path/gitlab-secrets.json"
ynh_restore_file --origin_path="$config_path/gitlab.rb"
ynh_restore_file --origin_path="$config_path/gitlab-persistent.rb"
#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_script_progression --message="Reinstalling gitlab..." --weight=200
update_src_version() {
source ../settings/scripts/upgrade.d/upgrade.last.sh
mkdir -p ../conf/
cp ../settings/conf/$architecture.src.default ../conf/$architecture.src
ynh_replace_string --match_string="__VERSION__" --replace_string="$gitlab_version" --target_file="../conf/$architecture.src"
ynh_replace_string --match_string="__SOURCE_FILENAME__" --replace_string="$gitlab_filename" --target_file="../conf/$architecture.src"
if [ $architecture = "x86-64" ]; then
ynh_replace_string --match_string="__SHA256_SUM__" --replace_string="$gitlab_x86_64_source_sha256" --target_file="../conf/$architecture.src"
elif [ $architecture = "arm" ]; then
ynh_replace_string --match_string="__SHA256_SUM__" --replace_string="$gitlab_arm_source_sha256" --target_file="../conf/$architecture.src"
fi
}
update_src_version
tempdir="$(mktemp -d)"
ynh_setup_source --dest_dir=$tempdir --source_id=$architecture
if IS_PACKAGE_CHECK; then
if ! ynh_exec_warn_less dpkg -i $tempdir/$gitlab_filename ; then # This command will fail in lxc env
ynh_replace_string --match_string="command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" --replace_string="command \"cat \/etc\/sysctl.conf\"" --target_file="$final_path/embedded/cookbooks/package/resources/sysctl.rb"
ynh_exec_warn_less dpkg --configure gitlab-ce
fi
else
ynh_exec_warn_less dpkg -i $tempdir/$gitlab_filename
fi
#=================================================
# SPECIFIC RESTORATION
#=================================================
# RESTORE GITLAB DATABASE
#=================================================
ynh_script_progression --message="Restoring Gitlab..." --weight=55
ynh_restore_file --origin_path="/var/opt/$app/backups/last_gitlab_backup.tar"
last_backup="last"
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# Use gitlab-rake to backup
ynh_exec_warn_less gitlab-rake gitlab:backup:restore force=yes BACKUP=$last_backup
gitlab-rake gitlab:check SANITIZE=true
#=================================================
# GENERIC FINALIZATION
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
yunohost service add "gitlab-runsvdir" --log "/var/log/$app/gitlab-rails/application.log" "/var/log/$app/gitlab-rails/api_json.log" "/var/log/$app/gitlab-rails/production.log" "/var/log/$app/gitlab-rails/production_json.log" "/var/log/$app/gitlab-rails/sidekiq.log" "/var/log/$app/unicorn/unicorn_stderr.log" "/var/log/$app/unicorn/current" "/var/log/$app/alertmanager/current" "/var/log/$app/gitaly/current" "/var/log/$app/gitlab-monitor/current" "/var/log/$app/gitlab-shell/gitlab-shell.log" "/var/log/$app/gitlab-workhorse/current" "/var/log/$app/logrotate/current" "/var/log/$app/nginx/current" "/var/log/$app/nginx/access.log" "/var/log/$app/nginx/error.log" "/var/log/$app/nginx/gitlab_access.log" "/var/log/$app/nginx/gitlab_error.log" "/var/log/$app/node-exporter/current" "/var/log/$app/postgres-exporter/current" "/var/log/$app/postgresql/current" "/var/log/$app/prometheus/current" "/var/log/$app/redis/current" "/var/log/$app/redis-exporter/current"
#=================================================
# WAITING GITLAB
#=================================================
ynh_script_progression --message="Waiting for gitlab..." --weight=14
ynh_systemd_action --action=restart --service_name="gitlab-runsvdir" --log_path="/var/log/$app/unicorn/current" --line_match="adopted" --timeout=3600
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading nginx web server..." --weight=1
ynh_systemd_action --action=reload --service_name=nginx
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Restoration completed for $app" --last