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