[Dev] Add positional arg start, stop or test in deployer

This commit is contained in:
samuel 2023-05-01 13:27:06 +02:00
parent 8b5600d38f
commit 13085feb9c
8 changed files with 107 additions and 45 deletions

View File

@ -82,6 +82,7 @@ install: conf/*.sample systemd/*.service bin/*
install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/deployer" install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/deployer"
install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/runner" install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/runner"
install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/pipeline" install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/pipeline"
install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/pids"
install --directory "$(DESTDIR)$(TMPDIR)/$(NAME)/repositories" install --directory "$(DESTDIR)$(TMPDIR)/$(NAME)/repositories"
.PHONY: check_format .PHONY: check_format

View File

@ -4,6 +4,9 @@ err_deployer_sock_dir_not_directory=12
err_deployer_sock_dir_not_writable=13 err_deployer_sock_dir_not_writable=13
err_deployer_sock_already_in_use=14 err_deployer_sock_already_in_use=14
err_deployer_sock_not_exist=15 err_deployer_sock_not_exist=15
err_deployer_pid_empty=16
err_deployer_pid_dir_not_directory=17
err_deployer_pid_dir_not_writable=18
err_repos_dir_empty=19 err_repos_dir_empty=19
err_repos_dir_not_directory=20 err_repos_dir_not_directory=20
err_repos_dir_not_accessible=21 err_repos_dir_not_accessible=21
@ -59,3 +62,8 @@ err_make_tarball=107
err_rpm_build=108 err_rpm_build=108
err_rpm_upgrade=109 err_rpm_upgrade=109
err_rpm_install=110 err_rpm_install=110
err_deployer_pid_not_exist=111
err_deployer_pid_not_readable=112
err_deployer_process_not_running=113
err_deployer_process_not_running=114
err_deployer_process_not_killed=115

View File

@ -10,6 +10,9 @@ deployer_username="netoik-cicd-deployer"
# Location of unixsock file used to send requests to the deployer server. # Location of unixsock file used to send requests to the deployer server.
deployer_sock="/run/netoik-cicd/deployer/deployer.sock" deployer_sock="/run/netoik-cicd/deployer/deployer.sock"
# Location of deployer pid.
deployer_pid="/run/netoik-cicd/pids/deployer.pid"
# Maximum number of seconds to wait for deployer response. # Maximum number of seconds to wait for deployer response.
# Set to 0 to disable timeout. # Set to 0 to disable timeout.
deployer_timeout=30 deployer_timeout=30

View File

@ -55,9 +55,7 @@ fi
# Reload systemctl daemon. # Reload systemctl daemon.
systemctl daemon-reload systemctl daemon-reload
# Restart deployer service. # Restart deployer service.
systemctl stop %{name}-deployer.service systemctl restart %{name}-deployer.service
rm --force %{_rundir}/%{name}/deployer/deployer.sock
systemctl start %{name}-deployer.service
systemctl enable %{name}-deployer.service systemctl enable %{name}-deployer.service
# Restart runner service. # Restart runner service.
systemctl stop %{name}-runner.service systemctl stop %{name}-runner.service
@ -69,7 +67,6 @@ systemctl enable %{name}-runner.service
# Stop service only if uninstalling. # Stop service only if uninstalling.
if [ $1 -eq 0 ]; then if [ $1 -eq 0 ]; then
systemctl disable --now %{name}-deployer.service systemctl disable --now %{name}-deployer.service
rm --force %{_rundir}/%{name}/deployer/deployer.sock
systemctl disable --now %{name}-runner.service systemctl disable --now %{name}-runner.service
rm --force %{_rundir}/%{name}/runner/runner.sock rm --force %{_rundir}/%{name}/runner/runner.sock
fi fi
@ -99,5 +96,6 @@ fi
%attr(755, %{name}-deployer, %{name}-deployer) %dir %{_rundir}/%{name}/deployer %attr(755, %{name}-deployer, %{name}-deployer) %dir %{_rundir}/%{name}/deployer
%attr(755, git, git) %dir %{_rundir}/%{name}/pipeline %attr(755, git, git) %dir %{_rundir}/%{name}/pipeline
%attr(755, %{name}-runner, %{name}-runner) %dir %{_rundir}/%{name}/runner %attr(755, %{name}-runner, %{name}-runner) %dir %{_rundir}/%{name}/runner
%attr(775, %{name}-deployer, %{name}-runner-deployer) %dir %{_rundir}/%{name}/pids
%attr(755, root, root) %dir %{_tmppath}/%{name} %attr(755, root, root) %dir %{_tmppath}/%{name}
%attr(755, %{name}-runner, %{name}-runner) %dir %{_tmppath}/%{name}/repositories %attr(755, %{name}-runner, %{name}-runner) %dir %{_tmppath}/%{name}/repositories

View File

@ -77,7 +77,7 @@ fail() (
) )
usage() ( usage() (
echo "Usage: ${PROGRAM_NAME} [OPTION]... echo "Usage: ${PROGRAM_NAME} [OPTION]... ACTION
Start deployer server, wait for unixsock requests to deploy rpm packages. Start deployer server, wait for unixsock requests to deploy rpm packages.
Mandatory argumentes for long options are mandatory for short options too. Mandatory argumentes for long options are mandatory for short options too.
@ -89,10 +89,14 @@ Mandatory argumentes for long options are mandatory for short options too.
${DEFAULT_ERRORS_FILE} ${DEFAULT_ERRORS_FILE}
-o, --once stop listening after first request process -o, --once stop listening after first request process
-q, --quiet set level verbosity to WARN, default to INFO -q, --quiet set level verbosity to WARN, default to INFO
-t, --test just test config file and do not run start loop
-v, --verbose set level verbosity to DEBUG, default to INFO -v, --verbose set level verbosity to DEBUG, default to INFO
-h, --help display this help message and exit -h, --help display this help message and exit
Positional argument ACTION
start start deployer server
stop stop deployer server
test test configuration and exit
" "
) )
@ -253,15 +257,14 @@ process_loop() (
main() ( main() (
# Parse arguments. # Parse arguments.
daemon="false" daemon="false"
testing="false"
keep_open="true" keep_open="true"
config_file="${DEFAULT_CONFIG_FILE}" config_file="${DEFAULT_CONFIG_FILE}"
errors_file="${DEFAULT_ERRORS_FILE}" errors_file="${DEFAULT_ERRORS_FILE}"
verbosity_option="" verbosity_option=""
verbosity_level="${DEFAULT_VERBOSITY_LEVEL}" verbosity_level="${DEFAULT_VERBOSITY_LEVEL}"
if ! args="$(getopt --name "${PROGRAM_NAME}" \ if ! args="$(getopt --name "${PROGRAM_NAME}" \
--options c:de:hoqtv \ --options c:de:hoqv \
--longoptions conf:,daemon,errs:,help,once,quiet,test,verbose \ --longoptions conf:,daemon,errs:,help,once,quiet,verbose \
-- "$@")"; then -- "$@")"; then
usage usage
fail "Bad arguments." fail "Bad arguments."
@ -299,10 +302,6 @@ main() (
((verbosity_level -= 10)) ((verbosity_level -= 10))
shift shift
;; ;;
-t | --test)
testing="true"
shift
;;
-v | --verbose) -v | --verbose)
if [ "${verbosity_option}" ]; then if [ "${verbosity_option}" ]; then
usage usage
@ -322,10 +321,19 @@ main() (
;; ;;
esac esac
done done
if [ "$@" ]; then if [ $# -ne 1 ]; then
usage usage
fail "Unexpected extra arguments '$*'." fail "Missing positional argument ACTION"
fi fi
case "$1" in
start | stop | test)
action="$1"
;;
*)
usage
fail "Bad positional argument ACTION '$1'."
;;
esac
# Load config file. # Load config file.
if [ ! -r "${config_file}" ]; then if [ ! -r "${config_file}" ]; then
@ -381,35 +389,73 @@ main() (
fi fi
if [ -z "${runner_deployer_groupname}" ]; then if [ -z "${runner_deployer_groupname}" ]; then
fail "Variable runner_deployer_username is empty." "${err_runner_deployer_groupname_empty}" fail "Variable runner_deployer_username is empty." \
"${err_runner_deployer_groupname_empty}"
fi fi
if ! getent group "${runner_deployer_groupname}"; then if ! getent group "${runner_deployer_groupname}"; then
fail "Runner-deployer group '${runner_deployer_groupname}' does not exist." \ fail "Runner-deployer group '${runner_deployer_groupname}' does not exist." \
"${err_runner_deployer_group_not_exist}" "${err_runner_deployer_group_not_exist}"
fi fi
# Stop now if we are only testing config. if [ -z "${deployer_pid}" ]; then
if "${testing}"; then fail "Variable deployer_pid is empty." "${err_deployer_pid_empty}"
fi
if [ ! -d "$(dirname "${deployer_pid}")" ]; then
fail "Dirname of deployer_pid='${deployer_pid}' is not a directory." \
"${err_deployer_pid_dir_not_directory}"
fi
if [ ! -w "$(dirname "${deployer_pid}")" ]; then
fail "Dirname of deployer_pid='${deployer_pid}' is not writable." \
"${err_deployer_pid_dir_not_writable}"
fi
case "${action}" in
test)
# Stop now if we are only testing config.
exit 0 exit 0
fi ;;
start)
# Set right access in background after nc listen.
(
inotifywait --timeout 1 --quiet --quiet \
--event create "$(dirname "${deployer_sock}")" || true
chmod 775 "${deployer_sock}"
chgrp "${runner_deployer_groupname}" "${deployer_sock}"
) &
# Set right access in background after nc listen. # Run process loop in background or foreground.
( if "${daemon}"; then
inotifywait --timeout 1 --quiet --quiet \ log_info "Run process loop in background."
--event create "$(dirname "${deployer_sock}")" || true process_loop &
chmod 775 "${deployer_sock}" echo "$!" >"${deployer_pid}"
chgrp "${runner_deployer_groupname}" "${deployer_sock}" else
) & log_info "Run process loop in foreground."
echo "$$" >"${deployer_pid}"
# Run process loop in background or foreground. process_loop
if "${daemon}"; then jobs -p | xargs kill 2>/dev/null || true
log_info "Run process loop in background." fi
process_loop & ;;
else stop)
log_info "Run process loop in foreground." if [ ! -f "${deployer_pid}" ]; then
process_loop fail "File deployer_pid='${deployer_pid}' does not exist." \
jobs -p | xargs kill 2>/dev/null || true "${err_deployer_pid_not_exist}"
fi fi
if [ ! -r "${deployer_pid}" ]; then
fail "File deployer_pid='${deployer_pid}' is not readable." \
"${err_deployer_pid_not_readable}"
fi
pid="$(cat "${deployer_pid}")"
if ! ps -p "${pid}"; then
fail "Deployer process with pid='${pid}' is not running." \
"${err_deployer_process_not_running}"
fi
if ! kill "${pid}" || kill --signal KILL "${pid}"; then
fail "Cannot kill deployer process." \
"${err_deployer_process_not_killed}"
fi
rm --force "${deployer_sock}"
;;
esac
) )
main "$@" main "$@"

View File

@ -5,7 +5,8 @@ After=network.target
[Service] [Service]
User=netoik-cicd-deployer User=netoik-cicd-deployer
Group=netoik-cicd-deployer Group=netoik-cicd-deployer
ExecStart=/usr/bin/netoik-cicd-deployer ExecStart=/usr/bin/netoik-cicd-deployer start
ExecStop=/usr/bin/netoik-cicd-deployer stop
Restart=always Restart=always
[Install] [Install]

View File

@ -22,34 +22,36 @@ teardown() {
} }
@test "run with bad arguments" { @test "run with bad arguments" {
run -1 "${deployer_bin}" -z run -1 "${deployer_bin}" -z test
run -1 "${deployer_bin}" --zzz run -1 "${deployer_bin}" --zzz test
run -1 "${deployer_bin}" zzz run -1 "${deployer_bin}" zzz
run -1 "${deployer_bin}" --conf run -1 "${deployer_bin}" --conf
run -1 "${deployer_bin}" --conf=/path/to/nowhere run -1 "${deployer_bin}" --conf=/path/to/nowhere test
run -1 "${deployer_bin}" --errs run -1 "${deployer_bin}" --errs
run -1 "${deployer_bin}" --errs=/path/to/nowhere run -1 "${deployer_bin}" --errs=/path/to/nowhere test
run -1 "${deployer_bin}" --verbose --quiet run -1 "${deployer_bin}" --verbose --quiet test
} }
@test "run without loop" { @test "run without loop" {
"${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" --test --verbose "${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" --verbose test
} }
send_request() ( send_request() (
request="$1" request="$1"
"${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" \ "${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" \
--daemon --once --verbose || return $? --once --daemon --verbose start || return $?
( (
inotifywait --timeout 1 --quiet --quiet \ inotifywait --timeout 1 --quiet --quiet \
--event create "${response_sock_dir}" || true --event create "${response_sock_dir}" || true
echo "${request}" | ncat --unixsock "${deployer_sock}" echo "${request}" | ncat --unixsock "${deployer_sock}"
) & ) &
return "$(ncat --listen --unixsock "${response_sock}" | jq .code)" code="$(ncat --listen --unixsock "${response_sock}" | jq .code)"
"${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" stop
return "${code}"
) )
@test "run with err_pkg_name_missing" { @test "run with err_pkg_name_missing" {

View File

@ -25,6 +25,9 @@ deployer_username="$(id --user --name)"
# Location of unixsock file used to send requests to the deployer server. # Location of unixsock file used to send requests to the deployer server.
deployer_sock="${temp}${run_dir}/${name}/deployer/deployer.sock" deployer_sock="${temp}${run_dir}/${name}/deployer/deployer.sock"
# Location of deployer pid.
deployer_pid="${temp}${run_dir}/${name}/pids/deployer.pid"
# Maximum number of seconds to wait for deployer response. # Maximum number of seconds to wait for deployer response.
# Set to 0 to disable timeout. # Set to 0 to disable timeout.
deployer_timeout=30 deployer_timeout=30