blob: 3d2f68d09d90e6edd7f8e48957c5bd14e841c122 [file] [log] [blame]
Sean Daguee263c822014-12-05 14:25:28 -05001#!/bin/bash
2#
Sean Dagueb562e6a2012-11-19 16:00:01 -05003# lib/horizon
4# Functions to control the configuration and operation of the horizon service
Sean Dagueb562e6a2012-11-19 16:00:01 -05005
6# Dependencies:
Adam Spiers6a5aa7c2013-10-24 11:27:02 +01007#
8# - ``functions`` file
9# - ``apache`` file
10# - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
Sean Dagueb562e6a2012-11-19 16:00:01 -050011
12# ``stack.sh`` calls the entry points in this order:
13#
Adam Spiers6a5aa7c2013-10-24 11:27:02 +010014# - install_horizon
15# - configure_horizon
16# - init_horizon
17# - start_horizon
18# - stop_horizon
19# - cleanup_horizon
Sean Dagueb562e6a2012-11-19 16:00:01 -050020
21# Save trace setting
Ian Wienand523f4882015-10-13 11:03:03 +110022_XTRACE_HORIZON=$(set +o | grep xtrace)
Sean Dagueb562e6a2012-11-19 16:00:01 -050023set +o xtrace
24
25
26# Defaults
27# --------
28
Sean Dagueb562e6a2012-11-19 16:00:01 -050029# Set up default directories
Sean Dague3c8973a2014-11-14 09:31:02 -050030GITDIR["django_openstack_auth"]=$DEST/django_openstack_auth
31
Sean Dagueb562e6a2012-11-19 16:00:01 -050032HORIZON_DIR=$DEST/horizon
33
Akihiro MOTOKI7104ab42013-03-27 19:47:11 +090034# local_settings.py is used to customize Dashboard settings.
35# The example file in Horizon repo is used by default.
36HORIZON_SETTINGS=${HORIZON_SETTINGS:-$HORIZON_DIR/openstack_dashboard/local/local_settings.py.example}
37
Dean Troyercc6b4432013-04-08 15:38:03 -050038# Functions
39# ---------
40
Eugene Nikanorovb663b332013-03-07 16:10:10 +040041# utility method of setting python option
Ian Wienandaee18c72014-02-21 15:35:08 +110042function _horizon_config_set {
Eugene Nikanorovb663b332013-03-07 16:10:10 +040043 local file=$1
44 local section=$2
45 local option=$3
46 local value=$4
47
Rob Crittendenc31fa402014-03-17 00:07:52 -040048 if [ -z "$section" ]; then
49 sed -e "/^$option/d" -i $local_settings
50 echo -e "\n$option=$value" >> $file
51 elif grep -q "^$section" $file; then
Ian Wienandada886d2015-10-07 14:06:26 +110052 local line
53 line=$(sed -ne "/^$section/,/^}/ { /^ *'$option':/ p; }" $file)
Eugene Nikanorovb663b332013-03-07 16:10:10 +040054 if [ -n "$line" ]; then
55 sed -i -e "/^$section/,/^}/ s/^\( *'$option'\) *:.*$/\1: $value,/" $file
56 else
Ravi Chunduru95c93e22013-07-16 04:18:47 -070057 sed -i -e "/^$section/a\ '$option': $value," $file
Eugene Nikanorovb663b332013-03-07 16:10:10 +040058 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
Dean Troyer1a6d4492013-06-03 16:47:36 -050065
Sean Dagueb562e6a2012-11-19 16:00:01 -050066# Entry Points
67# ------------
68
69# cleanup_horizon() - Remove residual data files, anything left over from previous
70# runs that a clean run would need to clean up
Ian Wienandaee18c72014-02-21 15:35:08 +110071function cleanup_horizon {
Rob Crittenden5631ca52016-08-02 13:19:14 -040072 disable_apache_site horizon
73 sudo rm -f $(apache_site_config_for horizon)
Sean Dagueb562e6a2012-11-19 16:00:01 -050074}
75
76# configure_horizon() - Set config files, create data dirs, etc
Ian Wienandaee18c72014-02-21 15:35:08 +110077function configure_horizon {
Sean Dagueb562e6a2012-11-19 16:00:01 -050078 setup_develop $HORIZON_DIR
Akihiro Motoki6518c0b2014-10-15 17:26:59 +090079
80 # Compile message catalogs.
81 # Horizon is installed as develop mode, so we can compile here.
82 # Message catalog compilation is handled by Django admin script,
83 # so compiling them after the installation avoids Django installation twice.
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -050084 (cd $HORIZON_DIR; $PYTHON manage.py compilemessages)
Sean Dagueb562e6a2012-11-19 16:00:01 -050085
Sean Dagueb562e6a2012-11-19 16:00:01 -050086 # ``local_settings.py`` is used to override horizon default settings.
Dean Troyer1bbfcc72014-07-25 12:50:14 -050087 local local_settings=$HORIZON_DIR/openstack_dashboard/local/local_settings.py
Akihiro MOTOKI7104ab42013-03-27 19:47:11 +090088 cp $HORIZON_SETTINGS $local_settings
Sean Dagueb562e6a2012-11-19 16:00:01 -050089
David Lyle7b105c52015-07-27 17:14:32 -060090 _horizon_config_set $local_settings "" WEBROOT \"$HORIZON_APACHE_ROOT/\"
David Lyle7b105c52015-07-27 17:14:32 -060091
David Lyle45ce9822014-09-11 17:50:08 -060092 _horizon_config_set $local_settings "" COMPRESS_OFFLINE True
Attila Fazekasc4c27232015-03-03 09:54:49 +010093 _horizon_config_set $local_settings "" OPENSTACK_KEYSTONE_DEFAULT_ROLE \"Member\"
David Lyle45ce9822014-09-11 17:50:08 -060094
Rob Crittendenc31fa402014-03-17 00:07:52 -040095 _horizon_config_set $local_settings "" OPENSTACK_HOST \"${KEYSTONE_SERVICE_HOST}\"
Samuel de Medeiros Queiroz3fd71d62015-05-03 14:54:45 -030096
Jamie Lennox05076fb2015-08-15 19:01:59 +100097 _horizon_config_set $local_settings "" OPENSTACK_API_VERSIONS {\"identity\":3}
Brant Knudsone86b91b2016-05-03 15:21:47 -050098 _horizon_config_set $local_settings "" OPENSTACK_KEYSTONE_URL "\"${KEYSTONE_SERVICE_URI}/v3\""
Rob Crittendenc31fa402014-03-17 00:07:52 -040099
Tomasz Trębski868a6632016-11-14 09:10:57 +0100100 # note(trebskit): if HOST_IP points at non-localhost ip address, horizon cannot be accessed
101 # from outside the virtual machine. This fixes is meant primarily for local development
102 # purpose
103 _horizon_config_set $local_settings "" ALLOWED_HOSTS [\"*\"]
104
Rob Crittendenc31fa402014-03-17 00:07:52 -0400105 if [ -f $SSL_BUNDLE_FILE ]; then
106 _horizon_config_set $local_settings "" OPENSTACK_SSL_CACERT \"${SSL_BUNDLE_FILE}\"
107 fi
108
Leticia Wanderleycc363972017-06-26 23:52:52 -0300109 if is_service_enabled ldap; then
110 _horizon_config_set $local_settings "" OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT "True"
111 fi
112
Sean Dagueb562e6a2012-11-19 16:00:01 -0500113 # Create an empty directory that apache uses as docroot
114 sudo mkdir -p $HORIZON_DIR/.blackhole
115
Ian Wienandada886d2015-10-07 14:06:26 +1100116 local horizon_conf
117 horizon_conf=$(apache_site_config_for horizon)
JordanP1e4587e2013-05-08 22:19:59 +0200118
Sean Dagueb562e6a2012-11-19 16:00:01 -0500119 # Configure apache to run horizon
Akihiro Motoki43f62c02015-12-15 16:44:41 +0900120 # Set up the django horizon application to serve via apache/wsgi
Sean Dagueb562e6a2012-11-19 16:00:01 -0500121 sudo sh -c "sed -e \"
122 s,%USER%,$APACHE_USER,g;
123 s,%GROUP%,$APACHE_GROUP,g;
124 s,%HORIZON_DIR%,$HORIZON_DIR,g;
125 s,%APACHE_NAME%,$APACHE_NAME,g;
126 s,%DEST%,$DEST,g;
David Lyle7b105c52015-07-27 17:14:32 -0600127 s,%WEBROOT%,$HORIZON_APACHE_ROOT,g;
zhang-hared98a5d02013-06-21 18:18:02 +0800128 \" $FILES/apache-horizon.template >$horizon_conf"
Attila Fazekasafda4ef2014-06-03 16:53:03 +0200129
130 if is_ubuntu; then
131 disable_apache_site 000-default
132 sudo touch $horizon_conf
Dan Kolb5c4691a2016-11-16 13:47:49 -0600133 elif is_fedora || is_suse; then
Attila Fazekasafda4ef2014-06-03 16:53:03 +0200134 : # nothing to do
135 else
136 exit_distro_not_supported "horizon apache configuration"
137 fi
138 enable_apache_site horizon
Akihiro Motoki43f62c02015-12-15 16:44:41 +0900139}
Attila Fazekasafda4ef2014-06-03 16:53:03 +0200140
Akihiro Motoki43f62c02015-12-15 16:44:41 +0900141# init_horizon() - Initialize databases, etc.
142function init_horizon {
Dean Troyerdc97cb72015-03-28 08:20:50 -0500143 # Remove old log files that could mess with how DevStack detects whether Horizon
Attila Fazekasafda4ef2014-06-03 16:53:03 +0200144 # has been successfully started (see start_horizon() and functions::screen_it())
Chris Dent2f27a0e2014-09-09 13:46:02 +0100145 # and run_process
Attila Fazekasafda4ef2014-06-03 16:53:03 +0200146 sudo rm -f /var/log/$APACHE_NAME/horizon_*
147
Mikhail S Medvedev088e6602014-11-18 12:11:26 -0600148 # Setup alias for django-admin which could be different depending on distro
149 local django_admin
150 if type -p django-admin > /dev/null; then
151 django_admin=django-admin
152 else
153 django_admin=django-admin.py
154 fi
155
Akihiro Motoki43f62c02015-12-15 16:44:41 +0900156 # These need to be run after horizon plugins are configured.
Mikhail S Medvedev088e6602014-11-18 12:11:26 -0600157 DJANGO_SETTINGS_MODULE=openstack_dashboard.settings $django_admin collectstatic --noinput
158 DJANGO_SETTINGS_MODULE=openstack_dashboard.settings $django_admin compress --force
David Lyle45ce9822014-09-11 17:50:08 -0600159
Sean Dagueb562e6a2012-11-19 16:00:01 -0500160}
161
Zhenguo Niue385d1e2014-03-12 16:58:12 +0800162# install_django_openstack_auth() - Collect source and prepare
163function install_django_openstack_auth {
Sean Dague3c8973a2014-11-14 09:31:02 -0500164 if use_library_from_git "django_openstack_auth"; then
165 local dir=${GITDIR["django_openstack_auth"]}
166 git_clone_by_name "django_openstack_auth"
167 # Compile message catalogs before installation
168 _prepare_message_catalog_compilation
Davanum Srinivas51ecf0a2017-01-05 16:11:17 -0500169 (cd $dir; $PYTHON setup.py compile_catalog)
Sean Dague3c8973a2014-11-14 09:31:02 -0500170 setup_dev_lib "django_openstack_auth"
171 fi
172 # if we aren't using this library from git, then we just let it
173 # get dragged in by the horizon setup.
Zhenguo Niue385d1e2014-03-12 16:58:12 +0800174}
175
Sean Dagueb562e6a2012-11-19 16:00:01 -0500176# install_horizon() - Collect source and prepare
Ian Wienandaee18c72014-02-21 15:35:08 +1100177function install_horizon {
Sean Dagueb562e6a2012-11-19 16:00:01 -0500178 # Apache installation, because we mark it NOPRIME
zhang-hared98a5d02013-06-21 18:18:02 +0800179 install_apache_wsgi
Sean Dagueb562e6a2012-11-19 16:00:01 -0500180
Sean Dague53753292014-12-04 19:38:15 -0500181 git_clone $HORIZON_REPO $HORIZON_DIR $HORIZON_BRANCH
Sean Dagueb562e6a2012-11-19 16:00:01 -0500182}
183
Sean Dague0eebeb42017-08-30 14:16:58 -0400184# start_horizon() - Start running processes
Ian Wienandaee18c72014-02-21 15:35:08 +1100185function start_horizon {
zhang-hared98a5d02013-06-21 18:18:02 +0800186 restart_apache_server
Sean Dagueb562e6a2012-11-19 16:00:01 -0500187}
188
Sean Dague0eebeb42017-08-30 14:16:58 -0400189# stop_horizon() - Stop running processes
Ian Wienandaee18c72014-02-21 15:35:08 +1100190function stop_horizon {
zhang-hared98a5d02013-06-21 18:18:02 +0800191 stop_apache_server
Sean Dagueb562e6a2012-11-19 16:00:01 -0500192}
193
Akihiro Motoki6518c0b2014-10-15 17:26:59 +0900194# NOTE: It can be moved to common functions, but it is only used by compilation
195# of django_openstack_auth catalogs at the moment.
196function _prepare_message_catalog_compilation {
Sean Dague60996b12015-04-08 09:06:49 -0400197 pip_install_gr Babel
Akihiro Motoki6518c0b2014-10-15 17:26:59 +0900198}
199
Dean Troyer1a6d4492013-06-03 16:47:36 -0500200
Sean Dagueb562e6a2012-11-19 16:00:01 -0500201# Restore xtrace
Ian Wienand523f4882015-10-13 11:03:03 +1100202$_XTRACE_HORIZON
Sean Dague584d90e2013-03-29 14:34:53 -0400203
Adam Spiers6a5aa7c2013-10-24 11:27:02 +0100204# Tell emacs to use shell-script-mode
205## Local variables:
206## mode: shell-script
207## End: