lib/horizon
e263c82e
 #!/bin/bash
 #
b562e6a7
 # lib/horizon
 # Functions to control the configuration and operation of the horizon service
 
 # Dependencies:
6a5aa7c6
 #
 # - ``functions`` file
 # - ``apache`` file
 # - ``SERVICE_{TENANT_NAME|PASSWORD}`` must be defined
b562e6a7
 
 # ``stack.sh`` calls the entry points in this order:
 #
6a5aa7c6
 # - install_horizon
 # - configure_horizon
 # - init_horizon
 # - start_horizon
 # - stop_horizon
 # - cleanup_horizon
b562e6a7
 
 # Save trace setting
 XTRACE=$(set +o | grep xtrace)
 set +o xtrace
 
 
 # Defaults
 # --------
 
 # Set up default directories
3c8973a9
 GITDIR["django_openstack_auth"]=$DEST/django_openstack_auth
 
b562e6a7
 HORIZON_DIR=$DEST/horizon
 
7104ab40
 # local_settings.py is used to customize Dashboard settings.
 # The example file in Horizon repo is used by default.
 HORIZON_SETTINGS=${HORIZON_SETTINGS:-$HORIZON_DIR/openstack_dashboard/local/local_settings.py.example}
 
cc6b4435
 # Functions
 # ---------
 
b663b33f
 # utility method of setting python option
aee18c74
 function _horizon_config_set {
b663b33f
     local file=$1
     local section=$2
     local option=$3
     local value=$4
 
c31fa40b
     if [ -z "$section" ]; then
         sed -e "/^$option/d" -i $local_settings
         echo -e "\n$option=$value" >> $file
     elif grep -q "^$section" $file; then
1bbfcc7a
         local line=$(sed -ne "/^$section/,/^}/ { /^ *'$option':/ p; }" $file)
b663b33f
         if [ -n "$line" ]; then
             sed -i -e "/^$section/,/^}/ s/^\( *'$option'\) *:.*$/\1: $value,/" $file
         else
95c93e2b
             sed -i -e "/^$section/a\    '$option': $value," $file
b663b33f
         fi
     else
         echo -e "\n\n$section = {\n    '$option': $value,\n}" >> $file
     fi
 }
b562e6a7
 
ad43b395
 
1a6d4492
 
b562e6a7
 # Entry Points
 # ------------
 
 # cleanup_horizon() - Remove residual data files, anything left over from previous
 # runs that a clean run would need to clean up
aee18c74
 function cleanup_horizon {
444a8d53
     local horizon_conf=$(apache_site_config_for horizon)
     sudo rm -f $horizon_conf
b562e6a7
 }
 
 # configure_horizon() - Set config files, create data dirs, etc
aee18c74
 function configure_horizon {
b562e6a7
     setup_develop $HORIZON_DIR
6518c0b8
 
     # Compile message catalogs.
     # Horizon is installed as develop mode, so we can compile here.
     # Message catalog compilation is handled by Django admin script,
     # so compiling them after the installation avoids Django installation twice.
3c8973a9
     (cd $HORIZON_DIR; ./run_tests.sh -N --compilemessages)
b562e6a7
 }
 
 # init_horizon() - Initialize databases, etc.
aee18c74
 function init_horizon {
b562e6a7
     # ``local_settings.py`` is used to override horizon default settings.
1bbfcc7a
     local local_settings=$HORIZON_DIR/openstack_dashboard/local/local_settings.py
7104ab40
     cp $HORIZON_SETTINGS $local_settings
b562e6a7
 
7b105c57
     _horizon_config_set $local_settings "" WEBROOT \"$HORIZON_APACHE_ROOT/\"
     _horizon_config_set $local_settings "" CUSTOM_THEME_PATH \"themes/webroot\"
 
45ce9827
     _horizon_config_set $local_settings "" COMPRESS_OFFLINE True
c4c27232
     _horizon_config_set $local_settings "" OPENSTACK_KEYSTONE_DEFAULT_ROLE \"Member\"
45ce9827
 
c31fa40b
     _horizon_config_set $local_settings "" OPENSTACK_HOST \"${KEYSTONE_SERVICE_HOST}\"
3fd71d68
 
     if [ "$ENABLE_IDENTITY_V2" == "False" ]; then
         # Only Identity v3 API is available; then use it with v3 auth tokens
7ef24649
         _horizon_config_set $local_settings "" OPENSTACK_API_VERSIONS {\"identity\":3}
3fd71d68
         _horizon_config_set $local_settings "" OPENSTACK_KEYSTONE_URL "\"${KEYSTONE_SERVICE_PROTOCOL}://${KEYSTONE_SERVICE_HOST}:${KEYSTONE_SERVICE_PORT}/v3\""
     else
         _horizon_config_set $local_settings "" OPENSTACK_KEYSTONE_URL "\"${KEYSTONE_SERVICE_PROTOCOL}://${KEYSTONE_SERVICE_HOST}:${KEYSTONE_SERVICE_PORT}/v2.0\""
     fi
c31fa40b
 
     if [ -f $SSL_BUNDLE_FILE ]; then
         _horizon_config_set $local_settings "" OPENSTACK_SSL_CACERT \"${SSL_BUNDLE_FILE}\"
     fi
 
b562e6a7
     # Create an empty directory that apache uses as docroot
     sudo mkdir -p $HORIZON_DIR/.blackhole
 
a688bc65
     local horizon_conf=$(apache_site_config_for horizon)
afda4efb
 
     # Configure apache to run horizon
     sudo sh -c "sed -e \"
         s,%USER%,$APACHE_USER,g;
         s,%GROUP%,$APACHE_GROUP,g;
         s,%HORIZON_DIR%,$HORIZON_DIR,g;
         s,%APACHE_NAME%,$APACHE_NAME,g;
         s,%DEST%,$DEST,g;
7b105c57
         s,%WEBROOT%,$HORIZON_APACHE_ROOT,g;
afda4efb
     \" $FILES/apache-horizon.template >$horizon_conf"
 
c18b9651
     if is_ubuntu; then
cbd97ca9
         disable_apache_site 000-default
d98a5d0a
         sudo touch $horizon_conf
00011c08
     elif is_fedora; then
         sudo sed '/^Listen/s/^.*$/Listen 0.0.0.0:80/' -i /etc/httpd/conf/httpd.conf
3ac8612b
     elif is_suse; then
         : # nothing to do
b562e6a7
     else
15aa0fc3
         exit_distro_not_supported "horizon apache configuration"
b562e6a7
     fi
afda4efb
     enable_apache_site horizon
b562e6a7
 
dc97cb71
     # Remove old log files that could mess with how DevStack detects whether Horizon
1e4587ef
     # has been successfully started (see start_horizon() and functions::screen_it())
2f27a0ed
     # and run_process
1e4587ef
     sudo rm -f /var/log/$APACHE_NAME/horizon_*
 
088e6602
     # Setup alias for django-admin which could be different depending on distro
     local django_admin
     if type -p django-admin > /dev/null; then
         django_admin=django-admin
     else
         django_admin=django-admin.py
     fi
 
     DJANGO_SETTINGS_MODULE=openstack_dashboard.settings $django_admin collectstatic --noinput
     DJANGO_SETTINGS_MODULE=openstack_dashboard.settings $django_admin compress --force
45ce9827
 
b562e6a7
 }
 
e385d1e0
 # install_django_openstack_auth() - Collect source and prepare
 function install_django_openstack_auth {
3c8973a9
     if use_library_from_git "django_openstack_auth"; then
         local dir=${GITDIR["django_openstack_auth"]}
         git_clone_by_name "django_openstack_auth"
         # Compile message catalogs before installation
         _prepare_message_catalog_compilation
         (cd $dir; python setup.py compile_catalog)
         setup_dev_lib "django_openstack_auth"
     fi
     # if we aren't using this library from git, then we just let it
     # get dragged in by the horizon setup.
e385d1e0
 }
 
b562e6a7
 # install_horizon() - Collect source and prepare
aee18c74
 function install_horizon {
b562e6a7
     # Apache installation, because we mark it NOPRIME
d98a5d0a
     install_apache_wsgi
b562e6a7
 
53753293
     git_clone $HORIZON_REPO $HORIZON_DIR $HORIZON_BRANCH
b562e6a7
 }
 
 # start_horizon() - Start running processes, including screen
aee18c74
 function start_horizon {
d98a5d0a
     restart_apache_server
2f27a0ed
     tail_log horizon /var/log/$APACHE_NAME/horizon_error.log
b562e6a7
 }
 
 # stop_horizon() - Stop running processes (non-screen)
aee18c74
 function stop_horizon {
d98a5d0a
     stop_apache_server
b562e6a7
 }
 
6518c0b8
 # NOTE: It can be moved to common functions, but it is only used by compilation
 # of django_openstack_auth catalogs at the moment.
 function _prepare_message_catalog_compilation {
60996b1b
     pip_install_gr Babel
6518c0b8
 }
 
1a6d4492
 
b562e6a7
 # Restore xtrace
 $XTRACE
584d90ec
 
6a5aa7c6
 # Tell emacs to use shell-script-mode
 ## Local variables:
 ## mode: shell-script
 ## End: