Merge remote-tracking branch 'remotes/github/pr/52'.
[gentoo.git] / eclass / webapp.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: webapp.eclass
6 # @MAINTAINER:
7 # web-apps@gentoo.org
8 # @BLURB: functions for installing applications to run under a web server
9 # @DESCRIPTION:
10 # The webapp eclass contains functions to handle web applications with
11 # webapp-config. Part of the implementation of GLEP #11
12
13 # @ECLASS-VARIABLE: WEBAPP_DEPEND
14 # @DESCRIPTION:
15 # An ebuild should use WEBAPP_DEPEND if a custom DEPEND needs to be built, most
16 # notably in combination with WEBAPP_OPTIONAL.
17 WEBAPP_DEPEND=">=app-admin/webapp-config-1.50.15"
18
19 # @ECLASS-VARIABLE: WEBAPP_NO_AUTO_INSTALL
20 # @DESCRIPTION:
21 # An ebuild sets this to `yes' if an automatic installation and/or upgrade is
22 # not possible. The ebuild should overwrite pkg_postinst() and explain the
23 # reason for this BEFORE calling webapp_pkg_postinst().
24
25 # @ECLASS-VARIABLE: WEBAPP_OPTIONAL
26 # @DESCRIPTION:
27 # An ebuild sets this to `yes' to make webapp support optional, in which case
28 # you also need to take care of USE-flags and dependencies.
29
30 if [[ "${WEBAPP_OPTIONAL}" != "yes" ]]; then
31         [[ "${WEBAPP_NO_AUTO_INSTALL}" == "yes" ]] || IUSE="vhosts"
32         SLOT="${PVR}"
33         DEPEND="${WEBAPP_DEPEND}"
34         RDEPEND="${DEPEND}"
35 fi
36
37 EXPORT_FUNCTIONS pkg_postinst pkg_setup src_install pkg_prerm
38
39 INSTALL_DIR="/${PN}"
40 IS_UPGRADE=0
41 IS_REPLACE=0
42
43 INSTALL_CHECK_FILE="installed_by_webapp_eclass"
44 SETUP_CHECK_FILE="setup_by_webapp_eclass"
45
46 ETC_CONFIG="${ROOT}etc/vhosts/webapp-config"
47 WEBAPP_CONFIG="${ROOT}usr/sbin/webapp-config"
48 WEBAPP_CLEANER="${ROOT}usr/sbin/webapp-cleaner"
49
50 # ==============================================================================
51 # INTERNAL FUNCTIONS
52 # ==============================================================================
53
54 # Load the config file /etc/vhosts/webapp-config
55 # Supports both the old bash version, and the new python version
56 webapp_read_config() {
57         debug-print-function $FUNCNAME $*
58
59         if has_version '>=app-admin/webapp-config-1.50'; then
60                 ENVVAR=$(${WEBAPP_CONFIG} --query ${PN} ${PVR}) || die "Could not read settings from webapp-config!"
61                 eval ${ENVVAR}
62         elif [[ "${WEBAPP_OPTIONAL}" != "yes" ]]; then
63                 # ETC_CONFIG might not be available
64                 . ${ETC_CONFIG} || die "Unable to read ${ETC_CONFIG}"
65         elif [[ -f "${ETC_CONFIG}" ]]; then
66                 # WEBAPP_OPTIONAL is set to yes
67                 # and this must run only if ETC_CONFIG actually exists
68                 . ${ETC_CONFIG} || die "Unable to read ${ETC_CONFIG}"
69         fi
70 }
71
72 # Check whether a specified file exists in the given directory (`.' by default)
73 webapp_checkfileexists() {
74         debug-print-function $FUNCNAME $*
75
76         local my_prefix=${2:+${2}/}
77
78         if [[ ! -e "${my_prefix}${1}" ]]; then
79                 msg="ebuild fault: file '${1}' not found"
80                 eerror "$msg"
81                 eerror "Please report this as a bug at https://bugs.gentoo.org/"
82                 die "$msg"
83         fi
84 }
85
86 webapp_check_installedat() {
87         debug-print-function $FUNCNAME $*
88         ${WEBAPP_CONFIG} --show-installed -h localhost -d "${INSTALL_DIR}" 2> /dev/null
89 }
90
91 webapp_strip_appdir() {
92         debug-print-function $FUNCNAME $*
93         echo "${1#${MY_APPDIR}/}"
94 }
95
96 webapp_strip_d() {
97         debug-print-function $FUNCNAME $*
98         echo "${1#${D}}"
99 }
100
101 webapp_strip_cwd() {
102         debug-print-function $FUNCNAME $*
103         echo "${1/#.\///}"
104 }
105
106 webapp_getinstalltype() {
107         debug-print-function $FUNCNAME $*
108
109         if ! has vhosts ${IUSE} || use vhosts; then
110                 return
111         fi
112
113         local my_output
114         my_output="$(webapp_check_installedat)"
115
116         if [[ $? -eq 0 ]]; then
117                 # something is already installed there
118                 # make sure it isn't the same version
119
120                 local my_pn="$(echo ${my_output} | awk '{ print $1 }')"
121                 local my_pvr="$(echo ${my_output} | awk '{ print $2 }')"
122
123                 REMOVE_PKG="${my_pn}-${my_pvr}"
124
125                 if [[ "${my_pn}" == "${PN}" ]]; then
126                         if [[ "${my_pvr}" != "${PVR}" ]]; then
127                                 elog "This is an upgrade"
128                                 IS_UPGRADE=1
129                                 # for binpkgs, reset status, var declared in global scope
130                                 IS_REPLACE=0
131                         else
132                                 elog "This is a re-installation"
133                                 IS_REPLACE=1
134                                 # for binpkgs, reset status, var declared in global scope
135                                 IS_UPGRADE=0
136                         fi
137                 else
138                         elog "${my_output} is installed there"
139                 fi
140         else
141                 # for binpkgs, reset status, var declared in global scope
142                 IS_REPLACE=0
143                 IS_UPGRADE=0
144                 elog "This is an installation"
145         fi
146 }
147
148 # ==============================================================================
149 # PUBLIC FUNCTIONS
150 # ==============================================================================
151
152 # @FUNCTION: need_httpd
153 # @DESCRIPTION:
154 # Call this function AFTER your ebuilds DEPEND line if any of the available
155 # webservers are able to run this application.
156 need_httpd() {
157         DEPEND="${DEPEND}
158                 || ( virtual/httpd-basic virtual/httpd-cgi virtual/httpd-fastcgi )"
159 }
160
161 # @FUNCTION: need_httpd_cgi
162 # @DESCRIPTION:
163 # Call this function AFTER your ebuilds DEPEND line if any of the available
164 # CGI-capable webservers are able to run this application.
165 need_httpd_cgi() {
166         DEPEND="${DEPEND}
167                 || ( virtual/httpd-cgi virtual/httpd-fastcgi )"
168 }
169
170 # @FUNCTION: need_httpd_fastcgi
171 # @DESCRIPTION:
172 # Call this function AFTER your ebuilds DEPEND line if any of the available
173 # FastCGI-capabale webservers are able to run this application.
174 need_httpd_fastcgi() {
175         DEPEND="${DEPEND}
176                 virtual/httpd-fastcgi"
177 }
178
179 # @FUNCTION: webapp_configfile
180 # @USAGE: <file> [more files ...]
181 # @DESCRIPTION:
182 # Mark a file config-protected for a web-based application.
183 webapp_configfile() {
184         debug-print-function $FUNCNAME $*
185
186         local m
187         for m in "$@"; do
188                 webapp_checkfileexists "${m}" "${D}"
189
190                 local my_file="$(webapp_strip_appdir "${m}")"
191                 my_file="$(webapp_strip_cwd "${my_file}")"
192
193                 elog "(config) ${my_file}"
194                 echo "${my_file}" >> ${D}/${WA_CONFIGLIST}
195         done
196 }
197
198 # @FUNCTION: webapp_hook_script
199 # @USAGE: <file>
200 # @DESCRIPTION:
201 # Install a script that will run after a virtual copy is created, and
202 # before a virtual copy has been removed.
203 webapp_hook_script() {
204         debug-print-function $FUNCNAME $*
205
206         webapp_checkfileexists "${1}"
207
208         elog "(hook) ${1}"
209         cp "${1}" "${D}/${MY_HOOKSCRIPTSDIR}/$(basename "${1}")" || die "Unable to install ${1} into ${D}/${MY_HOOKSCRIPTSDIR}/"
210         chmod 555 "${D}/${MY_HOOKSCRIPTSDIR}/$(basename "${1}")"
211 }
212
213 # @FUNCTION: webapp_postinst_txt
214 # @USAGE: <lang> <file>
215 # @DESCRIPTION:
216 # Install a text file containing post-installation instructions.
217 webapp_postinst_txt() {
218         debug-print-function $FUNCNAME $*
219
220         webapp_checkfileexists "${2}"
221
222         elog "(info) ${2} (lang: ${1})"
223         cp "${2}" "${D}/${MY_APPDIR}/postinst-${1}.txt"
224 }
225
226 # @FUNCTION: webapp_postupgrade_txt
227 # @USAGE: <lang> <file>
228 # @DESCRIPTION:
229 # Install a text file containing post-upgrade instructions.
230 webapp_postupgrade_txt() {
231         debug-print-function $FUNCNAME $*
232
233         webapp_checkfileexists "${2}"
234
235         elog "(info) ${2} (lang: ${1})"
236         cp "${2}" "${D}/${MY_APPDIR}/postupgrade-${1}.txt"
237 }
238
239 # helper for webapp_serverowned()
240 _webapp_serverowned() {
241         debug-print-function $FUNCNAME $*
242
243         webapp_checkfileexists "${1}" "${D}"
244         local my_file="$(webapp_strip_appdir "${1}")"
245         my_file="$(webapp_strip_cwd "${my_file}")"
246
247         echo "${my_file}" >> "${D}/${WA_SOLIST}"
248 }
249
250 # @FUNCTION: webapp_serverowned
251 # @USAGE: [-R] <file> [more files ...]
252 # @DESCRIPTION:
253 # Identify a file which must be owned by the webserver's user:group settings.
254 # The ownership of the file is NOT set until the application is installed using
255 # the webapp-config tool. If -R is given directories are handled recursively.
256 webapp_serverowned() {
257         debug-print-function $FUNCNAME $*
258
259         local a m
260         if [[ "${1}" == "-R" ]]; then
261                 shift
262                 for m in "$@"; do
263                         find "${D}${m}" | while read a; do
264                                 a=$(webapp_strip_d "${a}")
265                                 _webapp_serverowned "${a}"
266                         done
267                 done
268         else
269                 for m in "$@"; do
270                         _webapp_serverowned "${m}"
271                 done
272         fi
273 }
274
275 # @FUNCTION: webapp_server_configfile
276 # @USAGE: <server> <file> [new name]
277 # @DESCRIPTION:
278 # Install a configuration file for the webserver.  You need to specify a
279 # webapp-config supported <server>.  if no new name is given `basename $2' is
280 # used by default. Note: this function will automagically prepend $1 to the
281 # front of your config file's name.
282 webapp_server_configfile() {
283         debug-print-function $FUNCNAME $*
284
285         webapp_checkfileexists "${2}"
286
287         # WARNING:
288         #
289         # do NOT change the naming convention used here without changing all
290         # the other scripts that also rely upon these names
291
292         local my_file="${1}-${3:-$(basename "${2}")}"
293
294         elog "(${1}) config file '${my_file}'"
295         cp "${2}" "${D}/${MY_SERVERCONFIGDIR}/${my_file}"
296 }
297
298 # @FUNCTION: webapp_sqlscript
299 # @USAGE: <db> <file> [version]
300 # @DESCRIPTION:
301 # Install a SQL script that creates/upgrades a database schema for the web
302 # application. Currently supported database engines are mysql and postgres.
303 # If a version is given the script should upgrade the database schema from
304 # the given version to $PVR.
305 webapp_sqlscript() {
306         debug-print-function $FUNCNAME $*
307
308         webapp_checkfileexists "${2}"
309
310         dodir "${MY_SQLSCRIPTSDIR}/${1}"
311
312         # WARNING:
313         #
314         # do NOT change the naming convention used here without changing all
315         # the other scripts that also rely upon these names
316
317         if [[ -n "${3}" ]]; then
318                 elog "(${1}) upgrade script for ${PN}-${3} to ${PVR}"
319                 cp "${2}" "${D}${MY_SQLSCRIPTSDIR}/${1}/${3}_to_${PVR}.sql"
320                 chmod 600 "${D}${MY_SQLSCRIPTSDIR}/${1}/${3}_to_${PVR}.sql"
321         else
322                 elog "(${1}) create script for ${PN}-${PVR}"
323                 cp "${2}" "${D}/${MY_SQLSCRIPTSDIR}/${1}/${PVR}_create.sql"
324                 chmod 600 "${D}/${MY_SQLSCRIPTSDIR}/${1}/${PVR}_create.sql"
325         fi
326 }
327
328 # @FUNCTION: webapp_src_preinst
329 # @DESCRIPTION:
330 # You need to call this function in src_install() BEFORE anything else has run.
331 # For now we just create required webapp-config directories.
332 webapp_src_preinst() {
333         debug-print-function $FUNCNAME $*
334
335         # sanity checks, to catch bugs in the ebuild
336         if [[ ! -f "${T}/${SETUP_CHECK_FILE}" ]]; then
337                 eerror
338                 eerror "This ebuild did not call webapp_pkg_setup() at the beginning"
339                 eerror "of the pkg_setup() function"
340                 eerror
341                 eerror "Please log a bug on https://bugs.gentoo.org"
342                 eerror
343                 eerror "You should use emerge -C to remove this package, as the"
344                 eerror "installation is incomplete"
345                 eerror
346                 die "Ebuild did not call webapp_pkg_setup() - report to https://bugs.gentoo.org"
347         fi
348
349         # Hint, see the webapp_read_config() function to find where these are
350         # defined.
351         dodir "${MY_HTDOCSDIR}"
352         dodir "${MY_HOSTROOTDIR}"
353         dodir "${MY_CGIBINDIR}"
354         dodir "${MY_ICONSDIR}"
355         dodir "${MY_ERRORSDIR}"
356         dodir "${MY_SQLSCRIPTSDIR}"
357         dodir "${MY_HOOKSCRIPTSDIR}"
358         dodir "${MY_SERVERCONFIGDIR}"
359 }
360
361 # ==============================================================================
362 # EXPORTED FUNCTIONS
363 # ==============================================================================
364
365 # @FUNCTION: webapp_pkg_setup
366 # @DESCRIPTION:
367 # The default pkg_setup() for this eclass. This will gather required variables
368 # from webapp-config and check if there is an application installed to
369 # `${ROOT}/var/www/localhost/htdocs/${PN}/' if USE=vhosts is not set.
370 #
371 # You need to call this function BEFORE anything else has run in your custom
372 # pkg_setup().
373 webapp_pkg_setup() {
374         debug-print-function $FUNCNAME $*
375
376         # to test whether or not the ebuild has correctly called this function
377         # we add an empty file to the filesystem
378         #
379         # we used to just set a variable in the shell script, but we can
380         # no longer rely on Portage calling both webapp_pkg_setup() and
381         # webapp_src_install() within the same shell process
382         touch "${T}/${SETUP_CHECK_FILE}"
383
384         # special case - some ebuilds *do* need to overwride the SLOT
385         if [[ "${SLOT}+" != "${PVR}+" && "${WEBAPP_MANUAL_SLOT}" != "yes" ]]; then
386                 die "Set WEBAPP_MANUAL_SLOT=\"yes\" if you need to SLOT manually"
387         fi
388
389         # pull in the shared configuration file
390         G_HOSTNAME="localhost"
391         webapp_read_config
392
393         local my_dir="${ROOT}${VHOST_ROOT}/${MY_HTDOCSBASE}/${PN}"
394
395         # if USE=vhosts is enabled OR no application is installed we're done here
396         if ! has vhosts ${IUSE} || use vhosts || [[ ! -d "${my_dir}" ]]; then
397                 return
398         fi
399
400         local my_output
401         my_output="$(webapp_check_installedat)"
402
403         if [[ $? -ne 0 ]]; then
404                 # okay, whatever is there, it isn't webapp-config-compatible
405                 echo
406                 ewarn
407                 ewarn "You already have something installed in ${my_dir}"
408                 ewarn
409                 ewarn "Whatever is in ${my_dir}, it's not"
410                 ewarn "compatible with webapp-config."
411                 ewarn
412                 ewarn "This ebuild may be overwriting important files."
413                 ewarn
414                 echo
415                 if has "${EAPI:-0}" 0 1 2; then
416                         ebeep 10
417                 fi
418         elif [[ "$(echo ${my_output} | awk '{ print $1 }')" != "${PN}" ]]; then
419                 echo
420                 eerror "You already have ${my_output} installed in ${my_dir}"
421                 eerror
422                 eerror "I cannot upgrade a different application"
423                 eerror
424                 echo
425                 die "Cannot upgrade contents of ${my_dir}"
426         fi
427
428 }
429
430 # @FUNCTION: webapp_src_install
431 # @DESCRIPTION:
432 # This is the default src_install(). For now, we just make sure that root owns
433 # everything, and that there are no setuid files.
434 #
435 # You need to call this function AFTER everything else has run in your custom
436 # src_install().
437 webapp_src_install() {
438         debug-print-function $FUNCNAME $*
439
440         # to test whether or not the ebuild has correctly called this function
441         # we add an empty file to the filesystem
442         #
443         # we used to just set a variable in the shell script, but we can
444         # no longer rely on Portage calling both webapp_src_install() and
445         # webapp_pkg_postinst() within the same shell process
446         touch "${D}/${MY_APPDIR}/${INSTALL_CHECK_FILE}"
447
448         chown -R "${VHOST_DEFAULT_UID}:${VHOST_DEFAULT_GID}" "${D}/"
449         chmod -R u-s "${D}/"
450         chmod -R g-s "${D}/"
451
452         keepdir "${MY_PERSISTDIR}"
453         fowners "root:0" "${MY_PERSISTDIR}"
454         fperms 755 "${MY_PERSISTDIR}"
455 }
456
457 # @FUNCTION: webapp_pkg_postinst
458 # @DESCRIPTION:
459 # The default pkg_postinst() for this eclass. This installs the web application to
460 # `${ROOT}/var/www/localhost/htdocs/${PN}/' if USE=vhosts is not set. Otherwise
461 # display a short notice how to install this application with webapp-config.
462 #
463 # You need to call this function AFTER everything else has run in your custom
464 # pkg_postinst().
465 webapp_pkg_postinst() {
466         debug-print-function $FUNCNAME $*
467
468         webapp_read_config
469
470         # sanity checks, to catch bugs in the ebuild
471         if [[ ! -f "${ROOT}${MY_APPDIR}/${INSTALL_CHECK_FILE}" ]]; then
472                 eerror
473                 eerror "This ebuild did not call webapp_src_install() at the end"
474                 eerror "of the src_install() function"
475                 eerror
476                 eerror "Please log a bug on https://bugs.gentoo.org"
477                 eerror
478                 eerror "You should use emerge -C to remove this package, as the"
479                 eerror "installation is incomplete"
480                 eerror
481                 die "Ebuild did not call webapp_src_install() - report to https://bugs.gentoo.org"
482         fi
483
484         if has vhosts ${IUSE}; then
485                 if ! use vhosts; then
486                         echo
487                         elog "vhosts USE flag not set - auto-installing using webapp-config"
488
489                         G_HOSTNAME="localhost"
490                         webapp_read_config
491
492                         local my_mode=-I
493                         webapp_getinstalltype
494
495                         if [[ "${IS_REPLACE}" == "1" ]]; then
496                                 elog "${PN}-${PVR} is already installed - replacing"
497                                 my_mode=-I
498                         elif [[ "${IS_UPGRADE}" == "1" ]]; then
499                                 elog "${REMOVE_PKG} is already installed - upgrading"
500                                 my_mode=-U
501                         else
502                                 elog "${PN}-${PVR} is not installed - using install mode"
503                         fi
504
505                         my_cmd="${WEBAPP_CONFIG} -h localhost -u root -d ${INSTALL_DIR} ${my_mode} ${PN} ${PVR}"
506                         elog "Running ${my_cmd}"
507                         ${my_cmd}
508
509                         echo
510                         local cleaner="${WEBAPP_CLEANER} -p -C ${CATEGORY}/${PN}"
511                         einfo "Running ${cleaner}"
512                         ${cleaner}
513                 else
514                         elog
515                         elog "The 'vhosts' USE flag is switched ON"
516                         elog "This means that Portage will not automatically run webapp-config to"
517                         elog "complete the installation."
518                         elog
519                         elog "To install ${PN}-${PVR} into a virtual host, run the following command:"
520                         elog
521                         elog "    webapp-config -h <host> -d ${PN} -I ${PN} ${PVR}"
522                         elog
523                         elog "For more details, see the webapp-config(8) man page"
524                 fi
525         else
526                 elog
527                 elog "This ebuild does not support the 'vhosts' USE flag."
528                 elog "This means that Portage will not automatically run webapp-config to"
529                 elog "complete the installation."
530                 elog
531                 elog "To install ${PN}-${PVR} into a virtual host, run the following command:"
532                 elog
533                 elog "    webapp-config -h <host> -d ${PN} -I ${PN} ${PVR}"
534                 elog
535                 elog "For more details, see the webapp-config(8) man page"
536         fi
537 }
538
539 # @FUNCTION: webapp_pkg_prerm
540 # @DESCRIPTION:
541 # This is the default pkg_prerm() for this eclass. If USE=vhosts is not set
542 # remove all installed copies of this web application. Otherwise instruct the
543 # user to manually remove those copies. See bug #136959.
544 webapp_pkg_prerm() {
545         debug-print-function $FUNCNAME $*
546
547         local my_output=
548         my_output="$(${WEBAPP_CONFIG} --list-installs ${PN} ${PVR})"
549         [[ $? -ne 0 ]] && return
550
551         local x
552         if has vhosts ${IUSE} && ! use vhosts; then
553                 echo "${my_output}" | while read x; do
554                         if [[ -f "${x}"/.webapp ]]; then
555                                 . "${x}"/.webapp
556                                 if [[ -n "${WEB_HOSTNAME}" && -n "${WEB_INSTALLDIR}" ]]; then
557                                         ${WEBAPP_CONFIG} -h ${WEB_HOSTNAME} -d ${WEB_INSTALLDIR} -C ${PN} ${PVR}
558                                 fi
559                         else
560                                 ewarn "Cannot find file ${x}/.webapp"
561                         fi
562                 done
563         elif [[ "${my_output}" != "" ]]; then
564                 echo
565                 ewarn
566                 ewarn "Don't forget to use webapp-config to remove any copies of"
567                 ewarn "${PN}-${PVR} installed in"
568                 ewarn
569
570                 echo "${my_output}" | while read x; do
571                         if [[ -f "${x}"/.webapp ]]; then
572                                 ewarn "    ${x}"
573                         else
574                                 ewarn "Cannot find file ${x}/.webapp"
575                         fi
576                 done
577
578                 ewarn
579                 echo
580         fi
581 }