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

380 lines
15 KiB
Text
Raw Normal View History

2017-06-10 10:10:31 +02:00
#!/bin/bash
2018-12-19 00:02:20 +01:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
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-19 00:02:20 +01:00
#=================================================
# LOAD SETTINGS
#=================================================
2017-06-10 10:10:31 +02:00
app=$YNH_APP_INSTANCE_NAME
# Retrieve app settings
2019-05-09 01:13:48 +02:00
domain=$(ynh_app_setting_get --app="$app" --key=domain)
2019-05-13 15:53:21 +02:00
path_url=$(ynh_app_setting_get --app="$app" --key=path)
2019-05-09 01:13:48 +02:00
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)
2020-05-23 02:07:01 +02:00
portPuma=$(ynh_app_setting_get --app="$app" --key=puma_port)
2019-05-09 01:13:48 +02:00
portSidekiq=$(ynh_app_setting_get --app="$app" --key=sidekiq_port)
architecture=$(ynh_app_setting_get --app="$app" --key=architecture)
2021-02-24 15:06:07 +01:00
puma_worker_processes=$(ynh_app_setting_get --app="$app" --key=puma_workers)
2020-05-23 02:07:01 +02:00
puma_min_threads=$(ynh_app_setting_get --app="$app" --key=puma_min_threads)
puma_max_threads=$(ynh_app_setting_get --app="$app" --key=puma_max_threads)
2019-05-09 01:13:48 +02:00
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)
2017-06-10 10:10:31 +02: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
#=================================================
2019-11-07 11:22:54 +01:00
# Delete is_public if it exists
2019-11-07 10:55:15 +01:00
if [ ! -z $is_public ]; then
ynh_app_setting_delete --app=$app --key=is_public
2019-03-03 14:29:53 +01:00
fi
2019-03-16 02:38:12 +01:00
# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
final_path=/opt/$app
2019-05-09 01:13:48 +02:00
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2019-03-16 02:38:12 +01:00
fi
# If config_path doesn't exist, create it
if [ -z "$config_path" ]; then
config_path=/etc/$app
2019-05-09 01:13:48 +02:00
ynh_app_setting_set --app=$app --key=config_path --value=$config_path
2019-03-16 02:38:12 +01:00
fi
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-04-11 01:11:54 +02:00
# If overwrite_nginx doesn't exist, create it
if [ -z "$overwrite_nginx" ]; then
overwrite_nginx=1
2019-05-09 01:13:48 +02:00
ynh_app_setting_set --app=$app --key=overwrite_nginx --value=$overwrite_nginx
2019-04-11 01:11:54 +02:00
fi
2019-03-03 14:29:53 +01:00
# If domain doesn't exist, retrieve it
2019-03-03 15:03:26 +01:00
if [ -z "$domain" ]; then
2019-03-03 14:29:53 +01:00
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
2019-05-09 01:13:48 +02:00
ynh_app_setting_set --app=$app --key=domain --value=$domain
2019-03-03 14:29:53 +01:00
fi
# If path_url doesn't exist, retrieve it
2019-03-03 15:03:26 +01:00
if [ -z "$path_url" ]; then
2019-03-03 14:38:46 +01:00
path_url=$(grep "location" "/etc/nginx/conf.d/${domain}.d/gitlab.conf" | cut -d' ' -f2)
2019-03-03 14:29:53 +01:00
path_url=$(ynh_normalize_url_path $path_url)
2019-05-13 16:00:02 +02:00
ynh_app_setting_set --app=$app --key=path --value=path_url
2019-03-03 14:29:53 +01:00
fi
# If port doesn't exist, retrieve it
2019-03-03 15:03:26 +01:00
if [ -z "$port" ]; then
2019-03-03 14:38:46 +01:00
port=$(grep -F "nginx['listen_port']" "/etc/gitlab/gitlab.rb" | cut -d' ' -f3)
2019-05-09 01:13:48 +02:00
ynh_app_setting_set --app=$app --key=web_port --value=$port
2019-03-03 14:29:53 +01:00
fi
# If port doesn't exist, retrieve it
2020-05-23 02:07:01 +02:00
if [ -z "$portPuma" ]; then
if [ -z "$(ynh_app_setting_get --app="$app" --key=unicorn_port)" ]; then
portPuma=$(grep -F "unicorn['port']" "/etc/gitlab/gitlab.rb" | cut -d' ' -f3)
2020-05-23 23:08:49 +02:00
else
portPuma=$(ynh_app_setting_get --app="$app" --key=unicorn_port)
2020-05-23 02:07:01 +02:00
fi
ynh_app_setting_set --app=$app --key=puma_port --value=$portPuma
ynh_app_setting_delete --app=$app --key=unicorn_port
fi
if [ -z "$portSidekiq" ]; then
portSidekiq=$(ynh_find_port $(($portPuma + 1)))
ynh_app_setting_set --app=$app --key=sidekiq_port --value=$portSidekiq
2019-03-03 14:29:53 +01:00
fi
# 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
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2019-05-09 09:03:19 +02:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=10
2018-12-19 00:02:20 +01:00
# Backup the current version of the app
ynh_backup_before_upgrade
2019-03-16 02:38:12 +01:00
ynh_clean_setup () {
2019-05-09 01:13:48 +02:00
ynh_exec_warn_less ynh_secure_remove --file="$tempdir"
2019-03-16 02:38:12 +01:00
ynh_clean_check_starting
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
2018-12-19 00:02:20 +01:00
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
2019-03-16 02:38:12 +01:00
# INSTALL DEPENDENCIES
2018-12-19 00:02:20 +01:00
#=================================================
2019-05-09 09:03:19 +02:00
ynh_script_progression --message="Installing dependencies..." --weight=5
2018-12-19 00:02:20 +01:00
2019-03-16 02:38:12 +01:00
ynh_install_app_dependencies $pkg_dependencies
2017-06-10 10:10:31 +02:00
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
2021-02-23 13:52:33 +01:00
ynh_app_setting_set --app=$app --key=puma_workers --value=$puma_worker_processes
#=================================================
# 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
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
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-15 17:05:49 +02:00
cp $YNH_APP_BASEDIR/conf/$architecture.src.default $YNH_APP_BASEDIR/conf/$architecture.src
ynh_replace_string --match_string="__VERSION__" --replace_string="$gitlab_version" --target_file="$YNH_APP_BASEDIR/conf/$architecture.src"
ynh_replace_string --match_string="__SOURCE_FILENAME__" --replace_string="$gitlab_filename" --target_file="$YNH_APP_BASEDIR/conf/$architecture.src"
ynh_replace_string --match_string="__DEBIAN_VERSION__" --replace_string="$gitlab_debian_version" --target_file="$YNH_APP_BASEDIR/conf/$architecture.src"
ynh_replace_string --match_string="__SHA256_SUM__" --replace_string="$gitlab_source_sha256" --target_file="$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
2019-07-31 10:25:20 +02:00
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
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)
done
2019-03-16 02:38:12 +01:00
fi
2017-06-10 10:10:31 +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
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
ynh_backup_if_checksum_is_different --file="$config_path/gitlab.rb"
mkdir -p $config_path
2021-05-15 17:05:49 +02:00
cp -f $YNH_APP_BASEDIR/conf/gitlab.rb "$config_path/gitlab.rb"
2020-06-24 11:00:24 +02:00
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="__PUMA_PORT__" --replace_string="$portPuma" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__PUMA_WORKER_PROCESSES__" --replace_string="$puma_worker_processes" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__PUMA_MIN_THREADS__" --replace_string="$puma_min_threads" --target_file="$config_path/gitlab.rb"
ynh_replace_string --match_string="__PUMA_MAX_THREADS__" --replace_string="$puma_max_threads" --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_replace_string --match_string="__MODIFY_KERNEL_PARAMETERS__" --replace_string="$modify_kernel_parameters" --target_file="$config_path/gitlab.rb"
2020-06-24 11:00:24 +02:00
ynh_store_file_checksum --file="$config_path/gitlab.rb"
touch "$config_path/gitlab-persistent.rb"
chown admin: "$config_path/gitlab-persistent.rb"
gitlab-ctl reconfigure
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
2021-01-28 01:05:03 +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:$portPuma" --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
#=================================================
2020-12-23 17:08:21 +01:00
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2018-12-19 00:02:20 +01:00
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-03-16 02:38:12 +01:00
# END OF SCRIPT
#=================================================
2020-12-23 17:18:58 +01:00
ynh_script_progression --message="Upgrade of GitLab completed" --last