Browse code

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

Gabriel Assis Bezerra authored on 2014/05/28 05:58:22
Showing 5 changed files
... ...
@@ -11,6 +11,7 @@
11 11
 # - is_apache_enabled_service
12 12
 # - install_apache_wsgi
13 13
 # - config_apache_wsgi
14
+# - apache_site_config_for
14 15
 # - enable_apache_site
15 16
 # - disable_apache_site
16 17
 # - start_apache_server
... ...
@@ -78,6 +79,51 @@ function install_apache_wsgi {
78 78
     fi
79 79
 }
80 80
 
81
+# apache_site_config_for() - The filename of the site's configuration file.
82
+# This function uses the global variables APACHE_NAME and APACHE_CONF_DIR.
83
+#
84
+# On Ubuntu 14.04, the site configuration file must have a .conf suffix for a2ensite and a2dissite to
85
+# recognise it. a2ensite and a2dissite ignore the .conf suffix used as parameter. The default sites'
86
+# files are 000-default.conf and default-ssl.conf.
87
+#
88
+# On Ubuntu 12.04, the site configuration file may have any format, as long as it is in
89
+# /etc/apache2/sites-available/. a2ensite and a2dissite need the entire file name to work. The default
90
+# sites' files are default and default-ssl.
91
+#
92
+# On Fedora, any file in /etc/httpd/conf.d/ whose name ends with .conf is enabled.
93
+#
94
+# On RHEL and CentOS, things should hopefully work as in Fedora.
95
+#
96
+# The table below summarizes what should happen on each distribution:
97
+# +----------------------+--------------------+--------------------------+--------------------------+
98
+# | Distribution         | File name          | Site enabling command    | Site disabling command   |
99
+# +----------------------+--------------------+--------------------------+--------------------------+
100
+# | Ubuntu 12.04         | site               | a2ensite site            | a2dissite site           |
101
+# | Ubuntu 14.04         | site.conf          | a2ensite site            | a2dissite site           |
102
+# | Fedora, RHEL, CentOS | site.conf.disabled | mv site.conf{.disabled,} | mv site.conf{,.disabled} |
103
+# +----------------------+--------------------+--------------------------+--------------------------+
104
+function apache_site_config_for {
105
+    local site=$@
106
+    if is_ubuntu; then
107
+        local apache_version=$(sudo /usr/sbin/apache2ctl -v | awk '/Server version/ {print $3}' | cut -f2 -d/)
108
+        if [[ "$apache_version" =~ ^2\.2\. ]]; then
109
+            # Ubuntu 12.04 - Apache 2.2
110
+            echo /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
111
+        else
112
+            # Ubuntu 14.04 - Apache 2.4
113
+            echo /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
114
+        fi
115
+    elif is_fedora; then
116
+        # fedora conf.d is only imported if it ends with .conf so this is approx the same
117
+        local enabled_site_file="/etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf"
118
+        if [ -f $enabled_site_file ]; then
119
+            echo ${enabled_site_file}
120
+        else
121
+            echo ${enabled_site_file}.disabled
122
+        fi
123
+    fi
124
+}
125
+
81 126
 # enable_apache_site() - Enable a particular apache site
82 127
 function enable_apache_site {
83 128
     local site=$@
... ...
@@ -85,7 +131,7 @@ function enable_apache_site {
85 85
         sudo a2ensite ${site}
86 86
     elif is_fedora; then
87 87
         # fedora conf.d is only imported if it ends with .conf so this is approx the same
88
-        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site} /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
88
+        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf.disabled /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf
89 89
     fi
90 90
 }
91 91
 
... ...
@@ -95,7 +141,7 @@ function disable_apache_site {
95 95
     if is_ubuntu; then
96 96
         sudo a2dissite ${site}
97 97
     elif is_fedora; then
98
-        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}
98
+        sudo mv /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site}.conf.disabled
99 99
     fi
100 100
 }
101 101
 
... ...
@@ -122,11 +122,11 @@ function init_horizon {
122 122
         HORIZON_REQUIRE='Require all granted'
123 123
     fi
124 124
 
125
-    local horizon_conf=/etc/$APACHE_NAME/$APACHE_CONF_DIR/horizon.conf
125
+    local horizon_conf=$(apache_site_config_for horizon)
126 126
     if is_ubuntu; then
127 127
         disable_apache_site 000-default
128 128
         sudo touch $horizon_conf
129
-        enable_apache_site horizon.conf
129
+        enable_apache_site horizon
130 130
     elif is_fedora; then
131 131
         sudo sed '/^Listen/s/^.*$/Listen 0.0.0.0:80/' -i /etc/httpd/conf/httpd.conf
132 132
     elif is_suse; then
... ...
@@ -87,6 +87,9 @@ if is_ssl_enabled_service "key"; then
87 87
     KEYSTONE_SERVICE_PROTOCOL="https"
88 88
 fi
89 89
 
90
+# Apache configuration file for keystone
91
+KEYSTONE_APACHE_CONF_FILE=$(apache_site_config_for keystone)
92
+
90 93
 
91 94
 # Functions
92 95
 # ---------
... ...
@@ -103,7 +106,7 @@ function cleanup_keystone {
103 103
 function _cleanup_keystone_apache_wsgi {
104 104
     sudo rm -f $KEYSTONE_WSGI_DIR/*.wsgi
105 105
     disable_apache_site keystone
106
-    sudo rm -f /etc/$APACHE_NAME/$APACHE_CONF_DIR/keystone
106
+    sudo rm -f $KEYSTONE_APACHE_CONF_FILE
107 107
 }
108 108
 
109 109
 # _config_keystone_apache_wsgi() - Set WSGI config files of Keystone
... ...
@@ -114,7 +117,7 @@ function _config_keystone_apache_wsgi {
114 114
     sudo cp $KEYSTONE_DIR/httpd/keystone.py $KEYSTONE_WSGI_DIR/main
115 115
     sudo cp $KEYSTONE_DIR/httpd/keystone.py $KEYSTONE_WSGI_DIR/admin
116 116
 
117
-    sudo cp $FILES/apache-keystone.template /etc/$APACHE_NAME/$APACHE_CONF_DIR/keystone
117
+    sudo cp $FILES/apache-keystone.template $KEYSTONE_APACHE_CONF_FILE
118 118
     sudo sed -e "
119 119
         s|%PUBLICPORT%|$KEYSTONE_SERVICE_PORT|g;
120 120
         s|%ADMINPORT%|$KEYSTONE_AUTH_PORT|g;
... ...
@@ -122,7 +125,7 @@ function _config_keystone_apache_wsgi {
122 122
         s|%PUBLICWSGI%|$KEYSTONE_WSGI_DIR/main|g;
123 123
         s|%ADMINWSGI%|$KEYSTONE_WSGI_DIR/admin|g;
124 124
         s|%USER%|$STACK_USER|g
125
-    " -i /etc/$APACHE_NAME/$APACHE_CONF_DIR/keystone
125
+    " -i $KEYSTONE_APACHE_CONF_FILE
126 126
     enable_apache_site keystone
127 127
 }
128 128
 
... ...
@@ -28,7 +28,7 @@ TREMA_TMP_DIR=$TREMA_DATA_DIR/trema
28 28
 TREMA_LOG_LEVEL=${TREMA_LOG_LEVEL:-info}
29 29
 
30 30
 TREMA_SS_CONFIG=$TREMA_SS_ETC_DIR/sliceable.conf
31
-TREMA_SS_APACHE_CONFIG=/etc/apache2/sites-available/sliceable_switch.conf
31
+TREMA_SS_APACHE_CONFIG=$(apache_site_config_for sliceable_switch)
32 32
 
33 33
 # configure_trema - Set config files, create data dirs, etc
34 34
 function configure_trema {
... ...
@@ -61,8 +61,9 @@ function init_trema {
61 61
     sudo cp $TREMA_SS_DIR/apache/sliceable_switch $TREMA_SS_APACHE_CONFIG
62 62
     sudo sed -i -e "s|/home/sliceable_switch/script|$TREMA_SS_SCRIPT_DIR|" \
63 63
         $TREMA_SS_APACHE_CONFIG
64
+    # TODO(gabriel-bezerra): use some function from lib/apache to enable these modules
64 65
     sudo a2enmod rewrite actions
65
-    sudo a2ensite sliceable_switch.conf
66
+    enable_apache_site sliceable_switch
66 67
 
67 68
     cp $TREMA_SS_DIR/sliceable_switch_null.conf $TREMA_SS_CONFIG
68 69
     sed -i -e "s|^\$apps_dir.*$|\$apps_dir = \"$TREMA_DIR/apps\"|" \
... ...
@@ -98,8 +99,7 @@ function install_trema {
98 98
 }
99 99
 
100 100
 function start_trema {
101
-    # APACHE_NAME is defined in init_horizon (in lib/horizon)
102
-    restart_service $APACHE_NAME
101
+    restart_apache_server
103 102
 
104 103
     sudo LOGGING_LEVEL=$TREMA_LOG_LEVEL TREMA_TMP=$TREMA_TMP_DIR \
105 104
         trema run -d -c $TREMA_SS_CONFIG
... ...
@@ -152,7 +152,7 @@ function _cleanup_swift_apache_wsgi {
152 152
         for type in object container account; do
153 153
             site_name=${type}-server-${node_number}
154 154
             disable_apache_site ${site_name}
155
-            sudo rm -f /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site_name}
155
+            sudo rm -f $(apache_site_config_for ${site_name})
156 156
         done
157 157
     done
158 158
 }
... ...
@@ -160,18 +160,17 @@ function _cleanup_swift_apache_wsgi {
160 160
 # _config_swift_apache_wsgi() - Set WSGI config files of Swift
161 161
 function _config_swift_apache_wsgi {
162 162
     sudo mkdir -p ${SWIFT_APACHE_WSGI_DIR}
163
-    local apache_vhost_dir=/etc/${APACHE_NAME}/$APACHE_CONF_DIR
164 163
     local proxy_port=${SWIFT_DEFAULT_BIND_PORT:-8080}
165 164
 
166 165
     # copy proxy vhost and wsgi file
167
-    sudo cp ${SWIFT_DIR}/examples/apache2/proxy-server.template ${apache_vhost_dir}/proxy-server
166
+    sudo cp ${SWIFT_DIR}/examples/apache2/proxy-server.template $(apache_site_config_for proxy-server)
168 167
     sudo sed -e "
169 168
         /^#/d;/^$/d;
170 169
         s/%PORT%/$proxy_port/g;
171 170
         s/%SERVICENAME%/proxy-server/g;
172 171
         s/%APACHE_NAME%/${APACHE_NAME}/g;
173 172
         s/%USER%/${STACK_USER}/g;
174
-    " -i ${apache_vhost_dir}/proxy-server
173
+    " -i $(apache_site_config_for proxy-server)
175 174
     enable_apache_site proxy-server
176 175
 
177 176
     sudo cp ${SWIFT_DIR}/examples/wsgi/proxy-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/proxy-server.wsgi
... ...
@@ -186,13 +185,13 @@ function _config_swift_apache_wsgi {
186 186
         container_port=$[CONTAINER_PORT_BASE + 10 * ($node_number - 1)]
187 187
         account_port=$[ACCOUNT_PORT_BASE + 10 * ($node_number - 1)]
188 188
 
189
-        sudo cp ${SWIFT_DIR}/examples/apache2/object-server.template ${apache_vhost_dir}/object-server-${node_number}
189
+        sudo cp ${SWIFT_DIR}/examples/apache2/object-server.template $(apache_site_config_for object-server-${node_number})
190 190
         sudo sed -e "
191 191
             s/%PORT%/$object_port/g;
192 192
             s/%SERVICENAME%/object-server-${node_number}/g;
193 193
             s/%APACHE_NAME%/${APACHE_NAME}/g;
194 194
             s/%USER%/${STACK_USER}/g;
195
-        " -i ${apache_vhost_dir}/object-server-${node_number}
195
+        " -i $(apache_site_config_for object-server-${node_number})
196 196
         enable_apache_site object-server-${node_number}
197 197
 
198 198
         sudo cp ${SWIFT_DIR}/examples/wsgi/object-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/object-server-${node_number}.wsgi
... ...
@@ -201,14 +200,14 @@ function _config_swift_apache_wsgi {
201 201
             s/%SERVICECONF%/object-server\/${node_number}.conf/g;
202 202
         " -i ${SWIFT_APACHE_WSGI_DIR}/object-server-${node_number}.wsgi
203 203
 
204
-        sudo cp ${SWIFT_DIR}/examples/apache2/container-server.template ${apache_vhost_dir}/container-server-${node_number}
204
+        sudo cp ${SWIFT_DIR}/examples/apache2/container-server.template $(apache_site_config_for container-server-${node_number})
205 205
         sudo sed -e "
206 206
             /^#/d;/^$/d;
207 207
             s/%PORT%/$container_port/g;
208 208
             s/%SERVICENAME%/container-server-${node_number}/g;
209 209
             s/%APACHE_NAME%/${APACHE_NAME}/g;
210 210
             s/%USER%/${STACK_USER}/g;
211
-        " -i ${apache_vhost_dir}/container-server-${node_number}
211
+        " -i $(apache_site_config_for container-server-${node_number})
212 212
         enable_apache_site container-server-${node_number}
213 213
 
214 214
         sudo cp ${SWIFT_DIR}/examples/wsgi/container-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/container-server-${node_number}.wsgi
... ...
@@ -217,14 +216,14 @@ function _config_swift_apache_wsgi {
217 217
             s/%SERVICECONF%/container-server\/${node_number}.conf/g;
218 218
         " -i ${SWIFT_APACHE_WSGI_DIR}/container-server-${node_number}.wsgi
219 219
 
220
-        sudo cp ${SWIFT_DIR}/examples/apache2/account-server.template ${apache_vhost_dir}/account-server-${node_number}
220
+        sudo cp ${SWIFT_DIR}/examples/apache2/account-server.template $(apache_site_config_for account-server-${node_number})
221 221
         sudo sed -e "
222 222
             /^#/d;/^$/d;
223 223
             s/%PORT%/$account_port/g;
224 224
             s/%SERVICENAME%/account-server-${node_number}/g;
225 225
             s/%APACHE_NAME%/${APACHE_NAME}/g;
226 226
             s/%USER%/${STACK_USER}/g;
227
-        " -i ${apache_vhost_dir}/account-server-${node_number}
227
+        " -i $(apache_site_config_for account-server-${node_number})
228 228
         enable_apache_site account-server-${node_number}
229 229
 
230 230
         sudo cp ${SWIFT_DIR}/examples/wsgi/account-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/account-server-${node_number}.wsgi