1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: webapp.eclass
7 # @BLURB: functions for installing applications to run under a web server
9 # The webapp eclass contains functions to handle web applications with
10 # webapp-config. Part of the implementation of GLEP #11
12 # @ECLASS-VARIABLE: WEBAPP_DEPEND
14 # An ebuild should use WEBAPP_DEPEND if a custom DEPEND needs to be built, most
15 # notably in combination with WEBAPP_OPTIONAL.
16 WEBAPP_DEPEND=">=app-admin/webapp-config-1.50.15"
18 # @ECLASS-VARIABLE: WEBAPP_NO_AUTO_INSTALL
20 # An ebuild sets this to `yes' if an automatic installation and/or upgrade is
21 # not possible. The ebuild should overwrite pkg_postinst() and explain the
22 # reason for this BEFORE calling webapp_pkg_postinst().
24 # @ECLASS-VARIABLE: WEBAPP_OPTIONAL
26 # An ebuild sets this to `yes' to make webapp support optional, in which case
27 # you also need to take care of USE-flags and dependencies.
29 if [[ "${WEBAPP_OPTIONAL}" != "yes" ]]; then
30 [[ "${WEBAPP_NO_AUTO_INSTALL}" == "yes" ]] || IUSE="vhosts"
32 DEPEND="${WEBAPP_DEPEND}"
36 EXPORT_FUNCTIONS pkg_postinst pkg_setup src_install pkg_prerm
42 INSTALL_CHECK_FILE="installed_by_webapp_eclass"
43 SETUP_CHECK_FILE="setup_by_webapp_eclass"
45 ETC_CONFIG="${ROOT}etc/vhosts/webapp-config"
46 WEBAPP_CONFIG="${ROOT}usr/sbin/webapp-config"
47 WEBAPP_CLEANER="${ROOT}usr/sbin/webapp-cleaner"
49 # ==============================================================================
51 # ==============================================================================
53 # Load the config file /etc/vhosts/webapp-config
54 # Supports both the old bash version, and the new python version
55 webapp_read_config() {
56 debug-print-function $FUNCNAME $*
58 if has_version '>=app-admin/webapp-config-1.50'; then
59 ENVVAR=$(${WEBAPP_CONFIG} --query ${PN} ${PVR}) || die "Could not read settings from webapp-config!"
61 elif [[ "${WEBAPP_OPTIONAL}" != "yes" ]]; then
62 # ETC_CONFIG might not be available
63 . ${ETC_CONFIG} || die "Unable to read ${ETC_CONFIG}"
64 elif [[ -f "${ETC_CONFIG}" ]]; then
65 # WEBAPP_OPTIONAL is set to yes
66 # and this must run only if ETC_CONFIG actually exists
67 . ${ETC_CONFIG} || die "Unable to read ${ETC_CONFIG}"
71 # Check whether a specified file exists in the given directory (`.' by default)
72 webapp_checkfileexists() {
73 debug-print-function $FUNCNAME $*
75 local my_prefix=${2:+${2}/}
77 if [[ ! -e "${my_prefix}${1}" ]]; then
78 msg="ebuild fault: file '${1}' not found"
80 eerror "Please report this as a bug at https://bugs.gentoo.org/"
85 webapp_check_installedat() {
86 debug-print-function $FUNCNAME $*
87 ${WEBAPP_CONFIG} --show-installed -h localhost -d "${INSTALL_DIR}" 2> /dev/null
90 webapp_strip_appdir() {
91 debug-print-function $FUNCNAME $*
92 echo "${1#${MY_APPDIR}/}"
96 debug-print-function $FUNCNAME $*
101 debug-print-function $FUNCNAME $*
105 webapp_getinstalltype() {
106 debug-print-function $FUNCNAME $*
108 if ! has vhosts ${IUSE} || use vhosts; then
113 my_output="$(webapp_check_installedat)"
115 if [[ $? -eq 0 ]]; then
116 # something is already installed there
117 # make sure it isn't the same version
119 local my_pn="$(echo ${my_output} | awk '{ print $1 }')"
120 local my_pvr="$(echo ${my_output} | awk '{ print $2 }')"
122 REMOVE_PKG="${my_pn}-${my_pvr}"
124 if [[ "${my_pn}" == "${PN}" ]]; then
125 if [[ "${my_pvr}" != "${PVR}" ]]; then
126 elog "This is an upgrade"
128 # for binpkgs, reset status, var declared in global scope
131 elog "This is a re-installation"
133 # for binpkgs, reset status, var declared in global scope
137 elog "${my_output} is installed there"
140 # for binpkgs, reset status, var declared in global scope
143 elog "This is an installation"
147 # ==============================================================================
149 # ==============================================================================
151 # @FUNCTION: need_httpd
153 # Call this function AFTER your ebuilds DEPEND line if any of the available
154 # webservers are able to run this application.
157 || ( virtual/httpd-basic virtual/httpd-cgi virtual/httpd-fastcgi )"
160 # @FUNCTION: need_httpd_cgi
162 # Call this function AFTER your ebuilds DEPEND line if any of the available
163 # CGI-capable webservers are able to run this application.
166 || ( virtual/httpd-cgi virtual/httpd-fastcgi )"
169 # @FUNCTION: need_httpd_fastcgi
171 # Call this function AFTER your ebuilds DEPEND line if any of the available
172 # FastCGI-capabale webservers are able to run this application.
173 need_httpd_fastcgi() {
175 virtual/httpd-fastcgi"
178 # @FUNCTION: webapp_configfile
179 # @USAGE: <file> [more files ...]
181 # Mark a file config-protected for a web-based application.
182 webapp_configfile() {
183 debug-print-function $FUNCNAME $*
187 webapp_checkfileexists "${m}" "${D}"
189 local my_file="$(webapp_strip_appdir "${m}")"
190 my_file="$(webapp_strip_cwd "${my_file}")"
192 elog "(config) ${my_file}"
193 echo "${my_file}" >> ${D}/${WA_CONFIGLIST}
197 # @FUNCTION: webapp_hook_script
200 # Install a script that will run after a virtual copy is created, and
201 # before a virtual copy has been removed.
202 webapp_hook_script() {
203 debug-print-function $FUNCNAME $*
205 webapp_checkfileexists "${1}"
208 cp "${1}" "${D}/${MY_HOOKSCRIPTSDIR}/$(basename "${1}")" || die "Unable to install ${1} into ${D}/${MY_HOOKSCRIPTSDIR}/"
209 chmod 555 "${D}/${MY_HOOKSCRIPTSDIR}/$(basename "${1}")"
212 # @FUNCTION: webapp_postinst_txt
213 # @USAGE: <lang> <file>
215 # Install a text file containing post-installation instructions.
216 webapp_postinst_txt() {
217 debug-print-function $FUNCNAME $*
219 webapp_checkfileexists "${2}"
221 elog "(info) ${2} (lang: ${1})"
222 cp "${2}" "${D}/${MY_APPDIR}/postinst-${1}.txt"
225 # @FUNCTION: webapp_postupgrade_txt
226 # @USAGE: <lang> <file>
228 # Install a text file containing post-upgrade instructions.
229 webapp_postupgrade_txt() {
230 debug-print-function $FUNCNAME $*
232 webapp_checkfileexists "${2}"
234 elog "(info) ${2} (lang: ${1})"
235 cp "${2}" "${D}/${MY_APPDIR}/postupgrade-${1}.txt"
238 # helper for webapp_serverowned()
239 _webapp_serverowned() {
240 debug-print-function $FUNCNAME $*
242 webapp_checkfileexists "${1}" "${D}"
243 local my_file="$(webapp_strip_appdir "${1}")"
244 my_file="$(webapp_strip_cwd "${my_file}")"
246 echo "${my_file}" >> "${D}/${WA_SOLIST}"
249 # @FUNCTION: webapp_serverowned
250 # @USAGE: [-R] <file> [more files ...]
252 # Identify a file which must be owned by the webserver's user:group settings.
253 # The ownership of the file is NOT set until the application is installed using
254 # the webapp-config tool. If -R is given directories are handled recursively.
255 webapp_serverowned() {
256 debug-print-function $FUNCNAME $*
259 if [[ "${1}" == "-R" ]]; then
262 find "${D}${m}" | while read a; do
263 a=$(webapp_strip_d "${a}")
264 _webapp_serverowned "${a}"
269 _webapp_serverowned "${m}"
274 # @FUNCTION: webapp_server_configfile
275 # @USAGE: <server> <file> [new name]
277 # Install a configuration file for the webserver. You need to specify a
278 # webapp-config supported <server>. if no new name is given `basename $2' is
279 # used by default. Note: this function will automagically prepend $1 to the
280 # front of your config file's name.
281 webapp_server_configfile() {
282 debug-print-function $FUNCNAME $*
284 webapp_checkfileexists "${2}"
288 # do NOT change the naming convention used here without changing all
289 # the other scripts that also rely upon these names
291 local my_file="${1}-${3:-$(basename "${2}")}"
293 elog "(${1}) config file '${my_file}'"
294 cp "${2}" "${D}/${MY_SERVERCONFIGDIR}/${my_file}"
297 # @FUNCTION: webapp_sqlscript
298 # @USAGE: <db> <file> [version]
300 # Install a SQL script that creates/upgrades a database schema for the web
301 # application. Currently supported database engines are mysql and postgres.
302 # If a version is given the script should upgrade the database schema from
303 # the given version to $PVR.
305 debug-print-function $FUNCNAME $*
307 webapp_checkfileexists "${2}"
309 dodir "${MY_SQLSCRIPTSDIR}/${1}"
313 # do NOT change the naming convention used here without changing all
314 # the other scripts that also rely upon these names
316 if [[ -n "${3}" ]]; then
317 elog "(${1}) upgrade script for ${PN}-${3} to ${PVR}"
318 cp "${2}" "${D}${MY_SQLSCRIPTSDIR}/${1}/${3}_to_${PVR}.sql"
319 chmod 600 "${D}${MY_SQLSCRIPTSDIR}/${1}/${3}_to_${PVR}.sql"
321 elog "(${1}) create script for ${PN}-${PVR}"
322 cp "${2}" "${D}/${MY_SQLSCRIPTSDIR}/${1}/${PVR}_create.sql"
323 chmod 600 "${D}/${MY_SQLSCRIPTSDIR}/${1}/${PVR}_create.sql"
327 # @FUNCTION: webapp_src_preinst
329 # You need to call this function in src_install() BEFORE anything else has run.
330 # For now we just create required webapp-config directories.
331 webapp_src_preinst() {
332 debug-print-function $FUNCNAME $*
334 # sanity checks, to catch bugs in the ebuild
335 if [[ ! -f "${T}/${SETUP_CHECK_FILE}" ]]; then
337 eerror "This ebuild did not call webapp_pkg_setup() at the beginning"
338 eerror "of the pkg_setup() function"
340 eerror "Please log a bug on https://bugs.gentoo.org"
342 eerror "You should use emerge -C to remove this package, as the"
343 eerror "installation is incomplete"
345 die "Ebuild did not call webapp_pkg_setup() - report to https://bugs.gentoo.org"
348 # Hint, see the webapp_read_config() function to find where these are
350 dodir "${MY_HTDOCSDIR}"
351 dodir "${MY_HOSTROOTDIR}"
352 dodir "${MY_CGIBINDIR}"
353 dodir "${MY_ICONSDIR}"
354 dodir "${MY_ERRORSDIR}"
355 dodir "${MY_SQLSCRIPTSDIR}"
356 dodir "${MY_HOOKSCRIPTSDIR}"
357 dodir "${MY_SERVERCONFIGDIR}"
360 # ==============================================================================
362 # ==============================================================================
364 # @FUNCTION: webapp_pkg_setup
366 # The default pkg_setup() for this eclass. This will gather required variables
367 # from webapp-config and check if there is an application installed to
368 # `${ROOT}/var/www/localhost/htdocs/${PN}/' if USE=vhosts is not set.
370 # You need to call this function BEFORE anything else has run in your custom
373 debug-print-function $FUNCNAME $*
375 # to test whether or not the ebuild has correctly called this function
376 # we add an empty file to the filesystem
378 # we used to just set a variable in the shell script, but we can
379 # no longer rely on Portage calling both webapp_pkg_setup() and
380 # webapp_src_install() within the same shell process
381 touch "${T}/${SETUP_CHECK_FILE}"
383 # special case - some ebuilds *do* need to overwride the SLOT
384 if [[ "${SLOT}+" != "${PVR}+" && "${WEBAPP_MANUAL_SLOT}" != "yes" ]]; then
385 die "Set WEBAPP_MANUAL_SLOT=\"yes\" if you need to SLOT manually"
388 # pull in the shared configuration file
389 G_HOSTNAME="localhost"
392 local my_dir="${ROOT}${VHOST_ROOT}/${MY_HTDOCSBASE}/${PN}"
394 # if USE=vhosts is enabled OR no application is installed we're done here
395 if ! has vhosts ${IUSE} || use vhosts || [[ ! -d "${my_dir}" ]]; then
400 my_output="$(webapp_check_installedat)"
402 if [[ $? -ne 0 ]]; then
403 # okay, whatever is there, it isn't webapp-config-compatible
406 ewarn "You already have something installed in ${my_dir}"
408 ewarn "Whatever is in ${my_dir}, it's not"
409 ewarn "compatible with webapp-config."
411 ewarn "This ebuild may be overwriting important files."
414 elif [[ "$(echo ${my_output} | awk '{ print $1 }')" != "${PN}" ]]; then
416 eerror "You already have ${my_output} installed in ${my_dir}"
418 eerror "I cannot upgrade a different application"
421 die "Cannot upgrade contents of ${my_dir}"
426 # @FUNCTION: webapp_src_install
428 # This is the default src_install(). For now, we just make sure that root owns
429 # everything, and that there are no setuid files.
431 # You need to call this function AFTER everything else has run in your custom
433 webapp_src_install() {
434 debug-print-function $FUNCNAME $*
436 # to test whether or not the ebuild has correctly called this function
437 # we add an empty file to the filesystem
439 # we used to just set a variable in the shell script, but we can
440 # no longer rely on Portage calling both webapp_src_install() and
441 # webapp_pkg_postinst() within the same shell process
442 touch "${D}/${MY_APPDIR}/${INSTALL_CHECK_FILE}"
444 chown -R "${VHOST_DEFAULT_UID}:${VHOST_DEFAULT_GID}" "${D}/"
448 keepdir "${MY_PERSISTDIR}"
449 fowners "root:0" "${MY_PERSISTDIR}"
450 fperms 755 "${MY_PERSISTDIR}"
453 # @FUNCTION: webapp_pkg_postinst
455 # The default pkg_postinst() for this eclass. This installs the web application to
456 # `${ROOT}/var/www/localhost/htdocs/${PN}/' if USE=vhosts is not set. Otherwise
457 # display a short notice how to install this application with webapp-config.
459 # You need to call this function AFTER everything else has run in your custom
461 webapp_pkg_postinst() {
462 debug-print-function $FUNCNAME $*
466 # sanity checks, to catch bugs in the ebuild
467 if [[ ! -f "${ROOT}${MY_APPDIR}/${INSTALL_CHECK_FILE}" ]]; then
469 eerror "This ebuild did not call webapp_src_install() at the end"
470 eerror "of the src_install() function"
472 eerror "Please log a bug on https://bugs.gentoo.org"
474 eerror "You should use emerge -C to remove this package, as the"
475 eerror "installation is incomplete"
477 die "Ebuild did not call webapp_src_install() - report to https://bugs.gentoo.org"
480 if has vhosts ${IUSE}; then
481 if ! use vhosts; then
483 elog "vhosts USE flag not set - auto-installing using webapp-config"
485 G_HOSTNAME="localhost"
489 webapp_getinstalltype
491 if [[ "${IS_REPLACE}" == "1" ]]; then
492 elog "${PN}-${PVR} is already installed - replacing"
494 elif [[ "${IS_UPGRADE}" == "1" ]]; then
495 elog "${REMOVE_PKG} is already installed - upgrading"
498 elog "${PN}-${PVR} is not installed - using install mode"
501 my_cmd="${WEBAPP_CONFIG} -h localhost -u root -d ${INSTALL_DIR} ${my_mode} ${PN} ${PVR}"
502 elog "Running ${my_cmd}"
506 local cleaner="${WEBAPP_CLEANER} -p -C ${CATEGORY}/${PN}"
507 einfo "Running ${cleaner}"
511 elog "The 'vhosts' USE flag is switched ON"
512 elog "This means that Portage will not automatically run webapp-config to"
513 elog "complete the installation."
515 elog "To install ${PN}-${PVR} into a virtual host, run the following command:"
517 elog " webapp-config -h <host> -d ${PN} -I ${PN} ${PVR}"
519 elog "For more details, see the webapp-config(8) man page"
523 elog "This ebuild does not support the 'vhosts' USE flag."
524 elog "This means that Portage will not automatically run webapp-config to"
525 elog "complete the installation."
527 elog "To install ${PN}-${PVR} into a virtual host, run the following command:"
529 elog " webapp-config -h <host> -d ${PN} -I ${PN} ${PVR}"
531 elog "For more details, see the webapp-config(8) man page"
535 # @FUNCTION: webapp_pkg_prerm
537 # This is the default pkg_prerm() for this eclass. If USE=vhosts is not set
538 # remove all installed copies of this web application. Otherwise instruct the
539 # user to manually remove those copies. See bug #136959.
541 debug-print-function $FUNCNAME $*
544 my_output="$(${WEBAPP_CONFIG} --list-installs ${PN} ${PVR})"
545 [[ $? -ne 0 ]] && return
548 if has vhosts ${IUSE} && ! use vhosts; then
549 echo "${my_output}" | while read x; do
550 if [[ -f "${x}"/.webapp ]]; then
552 if [[ -n "${WEB_HOSTNAME}" && -n "${WEB_INSTALLDIR}" ]]; then
553 ${WEBAPP_CONFIG} -h ${WEB_HOSTNAME} -d ${WEB_INSTALLDIR} -C ${PN} ${PVR}
556 ewarn "Cannot find file ${x}/.webapp"
559 elif [[ "${my_output}" != "" ]]; then
562 ewarn "Don't forget to use webapp-config to remove any copies of"
563 ewarn "${PN}-${PVR} installed in"
566 echo "${my_output}" | while read x; do
567 if [[ -f "${x}"/.webapp ]]; then
570 ewarn "Cannot find file ${x}/.webapp"