zhang-hare | d98a5d0 | 2013-06-21 18:18:02 +0800 | [diff] [blame^] | 1 | # lib/apache |
| 2 | # Functions to control configuration and operation of apache web server |
| 3 | |
| 4 | # Dependencies: |
| 5 | # ``functions`` file |
| 6 | # is_apache_enabled_service |
| 7 | # change_apache_user_group |
| 8 | # install_apache_wsgi |
| 9 | # config_apache_wsgi |
| 10 | # start_apache_server |
| 11 | # stop_apache_server |
| 12 | # restart_apache_server |
| 13 | |
| 14 | # Save trace setting |
| 15 | XTRACE=$(set +o | grep xtrace) |
| 16 | set +o xtrace |
| 17 | |
| 18 | # Allow overriding the default Apache user and group, default to |
| 19 | # current user and his default group. |
| 20 | APACHE_USER=${APACHE_USER:-$USER} |
| 21 | APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)} |
| 22 | |
| 23 | |
| 24 | # Set up apache name and configuration directory |
| 25 | if is_ubuntu; then |
| 26 | APACHE_NAME=apache2 |
| 27 | APACHE_CONF_DIR=sites-available |
| 28 | elif is_fedora; then |
| 29 | APACHE_NAME=httpd |
| 30 | APACHE_CONF_DIR=conf.d |
| 31 | elif is_suse; then |
| 32 | APACHE_NAME=apache2 |
| 33 | APACHE_CONF_DIR=vhosts.d |
| 34 | fi |
| 35 | |
| 36 | # Functions |
| 37 | # --------- |
| 38 | |
| 39 | # is_apache_enabled_service() checks if the service(s) specified as arguments are |
| 40 | # apache enabled by the user in ``APACHE_ENABLED_SERVICES`` as web front end. |
| 41 | # |
| 42 | # Multiple services specified as arguments are ``OR``'ed together; the test |
| 43 | # is a short-circuit boolean, i.e it returns on the first match. |
| 44 | # |
| 45 | # Uses global ``APACHE_ENABLED_SERVICES`` |
| 46 | # APACHE_ENABLED_SERVICES service [service ...] |
| 47 | function is_apache_enabled_service() { |
| 48 | services=$@ |
| 49 | for service in ${services}; do |
| 50 | [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0 |
| 51 | done |
| 52 | return 1 |
| 53 | } |
| 54 | |
| 55 | # change_apache_user_group() - Change the User/Group to run Apache server |
| 56 | function change_apache_user_group(){ |
| 57 | local stack_user=$@ |
| 58 | if is_ubuntu; then |
| 59 | sudo sed -e " |
| 60 | s/^export APACHE_RUN_USER=.*/export APACHE_RUN_USER=${stack_user}/g; |
| 61 | s/^export APACHE_RUN_GROUP=.*/export APACHE_RUN_GROUP=${stack_user}/g |
| 62 | " -i /etc/${APACHE_NAME}/envvars |
| 63 | elif is_fedora; then |
| 64 | sudo sed -e " |
| 65 | s/^User .*/User ${stack_user}/g; |
| 66 | s/^Group .*/Group ${stack_user}/g |
| 67 | " -i /etc/${APACHE_NAME}/httpd.conf |
| 68 | elif is_suse; then |
| 69 | sudo sed -e " |
| 70 | s/^User .*/User ${stack_user}/g; |
| 71 | s/^Group .*/Group ${stack_user}/g |
| 72 | " -i /etc/${APACHE_NAME}/uid.conf |
| 73 | else |
| 74 | exit_distro_not_supported "apache user and group" |
| 75 | fi |
| 76 | } |
| 77 | |
| 78 | # install_apache_wsgi() - Install Apache server and wsgi module |
| 79 | function install_apache_wsgi() { |
| 80 | # Apache installation, because we mark it NOPRIME |
| 81 | if is_ubuntu; then |
| 82 | # Install apache2, which is NOPRIME'd |
| 83 | install_package apache2 libapache2-mod-wsgi |
| 84 | elif is_fedora; then |
| 85 | sudo rm -f /etc/httpd/conf.d/000-* |
| 86 | install_package httpd mod_wsgi |
| 87 | elif is_suse; then |
| 88 | install_package apache2 apache2-mod_wsgi |
| 89 | else |
| 90 | exit_distro_not_supported "apache installation" |
| 91 | fi |
| 92 | } |
| 93 | |
| 94 | # start_apache_server() - Start running apache server |
| 95 | function start_apache_server() { |
| 96 | start_service $APACHE_NAME |
| 97 | } |
| 98 | |
| 99 | # stop_apache_server() - Stop running apache server |
| 100 | function stop_apache_server() { |
| 101 | if [ -n "$APACHE_NAME" ]; then |
| 102 | stop_service $APACHE_NAME |
| 103 | else |
| 104 | exit_distro_not_supported "apache configuration" |
| 105 | fi |
| 106 | } |
| 107 | |
| 108 | # restart_apache_server |
| 109 | function restart_apache_server() { |
| 110 | restart_service $APACHE_NAME |
| 111 | } |
| 112 | |
| 113 | # Restore xtrace |
| 114 | $XTRACE |
| 115 | |
| 116 | # Local variables: |
| 117 | # mode: shell-script |
| 118 | # End: |