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/upgrade
2019-11-07 19:22:54 +09:00

373 lines
14 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
# IMPORT GENERIC HELPERS
source /usr/share/yunohost/helpers
# Load common variables and helpers
source ./_common.sh
#=================================================
# LOAD SETTINGS
#=================================================
app=$YNH_APP_INSTANCE_NAME
# Retrieve app settings
domain=$(ynh_app_setting_get --app="$app" --key=domain)
path_url=$(ynh_app_setting_get --app="$app" --key=path)
admin=$(ynh_app_setting_get --app="$app" --key=admin)
is_public=$(ynh_app_setting_get --app="$app" --key=is_public)
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)
portSidekiq=$(ynh_app_setting_get --app="$app" --key=sidekiq_port)
architecture=$(ynh_app_setting_get --app="$app" --key=architecture)
unicorn_worker_processes=$(ynh_app_setting_get --app="$app" --key=unicorn_worker_processes)
client_max_body_size=$(ynh_app_setting_get --app="$app" --key=client_max_body_size)
overwrite_nginx=$(ynh_app_setting_get --app="$app" --key=overwrite_nginx)
backup_db=$(ynh_app_setting_get --app="$app" --key=backup_db)
backup_uploads=$(ynh_app_setting_get --app="$app" --key=backup_uploads)
backup_repositories=$(ynh_app_setting_get --app="$app" --key=backup_repositories)
backup_builds=$(ynh_app_setting_get --app="$app" --key=backup_builds)
backup_artifacts=$(ynh_app_setting_get --app="$app" --key=backup_artifacts)
backup_lfs=$(ynh_app_setting_get --app="$app" --key=backup_lfs)
backup_registry=$(ynh_app_setting_get --app="$app" --key=backup_registry)
backup_pages=$(ynh_app_setting_get --app="$app" --key=backup_pages)
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
# Delete is_public if it exists
if [ ! -z $is_public ]; then
ynh_app_setting_delete --app=$app --key=is_public
fi
# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
final_path=/opt/$app
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
fi
# If config_path doesn't exist, create it
if [ -z "$config_path" ]; then
config_path=/etc/$app
ynh_app_setting_set --app=$app --key=config_path --value=$config_path
fi
if [ -z "$unicorn_worker_processes" ]; then
# https://docs.gitlab.com/ce/install/requirements.html#unicorn-workers
unicorn_worker_processes=$(($(nproc) + 1 ))
# If the server has at least 2GB of RAM
if [ $(free -g --si | grep Mem: | awk '{print $2}') -ge 2 ]; then
# Min 3 worker processes
unicorn_worker_processes=$(($unicorn_worker_processes>3?$unicorn_worker_processes:3))
else
# 2 worker processes
unicorn_worker_processes=2
fi
ynh_app_setting_set --app=$app --key=unicorn_worker_processes --value=$unicorn_worker_processes
fi
if [ -z "$portSidekiq" ]; then
portSidekiq=$(ynh_find_port $(($portUnicorn + 1)))
ynh_app_setting_set --app=$app --key=sidekiq_port --value=$portSidekiq
fi
# If architecture doesn't exist, create it
if [ -z "$architecture" ]; then
# Detect the system architecture
if [ -n "$(uname -m | grep 64)" ]; then
architecture="x86-64"
elif [ -n "$(uname -m | grep 86)" ]; then
ynh_die "Gitlab is not compatible with x86 architecture"
elif [ -n "$(uname -m | grep arm)" ]; then
architecture="arm"
else
ynh_die --message="Unable to detect your achitecture, please open a bug describing \
your hardware and the result of the command \"uname -m\"." 1
fi
ynh_app_setting_set --app=$app --key=architecture --value=$architecture
fi
# If client_max_body_size doesn't exist, create it
if [ -z "$client_max_body_size" ]; then
client_max_body_size="250m"
ynh_app_setting_set --app=$app --key=client_max_body_size --value=$client_max_body_size
fi
# If overwrite_nginx doesn't exist, create it
if [ -z "$overwrite_nginx" ]; then
overwrite_nginx=1
ynh_app_setting_set --app=$app --key=overwrite_nginx --value=$overwrite_nginx
fi
# If domain doesn't exist, retrieve it
if [ -z "$domain" ]; then
domain=$(grep "external_url" "/etc/gitlab/gitlab.rb" | cut -d'/' -f3) # retrieve $domain from conf file
if [ ${domain: -1} == "'" ]; then # if the last char of $domain is ' remove it
domain=${domain:0:-1}
fi
ynh_app_setting_set --app=$app --key=domain --value=$domain
fi
# If path_url doesn't exist, retrieve it
if [ -z "$path_url" ]; then
path_url=$(grep "location" "/etc/nginx/conf.d/${domain}.d/gitlab.conf" | cut -d' ' -f2)
path_url=$(ynh_normalize_url_path $path_url)
ynh_app_setting_set --app=$app --key=path --value=path_url
fi
# If port doesn't exist, retrieve it
if [ -z "$port" ]; then
port=$(grep -F "nginx['listen_port']" "/etc/gitlab/gitlab.rb" | cut -d' ' -f3)
ynh_app_setting_set --app=$app --key=web_port --value=$port
fi
# If port doesn't exist, retrieve it
if [ -z "$portUnicorn" ]; then
portUnicorn=$(grep -F "unicorn['port']" "/etc/gitlab/gitlab.rb" | cut -d' ' -f3)
ynh_app_setting_set --app=$app --key=unicorn_port --value=$portUnicorn
fi
# if this source file exist, remove it
if [ -e "/etc/apt/sources.list.d/gitlab-ce.list" ]; then
ynh_secure_remove --file="/etc/apt/sources.list.d/gitlab-ce.list"
fi
# Define the backup strategy: https://docs.gitlab.com/ce/raketasks/backup_restore.html#excluding-specific-directories-from-the-backup
if [ -z "$backup_db" ]; then
ynh_app_setting_set --app=$app --key=backup_db --value=1
fi
if [ -z "$backup_uploads" ]; then
ynh_app_setting_set --app=$app --key=backup_uploads --value=1
fi
if [ -z "$backup_repositories" ]; then
ynh_app_setting_set --app=$app --key=backup_repositories --value=1
fi
if [ -z "$backup_builds" ]; then
ynh_app_setting_set --app=$app --key=backup_builds --value=1
fi
if [ -z "$backup_artifacts" ]; then
ynh_app_setting_set --app=$app --key=backup_artifacts --value=1
fi
if [ -z "$backup_lfs" ]; then
ynh_app_setting_set --app=$app --key=backup_lfs --value=1
fi
if [ -z "$backup_registry" ]; then
ynh_app_setting_set --app=$app --key=backup_registry --value=1
fi
if [ -z "$backup_pages" ]; then
ynh_app_setting_set --app=$app --key=backup_pages --value=1
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=10
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
ynh_exec_warn_less ynh_secure_remove --file="$tempdir"
ynh_clean_check_starting
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# CHECK THE PATH
#=================================================
# Normalize the URL path syntax
path_url=$(ynh_normalize_url_path $path_url)
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# INSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Installing dependencies..." --weight=5
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/ce/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
#=================================================
# PRECONFIGURE GITLAB
#=================================================
ynh_script_progression --message="Preconfigure gitlab..." --weight=1
ynh_backup_if_checksum_is_different --file="$config_path/gitlab.rb"
mkdir -p $config_path
cp -f ../conf/gitlab.rb "$config_path/gitlab.rb"
ssh_port=$(grep -P "Port\s+\d+" /etc/ssh/sshd_config | grep -P -o "\d+")
ynh_replace_string --match_string="__GENERATED_EXTERNAL_URL__" --replace_string="https://$domain${path_url%/}" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__UNICORN_PORT__" --replace_string="$portUnicorn" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__UNICORN_WORKER_PROCESSES__" --replace_string="$unicorn_worker_processes" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__CLIENT_MAX_BODY_SIZE__" --replace_string="$client_max_body_size" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__SSH_PORT__" --replace_string="$ssh_port" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__SIDEKIQ_PORT__" --replace_string="$portSidekiq" --target_file="$config_path/gitlab.rb"
ynh_store_file_checksum --file="$config_path/gitlab.rb"
touch "$config_path/gitlab-persistent.rb"
chown admin: "$config_path/gitlab-persistent.rb"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Setting up source files..." --weight=200
# To avoid the automatic backup, already performed by YunoHost: https://docs.gitlab.com/omnibus/update/#updating-methods
touch $config_path/skip-auto-backup
current_version=$(grep gitlab-ce /opt/gitlab/version-manifest.txt | cut -d' ' -f2)
# Load the last available version
source ./upgrade.d/upgrade.last.sh
last_version=$gitlab_version
last_major_version=${gitlab_version%%.*}
# While the current version is not the last version, do an upgrade
while [ "$last_version" != "$current_version" ]
do
current_major_version=${current_version%%.*}
# If the current version is equal to the last minor version of upgrade.$current_major_version.sh, increment the major version
if [ "$gitlab_version" = "$current_version" ]; then
current_major_version=$(($current_major_version + 1))
fi
# If the current version has the same major version than the next one,
# then it's the last upgrade to do
if [ "$last_major_version" -eq "$current_major_version" ]; then
current_major_version=last
fi
source ./upgrade.d/upgrade.$current_major_version.sh
cp ../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
tempdir="$(mktemp -d)"
ynh_setup_source --dest_dir=$tempdir --source_id=$architecture
if ! ynh_exec_warn_less dpkg -i $tempdir/$gitlab_filename ;
then # This command will fail in lxc env
package_check_action # defined in upgrade.d/upgrade.X.sh
ynh_exec_warn_less dpkg --configure gitlab-ce
fi
ynh_exec_warn_less ynh_secure_remove --file="$tempdir"
current_version=$(grep gitlab-ce /opt/gitlab/version-manifest.txt | cut -d' ' -f2)
done
fi
#=================================================
# NGINX CONFIGURATION
#=================================================
# Overwrite the nginx configuration only if it's allowed
if [ $overwrite_nginx -eq 1 ]
then
ynh_script_progression --message="Configuring nginx web server..." --weight=2
# Create a dedicated nginx config
ynh_add_nginx_config client_max_body_size
fi
#=================================================
# 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
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Restarting gitlab..." --weight=15
ynh_systemd_action --action=restart --service_name="gitlab-runsvdir" --log_path="/var/log/$app/unicorn/current" --line_match="adopted" --timeout=300
fi
#=================================================
# 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="Upgrade of $app completed" --last