From 13085feb9c89ac090ad96ef7d7ecdd298c14df5a Mon Sep 17 00:00:00 2001 From: samuel Date: Mon, 1 May 2023 13:27:06 +0200 Subject: [PATCH] [Dev] Add positional arg start, stop or test in deployer --- Makefile | 1 + conf/errors.conf.sample | 8 +++ conf/netoik-cicd.conf.sample | 3 + netoik-cicd.spec | 6 +- src/deployer.sh | 110 +++++++++++++++++++++++++---------- systemd/deployer.service | 3 +- tests/deployer.bats | 18 +++--- tests/tests.conf | 3 + 8 files changed, 107 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index df14ba6..0c3e963 100644 --- a/Makefile +++ b/Makefile @@ -82,6 +82,7 @@ install: conf/*.sample systemd/*.service bin/* install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/deployer" install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/runner" install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/pipeline" + install --directory "$(DESTDIR)$(RUNDIR)/$(NAME)/pids" install --directory "$(DESTDIR)$(TMPDIR)/$(NAME)/repositories" .PHONY: check_format diff --git a/conf/errors.conf.sample b/conf/errors.conf.sample index 9006532..2129246 100644 --- a/conf/errors.conf.sample +++ b/conf/errors.conf.sample @@ -4,6 +4,9 @@ err_deployer_sock_dir_not_directory=12 err_deployer_sock_dir_not_writable=13 err_deployer_sock_already_in_use=14 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_not_directory=20 err_repos_dir_not_accessible=21 @@ -59,3 +62,8 @@ err_make_tarball=107 err_rpm_build=108 err_rpm_upgrade=109 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 diff --git a/conf/netoik-cicd.conf.sample b/conf/netoik-cicd.conf.sample index e0515fc..de791d9 100644 --- a/conf/netoik-cicd.conf.sample +++ b/conf/netoik-cicd.conf.sample @@ -10,6 +10,9 @@ deployer_username="netoik-cicd-deployer" # Location of unixsock file used to send requests to the deployer server. 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. # Set to 0 to disable timeout. deployer_timeout=30 diff --git a/netoik-cicd.spec b/netoik-cicd.spec index 972e6c6..26873cf 100644 --- a/netoik-cicd.spec +++ b/netoik-cicd.spec @@ -55,9 +55,7 @@ fi # Reload systemctl daemon. systemctl daemon-reload # Restart deployer service. -systemctl stop %{name}-deployer.service -rm --force %{_rundir}/%{name}/deployer/deployer.sock -systemctl start %{name}-deployer.service +systemctl restart %{name}-deployer.service systemctl enable %{name}-deployer.service # Restart runner service. systemctl stop %{name}-runner.service @@ -69,7 +67,6 @@ systemctl enable %{name}-runner.service # Stop service only if uninstalling. if [ $1 -eq 0 ]; then systemctl disable --now %{name}-deployer.service - rm --force %{_rundir}/%{name}/deployer/deployer.sock systemctl disable --now %{name}-runner.service rm --force %{_rundir}/%{name}/runner/runner.sock fi @@ -99,5 +96,6 @@ fi %attr(755, %{name}-deployer, %{name}-deployer) %dir %{_rundir}/%{name}/deployer %attr(755, git, git) %dir %{_rundir}/%{name}/pipeline %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, %{name}-runner, %{name}-runner) %dir %{_tmppath}/%{name}/repositories diff --git a/src/deployer.sh b/src/deployer.sh index 19c5500..9d6b50d 100755 --- a/src/deployer.sh +++ b/src/deployer.sh @@ -77,7 +77,7 @@ fail() ( ) usage() ( - echo "Usage: ${PROGRAM_NAME} [OPTION]... + echo "Usage: ${PROGRAM_NAME} [OPTION]... ACTION Start deployer server, wait for unixsock requests to deploy rpm packages. 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} -o, --once stop listening after first request process -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 -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() ( # Parse arguments. daemon="false" - testing="false" keep_open="true" config_file="${DEFAULT_CONFIG_FILE}" errors_file="${DEFAULT_ERRORS_FILE}" verbosity_option="" verbosity_level="${DEFAULT_VERBOSITY_LEVEL}" if ! args="$(getopt --name "${PROGRAM_NAME}" \ - --options c:de:hoqtv \ - --longoptions conf:,daemon,errs:,help,once,quiet,test,verbose \ + --options c:de:hoqv \ + --longoptions conf:,daemon,errs:,help,once,quiet,verbose \ -- "$@")"; then usage fail "Bad arguments." @@ -299,10 +302,6 @@ main() ( ((verbosity_level -= 10)) shift ;; - -t | --test) - testing="true" - shift - ;; -v | --verbose) if [ "${verbosity_option}" ]; then usage @@ -322,10 +321,19 @@ main() ( ;; esac done - if [ "$@" ]; then + if [ $# -ne 1 ]; then usage - fail "Unexpected extra arguments '$*'." + fail "Missing positional argument ACTION" fi + case "$1" in + start | stop | test) + action="$1" + ;; + *) + usage + fail "Bad positional argument ACTION '$1'." + ;; + esac # Load config file. if [ ! -r "${config_file}" ]; then @@ -381,35 +389,73 @@ main() ( fi 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 if ! getent group "${runner_deployer_groupname}"; then fail "Runner-deployer group '${runner_deployer_groupname}' does not exist." \ "${err_runner_deployer_group_not_exist}" fi - # Stop now if we are only testing config. - if "${testing}"; then + if [ -z "${deployer_pid}" ]; 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 - 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. - ( - inotifywait --timeout 1 --quiet --quiet \ - --event create "$(dirname "${deployer_sock}")" || true - chmod 775 "${deployer_sock}" - chgrp "${runner_deployer_groupname}" "${deployer_sock}" - ) & - - # Run process loop in background or foreground. - if "${daemon}"; then - log_info "Run process loop in background." - process_loop & - else - log_info "Run process loop in foreground." - process_loop - jobs -p | xargs kill 2>/dev/null || true - fi + # Run process loop in background or foreground. + if "${daemon}"; then + log_info "Run process loop in background." + process_loop & + echo "$!" >"${deployer_pid}" + else + log_info "Run process loop in foreground." + echo "$$" >"${deployer_pid}" + process_loop + jobs -p | xargs kill 2>/dev/null || true + fi + ;; + stop) + if [ ! -f "${deployer_pid}" ]; then + fail "File deployer_pid='${deployer_pid}' does not exist." \ + "${err_deployer_pid_not_exist}" + 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 "$@" diff --git a/systemd/deployer.service b/systemd/deployer.service index e7fe829..31c80da 100644 --- a/systemd/deployer.service +++ b/systemd/deployer.service @@ -5,7 +5,8 @@ After=network.target [Service] User=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 [Install] diff --git a/tests/deployer.bats b/tests/deployer.bats index 55e028b..1529c65 100644 --- a/tests/deployer.bats +++ b/tests/deployer.bats @@ -22,34 +22,36 @@ teardown() { } @test "run with bad arguments" { - run -1 "${deployer_bin}" -z - run -1 "${deployer_bin}" --zzz + run -1 "${deployer_bin}" -z test + run -1 "${deployer_bin}" --zzz test run -1 "${deployer_bin}" zzz 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=/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" { - "${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" --test --verbose + "${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" --verbose test } send_request() ( request="$1" "${deployer_bin}" --conf="${conf_file}" --errs="${errs_file}" \ - --daemon --once --verbose || return $? + --once --daemon --verbose start || return $? ( inotifywait --timeout 1 --quiet --quiet \ --event create "${response_sock_dir}" || true 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" { diff --git a/tests/tests.conf b/tests/tests.conf index 4ad3139..071f490 100644 --- a/tests/tests.conf +++ b/tests/tests.conf @@ -25,6 +25,9 @@ deployer_username="$(id --user --name)" # Location of unixsock file used to send requests to the deployer server. 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. # Set to 0 to disable timeout. deployer_timeout=30