public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-dev] New Eclasses: postgres and postgres-multi
@ 2016-01-12 19:22 Aaron W. Swenson
  2016-01-12 20:57 ` Manuel Rüger
  2016-01-24 23:29 ` Aaron W. Swenson
  0 siblings, 2 replies; 18+ messages in thread
From: Aaron W. Swenson @ 2016-01-12 19:22 UTC (permalink / raw
  To: gentoo-dev


[-- Attachment #1.1: Type: text/plain, Size: 879 bytes --]

There are several ebuilds that repeat the same checks and need to
perform the same duties when it comes to working with PostgreSQL. For
example, making sure the users' currently slot is compatible with the
ebuild requirements. postgres.eclass addresses this and has
additional conveniences to build a dependency string and add a new user
into the postgres system group.

Additionally, as most of you are aware, we have a slot capable
dev-db/postgresql. There is some difficulty that needed to be resolved
so that extensions could also be installed into multiple slots, which is
addressed by postgres-multi.eclass.

I've an overlay at:
https://github.com/titanofold/titanofold-gentoo-x86

With the pgsql-eclass branch containing the eclass and a postgres-multi
enabled PostGIS.

Naturally, the eclasses work for me, so far.

For your convenience, I've also attached the eclasses.

[-- Attachment #1.2: postgres-multi.eclass --]
[-- Type: text/plain, Size: 3715 bytes --]

# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

inherit multibuild postgres
EXPORT_FUNCTIONS pkg_setup src_prepare src_compile src_install src_test


# @ECLASS: postgres-multi
# @MAINTAINER:
# PostgreSQL <pgsql-bugs@gentoo.org>
# @AUTHOR: Aaron W. Swenson <titanofold@gentoo.org>
# @BLURB: An eclass for PostgreSQL-related packages with default functions
# @DESCRIPTION:
# postgres-multi enables ebuilds, particularly PostgreSQL extensions, to
# build against any and all compatible, installed PostgreSQL
# slots. Additionally exports default functions.


case ${EAPI:-0} in
  0|1|2|3|4) die "postgres-multi.eclass requires EAPI 5 or higher" ;;
  *) ;;
esac


# @ECLASS-VARIABLE: POSTGRES_COMPAT
# @REQUIRED
# @DESCRIPTION:
# A Bash array containing a list of compatible PostgreSQL slots as
# defined by the developer.
if ! declare -p POSTGRES_COMPAT &>/dev/null; then
	die 'Required variable POSTGRES_COMPAT not declared.'
fi

# @ECLASS-VARIABLE: _POSTGRES_UNION_SLOTS
# @INTERNAL
# @DESCRIPTION:
# A Bash array containing the union set of available slots installed on the
# system that are also in POSTGRES_COMPAT.
export _POSTGRES_UNION_SLOTS=( )

# @FUNCTION _postgres-multi_multibuild_wrapper
# @INTERNAL
# @USAGE: _postgres-multi_multibuild_wrapper <command> [<arg> ...]
# @DESCRIPTION:
# For the given variant, set the values of the PG_SLOT and PG_CONFIG
# environment variables accordingly and replace any appearance of
# @PG_SLOT@ in the command and arguments with value of ${PG_SLOT}.
_postgres-multi_multibuild_wrapper() {
	debug-print-function ${FUNCNAME} "${@}"
	export PG_SLOT=${MULTIBUILD_VARIANT}
	export PG_CONFIG=$(which pg_config${MULTIBUILD_VARIANT//./})
	$(echo "${@}" | sed "s/@PG_SLOT@/${PG_SLOT}/g")
}

# @FUNCTION: postgres-multi_foreach
# @USAGE: postgres-multi_foreach <command> <arg> [<arg> ...]
# @DESCRIPTION:
# Run the given command in the package's source directory for each
# installed PostgreSQL slot. Any appearance of @PG_SLOT@ in the command
# or arguments will be substituted with the slot of the current
# iteration.
postgres-multi_foreach() {
	local MULTIBUILD_VARIANTS=("${_POSTGRES_UNION_SLOTS[@]}")

	multibuild_foreach_variant \
		_postgres-multi_multibuild_wrapper run_in_build_dir ${@}
}

# @FUNCTION: postgres-multi_forbest
# @USAGE: postgres-multi_forbest <command> <arg> [<arg> ...]
# @DESCRIPTION:
# Run the given command in the package's source directory for the best
# installed, compatible PostgreSQL slot. Any appearance of @PG_SLOT@ in
# the command or arguments will be substituted with the matching slot.
postgres-multi_forbest() {
	# POSTGRES_COMPAT is reverse sorted once in postgres.eclass so
	# element 0 has the highest slot version.
	local MULTIBUILD_VARIANTS=("${_POSTGRES_UNION_SLOTS[0]}")

	multibuild_foreach_variant \
		_postgres-multi_multibuild_wrapper run_in_build_dir ${@}
}

# @FUNCTION: postgres-multi_pkg_setup
# @USAGE: postgres-multi_pkg_setup
# @DESCRIPTION:
# Initialize internal environment variable(s).
postgres-multi_pkg_setup() {
	local all_slots=$(eselect --brief postgresql list)
	local user_slot

	for user_slot in "${POSTGRES_COMPAT[@]}"; do
		has "${user_slot}" ${all_slots} && \
			_POSTGRES_UNION_SLOTS+=( "${user_slot}" )
	done

	elog "Multibuild variants: ${_POSTGRES_UNION_SLOTS[@]}"
}

postgres-multi_src_prepare() {
	local MULTIBUILD_VARIANT
	local MULTIBUILD_VARIANTS=("${_POSTGRES_UNION_SLOTS[@]}")
	multibuild_copy_sources
}

postgres-multi_src_compile() {
	postgres-multi_foreach emake
}

postgres-multi_src_install() {
	postgres-multi_foreach emake install DESTDIR="${D}"
}

postgres-multi_src_test() {
	postgres-multi_foreach emake installcheck
}

[-- Attachment #1.3: postgres.eclass --]
[-- Type: text/plain, Size: 3084 bytes --]

# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

inherit user

# @ECLASS: postgres
# @MAINTAINER:
# PostgreSQL <pgsql-bugs@gentoo.org>
# @AUTHOR: Aaron W. Swenson <titanofold@gentoo.org>
# @BLURB: An eclass for PostgreSQL-related packages
# @DESCRIPTION:
# This eclass provides common utility functions that many
# PostgreSQL-related packages perform, such as checking that the
# currently selected PostgreSQL slot is within a range, adding a system
# user to the postgres system group, and generating dependencies.


case ${EAPI:-0} in
  0|1|2|3|4) die "postgres.eclass requires EAPI 5 or higher" ;;
  *) ;;
esac


# @ECLASS-VARIABLE: POSTGRES_COMPAT
# @DESCRIPTION:
# A Bash array containing a list of compatible PostgreSQL slots as
# defined by the developer.

# @ECLASS-VARIABLE: POSTGRES_DEP
# @DESCRIPTION:
# An automatically generated dependency string suitable for use in
# DEPEND and RDEPEND declarations.

# @ECLASS-VARIABLE: POSTGRES_USEDEP
# @DESCRIPTION:
# Add the given, without brackets, 2-Style and/or 4-Style use
# dependencies to POSTGRES_DEP

if declare -p POSTGRES_COMPAT &> /dev/null ; then
	# Reverse sort the given POSTGRES_COMPAT so that the most recent
	# slot is preferred over an older slot.
	readarray -t POSTGRES_COMPAT < <(printf '%s\n' "${POSTGRES_COMPAT[@]}" | sort -nr)

	POSTGRES_DEP="|| ("
	for slot in "${POSTGRES_COMPAT[@]}" ; do
		POSTGRES_DEP+=" dev-db/postgresql:${slot}="
		declare -p POSTGRES_USEDEP &>/dev/null && \
			POSTGRES_DEP+="[${POSTGRES_USEDEP}]"
	done
	POSTGRES_DEP+=" )"
else
	POSTGRES_DEP="dev-db/postgresql"
	declare -p POSTGRES_USEDEP &>/dev/null && \
		POSTGRES_DEP+="[${POSTGRES_USEDEP}]"
fi


# @FUNCTION: postgres_check_slot
# @DESCRIPTION:
# Verify that the currently selected PostgreSQL slot is set to one of
# the slots defined in POSTGRES_COMPAT. Automatically dies unless a
# POSTGRES_COMPAT slot is selected. Should be called in pkg_pretend().
postgres_check_slot() {
	if ! declare -p POSTGRES_COMPAT &>/dev/null; then
		die 'POSTGRES_COMPAT not declared.'
	fi

	# Don't die because we can't run postgresql-config during pretend.
	[[ "$EBUILD_PHASE" = "pretend" && -z "$(which postgresql-config 2> /dev/null)" ]] \
		&& return 0

	if has $(postgresql-config show 2> /dev/null) "${POSTGRES_COMPAT[@]}"; then
		return 0
	else
		eerror "PostgreSQL slot must be set to one of: "
		eerror "    ${POSTGRES_COMPAT[@]}"
		die "Incompatible PostgreSQL slot eselected"
	fi
}

# @FUNCTION: postgres_new_user
# @DESCRIPTION:
# Creates the "postgres" system group and user -- which is separate from
# the database user -- in addition to the developer defined user. Takes
# the same arguments as "enewuser".
postgres_new_user() {
	enewgroup postgres 70
	enewuser postgres 70 /bin/bash /var/lib/postgresql postgres

	if [[ $# -gt 0 ]] ; then
		if [[ "$1" = "postgres" ]] ; then
			ewarn "Username 'postgres' implied, skipping"
		else
			local groups=$5
			[[ -n "${groups}" ]] && groups+=",postgres" || groups="postgres"
			enewuser $1 $2 $3 $4 ${groups}
		fi
	fi
}

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 345 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2016-01-26 11:14 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-12 19:22 [gentoo-dev] New Eclasses: postgres and postgres-multi Aaron W. Swenson
2016-01-12 20:57 ` Manuel Rüger
2016-01-12 21:53   ` Aaron W. Swenson
2016-01-13 16:11     ` Ian Stakenvicius
2016-01-15 16:43       ` Aaron W. Swenson
2016-01-15 17:43         ` Ian Stakenvicius
2016-01-15 18:26           ` Ian Stakenvicius
2016-01-15 19:24           ` Aaron W. Swenson
2016-01-22 16:51             ` Ian Stakenvicius
2016-01-22 20:11               ` Aaron W. Swenson
2016-01-23 15:51                 ` Ian Stakenvicius
2016-01-25 15:31                   ` Ian Stakenvicius
2016-01-26 11:06                     ` Aaron W. Swenson
2016-01-24 23:29 ` Aaron W. Swenson
2016-01-24 23:44   ` Michael Orlitzky
2016-01-25 12:29     ` Aaron W. Swenson
2016-01-26  3:34       ` [gentoo-dev] " Jonathan Callen
2016-01-26 11:14         ` Aaron W. Swenson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox