Browse code

enable apache2 server as front end for swift

1.install apache and wsgi module
2.config apache2 vhost and wsgi files for proxy,
account, container and object server.
3.refactor apache functions from horizon and swift
into lib/apache

Change-Id: I3a5d1e511c5dca1e6d01a1adca8fda0a43d4f632
Implements: blueprint enable-apache-frontend-for-swift

zhang-hare authored on 2013/06/21 19:18:02
Showing 6 changed files
... ...
@@ -83,6 +83,13 @@ Example (Qpid):
83 83
 
84 84
     ENABLED_SERVICES="$ENABLED_SERVICES,-rabbit,-zeromq,qpid"
85 85
 
86
+# Apache Frontend
87
+
88
+Apache web server is enabled for wsgi services by setting `APACHE_ENABLED_SERVICES` in your localrc. But remember to enable these services at first as above.
89
+
90
+Example:
91
+    APACHE_ENABLED_SERVICES+=keystone,swift
92
+
86 93
 # Swift
87 94
 
88 95
 Swift is disabled by default.  When enabled, it is configured with
89 96
new file mode 100644
... ...
@@ -0,0 +1,118 @@
0
+# lib/apache
1
+# Functions to control configuration and operation of apache web server
2
+
3
+# Dependencies:
4
+# ``functions`` file
5
+# is_apache_enabled_service
6
+# change_apache_user_group
7
+# install_apache_wsgi
8
+# config_apache_wsgi
9
+# start_apache_server
10
+# stop_apache_server
11
+# restart_apache_server
12
+
13
+# Save trace setting
14
+XTRACE=$(set +o | grep xtrace)
15
+set +o xtrace
16
+
17
+# Allow overriding the default Apache user and group, default to
18
+# current user and his default group.
19
+APACHE_USER=${APACHE_USER:-$USER}
20
+APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
21
+
22
+
23
+# Set up apache name and configuration directory
24
+if is_ubuntu; then
25
+    APACHE_NAME=apache2
26
+    APACHE_CONF_DIR=sites-available
27
+elif is_fedora; then
28
+    APACHE_NAME=httpd
29
+    APACHE_CONF_DIR=conf.d
30
+elif is_suse; then
31
+    APACHE_NAME=apache2
32
+    APACHE_CONF_DIR=vhosts.d
33
+fi
34
+
35
+# Functions
36
+# ---------
37
+
38
+# is_apache_enabled_service() checks if the service(s) specified as arguments are
39
+# apache enabled by the user in ``APACHE_ENABLED_SERVICES`` as web front end.
40
+#
41
+# Multiple services specified as arguments are ``OR``'ed together; the test
42
+# is a short-circuit boolean, i.e it returns on the first match.
43
+#
44
+# Uses global ``APACHE_ENABLED_SERVICES``
45
+# APACHE_ENABLED_SERVICES service [service ...]
46
+function is_apache_enabled_service() {
47
+    services=$@
48
+    for service in ${services}; do
49
+        [[ ,${APACHE_ENABLED_SERVICES}, =~ ,${service}, ]] && return 0
50
+    done
51
+    return 1
52
+}
53
+
54
+# change_apache_user_group() - Change the User/Group to run Apache server
55
+function change_apache_user_group(){
56
+    local stack_user=$@
57
+    if is_ubuntu; then
58
+        sudo sed -e "
59
+            s/^export APACHE_RUN_USER=.*/export APACHE_RUN_USER=${stack_user}/g;
60
+            s/^export APACHE_RUN_GROUP=.*/export APACHE_RUN_GROUP=${stack_user}/g
61
+        " -i /etc/${APACHE_NAME}/envvars
62
+    elif is_fedora; then
63
+        sudo sed -e "
64
+            s/^User .*/User ${stack_user}/g;
65
+            s/^Group .*/Group ${stack_user}/g
66
+        " -i /etc/${APACHE_NAME}/httpd.conf
67
+    elif is_suse; then
68
+        sudo sed -e "
69
+            s/^User .*/User ${stack_user}/g;
70
+            s/^Group .*/Group ${stack_user}/g
71
+        " -i /etc/${APACHE_NAME}/uid.conf
72
+    else
73
+        exit_distro_not_supported "apache user and group"
74
+    fi
75
+}
76
+
77
+# install_apache_wsgi() - Install Apache server and wsgi module
78
+function install_apache_wsgi() {
79
+    # Apache installation, because we mark it NOPRIME
80
+    if is_ubuntu; then
81
+        # Install apache2, which is NOPRIME'd
82
+        install_package apache2 libapache2-mod-wsgi
83
+    elif is_fedora; then
84
+        sudo rm -f /etc/httpd/conf.d/000-*
85
+        install_package httpd mod_wsgi
86
+    elif is_suse; then
87
+        install_package apache2 apache2-mod_wsgi
88
+    else
89
+        exit_distro_not_supported "apache installation"
90
+    fi
91
+}
92
+
93
+# start_apache_server() - Start running apache server
94
+function start_apache_server() {
95
+    start_service $APACHE_NAME
96
+}
97
+
98
+# stop_apache_server() - Stop running apache server
99
+function stop_apache_server() {
100
+    if [ -n "$APACHE_NAME" ]; then
101
+        stop_service $APACHE_NAME
102
+    else
103
+        exit_distro_not_supported "apache configuration"
104
+    fi
105
+}
106
+
107
+# restart_apache_server
108
+function restart_apache_server() {
109
+    restart_service $APACHE_NAME
110
+}
111
+
112
+# Restore xtrace
113
+$XTRACE
114
+
115
+# Local variables:
116
+# mode: shell-script
117
+# End:
... ...
@@ -4,6 +4,7 @@
4 4
 
5 5
 # Dependencies:
6 6
 # ``functions`` file
7
+# ``apache`` file
7 8
 # ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
8 9
 # <list other global vars that are assumed to be defined>
9 10
 
... ...
@@ -33,23 +34,6 @@ HORIZON_DIR=$DEST/horizon
33 33
 # The example file in Horizon repo is used by default.
34 34
 HORIZON_SETTINGS=${HORIZON_SETTINGS:-$HORIZON_DIR/openstack_dashboard/local/local_settings.py.example}
35 35
 
36
-# Allow overriding the default Apache user and group, default to
37
-# current user and his default group.
38
-APACHE_USER=${APACHE_USER:-$USER}
39
-APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
40
-
41
-# Set up service name and configuration path
42
-if is_ubuntu; then
43
-    APACHE_NAME=apache2
44
-    APACHE_CONF=sites-available/horizon
45
-elif is_fedora; then
46
-    APACHE_NAME=httpd
47
-    APACHE_CONF=conf.d/horizon.conf
48
-elif is_suse; then
49
-    APACHE_NAME=apache2
50
-    APACHE_CONF=vhosts.d/horizon.conf
51
-fi
52
-
53 36
 
54 37
 # Functions
55 38
 # ---------
... ...
@@ -119,11 +103,12 @@ function init_horizon() {
119 119
     sudo mkdir -p $HORIZON_DIR/.blackhole
120 120
 
121 121
     HORIZON_REQUIRE=''
122
+    local horizon_conf=/etc/$APACHE_NAME/$APACHE_CONF_DIR/horizon
122 123
     if is_ubuntu; then
123 124
         # Clean up the old config name
124 125
         sudo rm -f /etc/apache2/sites-enabled/000-default
125 126
         # Be a good citizen and use the distro tools here
126
-        sudo touch /etc/$APACHE_NAME/$APACHE_CONF
127
+        sudo touch $horizon_conf
127 128
         sudo a2ensite horizon
128 129
         # WSGI isn't enabled by default, enable it
129 130
         sudo a2enmod wsgi
... ...
@@ -153,23 +138,13 @@ function init_horizon() {
153 153
         s,%APACHE_NAME%,$APACHE_NAME,g;
154 154
         s,%DEST%,$DEST,g;
155 155
         s,%HORIZON_REQUIRE%,$HORIZON_REQUIRE,g;
156
-    \" $FILES/apache-horizon.template >/etc/$APACHE_NAME/$APACHE_CONF"
156
+    \" $FILES/apache-horizon.template >$horizon_conf"
157 157
 }
158 158
 
159 159
 # install_horizon() - Collect source and prepare
160 160
 function install_horizon() {
161 161
     # Apache installation, because we mark it NOPRIME
162
-    if is_ubuntu; then
163
-        # Install apache2, which is NOPRIME'd
164
-        install_package apache2 libapache2-mod-wsgi
165
-    elif is_fedora; then
166
-        sudo rm -f /etc/httpd/conf.d/000-*
167
-        install_package httpd mod_wsgi
168
-    elif is_suse; then
169
-        install_package apache2 apache2-mod_wsgi
170
-    else
171
-        exit_distro_not_supported "apache installation"
172
-    fi
162
+    install_apache_wsgi
173 163
 
174 164
     # NOTE(sdague) quantal changed the name of the node binary
175 165
     if is_ubuntu; then
... ...
@@ -185,17 +160,13 @@ function install_horizon() {
185 185
 
186 186
 # start_horizon() - Start running processes, including screen
187 187
 function start_horizon() {
188
-    restart_service $APACHE_NAME
188
+    restart_apache_server
189 189
     screen_it horizon "cd $HORIZON_DIR && sudo tail -f /var/log/$APACHE_NAME/horizon_error.log"
190 190
 }
191 191
 
192 192
 # stop_horizon() - Stop running processes (non-screen)
193 193
 function stop_horizon() {
194
-    if [ -n "$APACHE_NAME" ]; then
195
-        stop_service $APACHE_NAME
196
-    else
197
-        exit_distro_not_supported "apache configuration"
198
-    fi
194
+    stop_apache_server
199 195
 }
200 196
 
201 197
 
... ...
@@ -3,6 +3,7 @@
3 3
 
4 4
 # Dependencies:
5 5
 # ``functions`` file
6
+# ``apache`` file
6 7
 # ``DEST``, ``SCREEN_NAME``, `SWIFT_HASH` must be defined
7 8
 # ``STACK_USER`` must be defined
8 9
 # ``SWIFT_DATA_DIR`` or ``DATA_DIR`` must be defined
... ...
@@ -10,11 +11,13 @@
10 10
 # ``stack.sh`` calls the entry points in this order:
11 11
 #
12 12
 # install_swift
13
+# _config_swift_apache_wsgi
13 14
 # configure_swift
14 15
 # init_swift
15 16
 # start_swift
16 17
 # stop_swift
17 18
 # cleanup_swift
19
+# _cleanup_swift_apache_wsgi
18 20
 
19 21
 # Save trace setting
20 22
 XTRACE=$(set +o | grep xtrace)
... ...
@@ -28,6 +31,7 @@ set +o xtrace
28 28
 SWIFT_DIR=$DEST/swift
29 29
 SWIFTCLIENT_DIR=$DEST/python-swiftclient
30 30
 SWIFT_AUTH_CACHE_DIR=${SWIFT_AUTH_CACHE_DIR:-/var/cache/swift}
31
+SWIFT_APACHE_WSGI_DIR=${SWIFT_APACHE_WSGI_DIR:-/var/www/swift}
31 32
 SWIFT3_DIR=$DEST/swift3
32 33
 
33 34
 # TODO: add logging to different location.
... ...
@@ -97,6 +101,103 @@ function cleanup_swift() {
97 97
       rm ${SWIFT_DATA_DIR}/drives/images/swift.img
98 98
    fi
99 99
    rm -rf ${SWIFT_DATA_DIR}/run/
100
+   if is_apache_enabled_service swift; then
101
+       _cleanup_swift_apache_wsgi
102
+   fi
103
+}
104
+
105
+# _cleanup_swift_apache_wsgi() - Remove wsgi files, disable and remove apache vhost file
106
+function _cleanup_swift_apache_wsgi() {
107
+    sudo rm -f $SWIFT_APACHE_WSGI_DIR/*.wsgi
108
+    ! is_fedora && sudo a2dissite proxy-server
109
+    for node_number in ${SWIFT_REPLICAS_SEQ}; do
110
+        for type in object container account; do
111
+            site_name=${type}-server-${node_number}
112
+            ! is_fedora && sudo a2dissite ${site_name}
113
+            sudo rm -f /etc/$APACHE_NAME/$APACHE_CONF_DIR/${site_name}
114
+        done
115
+    done
116
+}
117
+
118
+# _config_swift_apache_wsgi() - Set WSGI config files of Swift
119
+function _config_swift_apache_wsgi() {
120
+    sudo mkdir -p ${SWIFT_APACHE_WSGI_DIR}
121
+    local apache_vhost_dir=/etc/${APACHE_NAME}/$APACHE_CONF_DIR
122
+    local proxy_port=${SWIFT_DEFAULT_BIND_PORT:-8080}
123
+
124
+    # copy proxy vhost and wsgi file
125
+    sudo cp ${SWIFT_DIR}/examples/apache2/proxy-server.template ${apache_vhost_dir}/proxy-server
126
+    sudo sed -e "
127
+        /^#/d;/^$/d;
128
+        s/%PORT%/$proxy_port/g;
129
+        s/%SERVICENAME%/proxy-server/g;
130
+        s/%APACHE_NAME%/${APACHE_NAME}/g;
131
+    " -i ${apache_vhost_dir}/proxy-server
132
+
133
+    sudo cp ${SWIFT_DIR}/examples/wsgi/proxy-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/proxy-server.wsgi
134
+    sudo sed -e "
135
+        /^#/d;/^$/d;
136
+        s/%SERVICECONF%/proxy-server.conf/g;
137
+    " -i ${SWIFT_APACHE_WSGI_DIR}/proxy-server.wsgi
138
+    ! is_fedora && sudo a2ensite proxy-server
139
+
140
+    # copy apache vhost file and set name and port
141
+    for node_number in ${SWIFT_REPLICAS_SEQ}; do
142
+        object_port=$[OBJECT_PORT_BASE + 10 * ($node_number - 1)]
143
+        container_port=$[CONTAINER_PORT_BASE + 10 * ($node_number - 1)]
144
+        account_port=$[ACCOUNT_PORT_BASE + 10 * ($node_number - 1)]
145
+
146
+        sudo cp ${SWIFT_DIR}/examples/apache2/object-server.template ${apache_vhost_dir}/object-server-${node_number}
147
+        sudo sed -e "
148
+            s/%PORT%/$object_port/g;
149
+            s/%SERVICENAME%/object-server-${node_number}/g;
150
+            s/%APACHE_NAME%/${APACHE_NAME}/g;
151
+        " -i ${apache_vhost_dir}/object-server-${node_number}
152
+        ! is_fedora && sudo a2ensite object-server-${node_number}
153
+
154
+        sudo cp ${SWIFT_DIR}/examples/wsgi/object-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/object-server-${node_number}.wsgi
155
+        sudo sed -e "
156
+            /^#/d;/^$/d;
157
+            s/%SERVICECONF%/object-server\/${node_number}.conf/g;
158
+        " -i ${SWIFT_APACHE_WSGI_DIR}/object-server-${node_number}.wsgi
159
+
160
+        sudo cp ${SWIFT_DIR}/examples/apache2/container-server.template ${apache_vhost_dir}/container-server-${node_number}
161
+        sudo sed -e "
162
+            /^#/d;/^$/d;
163
+            s/%PORT%/$container_port/g;
164
+            s/%SERVICENAME%/container-server-${node_number}/g;
165
+            s/%APACHE_NAME%/${APACHE_NAME}/g;
166
+        " -i ${apache_vhost_dir}/container-server-${node_number}
167
+        ! is_fedora && sudo a2ensite container-server-${node_number}
168
+
169
+        sudo cp ${SWIFT_DIR}/examples/wsgi/container-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/container-server-${node_number}.wsgi
170
+        sudo sed -e "
171
+            /^#/d;/^$/d;
172
+            s/%SERVICECONF%/container-server\/${node_number}.conf/g;
173
+        " -i ${SWIFT_APACHE_WSGI_DIR}/container-server-${node_number}.wsgi
174
+
175
+        sudo cp ${SWIFT_DIR}/examples/apache2/account-server.template ${apache_vhost_dir}/account-server-${node_number}
176
+        sudo sed -e "
177
+             /^#/d;/^$/d;
178
+            s/%PORT%/$account_port/g;
179
+            s/%SERVICENAME%/account-server-${node_number}/g;
180
+            s/%APACHE_NAME%/${APACHE_NAME}/g;
181
+        " -i ${apache_vhost_dir}/account-server-${node_number}
182
+        ! is_fedora && sudo a2ensite account-server-${node_number}
183
+
184
+        sudo cp ${SWIFT_DIR}/examples/wsgi/account-server.wsgi.template ${SWIFT_APACHE_WSGI_DIR}/account-server-${node_number}.wsgi
185
+        sudo sed -e "
186
+             /^#/d;/^$/d;
187
+            s/%SERVICECONF%/account-server\/${node_number}.conf/g;
188
+        " -i ${SWIFT_APACHE_WSGI_DIR}/account-server-${node_number}.wsgi
189
+
190
+    done
191
+
192
+    # run apache server as stack user
193
+    change_apache_user_group ${STACK_USER}
194
+
195
+    # WSGI isn't enabled by default, enable it
196
+    ! is_fedora && sudo a2enmod wsgi
100 197
 }
101 198
 
102 199
 # configure_swift() - Set config files, create data dirs and loop image
... ...
@@ -288,6 +389,9 @@ EOF
288 288
     sudo chown -R $USER:adm ${swift_log_dir}
289 289
     sed "s,%SWIFT_LOGDIR%,${swift_log_dir}," $FILES/swift/rsyslog.conf | sudo \
290 290
         tee /etc/rsyslog.d/10-swift.conf
291
+    if is_apache_enabled_service swift; then
292
+        _config_swift_apache_wsgi
293
+    fi
291 294
 }
292 295
 
293 296
 # create_swift_disk - Create Swift backing disk
... ...
@@ -423,6 +527,9 @@ function init_swift() {
423 423
 function install_swift() {
424 424
     git_clone $SWIFT_REPO $SWIFT_DIR $SWIFT_BRANCH
425 425
     setup_develop $SWIFT_DIR
426
+    if is_apache_enabled_service swift; then
427
+        install_apache_wsgi
428
+    fi
426 429
 }
427 430
 
428 431
 function install_swiftclient() {
... ...
@@ -444,6 +551,22 @@ function start_swift() {
444 444
         sudo systemctl start xinetd.service
445 445
     fi
446 446
 
447
+    if is_apache_enabled_service swift; then
448
+        # Make sure the apache lock dir is owned by $STACK_USER
449
+        # for running apache server to avoid failure of restarting
450
+        # apache server due to permission problem.
451
+        sudo chown -R $STACK_USER /var/run/lock/$APACHE_NAME
452
+        restart_apache_server
453
+        swift-init --run-dir=${SWIFT_DATA_DIR}/run rest start
454
+        screen_it s-proxy "cd $SWIFT_DIR && sudo tail -f /var/log/$APACHE_NAME/proxy-server"
455
+        if [[ ${SWIFT_REPLICAS} == 1 ]]; then
456
+            for type in object container account; do
457
+                screen_it s-${type} "cd $SWIFT_DIR && sudo tail -f /var/log/$APACHE_NAME/${type}-server-1"
458
+            done
459
+        fi
460
+        return 0
461
+    fi
462
+
447 463
    # By default with only one replica we are launching the proxy,
448 464
    # container, account and object server in screen in foreground and
449 465
    # other services in background. If we have SWIFT_REPLICAS set to something
... ...
@@ -460,7 +583,7 @@ function start_swift() {
460 460
    done
461 461
    screen_it s-proxy "cd $SWIFT_DIR && $SWIFT_DIR/bin/swift-proxy-server ${SWIFT_CONF_DIR}/proxy-server.conf -v"
462 462
    if [[ ${SWIFT_REPLICAS} == 1 ]]; then
463
-       for type in object container account;do
463
+       for type in object container account; do
464 464
            screen_it s-${type} "cd $SWIFT_DIR && $SWIFT_DIR/bin/swift-${type}-server ${SWIFT_CONF_DIR}/${type}-server/1.conf -v"
465 465
        done
466 466
    fi
... ...
@@ -468,6 +591,11 @@ function start_swift() {
468 468
 
469 469
 # stop_swift() - Stop running processes (non-screen)
470 470
 function stop_swift() {
471
+
472
+    if is_apache_enabled_service swift; then
473
+        swift-init --run-dir=${SWIFT_DATA_DIR}/run rest stop && return 0
474
+    fi
475
+
471 476
     # screen normally killed by unstack.sh
472 477
     if type -p swift-init >/dev/null; then
473 478
         swift-init --run-dir=${SWIFT_DATA_DIR}/run all stop || true
... ...
@@ -298,6 +298,7 @@ SERVICE_TIMEOUT=${SERVICE_TIMEOUT:-60}
298 298
 # ==================
299 299
 
300 300
 # Source project function libraries
301
+source $TOP_DIR/lib/apache
301 302
 source $TOP_DIR/lib/tls
302 303
 source $TOP_DIR/lib/horizon
303 304
 source $TOP_DIR/lib/keystone
... ...
@@ -24,6 +24,9 @@ source $TOP_DIR/stackrc
24 24
 # Destination path for service data
25 25
 DATA_DIR=${DATA_DIR:-${DEST}/data}
26 26
 
27
+# Import apache functions
28
+source $TOP_DIR/lib/apache
29
+
27 30
 # Get project function libraries
28 31
 source $TOP_DIR/lib/baremetal
29 32
 source $TOP_DIR/lib/cinder