2017-06-10 10:10:31 +02:00
#!/bin/bash
2018-12-19 00:02:20 +01:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2018-12-16 23:05:12 +01:00
2020-04-16 12:20:59 +02:00
source _common.sh
2018-12-19 00:02:20 +01:00
source /usr/share/yunohost/helpers
2018-12-16 23:05:12 +01:00
2019-05-09 10:41:49 +02:00
#=================================================
# CHECK VERSION
#=================================================
upgrade_type=$(ynh_check_app_version_changed)
2018-12-19 00:02:20 +01:00
#=================================================
2019-03-03 14:29:53 +01:00
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2023-07-04 13:30:13 +02:00
if [ -n $(ynh_app_setting_get --app=$app --key=web_port) ]
then
ynh_app_setting_set --app=$app --key=port --value=$(ynh_app_setting_get --app=$app --key=web_port)
ynh_app_setting_delete --app=$app --key=web_port
fi
2023-03-03 11:24:16 +01:00
config_path=/etc/$app
2023-03-30 10:41:18 +02:00
chmod 755 $install_dir
2019-03-16 02:38:12 +01:00
2021-02-23 13:52:33 +01:00
if [ -z "$puma_max_threads" ] || [ -z "$puma_min_threads" ]; then
2020-05-23 02:07:01 +02:00
# If the server has less than 2GB of RAM
2020-06-23 12:34:04 +02:00
if [ $(ynh_get_ram --total --ignore_swap) -lt 2000 ]; then
2020-05-23 02:07:01 +02:00
puma_min_threads=1
puma_max_threads=1
2019-03-27 17:54:04 +01:00
else
2020-05-23 02:07:01 +02:00
puma_min_threads=2
puma_max_threads=4
2019-03-27 17:54:04 +01:00
fi
2020-05-23 02:07:01 +02:00
ynh_app_setting_set --app=$app --key=puma_max_threads --value=$puma_max_threads
ynh_app_setting_set --app=$app --key=puma_min_threads --value=$puma_min_threads
ynh_app_setting_delete --app=$app --key=unicorn_worker_processes
2019-04-04 12:37:49 +02:00
fi
2019-03-16 02:38:12 +01:00
# If architecture doesn't exist, create it
if [ -z "$architecture" ]; then
2021-03-29 11:46:48 +02:00
if [ -n "$(uname -m | grep arm64)" ] || [ -n "$(uname -m | grep aarch64)" ]; then
architecture="arm64"
2020-08-27 12:33:33 +02:00
elif [ -n "$(uname -m | grep x86_64)" ]; then
2019-03-16 02:38:12 +01:00
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
2019-05-09 01:13:48 +02:00
ynh_die --message="Unable to detect your achitecture, please open a bug describing \
2019-03-16 02:38:12 +01:00
your hardware and the result of the command \"uname -m\"." 1
fi
2019-05-09 01:13:48 +02:00
ynh_app_setting_set --app=$app --key=architecture --value=$architecture
2019-03-16 02:38:12 +01:00
fi
2019-03-29 22:18:08 +01:00
# If client_max_body_size doesn't exist, create it
if [ -z "$client_max_body_size" ]; then
client_max_body_size="250m"
2019-05-13 15:21:35 +02:00
ynh_app_setting_set --app=$app --key=client_max_body_size --value=$client_max_body_size
2019-03-29 22:18:08 +01:00
fi
2019-03-03 14:29:53 +01:00
# if this source file exist, remove it
if [ -e "/etc/apt/sources.list.d/gitlab-ce.list" ]; then
2019-05-09 01:13:48 +02:00
ynh_secure_remove --file="/etc/apt/sources.list.d/gitlab-ce.list"
2019-03-03 14:29:53 +01:00
fi
2018-12-19 00:02:20 +01:00
#=================================================
# STANDARD UPGRADE STEPS
2019-05-06 21:17:27 +02:00
#=================================================
2021-02-23 13:52:33 +01:00
# DEFINE THE NUMBER OF WORKERS USED
2019-05-06 21:17:27 +02:00
#=================================================
2020-05-02 14:16:08 +02:00
total_memory=$(ynh_get_ram --total)
2019-05-06 21:17:27 +02:00
2021-02-23 13:52:33 +01:00
if [ $total_memory -lt 4096 ]; then
#https://docs.gitlab.com/omnibus/settings/puma.html#running-in-memory-constrained-environments
puma_worker_processes=0
else
#https://docs.gitlab.com/ce/install/requirements.html#puma-workers
puma_worker_processes=$(( $(nproc) > 2 ? $(($(nproc) - 1)) : 2 ))
2019-05-06 21:17:27 +02:00
fi
2023-06-06 12:03:04 +02:00
ynh_app_setting_set --app=$app --key=puma_worker_processes --value=$puma_worker_processes
2021-02-23 13:52:33 +01:00
#=================================================
# ADD SWAP IF NEEDED
#=================================================
total_swap=$(ynh_get_ram --total --only_swap)
swap_needed=0
2021-02-23 14:16:08 +01:00
# https://docs.gitlab.com/ce/install/requirements.html#memory
2019-05-07 00:37:07 +02:00
# Need at least 2Go of swap
2021-02-23 14:16:08 +01:00
if [ $total_swap -lt 2048 ]; then
swap_needed=$((2048 - total_swap))
2019-05-06 21:17:27 +02:00
fi
2021-02-23 13:52:33 +01:00
if [ $swap_needed -gt 0 ]; then
ynh_script_progression --message="Adding $swap_needed Mo to swap..."
2021-05-15 15:33:14 +02:00
if ! ynh_add_swap --size=$swap_needed; then
ynh_print_warn --message="Please add $swap_needed Mo to swap to make Gitlab work properly"
fi
2021-02-23 13:52:33 +01:00
fi
2019-05-06 21:17:27 +02:00
2021-06-24 16:31:46 +02:00
#=================================================
# CHECK IF KERNEL IS READ-ONLY
#=================================================
modify_kernel_parameters="true"
for value_to_check in "kernel.shmall" "kernel.shmmax" "kernel.sem" "net.core.somaxconn"
do
if ! ynh_exec_fully_quiet sysctl --write $value_to_check="$(sysctl --value $value_to_check)"; then
modify_kernel_parameters="false"
break
fi
done
# For gitlab rb
mkdir -p $config_path
ssh_port=$(grep -P "Port\s+\d+" /etc/ssh/sshd_config | grep -P -o "\d+")
2023-03-03 11:24:16 +01:00
generated_external_url="https://$domain${path%/}"
2021-06-24 16:31:46 +02:00
2018-12-19 00:02:20 +01:00
#=================================================
2019-03-16 02:38:12 +01:00
# DOWNLOAD, CHECK AND UNPACK SOURCE
2018-12-19 00:02:20 +01:00
#=================================================
2019-05-12 19:36:00 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-23 17:14:33 +01:00
ynh_script_progression --message="Setting up source files..." --weight=50
2019-05-12 19:36:00 +02:00
2019-10-05 19:36:58 +02:00
# To avoid the automatic backup, already performed by YunoHost: https://docs.gitlab.com/omnibus/update/#updating-methods
touch $config_path/skip-auto-backup
2019-07-04 00:24:20 +02:00
current_version=$(grep gitlab-ce /opt/gitlab/version-manifest.txt | cut -d' ' -f2)
# Load the last available version
2021-05-15 17:05:49 +02:00
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.last.sh
2019-07-04 00:24:20 +02:00
last_version=$gitlab_version
2019-03-16 02:38:12 +01:00
2020-05-23 20:33:47 +02:00
source_current_major_version () {
2021-05-15 17:05:49 +02:00
if [ -e "$YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.first.sh" ]; then
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.first.sh
elif [ -e "$YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.last.sh" ]; then
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.last.sh
2020-05-30 17:07:36 +02:00
# Finish with the last migration if the file doesn't exist
else
2021-05-15 17:05:49 +02:00
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.last.sh
2020-05-23 20:33:47 +02:00
fi
}
2020-05-23 23:08:49 +02:00
2020-12-23 17:08:21 +01:00
# To update GitLab from major version A to B, we have to go to the last minor version
2020-05-23 02:07:01 +02:00
# of the major version A and then go to the first minor version of the major version B
# to finally go to the current minor version of the major version B
# A.last -> B.first -> B.last
2019-03-16 02:38:12 +01:00
2019-07-04 00:24:20 +02:00
# While the current version is not the last version, do an upgrade
while [ "$last_version" != "$current_version" ]
do
2022-08-02 15:16:34 +02:00
# https://docs.gitlab.com/ee/update/#checking-for-background-migrations-before-upgrading
if dpkg --compare-versions "$current_version" "ge" "12.9"; then
checkBackgroundMigration=1
else
checkBackgroundMigration=0;
fi
if dpkg --compare-versions "$current_version" "ge" "15.1"; then
checkDBBackgroundMigration=2
elif dpkg --compare-versions "$current_version" "ge" "14.0"; then
checkDBBackgroundMigration=1
else
checkDBBackgroundMigration=0;
fi
if dpkg --compare-versions "$current_version" "ge" "14.7"; then
checkBatchedBackgroundMigration=1
else
checkBatchedBackgroundMigration=0;
fi
counter=0
while [ $checkBackgroundMigration -eq 1 ] || [ $checkDBBackgroundMigration -ge 1 ] || [ $checkBatchedBackgroundMigration -eq 1 ]
do
counter=$((counter + 1))
if [ $counter -gt 1200 ]
then
ynh_print_warn --message="Timeout: a background migration runs for at least 20min !"
break
fi
if [ $checkBackgroundMigration -eq 1 ] && gitlab-rails runner -e production 'puts Gitlab::BackgroundMigration.remaining' | grep -q -w 0
then
checkBackgroundMigration=0
fi
if [ $checkDBBackgroundMigration -eq 2 ] && gitlab-rails runner -e production 'puts Gitlab::Database::BackgroundMigration::BatchedMigration.queued.count' | grep -q -w 0
then
checkDBBackgroundMigration=0
elif [ $checkDBBackgroundMigration -eq 1 ] && gitlab-rails runner -e production 'puts Gitlab::Database::BackgroundMigrationJob.pending' | wc -l | grep -q -w 0
then
checkDBBackgroundMigration=0
fi
if [ $checkBatchedBackgroundMigration -eq 1 ] && echo "select COUNT(*) from batched_background_migrations where status <> 3;" | gitlab-psql -t -A | grep -q -w 0
then
checkBatchedBackgroundMigration=0
fi
ynh_print_info --message="Wait for the migration in the background to finish"
sleep 1
done
2019-03-16 02:38:12 +01:00
2019-07-04 00:24:20 +02:00
current_major_version=${current_version%%.*}
2019-05-09 10:41:49 +02:00
2020-05-23 20:33:47 +02:00
source_current_major_version
2020-05-12 14:11:01 +02:00
2020-05-23 02:07:01 +02:00
# if the version stored in the upgrade.$current_major_version.first.sh file is less than or equal
# to the current version, and if the version stored in the upgrade.$current_major_version.last.sh file
# increment the major version to upgrade to the next version
2020-05-12 14:11:01 +02:00
if dpkg --compare-versions "$gitlab_version" "le" "$current_version"; then
2021-05-15 17:05:49 +02:00
if [ -e "$YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.last.sh" ]; then
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.last.sh
2020-05-23 02:07:01 +02:00
else
2021-05-15 17:05:49 +02:00
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.last.sh
2020-05-23 02:07:01 +02:00
fi
if dpkg --compare-versions "$gitlab_version" "le" "$current_version"; then
current_major_version=$(($current_major_version + 1))
2020-05-23 20:33:47 +02:00
source_current_major_version
2020-05-23 02:07:01 +02:00
fi
2019-07-04 23:48:13 +02:00
fi
2021-05-28 16:13:02 +02:00
ynh_add_config --template="$YNH_APP_BASEDIR/conf/$architecture.src.default" --destination="$YNH_APP_BASEDIR/conf/$architecture.src"
2019-07-04 00:24:20 +02:00
tempdir="$(mktemp -d)"
ynh_setup_source --dest_dir=$tempdir --source_id=$architecture
2021-10-21 14:14:35 +02:00
if ! ynh_exec_warn_less dpkg -i $tempdir/$gitlab_filename; then # This command will fail in lxc env
2019-07-31 10:25:20 +02:00
package_check_action # defined in upgrade.d/upgrade.X.sh
ynh_exec_warn_less dpkg --configure gitlab-ce
2019-07-04 00:24:20 +02:00
fi
ynh_exec_warn_less ynh_secure_remove --file="$tempdir"
current_version=$(grep gitlab-ce /opt/gitlab/version-manifest.txt | cut -d' ' -f2)
2017-06-10 10:10:31 +02:00
2021-06-24 16:31:46 +02:00
# Sometimes we need to update the gitlab.rb configuration file in order to migrate to the next version.
if [ -e "$YNH_APP_BASEDIR/conf/gitlab.$current_major_version.rb" ]; then
if [ -e "$YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.last.sh" ]; then
source $YNH_APP_BASEDIR/scripts/upgrade.d/upgrade.$current_major_version.last.sh
fi
if dpkg --compare-versions "$gitlab_version" "ge" "$current_version"; then
ynh_add_config --template="$YNH_APP_BASEDIR/conf/gitlab.$current_major_version.rb" --destination="$config_path/gitlab.rb"
2021-02-24 15:09:16 +01:00
2021-06-24 16:31:46 +02:00
touch "$config_path/gitlab-persistent.rb"
chown root:root "$config_path/gitlab-persistent.rb"
chmod 640 "$config_path/gitlab-persistent.rb"
2021-02-24 15:09:16 +01:00
2021-11-02 11:55:29 +01:00
# During large migrations, the logs are too big to be sent to paste.yunohost.org
# Send the reconfigure logs in a file, and if the process succeeds, just delete it.
gitlab-ctl reconfigure > "/tmp/gitlab_upgrade_$current_version.log"
ynh_secure_remove --file="/tmp/gitlab_upgrade_$current_version.log"
2021-06-24 16:31:46 +02:00
fi
fi
done
fi
2021-02-24 15:09:16 +01:00
2020-06-24 11:00:24 +02:00
#=================================================
# RECONFIGURE GITLAB
#=================================================
2020-12-23 17:08:21 +01:00
ynh_script_progression --message="Reconfigure GitLab..." --weight=13
2020-06-24 11:00:24 +02:00
2021-05-28 16:13:02 +02:00
ynh_add_config --template="$YNH_APP_BASEDIR/conf/gitlab.rb" --destination="$config_path/gitlab.rb"
2020-06-24 11:00:24 +02:00
touch "$config_path/gitlab-persistent.rb"
2021-05-31 16:00:54 +02:00
chown root:root "$config_path/gitlab-persistent.rb"
chmod 640 "$config_path/gitlab-persistent.rb"
2020-06-24 11:00:24 +02:00
2023-05-24 17:54:17 +02:00
ynh_exec_warn_less gitlab-ctl reconfigure
2020-06-24 11:00:24 +02:00
2021-05-15 11:00:03 +02:00
# Allow ssh for git
usermod -a -G "ssh.app" "git"
2018-12-19 00:02:20 +01:00
#=================================================
2019-03-16 02:38:12 +01:00
# NGINX CONFIGURATION
2018-12-19 00:02:20 +01:00
#=================================================
2017-06-10 10:10:31 +02:00
2019-04-11 01:11:54 +02:00
# Overwrite the nginx configuration only if it's allowed
if [ $overwrite_nginx -eq 1 ]
then
2020-12-23 17:08:21 +01:00
ynh_script_progression --message="Configuring NGINX web server..." --weight=2
2019-04-11 01:11:54 +02:00
# Create a dedicated nginx config
ynh_add_nginx_config client_max_body_size
fi
2018-04-14 18:38:22 +02:00
2018-12-19 00:02:20 +01:00
#=================================================
# GENERIC FINALIZATION
2019-03-24 21:36:20 +01:00
#=================================================
# ADVERTISE SERVICE IN ADMIN PANEL
#=================================================
2020-05-23 02:07:01 +02:00
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/puma/puma_stderr.log" "/var/log/$app/puma/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"
2019-03-24 21:36:20 +01:00
2019-03-16 02:38:12 +01:00
#=================================================
2019-03-24 21:36:20 +01:00
# WAITING GITLAB
2019-03-16 02:38:12 +01:00
#=================================================
2019-05-13 10:27:19 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-23 17:08:21 +01:00
ynh_script_progression --message="Restarting GitLab..." --weight=15
2019-05-13 10:27:19 +02:00
2023-03-04 23:12:11 +01:00
ynh_systemd_action --action=restart --service_name="gitlab-runsvdir" --log_path="/var/log/$app/puma/current" --line_match="Listening on http://127.0.0.1:$port_puma" --timeout=300
2019-05-13 10:27:19 +02:00
fi
2019-03-16 02:38:12 +01:00
2018-12-19 00:02:20 +01:00
#=================================================
# RELOAD NGINX
#=================================================
2019-03-16 02:38:12 +01:00
ynh_systemd_action --action=reload --service_name=nginx
2019-01-05 11:12:03 +01:00
2019-01-13 15:44:16 +01:00
#=================================================
2019-03-16 02:38:12 +01:00
# END OF SCRIPT
2019-01-13 15:44:16 +01:00
#=================================================
2020-12-23 17:18:58 +01:00
ynh_script_progression --message="Upgrade of GitLab completed" --last