summaryrefslogtreecommitdiff
blob: 05ba7fb6df7f411a9b6d3dca1114c2f1da5c7037 (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
# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: $

inherit path-manipulation output

DESCRIPTION="Manage postgresql slots"
MAINTAINER="root@localhost"
SVN_DATE='$Date: $'
VERSION=$(svn_date_to_version "${SVN_DATE}" )

### list action

## {{{ list stuff
	describe_list() {
		echo "List available postgresql installations"
	}

	do_list() {
		write_list_start "Available postgresql installations"
		for s in /etc/eselect/postgresql/slots/* ; do
			[[ -d "$s" ]] || continue;
			local slot=$(basename $s)
			local postgres_ebuilds=""
			for src in ${s}/* ; do
				source "$src"
			done
			local kw="${slot}"
			if [[ "$(canonicalise /etc/eselect/postgresql/active)" = "$(canonicalise ${s})" ]] ; then
				kw="$kw <-*"
			fi
			if [[ "$(canonicalise /etc/eselect/postgresql/service)" = "$(canonicalise ${s})" ]] ; then
				kw="$kw <-S"
			fi
			write_kv_list_entry "${kw}" "${postgres_ebuilds//postgresql-/}"
		done
	}
## }}}

### show action

## {{{
	describe_show() {
		echo "Show currently selected PostgreSQL installation"
	}
	do_show() {
		if [[ -r "/etc/eselect/postgresql/active" ]] ; then
			local bn=$(basename $(canonicalise /etc/eselect/postgresql/active))
			local slot=${bn}
			echo "${slot}"
		else
			echo "(none)"
		fi
	}
## }}}

### show-all action

## {{{
	describe_show-all() {
		echo "Show currently selected PostgreSQL service"
	}
	do_show-all() {
		echo "Utilities:  $(do_show)"
		echo "Service:    $(do_show-service)"
	}
## }}}

# show-service action

## {{{
	describe_show-service() {
		echo "Show currently selected postgresql server"
	}
	do_show-service() {
		if [[ -r "/etc/eselect/postgresql/service" ]] ; then
			local bn=$(basename $(canonicalise /etc/eselect/postgresql/service))
			local slot=${bn}
			echo "${slot}"
		else
			echo "(none)"
		fi
	}
## }}}

### set action

## {{{
	describe_set() {
		echo "Set active postgresql installation"
	}

	do_set() {
		local alibdir='/usr/lib/'
		[[ ${#@} -ne 1 ]] && die -q "Only one parameter is expected"
		local slotdir="/etc/eselect/postgresql/slots/${1}"
		[[ -d "${slotdir}" ]] || die -q "No such version installed"
		do_reset
		unset postgres_symlinks
		unset postgres_bindir
		for src in ${slotdir}/* ; do
			[[ -f "${src}" ]] && source ${src}
		done
		local pgconfig="${postgres_bindir}/pg_config"
		[[ -x "${pgconfig}" ]] || die -q "Broken install: no pg_config found"
		local pglibdir="$(${pgconfig} --libdir)"
		[[ -d "${pglibdir}" ]] || die -q "Failed to find postgresql libdir"
		local slinks="${#postgres_symlinks[*]}"
		for l in ${pglibdir}/lib*.{a,so} ; do
			local bn=$(basename $l)
			postgres_symlinks[slinks++]="$l"
			postgres_symlinks[slinks++]="${alibdir}${bn}"
		done
		for (( i=0;i<slinks;i++ )); do
			local s=${postgres_symlinks[i++]};
			local d=${postgres_symlinks[i]};
			if ! [[ -e "${s}" ]] ; then
				write_warning_msg "No file '${s}' to symlink"
				continue
			fi
			if [[ -e "$d" ]] ; then
				[[ -L "$d" ]] || die -q "$d exists and is not a symbolic link"
			fi
		done
		mkdir -p /etc/eselect/postgresql &>/dev/null || die "Failed to create /etc/eselect/postgresql"
		exec 9>/etc/eselect/postgresql/active.links || die "Failed to store list of links"
		ln -sf slots/${1} /etc/eselect/postgresql/active
		for (( i=0;i<slinks;i++ )); do
			local s=${postgres_symlinks[i]};
			(( i++ ));
			local d=${postgres_symlinks[i]};
			rm -f "${d}"
			ln -s "${s}" "${d}"
			echo "${d}" >&9
		done
	}
## }}}

### set-service action

## {{{
	describe_set-service() {
		echo "Set active postgresql service"
	}

	do_set-service() {
		local alibdir='/usr/lib/'
		[[ ${#@} -ne 1 ]] && die -q "Only one parameter is expected"
		local slotdir="/etc/eselect/postgresql/slots/${1}"
		[[ -d "${slotdir}" ]] || die -q "No such version installed"
		do_reset-service
		unset postgres_service
		local p_service="$(for s in ${slotdir}/* ; do source $s ; done ; echo $postgres_service)"
		[[ -x "/etc/init.d/${p_service}" ]] || die -1 "Selected installation doesn't provide PostgreSQL service"
		mkdir -p /etc/eselect/postgresql &>/dev/null || die "Failed to create /etc/eselect/postgresql"
		rm -f /etc/eselect/postgresql/service
		ln -sf slots/${1} /etc/eselect/postgresql/service
		touch /etc/conf.d/${p_service}
	}
## }}}

### set-all action

## {{{
	describe_set-all() {
		echo "Set both active postgresql and corresponding service"
	}

	do_set-all() {
		do_set "$@"
		do_set-service "$@"
	}
## }}}


### reset action

## {{{
	describe_reset() {
		echo "Remove active postgresql installation links"
	}

	do_reset() {
		local alibdir='/usr/lib/'
		if [[ -d "$(canonicalise /etc/eselect/postgresql/active)" ]] ; then
			if [[ -r /etc/eselect/postgresql/active.links ]] ; then
				exec 9</etc/eselect/postgresql/active.links || die "Failed to access the list of links"
				while IFS='' read -u 9 slink ; do
					if [[ -e "$slink" ]] ; then
						if [[ -L "$slink" ]] ; then
							rm -f "${slink}"
						else
							write_warning_msg "$slink exists and is not a symbolic link"
						fi
					fi
				done
				rm -f /etc/eselect/postgresql/active.links
			fi
			rm -f /etc/eselect/postgresql/active
		fi
	}
## }}}

### reset-service action

## {{{
	describe_reset-service() {
		echo "Reset active postgresql service"
	}

	do_reset-service() {
		if [[ -L /etc/eselect/postgresql/service ]] ; then
			local p_service="$(for s in /etc/eselect/postgresql/service/* ; do source $s ; done ; echo "$postgres_service")"
			[[ -e "/etc/conf.d/${p_service}" ]] && touch "/etc/conf.d/${p_service}"
			rm -f /etc/eselect/postgresql/service
		fi
	}
## }}}

### reset-all action

## {{{
	describe_reset-all() {
		echo "Reset both selected PostgreSQL and PostgreSQL serice"
	}

	do_reset-all() {
		do_reset
		do_reset-service
	}
## }}}

## {{{
	describe_update() {
		echo "Update symbolic links"
	}

	do_update() {
		# collect all binaries names
		local binaries="$((
			for s in /etc/eselect/postgresql/slots/* ; do
				[[ -d "$s" ]] || continue;
				local slot=$(basename $s)
				for src in ${s}/* ; do
					source "$src"
				done
				ls -1 ${postgres_bindir}/
			done
		) | sort | uniq)"
		local links="$(find /usr/bin/* -type d -prune -o -lname /usr/lib/eselect-postgresql/binwrapper -print)"
		local i
		for i in ${binaries} ; do
			local lip="/usr/bin/${i}"
			links="$(echo "${links}"|grep -v "^${lip}\$")"
			[[ -L "${lip}" ]] && continue
			[[ -e "${lip}" ]] && die "${lip} exists and is not a symbolic link"
			echo "Creating ${lip} symlink..."
			ln -s /usr/lib/eselect-postgresql/binwrapper "${lip}"
		done
		# cleanup leftovers
		for i in ${links} ; do
			echo "Removing ${i} symlink.."
			rm "${i}"
		done
		if test -L /etc/eselect/postgresql/active -a ! -d /etc/eselect/postgresql/active ; then
			echo "Resetting uninstalled selection"
			do_reset
		fi
		if test -L /etc/eselect/postgresql/service -a ! -d /etc/eselect/postgresql/service ; then
			echo "Resetting uninstalled selection"
			do_reset-service
		fi
	}
## }}}

# vim: ts=4 sw=4 noet fdm=marker