dev-python/josepy: 1.1.0 cleanup
[gentoo.git] / eclass / webapp.eclass
1 # Copyright 1999-2018 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         elif [[ "$(echo ${my_output} | awk '{ print $1 }')" != "${PN}" ]]; then
415                 echo
416                 eerror "You already have ${my_output} installed in ${my_dir}"
417                 eerror
418                 eerror "I cannot upgrade a different application"
419                 eerror
420                 echo
421                 die "Cannot upgrade contents of ${my_dir}"
422         fi
423
424 }
425
426 # @FUNCTION: webapp_src_install
427 # @DESCRIPTION:
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.
430 #
431 # You need to call this function AFTER everything else has run in your custom
432 # src_install().
433 webapp_src_install() {
434         debug-print-function $FUNCNAME $*
435
436         # to test whether or not the ebuild has correctly called this function
437         # we add an empty file to the filesystem
438         #
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}"
443
444         chown -R "${VHOST_DEFAULT_UID}:${VHOST_DEFAULT_GID}" "${D}/"
445         chmod -R u-s "${D}/"
446         chmod -R g-s "${D}/"
447
448         keepdir "${MY_PERSISTDIR}"
449         fowners "root:0" "${MY_PERSISTDIR}"
450         fperms 755 "${MY_PERSISTDIR}"
451 }
452
453 # @FUNCTION: webapp_pkg_postinst
454 # @DESCRIPTION:
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.
458 #
459 # You need to call this function AFTER everything else has run in your custom
460 # pkg_postinst().
461 webapp_pkg_postinst() {
462         debug-print-function $FUNCNAME $*
463
464         webapp_read_config
465
466         # sanity checks, to catch bugs in the ebuild
467         if [[ ! -f "${ROOT%/}/${MY_APPDIR}/${INSTALL_CHECK_FILE}" ]]; then
468                 eerror
469                 eerror "This ebuild did not call webapp_src_install() at the end"
470                 eerror "of the src_install() function"
471                 eerror
472                 eerror "Please log a bug on https://bugs.gentoo.org"
473                 eerror
474                 eerror "You should use emerge -C to remove this package, as the"
475                 eerror "installation is incomplete"
476                 eerror
477                 die "Ebuild did not call webapp_src_install() - report to https://bugs.gentoo.org"
478         fi
479
480         if has vhosts ${IUSE}; then
481                 if ! use vhosts; then
482                         echo
483                         elog "vhosts USE flag not set - auto-installing using webapp-config"
484
485                         G_HOSTNAME="localhost"
486                         webapp_read_config
487
488                         local my_mode=-I
489                         webapp_getinstalltype
490
491                         if [[ "${IS_REPLACE}" == "1" ]]; then
492                                 elog "${PN}-${PVR} is already installed - replacing"
493                                 my_mode=-I
494                         elif [[ "${IS_UPGRADE}" == "1" ]]; then
495                                 elog "${REMOVE_PKG} is already installed - upgrading"
496                                 my_mode=-U
497                         else
498                                 elog "${PN}-${PVR} is not installed - using install mode"
499                         fi
500
501                         my_cmd="${WEBAPP_CONFIG} -h localhost -u root -d ${INSTALL_DIR} ${my_mode} ${PN} ${PVR}"
502                         elog "Running ${my_cmd}"
503                         ${my_cmd}
504
505                         echo
506                         local cleaner="${WEBAPP_CLEANER} -p -C ${CATEGORY}/${PN}"
507                         einfo "Running ${cleaner}"
508                         ${cleaner}
509                 else
510                         elog
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."
514                         elog
515                         elog "To install ${PN}-${PVR} into a virtual host, run the following command:"
516                         elog
517                         elog "    webapp-config -h <host> -d ${PN} -I ${PN} ${PVR}"
518                         elog
519                         elog "For more details, see the webapp-config(8) man page"
520                 fi
521         else
522                 elog
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."
526                 elog
527                 elog "To install ${PN}-${PVR} into a virtual host, run the following command:"
528                 elog
529                 elog "    webapp-config -h <host> -d ${PN} -I ${PN} ${PVR}"
530                 elog
531                 elog "For more details, see the webapp-config(8) man page"
532         fi
533 }
534
535 # @FUNCTION: webapp_pkg_prerm
536 # @DESCRIPTION:
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.
540 webapp_pkg_prerm() {
541         debug-print-function $FUNCNAME $*
542
543         local my_output=
544         my_output="$(${WEBAPP_CONFIG} --list-installs ${PN} ${PVR})"
545         [[ $? -ne 0 ]] && return
546
547         local x
548         if has vhosts ${IUSE} && ! use vhosts; then
549                 echo "${my_output}" | while read x; do
550                         if [[ -f "${x}"/.webapp ]]; then
551                                 . "${x}"/.webapp
552                                 if [[ -n "${WEB_HOSTNAME}" && -n "${WEB_INSTALLDIR}" ]]; then
553                                         ${WEBAPP_CONFIG} -h ${WEB_HOSTNAME} -d ${WEB_INSTALLDIR} -C ${PN} ${PVR}
554                                 fi
555                         else
556                                 ewarn "Cannot find file ${x}/.webapp"
557                         fi
558                 done
559         elif [[ "${my_output}" != "" ]]; then
560                 echo
561                 ewarn
562                 ewarn "Don't forget to use webapp-config to remove any copies of"
563                 ewarn "${PN}-${PVR} installed in"
564                 ewarn
565
566                 echo "${my_output}" | while read x; do
567                         if [[ -f "${x}"/.webapp ]]; then
568                                 ewarn "    ${x}"
569                         else
570                                 ewarn "Cannot find file ${x}/.webapp"
571                         fi
572                 done
573
574                 ewarn
575                 echo
576         fi
577 }