From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 1340E15815E for ; Sun, 4 Feb 2024 19:26:04 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id DB4B12BC015; Sun, 4 Feb 2024 19:26:02 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id C201F2BC015 for ; Sun, 4 Feb 2024 19:26:02 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id EFEA1343163 for ; Sun, 4 Feb 2024 19:26:01 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 8C75B14AB for ; Sun, 4 Feb 2024 19:26:00 +0000 (UTC) From: "Anna Vyalkova" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Anna Vyalkova" Message-ID: <1707074601.6b0464e027de7a13d7a0b39a2974f49fb32ff576.cybertailor@gentoo> Subject: [gentoo-commits] repo/proj/guru:dev commit in: eclass/ X-VCS-Repository: repo/proj/guru X-VCS-Files: eclass/daemons.eclass X-VCS-Directories: eclass/ X-VCS-Committer: cybertailor X-VCS-Committer-Name: Anna Vyalkova X-VCS-Revision: 6b0464e027de7a13d7a0b39a2974f49fb32ff576 X-VCS-Branch: dev Date: Sun, 4 Feb 2024 19:26:00 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 39b8c30e-a0d8-47b3-9007-65af40ef4501 X-Archives-Hash: 8c0b9c01afa1135581957c11c2ba8863 commit: 6b0464e027de7a13d7a0b39a2974f49fb32ff576 Author: Anna (cybertailor) Vyalkova sysrq in> AuthorDate: Sat Feb 3 06:31:43 2024 +0000 Commit: Anna Vyalkova sysrq in> CommitDate: Sun Feb 4 19:23:21 2024 +0000 URL: https://gitweb.gentoo.org/repo/proj/guru.git/commit/?id=6b0464e0 daemons.eclass: new eclass Signed-off-by: Anna (cybertailor) Vyalkova sysrq.in> eclass/daemons.eclass | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 213 insertions(+) diff --git a/eclass/daemons.eclass b/eclass/daemons.eclass new file mode 100644 index 0000000000..24798158d3 --- /dev/null +++ b/eclass/daemons.eclass @@ -0,0 +1,213 @@ +# Copyright 2022-2024 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: daemons.eclass +# @MAINTAINER: +# Anna +# @AUTHOR: +# Anna +# @SUPPORTED_EAPIS: 8 +# @BLURB: eclass to test packages against running daemons +# @DESCRIPTION: +# A utility eclass providing functions for starting and stopping daemons with +# Pifpaf. +# +# This eclass does not set any metadata variables nor export any phase, so it +# can be inherited safely. +# +# @SUBSECTION Supported daemons +# +# - ceph +# +# - consul +# +# - httpbin +# +# - kafka +# +# - memcached +# +# - mysql +# +# - postgresql +# +# - redis +# +# - vault +# +# @EXAMPLE: +# +# @CODE +# EAPI=8 +# +# ... +# +# DAEMONS_REQ_USE=( +# [postgresql]="xml" +# ) +# inherit daemons distutils-r1 +# +# ... +# +# distutils_enable_tests pytest +# +# daemons_enable postgresql test +# +# src_test() { +# daemons_start postgresql +# distutils-r1_src_test +# daemons_stop postgresql +# } +# @CODE + +case ${EAPI} in + 8) ;; + *) die "${ECLASS}: EAPI ${EAPI} unsupported." +esac + +if [[ ! ${_DAEMONS_ECLASS} ]]; then +_DAEMONS_ECLASS=1 + +# @ECLASS_VARIABLE: DAEMONS_REQ_USE +# @PRE_INHERIT +# @DEFAULT_UNSET +# @DESCRIPTION: +# Bash associative array of USE flags required to be enabled on daemons, +# formed as a USE-dependency string. + +# @ECLASS_VARIABLE: DAEMONS_DEPEND +# @OUTPUT_VARIABLE +# @DESCRIPTION: +# This is an eclass-generated bash associative array of dependency strings for +# daemons. +declare -Ag DAEMONS_DEPEND=() + +# @FUNCTION: _daemons_set_globals +# @INTERNAL +# @DESCRIPTION: +# Set the DAEMONS_DEPEND variable. +_daemons_set_globals() { + local -A pkgs=( + [ceph]="sys-cluster/ceph" + [consul]="app-admin/consul" + [httpbin]="dev-python/httpbin" + [kafka]="net-misc/kafka-bin" + [memcached]="net-misc/memcached" + [mysql]="virtual/mysql" + [postgresql]="dev-db/postgresql" + [redis]="dev-db/redis" + [vault]="app-admin/vault" + ) + + local -A useflags=( + [mysql]="server" + [postgresql]="server" + ) + + if declare -p DAEMONS_REQ_USE &>/dev/null; then + [[ $(declare -p DAEMONS_REQ_USE) == "declare -A"* ]] || \ + die "DAEMONS_REQ_USE must be declared as an associative array" + fi + + local name dep usestr + for name in "${!pkgs[@]}"; do + dep=${pkgs[${name:?}]:?} + usestr=${useflags[${name:?}]} + usestr+=",${DAEMONS_REQ_USE[${name:?}]}" + # strip leading/trailing commas + usestr=${usestr#,} + usestr=${usestr%,} + + [[ ${usestr?} ]] && usestr="[${usestr?}]" + DAEMONS_DEPEND[${name:?}]="dev-util/pifpaf ${dep:?}${usestr?}" + done + + readonly DAEMONS_DEPEND +} +_daemons_set_globals +unset -f _daemons_set_globals + +# @FUNCTION: daemons_enable +# @USAGE: +# @DESCRIPTION: +# Add the daemon package to build-time dependencies under the given USE flag +# (IUSE will be set automatically). +daemons_enable() { + debug-print-function ${FUNCNAME} "${@}" + + local daemon=${1:?} + local useflag=${2:?} + + IUSE+=" ${useflag:?}" + BDEPEND+=" ${useflag:?}? ( ${DAEMONS_DEPEND[${daemon:?}]:?} )" + if [[ ${useflag:?} == "test" ]]; then + RESTRICT+=" !test? ( test )" + fi +} + +# @FUNCTION: daemons_start +# @USAGE: [args...] +# @DESCRIPTION: +# Start the daemon. All arguments are passes to Pifpaf. +# +# Pifpaf will set some environment variables for you, they will be prefixed by +# uppercase daemon name. See upstream documentation for details. +daemons_start() { + debug-print-function ${FUNCNAME} "${@}" + + local daemon=${1:?} + shift + + local logfile="${T?}/daemon-${daemon:?}.log" + local tempfile="${T?}/daemon-${daemon:?}.sh" + local myargs=( + --env-prefix "${daemon^^}" + --log-file "${logfile:?}" + --verbose + ) + + ebegin "Starting ${daemon:?}" + pifpaf "${myargs[@]}" run "${daemon:?}" "${@}" > "${tempfile:?}" && \ + source "${tempfile:?}" && \ + rm -f "${tempfile:?}" + eend $? || die "Starting ${daemon:?} failed" +} + +# @FUNCTION: daemons_stop +# @USAGE: +# @DESCRIPTION: +# Stop a running daemon. +daemons_stop() { + debug-print-function ${FUNCNAME} "${@}" + + local daemon=${1:?} + local stop_fn="${daemon:?}_stop" + declare -f "${stop_fn:?}" >/dev/null || die "${daemon:?} is not running" + + ebegin "Stopping ${daemon:?}" + "${stop_fn:?}" + eend $? || die "Stopping ${daemon:?} failed" +} + +# @FUNCTION: daemons_death_notice +# @INTERNAL +# @DESCRIPTION: +# Creates archive with daemon logs and prints a log message with its location. +daemons_death_notice() { + shopt -s nullglob + local logfiles=( "${T}"/daemon-*.log ) + local logarchive="${T}/daemon-logs.tar.xz" + shopt -u nullglob + + if [[ "${logfiles[*]}" ]]; then + pushd "${T}" >/dev/null + tar -acf "${logarchive}" "${logfiles[@]#${T}/}" + popd >/dev/null + eerror + eerror "Please include ${logarchive} in your bug report." + eerror + fi +} +has daemons_death_notice ${EBUILD_DEATH_HOOKS} || EBUILD_DEATH_HOOKS+=" daemons_death_notice" + +fi