summaryrefslogtreecommitdiff
blob: 5a39a036765757c7eccc5a2de1fa645ef0878db8 (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# Copyright 1999-2004 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/webapp.eclass,v 1.21 2004/05/25 10:22:02 stuart Exp $
#
# eclass/webapp.eclass
#				Eclass for installing applications to run under a web server
#
#				Part of the implementation of GLEP #11
#
# Author(s)		Stuart Herbert <stuart@gentoo.org>
#
# ------------------------------------------------------------------------
#
# The master copy of this eclass is held in Stu's subversion repository.
#
# If you make changes to this file and don't tell Stu, chances are that
# your changes will be overwritten the next time Stu releases a new version
# of webapp-config.
#
# ------------------------------------------------------------------------

ECLASS=webapp
INHERITED="$INHERITED $ECLASS"
SLOT="${PVR}"
IUSE="$IUSE vhosts"
DEPEND="$DEPEND >=net-www/webapp-config-1.7 app-portage/gentoolkit"

EXPORT_FUNCTIONS pkg_postinst pkg_setup src_install pkg_prerm

INSTALL_DIR="/$PN"
IS_UPGRADE=0
IS_REPLACE=0

INSTALL_CHECK_FILE="installed_by_webapp_eclass"

ETC_CONFIG="/etc/vhosts/webapp-config"

# ------------------------------------------------------------------------
# INTERNAL FUNCTION - USED BY THIS ECLASS ONLY
#
# Check whether a specified file exists within the image/ directory
# or not.
#
# @param 	$1 - file to look for
# @param	$2 - prefix directory to use
# @return	0 on success, never returns on an error
# ------------------------------------------------------------------------

function webapp_checkfileexists ()
{
	local my_prefix

	[ -n "$2" ] && my_prefix="$2/" || my_prefix=

	if [ ! -e ${my_prefix}$1 ]; then
		msg="ebuild fault: file $1 not found"
		eerror "$msg"
		eerror "Please report this as a bug at http://bugs.gentoo.org/"
		die "$msg"
	fi
}

# ------------------------------------------------------------------------
# INTERNAL FUNCTION - USED BY THIS ECLASS ONLY
# ------------------------------------------------------------------------

function webapp_check_installedat
{
	local my_output

	/usr/sbin/webapp-config --show-installed -h localhost -d $INSTALL_DIR 2> /dev/null
}

# ------------------------------------------------------------------------
# INTERNAL FUNCTION - USED BY THIS ECLASS ONLY
# ------------------------------------------------------------------------

function webapp_import_config ()
{
	if [ -z "${MY_HTDOCSDIR}" ]; then
		. /etc/conf.d/webapp-config
	fi

	if [ -z "${MY_HTDOCSDIR}" ]; then
		libsh_edie "/etc/conf.d/webapp-config not imported"
	fi
}

# ------------------------------------------------------------------------
# INTERNAL FUNCTION - USED BY THIS ECLASS ONLY
#
# ------------------------------------------------------------------------

function webapp_strip_appdir ()
{
	local my_stripped="$1"
	echo "$1" | sed -e "s|${MY_APPDIR}/||g;"
}

function webapp_strip_d ()
{
	echo "$1" | sed -e "s|${D}||g;"
}

function webapp_strip_cwd ()
{
	local my_stripped="$1"
	echo "$1" | sed -e 's|/./|/|g;'
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# Identify a config file for a web-based application.
#
# @param	$1 - config file
# ------------------------------------------------------------------------

function webapp_configfile ()
{
	webapp_checkfileexists "$1" "$D"
	echo $1
	local MY_FILE="`webapp_strip_appdir $1`"
	echo $MY_FILE

	einfo "(config) $MY_FILE"
	echo "$MY_FILE" >> ${D}${WA_CONFIGLIST}
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# Install a script that will run after a virtual copy is created, and
# before a virtual copy has been removed
#
# @param	$1 - the script to run
# ------------------------------------------------------------------------

function webapp_hook_script ()
{
	webapp_checkfileexists "$2"

	einfo "(hook) $1"
	cp "$1" "${D}${MY_HOOKSCRIPTSDIR}/`basename $1`" || die "Unable to install $1 into ${D}${MY_HOOKSCRIPTSDIR}/"
	chmod 555 "${D}${MY_HOOKSCRIPTSDIR}/`basename $1`"
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# Install a text file containing post-installation instructions.
#
# @param	$1 - language code (use 'en' for now)
# @param	$2 - the file to install
# ------------------------------------------------------------------------

function webapp_postinst_txt
{
	webapp_checkfileexists "$2"

	einfo "(rtfm) $2 (lang: $1)"
	cp "$2" "${D}${MY_APPDIR}/postinst-$1.txt"
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# Identify a script file (usually, but not always PHP or Perl) which is
#
# Files in this list may be modified to #! the required CGI engine when
# installed by webapp-config tool in the future.
#
# @param	$1 - the cgi engine to use
# @param	$2 - the script file that could run under a cgi-bin
#
# ------------------------------------------------------------------------

function webapp_runbycgibin ()
{
	webapp_checkfileexists "$2" "$D"
	local MY_FILE="`webapp_strip_appdir $2`"
	MY_FILE="`webapp_strip_cwd $MY_FILE`"

	einfo "(cgi-bin) $1 - $MY_FILE"
	echo "$1 $MY_FILE" >> ${D}${WA_RUNBYCGIBINLIST}
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# Identify a file which must be owned by the webserver's user:group
# settings.
#
# The ownership of the file is NOT set until the application is installed
# using the webapp-config tool.
# 
# @param	$1 - file to be owned by the webserver user:group combo
#
# ------------------------------------------------------------------------

function webapp_serverowned ()
{
	webapp_checkfileexists "$1" "$D"
	local MY_FILE="`webapp_strip_appdir $1`" 
	
	einfo "(server owned) $MY_FILE"
	echo "$MY_FILE" >> ${D}${WA_SOLIST}
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# @param	$1 - the webserver to install the config file for
#			     (one of apache1, apache2, cherokee)
# @param	$2 - the config file to install
# @param	$3 - new name for the config file (default is `basename $2`)
#				 this is an optional parameter
#
# NOTE:
#	this function will automagically prepend $1 to the front of your
#	config file's name
# ------------------------------------------------------------------------

function webapp_server_config ()
{
	webapp_checkfileexists "$2"

	# sort out what the name will be of the config file

	local my_file

	if [ -z "$3" ]; then
		$my_file="$1-`basename $2`"
	else
		$my_file="$1-$3"
	fi

	# warning:
	#
	# do NOT change the naming convention used here without changing all
	# the other scripts that also rely upon these names
 
	einfo "($1) config file '$my_file'"
	cp "$2" "${D}${MY_SERVERCONFIGDIR}/${my_file}"
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - FOR USE IN EBUILDS
#
# @param	$1 - the db engine that the script is for
#				 (one of: mysql|postgres)
# @param	$2 - the sql script to be installed
# @param	$3 - the older version of the app that this db script
#				 will upgrade from
#				 (do not pass this option if your SQL script only creates
#				  a new db from scratch)
# ------------------------------------------------------------------------

function webapp_sqlscript ()
{
	webapp_checkfileexists "$2"

	# create the directory where this script will go
	#
	# scripts for specific database engines go into their own subdirectory
	# just to keep things readable on the filesystem

	if [ ! -d "${D}${MY_SQLSCRIPTSDIR}/$1" ]; then
		mkdir -p "${D}${MY_SQLSCRIPTSDIR}/$1" || libsh_die "unable to create directory ${D}${MY_SQLSCRIPTSDIR}/$1"
	fi

	# warning:
	#
	# do NOT change the naming convention used here without changing all
	# the other scripts that also rely upon these names
 
	# are we dealing with an 'upgrade'-type script?
	if [ -n "$3" ]; then
		# yes we are
		einfo "($1) upgrade script from ${PN}-${PVR} to $3"
		cp $2 ${D}${MY_SQLSCRIPTSDIR}/$1/${3}_to_${PVR}.sql
	else
		# no, we are not
		einfo "($1) create script for ${PN}-${PVR}"
		cp $2 ${D}${MY_SQLSCRIPTSDIR}/$1/${PVR}_create.sql
	fi
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - call from inside your ebuild's src_install AFTER
# everything else has run
#
# For now, we just make sure that root owns everything, and that there
# are no setuid files.  I'm sure this will change significantly before
# the final version!
# ------------------------------------------------------------------------

function webapp_src_install ()
{
	chown -R ${VHOST_DEFAULT_UID}:${VHOST_DEFAULT_GID} ${D}/
	chmod -R u-s ${D}/
	chmod -R g-s ${D}/

	keepdir ${MY_PERSISTDIR}
	fowners root:root ${MY_PERSISTDIR}
	fperms 755 ${MY_PERSISTDIR}

	# to test whether or not the ebuild has correctly called this function
	# we add an empty file to the filesystem
	#
	# we used to just set a variable in the shell script, but we can
	# no longer rely on Portage calling both webapp_src_install() and
	# webapp_pkg_postinst() within the same shell process

	touch ${D}/${MY_APPDIR}/${INSTALL_CHECK_FILE}
}

# ------------------------------------------------------------------------
# EXPORTED FUNCTION - call from inside your ebuild's pkg_config AFTER
# everything else has run
#
# If 'vhosts' USE flag is not set, auto-install this app
#
# ------------------------------------------------------------------------

function webapp_pkg_setup ()
{
	# add sanity checks here

	if [ "$SLOT+" != "${PVR}+" ]; then
		die "ebuild sets SLOT, overrides webapp.eclass"
	fi

	# pull in the shared configuration file

	G_HOSTNAME="localhost"
	. ${ETC_CONFIG} || die "Unable to open /etc/vhosts/webapp-config file"

	# are we installing a webapp-config solution over the top of a 
	# non-webapp-config solution?

	if ! use vhosts ; then
		local my_dir="$VHOST_ROOT/$MY_HTDOCSBASE/$PN"
		local my_output

		if [ -d "$my_dir" ] ; then
			einfo "You already have something installed in $my_dir"
			einfo "Are you trying to install over the top of something I cannot upgrade?"

			my_output="`webapp_check_installedat`"

			if [ "$?" != "0" ]; then

				# okay, whatever is there, it isn't webapp-config-compatible
				ewarn
				ewarn "Whatever is in $my_dir, it's not"
				ewarn "compatible with webapp-config."
				ewarn
				ewarn "This ebuild may be overwriting important files."
				ewarn
			elif [ "`echo $my_output | awk '{ print $1 }'`" != "$PN" ]; then
				eerror "$my_dir contains $my_output"
				eerror "I cannot upgrade that"
				die "Cannot upgrade contents of $my_dir"
			else
				einfo
				einfo "I can upgrade the contents of $my_dir"
				einfo
			fi
		fi
	fi
}

function webapp_someunusedfunction ()
{
	# are we emerging something that is already installed?

	if [ -d "${D}${MY_APPROOT}/${MY_APPSUFFIX}" ]; then
		# yes we are
		ewarn "Removing existing copy of ${PN}-${PVR}"
		rm -rf "${D}${MY_APPROOT}/${MY_APPSUFFIX}"
	fi
}

function webapp_getinstalltype ()
{
	# or are we upgrading?

	if ! use vhosts ; then
		# we only run webapp-config if vhosts USE flag is not set

		local my_output

		my_output="`webapp_check_installedat`"

		if [ "$?" = "0" ] ; then
			# something is already installed there
			#
			# make sure it isn't the same version

			local my_pn="`echo $my_output | awk '{ print $1 }'`"
			local my_pvr="`echo $my_output | awk '{ print $2 }'`"

			REMOVE_PKG="${my_pn}-${my_pvr}"

			if [ "$my_pn" == "$PN" ]; then
				if [ "$my_pvr" != "$PVR" ]; then
					einfo "This is an upgrade"
					IS_UPGRADE=1
				else
					einfo "This is a re-installation"
					IS_REPLACE=1
				fi
			else
				einfo "$my_ouptut is installed there"
			fi
		else
			einfo "This is an installation"
		fi
	fi
}

function webapp_src_preinst ()
{
	# create the directories that we need

	dodir ${MY_HTDOCSDIR}
	dodir ${MY_HOSTROOTDIR}
	dodir ${MY_CGIBINDIR}
	dodir ${MY_ICONSDIR}
	dodir ${MY_ERRORSDIR}
	dodir ${MY_SQLSCRIPTSDIR}
}

function webapp_pkg_postinst ()
{
	. ${ETC_CONFIG}

	# sanity checks, to catch bugs in the ebuild

	if [ ! -f ${MY_APPDIR}/${INSTALL_CHECK_FILE} ]; then
		eerror
		eerror "This ebuild did not call webapp_src_install() at the end"
		eerror "of the src_install() function"
		eerror
		eerror "Please log a bug on http://bugs.gentoo.org"
		eerror
		eerror "You should use emerge -C to remove this package, as the"
		eerror "installation is incomplete"
		eerror
		die "Ebuild did not call webapp_src_install() - report to http://bugs.gentoo.org"
	fi

	# if 'vhosts' is not set in your USE flags, we install a copy of
	# this application in /var/www/localhost/htdocs/${PN}/ for you
	
	if ! use vhosts ; then
		echo
		einfo "vhosts USE flag not set - auto-installing using webapp-config"

		webapp_getinstalltype

		G_HOSTNAME="localhost"
		. /etc/vhosts/webapp-config

		local my_mode=-I

		if [ "$IS_REPLACE" = "1" ]; then
			einfo "${PN}-${PVR} is already installed - replacing"
			/usr/sbin/webapp-config -C -d "$INSTALL_DIR"
		elif [ "$IS_UPGRADE" = "1" ]; then
			einfo "$REMOVE_PKG is already installed - upgrading"
			my_mode=-U
		else
			einfo "${PN}-${PVR} is not installed - using install mode"
		fi
	
		my_cmd="/usr/sbin/webapp-config $my_mode -h localhost -u root -d $INSTALL_DIR ${PN} ${PVR}"
		einfo "Running $my_cmd"
		$my_cmd

		# remove the old version
		#
		# why do we do this?  well ...
		#
		# normally, emerge -u installs a new version and then removes the
		# old version.  however, if the new version goes into a different
		# slot to the old version, then the old version gets left behind
		#
		# if USE=-vhosts, then we want to remove the old version, because
		# the user is relying on portage to do the magical thing for it

		if [ "$IS_UPGRADE" = "1" ] ; then
			einfo "Removing old version $REMOVE_PKG"

			emerge -C $CATEGORY/$REMOVE_PKG
		fi
	else
		# vhosts flag is on
		#
		# let's tell the administrator what to do next

		einfo
		einfo "The 'vhosts' USE flag is switched ON"
		einfo "This means that Portage will not automatically run webapp-config to"
		einfo "complete the installation."
		einfo
		einfo "To install $PN-$PVR into a virtual host, run the following command:"
		einfo
		einfo "    webapp-config -I -h <host> -d $PN $PN $PVR"
		einfo
		einfo "For more details, see the webapp-config(8) man page"
	fi

	return 0
}

function webapp_pkg_prerm ()
{
	# remove any virtual installs that there are

	local my_output
	local x

	my_output="`webapp-config --list-installs $PN $PVR`"

	if [ "$?" != "0" ]; then
		return
	fi

	# the changes to IFS here are necessary to ensure that we can cope
	# with directories that contain spaces in the file names

	# OLD_IFS="$IFS"
	# IFS="
"

	for x in $my_output ; do
		# IFS="$OLD_IFS"

		[ -f $x/.webapp ] && . $x/.webapp || ewarn "Cannot find file $x/.webapp"

		if [ -z "WEB_HOSTNAME" -o -z "WEB_INSTALLDIR" ]; then
			ewarn "Don't forget to use webapp-config to remove the copy of"
			ewarn "${PN}-${PVR} installed in"
			ewarn
			ewarn "    $x"
			ewarn
		else
			# we have enough information to remove the virtual copy ourself

			webapp-config -C -h ${WEB_HOSTNAME} -d ${WEB_INSTALLDIR}

			# if the removal fails - we carry on anyway!
		fi
		# IFS="
"
	done

	# IFS="$OLD_IFS"
}