blob: 3d8b3e6d1f0c77fd505b8ed5d9be19de5322991b [file] [log] [blame]
Sean Dagueb562e6a2012-11-19 16:00:01 -05001# lib/horizon
2# Functions to control the configuration and operation of the horizon service
3# <do not include this template file in ``stack.sh``!>
4
5# Dependencies:
6# ``functions`` file
7# ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
8# <list other global vars that are assumed to be defined>
9
10# ``stack.sh`` calls the entry points in this order:
11#
12# install_horizon
13# configure_horizon
14# init_horizon
15# start_horizon
16# stop_horizon
17# cleanup_horizon
18
19# Save trace setting
20XTRACE=$(set +o | grep xtrace)
21set +o xtrace
22
23
24# Defaults
25# --------
26
27# <define global variables here that belong to this project>
28
29# Set up default directories
30HORIZON_DIR=$DEST/horizon
31
Akihiro MOTOKI7104ab42013-03-27 19:47:11 +090032# local_settings.py is used to customize Dashboard settings.
33# The example file in Horizon repo is used by default.
34HORIZON_SETTINGS=${HORIZON_SETTINGS:-$HORIZON_DIR/openstack_dashboard/local/local_settings.py.example}
35
Martin Vidner2ed63f42012-12-04 10:33:49 +010036# Allow overriding the default Apache user and group, default to
37# current user and his default group.
Sean Dagueb562e6a2012-11-19 16:00:01 -050038APACHE_USER=${APACHE_USER:-$USER}
Martin Vidner2ed63f42012-12-04 10:33:49 +010039APACHE_GROUP=${APACHE_GROUP:-$(id -gn $APACHE_USER)}
Sean Dagueb562e6a2012-11-19 16:00:01 -050040
Dean Troyercc6b4432013-04-08 15:38:03 -050041
42# Functions
43# ---------
44
Eugene Nikanorovb663b332013-03-07 16:10:10 +040045# utility method of setting python option
46function _horizon_config_set() {
47 local file=$1
48 local section=$2
49 local option=$3
50 local value=$4
51
52 if grep -q "^$section" $file; then
53 line=$(sed -ne "/^$section/,/^}/ { /^ *'$option':/ p; }" $file)
54 if [ -n "$line" ]; then
55 sed -i -e "/^$section/,/^}/ s/^\( *'$option'\) *:.*$/\1: $value,/" $file
56 else
57 sed -i -e "/^$section/ a\n '$option': $value,\n" $file
58 fi
59 else
60 echo -e "\n\n$section = {\n '$option': $value,\n}" >> $file
61 fi
62}
Sean Dagueb562e6a2012-11-19 16:00:01 -050063
Ian Wienandad43b392013-04-11 11:13:09 +100064# Basic install of upstream nodejs for platforms that want it
65function install_nodejs() {
66 if [[ $(which node) ]]; then
67 echo "You already appear to have nodejs, skipping install"
68 return
69 fi
70
71 # There are several node deployment scripts; one may be more
72 # appropriate at some future point, but for now direct download is
73 # the simplest way. The version barely matters for lesscss which
74 # doesn't use anything fancy.
75 local ver=0.10.1
76 local nodejs=node-v${ver}-linux-x64
77 local tar=$nodejs.tar.gz
78 local nodejs_url=http://nodejs.org/dist/v${ver}/${tar}
79
80 curl -Ss ${nodejs_url} | tar -C ${DEST} -xz
81 if [ $? -ne 0 ]; then
82 echo "*** Download of nodejs failed"
83 return 1
84 fi
85
86 # /usr/bin so it gets found in the PATH available to horizon
87 sudo ln -s $DEST/$nodejs/bin/node /usr/bin/node
88}
89
Sean Dagueb562e6a2012-11-19 16:00:01 -050090# Entry Points
91# ------------
92
93# cleanup_horizon() - Remove residual data files, anything left over from previous
94# runs that a clean run would need to clean up
95function cleanup_horizon() {
Ian Wienandad43b392013-04-11 11:13:09 +100096
97 if [[ is_fedora && $DISTRO =~ (rhel6) ]]; then
98 # if the /usr/bin/node link looks like it's pointing into $DEST,
99 # then we installed it via install_nodejs
100 if [[ $(readlink -f /usr/bin/node) =~ ($DEST) ]]; then
101 sudo rm /usr/bin/node
102 fi
103 fi
104
Sean Dagueb562e6a2012-11-19 16:00:01 -0500105}
106
107# configure_horizon() - Set config files, create data dirs, etc
108function configure_horizon() {
109 setup_develop $HORIZON_DIR
110}
111
112# init_horizon() - Initialize databases, etc.
113function init_horizon() {
114 # Remove stale session database.
115 rm -f $HORIZON_DIR/openstack_dashboard/local/dashboard_openstack.sqlite3
116
117 # ``local_settings.py`` is used to override horizon default settings.
118 local_settings=$HORIZON_DIR/openstack_dashboard/local/local_settings.py
Akihiro MOTOKI7104ab42013-03-27 19:47:11 +0900119 cp $HORIZON_SETTINGS $local_settings
Sean Dagueb562e6a2012-11-19 16:00:01 -0500120
Eugene Nikanorovb663b332013-03-07 16:10:10 +0400121 # enable loadbalancer dashboard in case service is enabled
122 if is_service_enabled q-lbaas; then
123 _horizon_config_set $local_settings OPENSTACK_QUANTUM_NETWORK enable_lb True
124 fi
125
Sean Dagueb562e6a2012-11-19 16:00:01 -0500126 # Initialize the horizon database (it stores sessions and notices shown to
127 # users). The user system is external (keystone).
128 cd $HORIZON_DIR
129 python manage.py syncdb --noinput
130 cd $TOP_DIR
131
132 # Create an empty directory that apache uses as docroot
133 sudo mkdir -p $HORIZON_DIR/.blackhole
134
135
Sunil Thaha627d9c72013-04-10 14:11:44 +1000136 HORIZON_REQUIRE=''
Vincent Untzc18b9652012-12-04 12:36:34 +0100137 if is_ubuntu; then
Sean Dagueb562e6a2012-11-19 16:00:01 -0500138 APACHE_NAME=apache2
139 APACHE_CONF=sites-available/horizon
140 # Clean up the old config name
141 sudo rm -f /etc/apache2/sites-enabled/000-default
142 # Be a good citizen and use the distro tools here
143 sudo touch /etc/$APACHE_NAME/$APACHE_CONF
144 sudo a2ensite horizon
Vincent Untzf2a18c02012-12-04 18:34:25 +0100145 # WSGI isn't enabled by default, enable it
Sean Daguee1864c32012-11-29 14:20:34 -0500146 sudo a2enmod wsgi
Vincent Untz00011c02012-12-06 09:56:32 +0100147 elif is_fedora; then
148 APACHE_NAME=httpd
149 APACHE_CONF=conf.d/horizon.conf
Sunil Thaha627d9c72013-04-10 14:11:44 +1000150
151 if [[ "$os_RELEASE" -ge "18" ]]; then
152 # fedora 18 has Require all denied in its httpd.conf
153 # and requires explicit Require all granted
154 HORIZON_REQUIRE='Require all granted'
155 fi
Vincent Untz00011c02012-12-06 09:56:32 +0100156 sudo sed '/^Listen/s/^.*$/Listen 0.0.0.0:80/' -i /etc/httpd/conf/httpd.conf
157 elif is_suse; then
158 APACHE_NAME=apache2
159 APACHE_CONF=vhosts.d/horizon.conf
Vincent Untzf2a18c02012-12-04 18:34:25 +0100160 # WSGI isn't enabled by default, enable it
161 sudo a2enmod wsgi
Sean Dagueb562e6a2012-11-19 16:00:01 -0500162 else
Vincent Untz00011c02012-12-06 09:56:32 +0100163 exit_distro_not_supported "apache configuration"
Sean Dagueb562e6a2012-11-19 16:00:01 -0500164 fi
165
166 # Configure apache to run horizon
167 sudo sh -c "sed -e \"
168 s,%USER%,$APACHE_USER,g;
169 s,%GROUP%,$APACHE_GROUP,g;
170 s,%HORIZON_DIR%,$HORIZON_DIR,g;
171 s,%APACHE_NAME%,$APACHE_NAME,g;
172 s,%DEST%,$DEST,g;
Sunil Thaha627d9c72013-04-10 14:11:44 +1000173 s,%HORIZON_REQUIRE%,$HORIZON_REQUIRE,g;
Sean Dagueb562e6a2012-11-19 16:00:01 -0500174 \" $FILES/apache-horizon.template >/etc/$APACHE_NAME/$APACHE_CONF"
175
176}
177
178# install_horizon() - Collect source and prepare
179function install_horizon() {
180 # Apache installation, because we mark it NOPRIME
Vincent Untzc18b9652012-12-04 12:36:34 +0100181 if is_ubuntu; then
Sean Dagueb562e6a2012-11-19 16:00:01 -0500182 # Install apache2, which is NOPRIME'd
183 install_package apache2 libapache2-mod-wsgi
Vincent Untz00011c02012-12-06 09:56:32 +0100184 elif is_fedora; then
185 sudo rm -f /etc/httpd/conf.d/000-*
186 install_package httpd mod_wsgi
Vincent Untzca5c4712012-11-21 17:45:49 +0100187 elif is_suse; then
188 install_package apache2 apache2-mod_wsgi
Sean Dagueb562e6a2012-11-19 16:00:01 -0500189 else
Vincent Untz00011c02012-12-06 09:56:32 +0100190 exit_distro_not_supported "apache installation"
Sean Dagueb562e6a2012-11-19 16:00:01 -0500191 fi
192
Ian Wienandad43b392013-04-11 11:13:09 +1000193 if [[ is_fedora && $DISTRO =~ (rhel6) ]]; then
194 # RHEL6 currently has no native way to get nodejs, so we do a
195 # basic install here (see cleanup_horizon too).
196 # TODO: does nova have a better way that we can limit
197 # requirement of site-wide nodejs install?
198 install_nodejs
199 fi
200
Sean Dagueb562e6a2012-11-19 16:00:01 -0500201 # NOTE(sdague) quantal changed the name of the node binary
Vincent Untzc18b9652012-12-04 12:36:34 +0100202 if is_ubuntu; then
Sean Dagueb562e6a2012-11-19 16:00:01 -0500203 if [[ ! -e "/usr/bin/node" ]]; then
204 install_package nodejs-legacy
205 fi
Sunil Thaha627d9c72013-04-10 14:11:44 +1000206 elif is_fedora && [[ "$os_RELEASE" -ge "18" ]]; then
207 # fedora 18 and higher gets nodejs
208 install_package nodejs
Sean Dagueb562e6a2012-11-19 16:00:01 -0500209 fi
210
211 git_clone $HORIZON_REPO $HORIZON_DIR $HORIZON_BRANCH $HORIZON_TAG
212}
213
214# start_horizon() - Start running processes, including screen
215function start_horizon() {
216 restart_service $APACHE_NAME
217 screen_it horizon "cd $HORIZON_DIR && sudo tail -f /var/log/$APACHE_NAME/horizon_error.log"
218}
219
220# stop_horizon() - Stop running processes (non-screen)
221function stop_horizon() {
Steven Dake532908f2013-01-14 11:35:17 -0700222 if is_ubuntu; then
223 stop_service apache2
224 elif is_fedora; then
225 stop_service httpd
226 elif is_suse; then
227 stop_service apache2
228 else
229 exit_distro_not_supported "apache configuration"
230 fi
Sean Dagueb562e6a2012-11-19 16:00:01 -0500231}
232
233# Restore xtrace
234$XTRACE
Sean Dague584d90e2013-03-29 14:34:53 -0400235
236# Local variables:
237# mode: shell-script
238# End: