<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

#    Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from devstack import component as comp
from devstack import exceptions as excp
from devstack import log as logging
from devstack import settings
from devstack import shell as sh
from devstack import utils

#id
TYPE = settings.HORIZON
LOG = logging.getLogger("devstack.components.horizon")

#actual dir names
ROOT_HORIZON = 'horizon'
ROOT_DASH = 'openstack_dashboard'

#name used for python install trace
HORIZON_NAME = ROOT_HORIZON
DASH_NAME = 'dashboard'

#config files messed with
HORIZON_PY_CONF = "horizon_settings.py"
HORIZON_PY_CONF_TGT = ['local', 'local_settings.py']
HORIZON_APACHE_CONF = '000-default'
CONFIGS = [HORIZON_PY_CONF, HORIZON_APACHE_CONF]

#http://wiki.apache.org/httpd/DistrosDefaultLayout
#TODO: maybe this should be a subclass that handles these differences
APACHE_CONF_TARGETS = {
    settings.UBUNTU11: '/etc/apache2/sites-enabled/000-default',
    #ensure runs after wsgi.conf (naming wise)
    settings.RHEL6: '/etc/httpd/conf.d/wsgi-horizon-000-default.conf',
    settings.FEDORA16: '/etc/httpd/conf.d/wsgi-horizon-000-default.conf',
}

#db sync that needs to happen for horizon
DB_SYNC_CMD = ['python', 'manage.py', 'syncdb']

#special apache directory (TODO describe more about this)
BLACKHOLE_DIR = '.blackhole'

#hopefully this will be distro independent ??
#of course they aren't!
#TODO: maybe this should be a subclass that handles these differences
APACHE_SVC_NAME = {
    settings.RHEL6: 'httpd',
    settings.UBUNTU11: 'apache2',
}
APACHE_RESTART_CMD = ['service', '%SERVICE%', 'restart']
APACHE_START_CMD = ['service', '%SERVICE%', 'start']
APACHE_STOP_CMD = ['service', '%SERVICE%', 'stop']
APACHE_STATUS_CMD = ['service', '%SERVICE%', 'status']
APACHE_ERROR_LOG_FN = "error.log"
APACHE_ACCESS_LOG_FN = "access.log"
APACHE_DEF_PORT = 80

#TODO: maybe this should be a subclass that handles these differences
APACHE_FIXUPS = {
    'SOCKET_CONF': "/etc/httpd/conf.d/wsgi-socket-prefix.conf",
    'HTTPD_CONF': '/etc/httpd/conf/httpd.conf',
}
APACHE_FIXUPS_DISTROS = [settings.RHEL6]

#users which apache may not like starting as
BAD_APACHE_USERS = ['root']

#apache logs will go here
LOGS_DIR = "logs"

#the pkg json files horizon requires for installation
REQ_PKGS = ['general.json', 'horizon.json']

#pip files that horizon requires
REQ_PIPS = ['general.json', 'horizon.json']


class HorizonUninstaller(comp.PythonUninstallComponent):
    def __init__(self, *args, **kargs):
        comp.PythonUninstallComponent.__init__(self, TYPE, *args, **kargs)


class HorizonInstaller(comp.PythonInstallComponent):
    def __init__(self, *args, **kargs):
        comp.PythonInstallComponent.__init__(self, TYPE, *args, **kargs)
        self.horizon_dir = sh.joinpths(self.appdir, ROOT_HORIZON)
        self.dash_dir = sh.joinpths(self.appdir, ROOT_DASH)
        self.log_dir = sh.joinpths(self.component_root, LOGS_DIR)

    def _get_download_locations(self):
        places = list()
        places.append({
            'uri': ("git", "horizon_repo"),
            'branch': ("git", "horizon_branch"),
        })
        return places

    def verify(self):
        self._check_ug()

    def _get_symlinks(self):
        links = comp.PythonInstallComponent._get_symlinks(self)
        src = self._get_target_config_name(HORIZON_APACHE_CONF)
        links[src] = APACHE_CONF_TARGETS[self.distro]
        if utils.service_enabled(settings.QUANTUM_CLIENT, self.instances, False):
            #TODO remove this junk, blah, puke that we have to do this
            qc = self.instances[settings.QUANTUM_CLIENT]
            src_pth = sh.joinpths(qc.appdir, 'quantum')
            tgt_dir = sh.joinpths(self.dash_dir, 'quantum')
            links[src_pth] = tgt_dir
        return links

    def _check_ug(self):
        (user, group) = self._get_apache_user_group()
        if not sh.user_exists(user):
            msg = "No user named %s exists on this system!" % (user)
            raise excp.ConfigException(msg)
        if not sh.group_exists(group):
            msg = "No group named %s exists on this system!" % (group)
            raise excp.ConfigException(msg)
        if user in BAD_APACHE_USERS:
            msg = "You may want to adjust your configuration, (user=%s, group=%s) will not work with apache!" % (user, group)
            raise excp.ConfigException(msg)

    def _get_pkgs(self):
        return list(REQ_PKGS)

    def _get_pips(self):
        return list(REQ_PIPS)

    def _get_target_config_name(self, config_name):
        if config_name == HORIZON_PY_CONF:
            return sh.joinpths(self.dash_dir, *HORIZON_PY_CONF_TGT)
        else:
            return comp.PythonInstallComponent._get_target_config_name(self, config_name)

    def _get_config_files(self):
        return list(CONFIGS)

    def _setup_blackhole(self):
        #create an empty directory that apache uses as docroot
        self.tracewriter.make_dir(sh.joinpths(self.appdir, BLACKHOLE_DIR))

    def _sync_db(self):
        #Initialize the horizon database (it stores sessions and notices shown to users).
        #The user system is external (keystone).
        LOG.info("Initializing the horizon database.")
        sh.execute(*DB_SYNC_CMD, cwd=self.appdir)

    def _ensure_db_access(self):
        # ../openstack-dashboard/local needs to be writeable by the runtime user
        # since currently its storing the sql-lite databases there (TODO fix that)
        path = sh.joinpths(self.dash_dir, 'local')
        if sh.isdir(path):
            (user, group) = self._get_apache_user_group()
            LOG.debug("Changing ownership (recursively) of %s so that it can be used by %s - %s",
                            path, user, group)
            uid = sh.getuid(user)
            gid = sh.getgid(group)
            sh.chown_r(path, uid, gid)

    def pre_install(self):
        comp.PythonInstallComponent.pre_install(self)
        self.tracewriter.make_dir(self.log_dir)
        if utils.service_enabled(settings.QUANTUM_CLIENT, self.instances, False):
            #TODO remove this junk, blah, puke that we have to do this
            tgt_dir = sh.joinpths(self.dash_dir, 'quantum')
            if sh.isdir(tgt_dir):
                #whhhhy???
                sh.deldir(tgt_dir)

    def _config_fixups(self):
        #currently just handling rhel fixups
        #TODO: maybe this should be a subclass that handles these differences
        if not (self.distro in APACHE_FIXUPS_DISTROS):
            return
        #it seems like to get this to work
        #we need to do some conf.d/conf work which sort of sucks
        (user, group) = self._get_apache_user_group()
        socket_fn = APACHE_FIXUPS.get("SOCKET_CONF")
        self.tracewriter.file_touched(socket_fn)
        #not recorded since we aren't really creating this
        httpd_fn = APACHE_FIXUPS.get("HTTPD_CONF")
        with sh.Rooted(True):
            #fix the socket prefix to someplace we can use
            fc = "WSGISocketPrefix %s" % (sh.joinpths(self.log_dir, "wsgi-socket"))
            sh.write_file(socket_fn, fc)
            #now adjust the run user and group (of httpd.conf)
            new_lines = list()
            for line in sh.load_file(httpd_fn).splitlines():
                if line.startswith("User "):
                    line = "User %s" % (user)
                if line.startswith("Group "):
                    line = "Group %s" % (group)
                new_lines.append(line)<commit_msg>Fixing horizon knocking off quantum.<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

#    Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from devstack import component as comp
from devstack import exceptions as excp
from devstack import log as logging
from devstack import settings
from devstack import shell as sh
from devstack import utils

#id
TYPE = settings.HORIZON
LOG = logging.getLogger("devstack.components.horizon")

#actual dir names
ROOT_HORIZON = 'horizon'
ROOT_DASH = 'openstack_dashboard'

#name used for python install trace
HORIZON_NAME = ROOT_HORIZON
DASH_NAME = 'dashboard'

#config files messed with
HORIZON_PY_CONF = "horizon_settings.py"
HORIZON_PY_CONF_TGT = ['local', 'local_settings.py']
HORIZON_APACHE_CONF = '000-default'
CONFIGS = [HORIZON_PY_CONF, HORIZON_APACHE_CONF]

#http://wiki.apache.org/httpd/DistrosDefaultLayout
#TODO: maybe this should be a subclass that handles these differences
APACHE_CONF_TARGETS = {
    settings.UBUNTU11: '/etc/apache2/sites-enabled/000-default',
    #ensure runs after wsgi.conf (naming wise)
    settings.RHEL6: '/etc/httpd/conf.d/wsgi-horizon-000-default.conf',
    settings.FEDORA16: '/etc/httpd/conf.d/wsgi-horizon-000-default.conf',
}

#db sync that needs to happen for horizon
DB_SYNC_CMD = ['python', 'manage.py', 'syncdb']

#special apache directory (TODO describe more about this)
BLACKHOLE_DIR = '.blackhole'

#hopefully this will be distro independent ??
#of course they aren't!
#TODO: maybe this should be a subclass that handles these differences
APACHE_SVC_NAME = {
    settings.RHEL6: 'httpd',
    settings.UBUNTU11: 'apache2',
}
APACHE_RESTART_CMD = ['service', '%SERVICE%', 'restart']
APACHE_START_CMD = ['service', '%SERVICE%', 'start']
APACHE_STOP_CMD = ['service', '%SERVICE%', 'stop']
APACHE_STATUS_CMD = ['service', '%SERVICE%', 'status']
APACHE_ERROR_LOG_FN = "error.log"
APACHE_ACCESS_LOG_FN = "access.log"
APACHE_DEF_PORT = 80

#TODO: maybe this should be a subclass that handles these differences
APACHE_FIXUPS = {
    'SOCKET_CONF': "/etc/httpd/conf.d/wsgi-socket-prefix.conf",
    'HTTPD_CONF': '/etc/httpd/conf/httpd.conf',
}
APACHE_FIXUPS_DISTROS = [settings.RHEL6]

#users which apache may not like starting as
BAD_APACHE_USERS = ['root']

#apache logs will go here
LOGS_DIR = "logs"

#the pkg json files horizon requires for installation
REQ_PKGS = ['general.json', 'horizon.json']

#pip files that horizon requires
REQ_PIPS = ['general.json', 'horizon.json']


class HorizonUninstaller(comp.PythonUninstallComponent):
    def __init__(self, *args, **kargs):
        comp.PythonUninstallComponent.__init__(self, TYPE, *args, **kargs)


class HorizonInstaller(comp.PythonInstallComponent):
    def __init__(self, *args, **kargs):
        comp.PythonInstallComponent.__init__(self, TYPE, *args, **kargs)
        self.horizon_dir = sh.joinpths(self.appdir, ROOT_HORIZON)
        self.dash_dir = sh.joinpths(self.appdir, ROOT_DASH)
        self.log_dir = sh.joinpths(self.component_root, LOGS_DIR)

    def _get_download_locations(self):
        places = list()
        places.append({
            'uri': ("git", "horizon_repo"),
            'branch': ("git", "horizon_branch"),
        })
        return places

    def verify(self):
        self._check_ug()

    def _get_symlinks(self):
        links = comp.PythonInstallComponent._get_symlinks(self)
        src = self._get_target_config_name(HORIZON_APACHE_CONF)
        links[src] = APACHE_CONF_TARGETS[self.distro]
        if utils.service_enabled(settings.QUANTUM_CLIENT, self.instances, False):
            #TODO remove this junk, blah, puke that we have to do this
            qc = self.instances[settings.QUANTUM_CLIENT]
            src_pth = sh.joinpths(qc.appdir, 'quantum')
            tgt_dir = sh.joinpths(self.dash_dir, 'quantum')
            links[src_pth] = tgt_dir
        return links

    def _check_ug(self):
        (user, group) = self._get_apache_user_group()
        if not sh.user_exists(user):
            msg = "No user named %s exists on this system!" % (user)
            raise excp.ConfigException(msg)
        if not sh.group_exists(group):
            msg = "No group named %s exists on this system!" % (group)
            raise excp.ConfigException(msg)
        if user in BAD_APACHE_USERS:
            msg = "You may want to adjust your configuration, (user=%s, group=%s) will not work with apache!" % (user, group)
            raise excp.ConfigException(msg)

    def _get_pkgs(self):
        return list(REQ_PKGS)

    def _get_pips(self):
        return list(REQ_PIPS)

    def _get_target_config_name(self, config_name):
        if config_name == HORIZON_PY_CONF:
            return sh.joinpths(self.dash_dir, *HORIZON_PY_CONF_TGT)
        else:
            return comp.PythonInstallComponent._get_target_config_name(self, config_name)

    def _get_config_files(self):
        return list(CONFIGS)

    def _setup_blackhole(self):
        #create an empty directory that apache uses as docroot
        self.tracewriter.make_dir(sh.joinpths(self.appdir, BLACKHOLE_DIR))

    def _sync_db(self):
        #Initialize the horizon database (it stores sessions and notices shown to users).
        #The user system is external (keystone).
        LOG.info("Initializing the horizon database.")
        sh.execute(*DB_SYNC_CMD, cwd=self.appdir)

    def _ensure_db_access(self):
        # ../openstack-dashboard/local needs to be writeable by the runtime user
        # since currently its storing the sql-lite databases there (TODO fix that)
        path = sh.joinpths(self.dash_dir, 'local')
        if sh.isdir(path):
            (user, group) = self._get_apache_user_group()
            LOG.debug("Changing ownership (recursively) of %s so that it can be used by %s - %s",
                            path, user, group)
            uid = sh.getuid(user)
            gid = sh.getgid(group)
            sh.chown_r(path, uid, gid)

    def pre_install(self):
        comp.PythonInstallComponent.pre_install(self)
        self.tracewriter.make_dir(self.log_dir)

    def _config_fixups(self):
        #currently just handling rhel fixups
        #TODO: maybe this should be a subclass that handles these differences
        if not (self.distro in APACHE_FIXUPS_DISTROS):
            return
        #it seems like to get this to work
        #we need to do some conf.d/conf work which sort of sucks
        (user, group) = self._get_apache_user_group()
        socket_fn = APACHE_FIXUPS.get("SOCKET_CONF")
        self.tracewriter.file_touched(socket_fn)
        #not recorded since we aren't really creating this
        httpd_fn = APACHE_FIXUPS.get("HTTPD_CONF")
        with sh.Rooted(True):
            #fix the socket prefix to someplace we can use
            fc = "WSGISocketPrefix %s" % (sh.joinpths(self.log_dir, "wsgi-socket"))
            sh.write_file(socket_fn, fc)
            #now adjust the run user and group (of httpd.conf)
            new_lines = list()
            for line in sh.load_file(httpd_fn).splitlines():
                if line.startswith("User "):
                    line = "User %s" % (user)
                if line.startswith("Group "):
                    line = "Group %s" % (group)
                new_lines.append(line)
{'fraction_non_alphanumeric': 0.10037664783427495, 'fraction_numerical': 0.004519774011299435, 'mean_word_length': 3.0671432218534593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4714191', 'n_tokens_mistral': 5186, 'n_tokens_neox': 5051, 'n_words': 1374}