mirror of
https://github.com/YunoHost-Apps/gitlab_ynh.git
synced 2024-09-03 18:36:35 +02:00
278 lines
12 KiB
Bash
278 lines
12 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# GENERIC START
|
|
#=================================================
|
|
# IMPORT GENERIC HELPERS
|
|
#=================================================
|
|
|
|
source _common.sh
|
|
source /usr/share/yunohost/helpers
|
|
|
|
#=================================================
|
|
# MANAGE SCRIPT FAILURE
|
|
#=================================================
|
|
|
|
ynh_clean_setup () {
|
|
ynh_exec_warn_less ynh_secure_remove --file="$tempdir"
|
|
|
|
ynh_clean_check_starting
|
|
}
|
|
# Exit if an error occurs during the execution of the script
|
|
ynh_abort_if_errors
|
|
|
|
#=================================================
|
|
# RETRIEVE ARGUMENTS FROM THE MANIFEST
|
|
#=================================================
|
|
|
|
domain=$YNH_APP_ARG_DOMAIN
|
|
path_url=$YNH_APP_ARG_PATH
|
|
is_public=$YNH_APP_ARG_IS_PUBLIC
|
|
use_web_account=$YNH_APP_ARG_USE_WEB_ACCOUNT
|
|
admin=$YNH_APP_ARG_ADMIN
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
#=================================================
|
|
# CHECK IF THE APP CAN BE INSTALLED WITH THESE ARGS
|
|
#=================================================
|
|
ynh_script_progression --message="Validating installation parameters..." --weight=1
|
|
|
|
config_path=/etc/$app
|
|
final_path=/opt/$app
|
|
test ! -e "$final_path" || ynh_die --message="This path already contains a folder"
|
|
|
|
# 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
|
|
|
|
# Could be an option?
|
|
client_max_body_size="250m"
|
|
|
|
# Register (book) web path
|
|
ynh_webpath_register --app=$app --domain=$domain --path_url=$path_url
|
|
|
|
#=================================================
|
|
# STORE SETTINGS FROM MANIFEST
|
|
#=================================================
|
|
ynh_script_progression --message="Storing installation settings..." --weight=2
|
|
|
|
ynh_app_setting_set --app=$app --key=admin --value=$admin
|
|
ynh_app_setting_set --app=$app --key=path --value=$path_url
|
|
ynh_app_setting_set --app=$app --key=is_public --value=$is_public
|
|
ynh_app_setting_set --app=$app --key=use_web_account --value=$use_web_account
|
|
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
|
|
ynh_app_setting_set --app=$app --key=config_path --value=$config_path
|
|
ynh_app_setting_set --app=$app --key=architecture --value=$architecture
|
|
ynh_app_setting_set --app=$app --key=client_max_body_size --value=$client_max_body_size
|
|
|
|
ynh_app_setting_set --app=$app --key=overwrite_nginx --value="1"
|
|
|
|
#=================================================
|
|
# STANDARD MODIFICATIONS
|
|
#=================================================
|
|
# FIND A PORT
|
|
#=================================================
|
|
ynh_script_progression --message="Find internal port..." --weight=1
|
|
|
|
# Find free ports
|
|
port=$(ynh_find_port --port=8080)
|
|
portUnicorn=$(ynh_find_port --port=$(($port + 1)))
|
|
portSidekiq=$(ynh_find_port --port=$(($portUnicorn + 1)))
|
|
|
|
ynh_app_setting_set --app=$app --key=web_port --value=$port
|
|
ynh_app_setting_set --app=$app --key=unicorn_port --value=$portUnicorn
|
|
ynh_app_setting_set --app=$app --key=sidekiq_port --value=$portSidekiq
|
|
|
|
#=================================================
|
|
# INSTALL DEPENDENCIES
|
|
#=================================================
|
|
ynh_script_progression --message="Installing dependencies..." --weight=5
|
|
|
|
ynh_install_app_dependencies $pkg_dependencies
|
|
|
|
#=================================================
|
|
# DEFINE THE NUMBER OF WORKERS USED
|
|
#=================================================
|
|
|
|
# https://docs.gitlab.com/ce/install/requirements.html#unicorn-workers
|
|
unicorn_worker_processes=$(bc <<< "($(nproc) * 1.5 + 1) / 1")
|
|
|
|
# If the server has at least 2GB of RAM
|
|
if [ $(ynh_check_ram --no_swap) -ge 2000 ]; 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
|
|
|
|
#=================================================
|
|
# 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
|
|
|
|
mkdir -p $config_path
|
|
|
|
touch "$config_path/gitlab-persistent.rb"
|
|
chown admin: "$config_path/gitlab-persistent.rb"
|
|
|
|
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"
|
|
|
|
#=================================================
|
|
# STORE THE CONFIG FILE CHECKSUM
|
|
#=================================================
|
|
|
|
ynh_store_file_checksum --file="$config_path/gitlab.rb"
|
|
|
|
#=================================================
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
#=================================================
|
|
ynh_script_progression --message="Setting up source files..." --weight=200
|
|
|
|
source ./upgrade.d/upgrade.last.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.last.sh
|
|
ynh_exec_warn_less dpkg --configure gitlab-ce
|
|
fi
|
|
|
|
#=================================================
|
|
# NGINX CONFIGURATION
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring nginx web server..." --weight=2
|
|
|
|
# Create a dedicated nginx config
|
|
ynh_add_nginx_config client_max_body_size
|
|
|
|
#=================================================
|
|
# SPECIFIC SETUP
|
|
#=================================================
|
|
# ADD USER AND CONFIGURE SIGN IN SYSTEM
|
|
#=================================================
|
|
ynh_script_progression --message="Creating an administrator user..." --weight=13
|
|
|
|
mailadmin=$(ynh_user_get_info --username=$admin --key=mail)
|
|
rdmPass=$(ynh_string_random --length=30)
|
|
|
|
echo "newuser = User.new({ \"email\"=>'$mailadmin', \"username\"=>'$admin', \"name\"=>'$admin', \"password\"=>'$rdmPass'})
|
|
newuser.admin = true
|
|
newuser.confirmed_at = Time.now
|
|
newuser.confirmation_token = nil
|
|
newuser.save
|
|
ApplicationSetting.last.update_attributes(password_authentication_enabled_for_web: $use_web_account, signup_enabled: $use_web_account)" | gitlab-rails console
|
|
|
|
|
|
#=================================================
|
|
# DEFINE THE BACKUP STRATEGY: https://docs.gitlab.com/ce/raketasks/backup_restore.html#excluding-specific-directories-from-the-backup
|
|
#=================================================
|
|
|
|
ynh_app_setting_set --app=$app --key=backup_db --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_uploads --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_repositories --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_builds --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_artifacts --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_lfs --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_registry --value=1
|
|
ynh_app_setting_set --app=$app --key=backup_pages --value=1
|
|
|
|
#=================================================
|
|
# RECONFIGURE TO TAKE INTO ACCOUNT CHANGES
|
|
#=================================================
|
|
ynh_script_progression --message="Reconfigure gitlab..." --weight=13
|
|
|
|
gitlab-ctl reconfigure
|
|
|
|
#=================================================
|
|
# 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"
|
|
|
|
#=================================================
|
|
# SETUP SSOWAT
|
|
#=================================================
|
|
ynh_script_progression --message="Configuring SSOwat..." --weight=1
|
|
|
|
# Make app public if necessary
|
|
if [ $is_public -eq 1 ]; then
|
|
# unprotected_uris allows SSO credentials to be passed anyway.
|
|
ynh_app_setting_set --app=$app --key=unprotected_uris --value="/"
|
|
fi
|
|
|
|
#=================================================
|
|
# RELOAD NGINX
|
|
#=================================================
|
|
ynh_script_progression --message="Reloading nginx web server..." --weight=1
|
|
|
|
ynh_systemd_action --action=reload --service_name=nginx
|
|
|
|
#=================================================
|
|
# RESTART GITLAB
|
|
#=================================================
|
|
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
|
|
|
|
#=================================================
|
|
# END OF SCRIPT
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Installation of $app completed" --last
|