aboutsummaryrefslogtreecommitdiff
blob: 7ff9b83eda3d4657832881aed3de7772c88ce6cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: $

inherit config multilib

DESCRIPTION="Manage active PostgreSQL client applications and libraries"
MAINTAINER="pgsql-bugs@gentoo.org"
VERSION="1.3"

# Global Data
USR_PATH="${EROOT%/}/usr"
ETC_PATH="${EROOT%/}/etc/eselect/postgresql"
ENV_FILE="${EROOT%/}/etc/env.d/50postgresql"

# This list of files/directories are the symbolic link targets that need to be
# created when a slot is set.
#
# If you change this list, remember to change include_sources in do_set. And,
# they must be listed in the same order.
INCLUDE_TARGETS=(
	"${USR_PATH}"/include/postgresql
	"${USR_PATH}"/include/libpq-fe.h
	"${USR_PATH}"/include/pg_config_ext.h
	"${USR_PATH}"/include/pg_config_manual.h
	"${USR_PATH}"/include/libpq
	"${USR_PATH}"/include/postgres_ext.h
)

active_slot() {
    # ${USR_PATH}/share/postgresql is a symlink. See if it's there, then
    # find out where it links to
	if [[ -h "${USR_PATH}/share/postgresql" ]] ; then
		canonicalise "${USR_PATH}/share/postgresql" | \
            sed 's|.*postgresql-\([1-9][0-9.]*\)|\1|'
	else
		echo "(none)"
	fi
}

lib_dir() {
	local lib_list=$(list_libdirs)
	if [[ ${lib_list} =~ .*lib64.* && \
		-n $(ls -d ${USR_PATH}/lib64/postgresql-*/lib64 2> /dev/null) ]] ; then
		echo "lib64"
	elif [[ ${lib_list} =~ .*lib32.* && \
		-n $(ls -d ${USR_PATH}/lib32/postgresql-*/lib32 2> /dev/null) ]] ; then
		echo "lib32"
	elif [[ ${lib_list} =~ .*libx32.* && \
		-n $(ls -d ${USR_PATH}/libx32/postgresql-*/libx32 2> /dev/null) ]] ; then
		echo "libx32"
	else
		echo "lib"
	fi
}

### Finder Function ###
# Takes two arguments:
#   - Absolute path to directory to search
#   - Pattern to search for
finder() {
	local source_dir=$1
	local pattern=$2

	# Prevent passed patterns from being globbed
	# If this module is run in /usr, '-name lib*' ends up globbing 'lib*',
	# passing to 'find' the pattern '-name lib lib32 lib64' and find interprets
	# those as path arguments causing failure.
	set -f
	find -L "${source_dir}" -maxdepth 1 -mindepth 1 ${pattern}
	set +f
}

### Linker Function ###
# Takes four arguments:
#   - Full source path (e.g. /usr/lib/postgresql-9.0/lib)
#   - Pattern to search for
#   - Full target directory path (e.g. /usr/bin)
#   - Suffix (Optional) (e.g 84 to make /usr/bin/psql84)
linker() {
	local source_dir=$1
	local pattern=$2
	local target_dir=$3
	local suffix=$4
	local link_source
	local findings
	local rel_source

	findings=$(finder "${source_dir}" ${pattern})

	for link_source in ${findings} ; do
		local link_target="${target_dir%/}/$(basename ${link_source})${suffix}"

		# For good measure, remove target before creating the symlink
		[[ -h ${link_target} ]] && rm -f "${link_target}"
		[[ -e ${link_target} ]] && \
			die -q "The target '${link_target}' still exists and could not be removed!"

		# Create relative links so that they work both here and inside the new
		# root if $ROOT is not "/".
		rel_source=$(relative_name "${link_source}" "${target_dir}")
		ln -s "${rel_source}" "${link_target}" || die -q "Unable to create link!"
	done
}

### Get Slots Function ###
# Find all available slots in the preferred lib_dir() and return them.
get_slots() {
	local slot
	local found_slots

	for slot in $(find "${USR_PATH}/$(lib_dir)/" -maxdepth 1 -type d \
		-regex '.*/postgresql-[1-9][0-9]*\.*[0-9]*' | \
		sed -re 's#.*([1-9][0-9]*\.*[0-9]*)$#\1#' | sort -n)
	do
		# Check that pg_config exists for this slot, otherwise we have
		# a false positive.
		[[ -x "${USR_PATH}/$(lib_dir)/postgresql-${slot}/bin/pg_config" ]] && \
			found_slots+=( ${slot} )
	done

	echo ${found_slots[@]}
}

### List Action ###
describe_list() {
	echo "List available PostgreSQL slots."
}

do_list() {
	if $(is_output_mode brief) ; then
		echo $(get_slots)
	else
		write_list_start "Available PostgreSQL Slots"

		local provider
		local slot
		local bindir
		for slot in $(get_slots) ; do
			bindir="${USR_PATH}/$(lib_dir)/postgresql-${slot}/bin"

			# The output of `pg_config --version` also includes "PostgreSQL" in
			# the string, which is a bit redundant.
			provider=$("${bindir}"/pg_config --version | \
							  sed 's/[^0-9]*\(.*\)/\1/')

			# Unless a file exists that's controlled by the 'server' use flag,
			# report that it's client only.
			[[ -e "${bindir}/postmaster" ]] || provider+=' (Clients Only)'

			case "${slot}" in
				"$(active_slot)" )
					write_kv_list_entry \
						"$(highlight_marker ${slot})" "${provider}";;
				* )
					write_kv_list_entry "${slot}" "${provider}";;
			esac
		done

		[[ -z "$(get_slots)" ]] && write_warning_msg "No slots available."
	fi
}

### Show Action ###
describe_show() {
	echo "Show which slot is currently active."
}

do_show() {
	echo $(active_slot)
}

### Show Service Action ###
# Here for backwards compatibility with ebuilds
describe_show-service()  {
	echo "Deprecated. For ebuild use; returns no useful information."
}

do_show-service() {
	echo 1
}

### Set Action ###
describe_set() {
	echo "Create symbolic links for PostgreSQL libraries and applications."
}

do_set() {
	local SLOT=$1

	if [[ ! -d ${USR_PATH}/$(lib_dir)/postgresql-${SLOT} ]] ; then
		die -q "Not a valid slot."
	fi

	# If there's an active slot, unset that one first
	if [[  "$(active_slot)" == "${SLOT}" ]] ; then
		echo $(highlight "No work to do.")
		echo "If you think the links need to be reset, use the reset action."
		return 0
	elif [[ "$(active_slot)" != "(none)" ]] ; then
		echo -ne "\tRemoving old links..."
		do_unset $(active_slot)
		echo "done."
	fi

	echo "Setting ${SLOT} as the default installation..."

	echo -ne "\tGenerating new links..."
	# Sources for header files
	# Targets are listed in the global variable INCLUDE_TARGETS.
	#
	# If you change this list, you must change the INCLUDE_TARGETS list,
	# too. And, they must be listed in the same order.
	local include_sources=(
		"${USR_PATH}"/include/postgresql-${SLOT}
		"${USR_PATH}"/include/postgresql-${SLOT}/libpq-fe.h
		"${USR_PATH}"/include/postgresql-${SLOT}/pg_config_ext.h
		"${USR_PATH}"/include/postgresql-${SLOT}/pg_config_manual.h
		"${USR_PATH}"/include/postgresql-${SLOT}/libpq
		"${USR_PATH}"/include/postgresql-${SLOT}/postgres_ext.h
	)

	# The linker function cannot accommodate this special purpose.
	local rel_source
	local i
	for (( i=0; $i < ${#include_sources[@]}; i++ )) ; do
		# Some headers are present only in specific versions of PostgreSQL
		[[ -e ${include_sources[$i]} ]] || continue

		# Create relative links so that they work both here and inside the new
		# root if $ROOT is not "/"
		rel_source=$(relative_name "${include_sources[$i]}" "$(dirname "${INCLUDE_TARGETS[$i]}")")

		ln -s "$rel_source" "${INCLUDE_TARGETS[$i]}" || \
			die -q "Unable to create link!"
	done

	# Link modules to /usr/lib{,32,64}/
	local x
	for x in $(list_libdirs) ; do
		if [[ -d ${USR_PATH}/${x}/postgresql-${SLOT}/${x} ]] ; then
			# 'linker' function doesn't work for linking directories.
			# Default lib path - create a relative link
			ln -s "postgresql-${SLOT}/${x}" "${USR_PATH}/${x}/postgresql"

			# Linker works for files
			linker "${USR_PATH}/${x}/postgresql-${SLOT}/${x}/" \
				"-name lib*" "${USR_PATH}/${x}"
		fi
	done

	# Link binaries to /usr/bin/
	linker "${USR_PATH}/$(lib_dir)/postgresql-${SLOT}/bin/" \
		"-type f" "${USR_PATH}/bin"

	# Default share path - use a relative link here by just specifying the
	# base name
	ln -s "postgresql-${SLOT}" "${USR_PATH}/share/postgresql"

	echo "done."
	echo "Setting ${SLOT} as default was successful!"
}

### Unset Action ###
describe_unset() {
	echo "Remove symbolic links."
}

# Undo everything done by do_set().
do_unset() {
	local SLOT=$1
	if [[ ${SLOT} != $(active_slot) ]] ; then
		echo "Slot already inactive; no work to do."
		return 0
	fi

	local l
	for l in ${INCLUDE_TARGETS[@]} "${USR_PATH}/share/postgresql" ; do
		[[ -h ${l} ]] && rm -f "${l}"

		# Check if include target still exists
		[[ -e ${l} ]] && \
			die -q "The target '${l}' exists and could not be removed!"
	done

	for l in $(find "${USR_PATH}/bin" -type l) ; do
		if [[ $(realpath ${l} | grep -c postgresql) -ge 1 ]] ; then
			rm "${l}"
		fi
	done

	for x in $(list_libdirs) ; do
		if [[ -h "${USR_PATH}/${x}/postgresql" ]] ; then
			for l in $(find "${USR_PATH}/${x}" -type l) ; do
				if [[ $(realpath ${l} | grep -c postgresql) -ge 1 ]] ; then
					rm "${l}"
				fi
			done
		fi
	done
}

### Reset Action ###
describe_reset() {
	echo "Recreate symbolic links for currently active slot."
}

do_reset() {
	local SLOT=$(active_slot)
	[[ ${SLOT} = "(none)" ]] && die -q "No active slot to reset."
	do_unset ${SLOT}
	do_set ${SLOT}
}

### Update Action ###
describe_update() {
	echo "Refreshes all symbolic links managed by this module"
}

do_update() {
	local slot=$(active_slot)

	## CLEAN UP ##
	#
	# Older versions of this module generated state and environment files of
	# some sort or another. They're useless now and are just a waste of space.
	# Remove environment files that have been generated by the ebuilds
	rm -f "${ENV_FILE}"-*

	if [[ -d ${ETC_PATH} ]] ; then
		# Remove some files outright as they're entirely useless now.
		#   ${ETC_PATH}/active: Contents was the active slot (e.g., 9.5)
		#   ${ETC_PATH}/service: Told the initscript which slot to start
		local f
		for f in "${ETC_PATH}/active" "${ETC_PATH}/service" ; do
			[[ -e "${f}" ]] && rm -f "${f}"
		done

		local active_link_file
		for active_link_file in "${ETC_PATH}"/active.links* ; do
			local active_links=($(<"${active_link_file}"))
			for (( i=0; $i < ${#active_links[@]}; i++ )) ; do
				[[ -h "${ROOT%/}/${active_links[$i]}" ]] && \
					rm -f "${ROOT%/}/${active_links[$i]}"
				[[ -e "${ROOT%/}/${active_links[$i]}" ]] && \
					die -q "The target '${active_links[$i]}' still exists and could not be removed!"
			done

			rm "${active_link_file}" || \
				write_warning_msg "Can't remove '${active_link_file}'"
		done

		local unused_files
		unused_file=$(find "${ETC_PATH}" -type f -not -name '.keep*')
		if [[ -n "${unused_file[@]}" ]] ; then
			write_warning_msg "You have unused files that should be removed:"
			for f in ${unused_file[@]} ; do
				write_warning_msg $f
			done
		else
			echo "It's should be safe for you to remove '${ETC_PATH}'"
		fi
	fi

	## End Clean Up

	local slots=($(get_slots))
	local index=${#slots[@]}

	if [[ ${index} -eq 0 ]] ; then
		write_warning_msg "No slots found!"
		rm -f "${ENV_FILE}"
		do_action env update &> /dev/null
		return 0
	fi

	# Reset, otherwise set the highest slot available.
	if [[ ${slots[@]} =~ ${slot} ]] ; then
		do_reset ${slot}
	else
		# best_version doesn't work here as pkg_postrm runs before the world
		# file is updated, thereby returning a false positive.
		do_set ${slots[$index-1]}
	fi

	# Update paths to libs and man pages
	local ldpath
	local pcpath
	local x
	for x in $(list_libdirs) ; do
		if [[ -h ${USR_PATH}/${x}/postgresql ]] ; then
			ldpath+="${USR_PATH}/${x}/postgresql:"
			pcpath+="${USR_PATH}/${x}/postgresql/pkgconfig:"
		fi
	done
	ldpath="${ldpath%:}"
	pcdpath="${pcpath%:}"
	local manpath="${USR_PATH}/share/postgresql/man/"
	while [[ $[--index] -gt -1 ]] ; do
		local curslot="${slots[$index]}"
		for x in $(list_libdirs) ; do
			local lib_path="${USR_PATH}/${x}/postgresql-${curslot}/${x}/"
			local pkg_path="${lib_path}/pkgconfig"
			[[ -d ${lib_path} ]] && ldpath+=":${lib_path}"
			[[ -d ${pkg_path} ]] && pcpath+=":${pcpath}"
		done
		local tmp_manpath="${USR_PATH}/share/postgresql-${curslot}/man/"
		[[ -d ${tmp_manpath} ]] && manpath+=":${tmp_manpath}"
		echo "done."
	done

	store_config "${ENV_FILE}" LDPATH "${ldpath}"
	store_config "${ENV_FILE}" MANPATH "${manpath}"
	store_config "${ENV_FILE}" PKG_CONFIG_PATH "${pcpath}"
	do_action env update &> /dev/null
}