1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/gitlab_ynh.git synced 2024-09-03 18:36:35 +02:00

Rework gitlab_ctl_action

This commit is contained in:
Pierre Bourré 2019-03-17 20:15:14 +01:00
parent 01bebbfda6
commit e43373a423
5 changed files with 71 additions and 60 deletions

View file

@ -117,77 +117,87 @@ ynh_systemd_action() {
# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started
#
# usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ]
# | arg: -n, --service_name= - Name of the service to start. Default : $app
# usage: ynh_systemd_action [-a action] [ [-t timeout] ]
# | arg: -a, --action= - Action to perform with systemctl. Default: start
# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot.
# If not defined it don't wait until the service is completely started.
# WARNING: When using --line_match, you should always add `ynh_clean_check_starting` into your
# `ynh_clean_setup` at the beginning of the script. Otherwise, tail will not stop in case of failure
# of the script. The script will then hang forever.
# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log
# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds.
# | arg: -e, --length= - Length of the error log : Default : 20
gitlab_ctl_action() {
# Declare an array to define the options of this helper.
declare -Ar args_array=( [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= )
declare -Ar args_array=( [a]=action= [t]=timeout= )
local action
local line_match
local log_path
local timeout
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
local line_match_new="adopted new unicorn master"
local line_match_existing="adopted existing unicorn master"
local line_match_error="master failed to start"
local log_path="/var/log/gitlab/unicorn/current"
local action=${action:-start}
local log_path="${log_path:-/var/log/gitlab/unicorn/current}"
local timeout=${timeout:-300}
# Start to read the log
if [[ -n "${line_match:-}" ]]
then
local templog="$(mktemp)"
# Read the specified log file
tail -F -n0 "$log_path" > "$templog" 2>&1 &
# Get the PID of the tail command
local pid_tail=$!
fi
local templog="$(mktemp)"
# Read the specified log file
tail -F -n0 "$log_path" > "$templog" 2>&1 &
# Get the PID of the tail command
local pid_tail=$!
ynh_print_info --message="${action^} gitlab"
gitlab-ctl $action
# Start the timeout and try to find line_match
if [[ -n "${line_match:-}" ]]
then
local i=0
for i in $(seq 1 $timeout)
do
# Read the log until the sentence is found, that means the app finished to start. Or run until the timeout
if grep --quiet "$line_match" "$templog"
then
ynh_print_info --message="Gitlab has correctly started."
break
fi
if [ $i -eq 3 ]; then
echo -n "Please wait, Gitlab is ${action}ing" >&2
fi
if [ $i -ge 3 ]; then
echo -n "." >&2
fi
sleep 1
done
if [ $i -ge 3 ]; then
echo "" >&2
fi
if [ $i -eq $timeout ]
# Start the timeout and try to find line_match_new or line_match_existing
local i=0
for i in $(seq 1 $timeout)
do
# Read the log until the sentence is found, that means the app finished to start. Or run until the timeout
if grep --quiet "$line_match_new" "$templog"
then
ynh_print_warn --message="Gitlab didn't fully started before the timeout."
ynh_print_warn --message="Please find here an extract of the end of the log of Gitlab:"
test -e "$log_path" && echo "--" >&2 && tail --lines=$length "$log_path" >&2
ynh_print_info --message="Gitlab has correctly started."
break
fi
ynh_clean_check_starting
if grep --quiet "$line_match_existing" "$templog"
then
ynh_print_info --message="Gitlab has correctly started."
break
fi
if grep --quiet "$line_match_error" "$templog"
then
ynh_print_warn "Error during ${action}ing, reconfiguring and restarting gitlab"
ynh_clean_check_starting
# Start to read the log
local templog="$(mktemp)"
# Read the specified log file
tail -F -n0 "$log_path" > "$templog" 2>&1 &
# Get the PID of the tail command
local pid_tail=$!
gitlab-ctl reconfigure
gitlab-ctl restart
# Force restart unicorn
gitlab-ctl restart unicorn
fi
if [ $i -eq 3 ]; then
echo -n "Please wait, Gitlab is ${action}ing" >&2
fi
if [ $i -ge 3 ]; then
echo -n "." >&2
fi
sleep 1
done
if [ $i -ge 3 ]; then
echo "" >&2
fi
if [ $i -eq $timeout ]
then
ynh_print_warn --message="Gitlab didn't fully started before the timeout."
ynh_print_warn --message="Please find here an extract of the end of the log of Gitlab:"
test -e "$log_path" && echo "--" >&2 && tail --lines=$length "$log_path" >&2
fi
ynh_clean_check_starting
}
# Clean temporary process and file used by ynh_check_starting

View file

@ -117,7 +117,7 @@ ynh_store_file_checksum "$config_path/gitlab.rb"
gitlab-ctl reconfigure
gitlab_ctl_action --action=restart --line_match="adopted new unicorn master" --log_path="/var/log/gitlab/unicorn/current" --timeout=3600
gitlab_ctl_action --action=restart --timeout=3600
#=================================================
# GENERIC FINALISATION

View file

@ -141,8 +141,8 @@ ynh_setup_source $tempdir $architecture
if IS_PACKAGE_CHECK; then
if [ ! dpkg -i $tempdir/$gitlab_filename ]; then # This command will fail in lxc env
ynh_replace_string "command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" "command \"cat \/etc\/sysctl.conf\"" "$final_path/embedded/cookbooks/package/resources/sysctl.rb"
dpkg --configure gitlab-ce
ynh_replace_string "command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" "command \"cat \/etc\/sysctl.conf\"" "$final_path/embedded/cookbooks/package/resources/sysctl.rb"
dpkg --configure gitlab-ce
fi
else
dpkg -i $tempdir/$gitlab_filename
@ -198,7 +198,7 @@ fi
#=================================================
ynh_print_info "Restarting gitlab..."
gitlab_ctl_action --action=restart --line_match="adopted new unicorn master" --log_path="/var/log/gitlab/unicorn/current" --timeout=3600
gitlab_ctl_action --action=restart --timeout=3600
#=================================================
# RELOAD NGINX

View file

@ -96,8 +96,8 @@ ynh_setup_source $tempdir $architecture
if IS_PACKAGE_CHECK; then
if [ ! dpkg -i $tempdir/$gitlab_filename ]; then # This command will fail in lxc env
ynh_replace_string "command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" "command \"cat \/etc\/sysctl.conf\"" "$final_path/embedded/cookbooks/package/resources/sysctl.rb"
dpkg --configure gitlab-ce
ynh_replace_string "command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" "command \"cat \/etc\/sysctl.conf\"" "$final_path/embedded/cookbooks/package/resources/sysctl.rb"
dpkg --configure gitlab-ce
fi
else
dpkg -i $tempdir/$gitlab_filename
@ -118,9 +118,10 @@ gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# Use gitlab-rake to backup
gitlab-rake gitlab:backup:restore force=yes BACKUP=$last_backup
ynh_exec_warn_less gitlab-rake gitlab:backup:restore force=yes BACKUP=$last_backup
gitlab_ctl_action --action=restart --timeout=3600
gitlab-ctl restart
gitlab-rake gitlab:check SANITIZE=true
#=================================================

View file

@ -180,8 +180,8 @@ ynh_setup_source $tempdir $architecture
if IS_PACKAGE_CHECK; then
if [ ! dpkg -i $tempdir/$gitlab_filename ]; then # This command will fail in lxc env
ynh_replace_string "command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" "command \"cat \/etc\/sysctl.conf\"" "$final_path/embedded/cookbooks/package/resources/sysctl.rb"
dpkg --configure gitlab-ce
ynh_replace_string "command \"cat \/etc\/sysctl.conf \/etc\/sysctl.d\/\*.conf | sysctl -e -p -\"" "command \"cat \/etc\/sysctl.conf\"" "$final_path/embedded/cookbooks/package/resources/sysctl.rb"
dpkg --configure gitlab-ce
fi
else
dpkg -i $tempdir/$gitlab_filename
@ -212,7 +212,7 @@ fi
#=================================================
ynh_print_info "Restarting gitlab..."
gitlab_ctl_action --action=restart --line_match="adopted new unicorn master" --log_path="/var/log/gitlab/unicorn/current" --timeout=3600
gitlab_ctl_action --action=restart --timeout=3600
#=================================================
# RELOAD NGINX