Fix the way Apache site configuration files are used, to improve OS portability

On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
files are 000-default.conf and default-ssl.conf.

On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
/etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
sites' files are default and default-ssl.

On Fedora, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled.

On RHEL and CentOS, things should hopefully work as in Fedora.

This change puts all distribution-related site configuration file name differences in lib/apache and
the other services gets the file name for its sites using the new exported function
apache_site_config_for <sitename>.

It also makes Fedora disabled sites use the .conf.disabled suffix instead of removing the .conf from
the file name.

The table below summarizes what should happen on each distribution:
+----------------------+--------------------+--------------------------+--------------------------+
| Distribution         | File name          | Site enabling command    | Site disabling command   |
+----------------------+--------------------+--------------------------+--------------------------+
| Ubuntu 12.04         | site               | a2ensite site            | a2dissite site           |
| Ubuntu 14.04         | site.conf          | a2ensite site            | a2dissite site           |
| Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
+----------------------+--------------------+--------------------------+--------------------------+

Change-Id: Ia2ba3cb7caccb6e9b65380f9d51d9d21180b894e
Closes-bug: #1313765
diff --git a/lib/apache b/lib/apache
index 2d5e39a..55083e7 100644
--- a/lib/apache
+++ b/lib/apache
@@ -11,6 +11,7 @@
 # - is_apache_enabled_service
 # - install_apache_wsgi
 # - config_apache_wsgi
+# - apache_site_config_for
 # - enable_apache_site
 # - disable_apache_site
 # - start_apache_server
@@ -78,6 +79,51 @@
     fi
 }
 
+# apache_site_config_for() - The filename of the site's configuration file.
+# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
+#
+# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
+# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
+# files are 000-default.conf and default-ssl.conf.
+#
+# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
+# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
+# sites' files are default and default-ssl.
+#
+# On Fedora, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled.
+#
+# On RHEL and CentOS, things should hopefully work as in Fedora.
+#
+# The table below summarizes what should happen on each distribution:
+# +----------------------+--------------------+--------------------------+--------------------------+
+# | Distribution         | File name          | Site enabling command    | Site disabling command   |
+# +----------------------+--------------------+--------------------------+--------------------------+
+# | Ubuntu 12.04         | site               | a2ensite site            | a2dissite site           |
+# | Ubuntu 14.04         | site.conf          | a2ensite site            | a2dissite site           |
+# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
+# +----------------------+--------------------+--------------------------+--------------------------+
+function apache_site_config_for {
+    local site=$@
+    if is_ubuntu; then
+        local apache_version=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
+        if [[ "$apache_version" =~ ^2\.2\. ]]; then
+            # Ubuntu 12.04 - Apache 2.2
+            echo /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
+        else
+            # Ubuntu 14.04 - Apache 2.4
+            echo /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
+        fi
+    elif is_fedora; then
+        # fedora conf.d is only imported if it ends with .conf so this is approx the same
+        local enabled_site_file="/etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf"
+        if [ -f $enabled_site_file ]; then
+            echo ${enabled_site_file}
+        else
+            echo ${enabled_site_file}.disabled
+        fi
+    fi
+}
+
 # enable_apache_site() - Enable a particular apache site
 function enable_apache_site {
     local site=$@
@@ -85,7 +131,7 @@
         sudo a2ensite ${site}
     elif is_fedora; then
         # fedora conf.d is only imported if it ends with .conf so this is approx the same
-        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site} /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
+        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf.disabled /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
     fi
 }
 
@@ -95,7 +141,7 @@
     if is_ubuntu; then
         sudo a2dissite ${site}
     elif is_fedora; then
-        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
+        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf.disabled
     fi
 }
 
diff --git a/lib/horizon b/lib/horizon
index 9ce4853..9e4f2f9 100644
--- a/lib/horizon
+++ b/lib/horizon
@@ -122,11 +122,11 @@
         HORIZON_REQUIRE='Require all granted'
     fi
 
-    local horizon_conf=/etc/$APACHE_NAME/$APACHE_CONF_DIR/horizon.conf
+    local horizon_conf=$(apache_site_config_for horizon)
     if is_ubuntu; then
         disable_apache_site 000-default
         sudo touch $horizon_conf
-        enable_apache_site horizon.conf
+        enable_apache_site horizon
     elif is_fedora; then
         sudo sed '/^Listen/s/^.*$/Listen 0.0.0.0:80/' -i /etc/httpd/conf/httpd.conf
     elif is_suse; then
diff --git a/lib/keystone b/lib/keystone
index f8e92f4..c4266b9 100644
--- a/lib/keystone
+++ b/lib/keystone
@@ -87,6 +87,9 @@
     KEYSTONE_SERVICE_PROTOCOL="https"
 fi
 
+# Apache configuration file for keystone
+KEYSTONE_APACHE_CONF_FILE=$(apache_site_config_for keystone)
+
 
 # Functions
 # ---------
@@ -103,7 +106,7 @@
 function _cleanup_keystone_apache_wsgi {
     sudo rm -f $KEYSTONE_WSGI_DIR/*.wsgi
     disable_apache_site keystone
-    sudo rm -f /etc/$APACHE_NAME/$APACHE_CONF_DIR/keystone
+    sudo rm -f $KEYSTONE_APACHE_CONF_FILE
 }
 
 # _config_keystone_apache_wsgi() - Set WSGI config files of Keystone
@@ -114,7 +117,7 @@
     sudo cp $KEYSTONE_DIR/httpd/keystone.py $KEYSTONE_WSGI_DIR/main
     sudo cp $KEYSTONE_DIR/httpd/keystone.py $KEYSTONE_WSGI_DIR/admin
 
-    sudo cp $FILES/apache-keystone.template /etc/$APACHE_NAME/$APACHE_CONF_DIR/keystone
+    sudo cp $FILES/apache-keystone.template $KEYSTONE_APACHE_CONF_FILE
     sudo sed -e "
         s|%PUBLICPORT%|$KEYSTONE_SERVICE_PORT|g;
         s|%ADMINPORT%|$KEYSTONE_AUTH_PORT|g;
@@ -122,7 +125,7 @@
         s|%PUBLICWSGI%|$KEYSTONE_WSGI_DIR/main|g;
         s|%ADMINWSGI%|$KEYSTONE_WSGI_DIR/admin|g;
         s|%USER%|$STACK_USER|g
-    " -i /etc/$APACHE_NAME/$APACHE_CONF_DIR/keystone
+    " -i $KEYSTONE_APACHE_CONF_FILE
     enable_apache_site keystone
 }
 
diff --git a/lib/neutron_thirdparty/trema b/lib/neutron_thirdparty/trema
index f829aa8..3e59ef2 100644
--- a/lib/neutron_thirdparty/trema
+++ b/lib/neutron_thirdparty/trema
@@ -28,7 +28,7 @@
 TREMA_LOG_LEVEL=${TREMA_LOG_LEVEL:-info}
 
 TREMA_SS_CONFIG=$TREMA_SS_ETC_DIR/sliceable.conf
-TREMA_SS_APACHE_CONFIG=/etc/apache2/sites-available/sliceable_switch.conf
+TREMA_SS_APACHE_CONFIG=$(apache_site_config_for sliceable_switch)
 
 # configure_trema - Set config files, create data dirs, etc
 function configure_trema {
@@ -61,8 +61,9 @@
     sudo cp $TREMA_SS_DIR/apache/sliceable_switch $TREMA_SS_APACHE_CONFIG
     sudo sed -i -e "s|/home/sliceable_switch/script|$TREMA_SS_SCRIPT_DIR|" \
         $TREMA_SS_APACHE_CONFIG
+    # TODO(gabriel-bezerra): use some function from lib/apache to enable these modules
     sudo a2enmod rewrite actions
-    sudo a2ensite sliceable_switch.conf
+    enable_apache_site sliceable_switch
 
     cp $TREMA_SS_DIR/sliceable_switch_null.conf $TREMA_SS_CONFIG
     sed -i -e "s|^\$apps_dir.*$|\$apps_dir = \"$TREMA_DIR/apps\"|" \
@@ -98,8 +99,7 @@
 }
 
 function start_trema {
-    # APACHE_NAME is defined in init_horizon (in lib/horizon)
-    restart_service $APACHE_NAME
+    restart_apache_server
 
     sudo LOGGING_LEVEL=$TREMA_LOG_LEVEL TREMA_TMP=$TREMA_TMP_DIR \
         trema run -d -c $TREMA_SS_CONFIG
diff --git a/lib/swift b/lib/swift
index 1e24c2c..c47b09f 100644
--- a/lib/swift
+++ b/lib/swift
@@ -152,7 +152,7 @@
         for type in object container account; do
             site_name=${type}-server-${node_number}
             disable_apache_site ${site_name}
-            sudo rm -f /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site_name}
+            sudo rm -f $(apache_site_config_for ${site_name})
         done
     done
 }
@@ -160,18 +160,17 @@
 # _config_swift_apache_wsgi() - Set WSGI config files of Swift
 function _config_swift_apache_wsgi {
     sudo mkdir -p ${SWIFT_APACHE_WSGI_DIR}
-    local apache_vhost_dir=/etc/${APACHE_NAME}/$APACHE_CONF_DIR
     local proxy_port=${SWIFT_DEFAULT_BIND_PORT:-8080}
 
     # copy proxy vhost and wsgi file
-    sudo cp ${SWIFT_DIR}/examples/apache2/proxy-server.template ${apache_vhost_dir}/proxy-server
+    sudo cp ${SWIFT_DIR}/examples/apache2/proxy-server.template $(apache_site_config_for proxy-server)
     sudo sed -e "
         /^#/d;/^$/d;
         s/%PORT%/$proxy_port/g;
         s/%SERVICENAME%/proxy-server/g;
         s/%APACHE_NAME%/${APACHE_NAME}/g;
         s/%USER%/${STACK_USER}/g;
-    " -i ${apache_vhost_dir}/proxy-server
+    " -i $(apache_site_config_for proxy-server)
     enable_apache_site proxy-server
 
     sudo cp ${SWIFT_DIR}/examples/wsgi/proxy-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/proxy-server.wsgi
@@ -186,13 +185,13 @@
         container_port=$[CONTAINER_PORT_BASE + 10 * ($node_number - 1)]
         account_port=$[ACCOUNT_PORT_BASE + 10 * ($node_number - 1)]
 
-        sudo cp ${SWIFT_DIR}/examples/apache2/object-server.template ${apache_vhost_dir}/object-server-${node_number}
+        sudo cp ${SWIFT_DIR}/examples/apache2/object-server.template $(apache_site_config_for object-server-${node_number})
         sudo sed -e "
             s/%PORT%/$object_port/g;
             s/%SERVICENAME%/object-server-${node_number}/g;
             s/%APACHE_NAME%/${APACHE_NAME}/g;
             s/%USER%/${STACK_USER}/g;
-        " -i ${apache_vhost_dir}/object-server-${node_number}
+        " -i $(apache_site_config_for object-server-${node_number})
         enable_apache_site object-server-${node_number}
 
         sudo cp ${SWIFT_DIR}/examples/wsgi/object-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/object-server-${node_number}.wsgi
@@ -201,14 +200,14 @@
             s/%SERVICECONF%/object-server\/${node_number}.conf/g;
         " -i ${SWIFT_APACHE_WSGI_DIR}/object-server-${node_number}.wsgi
 
-        sudo cp ${SWIFT_DIR}/examples/apache2/container-server.template ${apache_vhost_dir}/container-server-${node_number}
+        sudo cp ${SWIFT_DIR}/examples/apache2/container-server.template $(apache_site_config_for container-server-${node_number})
         sudo sed -e "
             /^#/d;/^$/d;
             s/%PORT%/$container_port/g;
             s/%SERVICENAME%/container-server-${node_number}/g;
             s/%APACHE_NAME%/${APACHE_NAME}/g;
             s/%USER%/${STACK_USER}/g;
-        " -i ${apache_vhost_dir}/container-server-${node_number}
+        " -i $(apache_site_config_for container-server-${node_number})
         enable_apache_site container-server-${node_number}
 
         sudo cp ${SWIFT_DIR}/examples/wsgi/container-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/container-server-${node_number}.wsgi
@@ -217,14 +216,14 @@
             s/%SERVICECONF%/container-server\/${node_number}.conf/g;
         " -i ${SWIFT_APACHE_WSGI_DIR}/container-server-${node_number}.wsgi
 
-        sudo cp ${SWIFT_DIR}/examples/apache2/account-server.template ${apache_vhost_dir}/account-server-${node_number}
+        sudo cp ${SWIFT_DIR}/examples/apache2/account-server.template $(apache_site_config_for account-server-${node_number})
         sudo sed -e "
             /^#/d;/^$/d;
             s/%PORT%/$account_port/g;
             s/%SERVICENAME%/account-server-${node_number}/g;
             s/%APACHE_NAME%/${APACHE_NAME}/g;
             s/%USER%/${STACK_USER}/g;
-        " -i ${apache_vhost_dir}/account-server-${node_number}
+        " -i $(apache_site_config_for account-server-${node_number})
         enable_apache_site account-server-${node_number}
 
         sudo cp ${SWIFT_DIR}/examples/wsgi/account-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/account-server-${node_number}.wsgi