<commit_before>#!/usr/bin/python

# Copyright 2012 Red Hat
#
# 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.

# Start/stop oVirt Engine
#
# chkconfig: - 65 34
# description: oVirt Engine
# pidfile: /var/run/ovirt-engine.pid

import configobj
import errno
import glob
import grp
import optparse
import os
import pwd
import resource
import shutil
import signal
import stat
import string
import sys
import syslog
import time
import traceback

from Cheetah.Template import Template


# The name of the engine:
engineName = "engine-service"

# The engine system configuration variables:
engineConfig = None

# The name of the user and group that should run the service:
engineUser = None
engineGroup = None
engineUid = 0
engineGid = 0

# Java home directory:
javaHomeDir = None

# Java virtual machine launcher:
javaLauncher = None

# JBoss directories:
jbossHomeDir = None

# JBoss files:
jbossModulesJar = None

# Engine directories:
engineEtcDir = None
engineLogDir = None
engineTmpDir = None
engineUsrDir = None
engineVarDir = None
engineLockDir = None
engineContentDir = None
engineDeploymentsDir = None

# Engine files:
enginePidFile = None
engineLoggingFile = None
engineConfigTemplateFile = None
engineConfigFile = None
engineLogFile = None
engineBootLogFile = None
engineConsoleLogFile = None
engineServerLogFile = None


# Helper class to simplify getting values from the configuration, specially
# from the template used to generate the application server configuration
# file:
class Config(configobj.ConfigObj):
    def __init__(self, name):
        configobj.ConfigObj.__init__(self, name)

    def getString(self, name, default=None):
        return self.get(name, default)

    def getBoolean(self, name, default=False):
        text = self.get(name)
        if text is None:
            return default
        return text.lower() in ["t", "true", "y", "yes", "1"]

    def getInteger(self, name, default=0):
        text = self.get(name)
        if text is None:
            return default
        try:
            return int(text)
        except:
            raise Exception("The value \"%s\" of parameter \"%s\" is not a valid integer." % (text, name))


def loadConfig():
    # Load the configuration file:
    engineConfigFile = os.getenv("ENGINE_VARS", "/etc/sysconfig/ovirt-engine")
    if not os.path.exists(engineConfigFile):
        raise Exception("The engine configuration file \"%s\" doesn't exist." % engineConfigFile)
    global engineConfig
    engineConfig = Config(engineConfigFile)

    # Get the id of the engine user:
    global engineUser
    global engineUid
    engineUser = engineConfig.getString("ENGINE_USER", "ovirt")
    try:
        engineUid = pwd.getpwnam(engineUser).pw_uid
    except:
        raise Exception("The engine user \"%s\" doesn't exist." % engineUser)

    # Get id of the engine group:
    global engineGroup
    global engineGid
    engineGroup = engineConfig.getString("ENGINE_GROUP", "ovirt")
    try:
        engineGid = grp.getgrnam(engineGroup).gr_gid
    except:
        raise Exception("The engine group \"%s\" doesn't exist." % engineGroup)

    # Java home directory:
    global javaHomeDir
    javaHomeDir = engineConfig.getString("JAVA_HOME", "/usr/lib/jvm/jre-1.7.0-openjdk.x86_64")

    # Java launcher:
    global javaLauncher
    javaLauncher = os.path.join(javaHomeDir, "bin/java")

    # JBoss directories:
    global jbossHomeDir
    jbossHomeDir = engineConfig.getString("JBOSS_HOME", "/usr/share/jboss-as")

    # JBoss files:
    global jbossModulesJar
    jbossModulesJar = os.path.join(jbossHomeDir, "jboss-modules.jar")

    # Engine directories:
    global engineEtcDir
    global engineLogDir
    global engineTmpDir
    global engineUsrDir
    global engineVarDir
    global engineLockDir
    global engineServiceDir
    global engineContentDir
    global engineDeploymentsDir
    engineEtcDir = engineConfig.getString("ENGINE_ETC", "/etc/ovirt-engine")
    engineLogDir = engineConfig.getString("ENGINE_LOG", "/var/log/ovirt-engine")
    engineTmpDir = engineConfig.getString("ENGINE_TMP", "/var/tmp/ovirt-engine")
    engineUsrDir = engineConfig.getString("ENGINE_USR", "/usr/share/ovirt-engine")
    engineVarDir = engineConfig.getString("ENGINE_VAR", "/var/lib/ovirt-engine")
    engineLockDir = engineConfig.getString("ENGINE_LOCK", "/var/lock/ovirt-engine")
    engineServiceDir = os.path.join(engineUsrDir, "service")
    engineContentDir = os.path.join(engineVarDir, "content")
    engineDeploymentsDir = os.path.join(engineVarDir, "deployments")

    # Engine files:
    global enginePidFile
    global engineLoggingFile
    global engineLogFile
    global jbossConfigTemplateFile
    global jbossConfigFile
    global engineBootLogFile
    global engineConsoleLogFile
    global engineServerLogFile
    enginePidFile = engineConfig.getString("ENGINE_PID", "/var/run/ovirt-engine.pid")
    engineLoggingFile = os.path.join(engineServiceDir, "engine-service-logging.properties")
    engineLogFile = os.path.join(engineLogDir, "engine.log")
    jbossConfigTemplateFile = os.path.join(engineServiceDir, "engine-service.xml.in")
    jbossConfigFile = os.path.join(engineTmpDir, "engine-service.xml")
    engineBootLogFile = os.path.join(engineLogDir, "boot.log")
    engineConsoleLogFile = os.path.join(engineLogDir, "console.log")
    engineServerLogFile = os.path.join(engineLogDir, "server.log")


def checkIdentity():
    if os.getuid() != 0:
        raise Exception("This script should run with the root user.")


def checkOwnership(name, uid=None, gid=None):
    # Get the metadata of the file:
    st = os.stat(name)

    # Check that the file is owned by the given user:
    if uid and st[stat.ST_UID] != uid:
        user = pwd.getpwuid(uid).pw_name
        owner = pwd.getpwuid(st[stat.ST_UID]).pw_name
        if os.path.isdir(name):
            raise Exception("The directory \"%s\" is not owned by user \"%s\", but by \"%s\"." % (name, user, owner))
        else:
            raise Exception("The file \"%s\" is not owned by user \"%s\", but by \"%s\"." % (name, user, owner))

    # Check that the file is owned by the given group:
    if gid and st[stat.ST_GID] != gid:
        group = grp.getgrgid(gid).gr_name
        owner = grp.getgrgid(st[stat.ST_GID]).gr_name
        if os.path.isdir(name):
            raise Exception("The directory \"%s\" is not owned by group \"%s\", but by \"%s\"." % (name, group, owner))
        else:
            raise Exception("The file \"%s\" is not owned by group \"%s\", but by \"%s\"." % (name, group, owner))


def checkDirectory(name, uid=None, gid=None):
    if not os.path.isdir(name):
        raise Exception("The directory \"%s\" doesn't exist." % name)
    checkOwnership(name, uid, gid)


def checkFile(name, uid=None, gid=None):
    if not os.path.isfile(name):
        raise Exception("The file \"%s\" doesn't exist." % name)
    checkOwnership(name, uid, gid)


def checkLog(name):
    log = os.path.join(engineLogDir, name)
    if os.path.exists(log):
        checkOwnership(log, engineUid, engineGid)


def checkInstallation():
    # Check that the Java home directory exists and that it contais at least
    # the java executable:
    checkDirectory(javaHomeDir)
    checkFile(javaLauncher)

    # Check the required JBoss directories and files:
    checkDirectory(jbossHomeDir)
    checkFile(jbossModulesJar)

    # Check the required engine directories and files:
    checkDirectory(engineEtcDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineLogDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineUsrDir, uid=0, gid=0)
    checkDirectory(engineVarDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineLockDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineServiceDir, uid=0, gid=0)
    checkDirectory(engineContentDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineDeploymentsDir, uid=engineUid, gid=engineGid)
    checkFile(engineLoggingFile)
    checkFile(jbossConfigTemplateFile)

    # Check that log files are owned by the engine user, if they exist:
    checkLog(engineLogFile)
    checkLog(engineBootLogFile)
    checkLog(engineConsoleLogFile)
    checkLog(engineServerLogFile)

    # XXX: Add more checks here!


def loadEnginePid():
    if not os.path.exists(enginePidFile):
        return None
    with open(enginePidFile, "r") as enginePidFd:
        return int(enginePidFd.read())


def saveEnginePid(pid):
    with open(enginePidFile, "w") as enginePidFd:
        enginePidFd.write(str(pid) + "\n")


def removeEnginePid():
    if os.path.exists(enginePidFile):
        os.remove(enginePidFile)


def startEngine():
    # Get the PID:
    enginePid = loadEnginePid()
    if enginePid:
        syslog.syslog(syslog.LOG_WARNING, "The engine PID file \"%s\" already exists." % enginePidFile)
        return

    # The list of applications to be deployed:
    engineApps = engineConfig.getString("ENGINE_APPS", "engine.ear").split()

    for engineApp in engineApps:
        # Do nothing if the application is not available:
        engineAppDir = os.path.join(engineUsrDir, engineApp)
        if not os.path.exists(engineAppDir):
            syslog.syslog(syslog.LOG_WARNING, "The application \"%s\" doesn't exist, it will be ignored." % engineAppDir)
            continue

        # Make sure the application is linked in the deployments directory, if not
        # link it now:
        engineAppLink = os.path.join(engineDeploymentsDir, engineApp)
        if not os.path.islink(engineAppLink):
            syslog.syslog(syslog.LOG_INFO, "The symbolic link \"%s\" doesn't exist, will create it now." % engineAppLink)
            try:
                os.symlink(engineAppDir, engineAppLink)<commit_msg>packaging: Better failed start message (#838527)<commit_after>#!/usr/bin/python

# Copyright 2012 Red Hat
#
# 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.

# Start/stop oVirt Engine
#
# chkconfig: - 65 34
# description: oVirt Engine
# pidfile: /var/run/ovirt-engine.pid

import configobj
import errno
import glob
import grp
import optparse
import os
import pwd
import resource
import shutil
import signal
import stat
import string
import sys
import syslog
import time
import traceback

from Cheetah.Template import Template


# The name of the engine:
engineName = "engine-service"

# The engine system configuration variables:
engineConfig = None

# The name of the user and group that should run the service:
engineUser = None
engineGroup = None
engineUid = 0
engineGid = 0

# Java home directory:
javaHomeDir = None

# Java virtual machine launcher:
javaLauncher = None

# JBoss directories:
jbossHomeDir = None

# JBoss files:
jbossModulesJar = None

# Engine directories:
engineEtcDir = None
engineLogDir = None
engineTmpDir = None
engineUsrDir = None
engineVarDir = None
engineLockDir = None
engineContentDir = None
engineDeploymentsDir = None

# Engine files:
enginePidFile = None
engineLoggingFile = None
engineConfigTemplateFile = None
engineConfigFile = None
engineLogFile = None
engineBootLogFile = None
engineConsoleLogFile = None
engineServerLogFile = None


# Helper class to simplify getting values from the configuration, specially
# from the template used to generate the application server configuration
# file:
class Config(configobj.ConfigObj):
    def __init__(self, name):
        configobj.ConfigObj.__init__(self, name)

    def getString(self, name, default=None):
        return self.get(name, default)

    def getBoolean(self, name, default=False):
        text = self.get(name)
        if text is None:
            return default
        return text.lower() in ["t", "true", "y", "yes", "1"]

    def getInteger(self, name, default=0):
        text = self.get(name)
        if text is None:
            return default
        try:
            return int(text)
        except:
            raise Exception("The value \"%s\" of parameter \"%s\" is not a valid integer." % (text, name))


def loadConfig():
    # Load the configuration file:
    engineConfigFile = os.getenv("ENGINE_VARS", "/etc/sysconfig/ovirt-engine")
    if not os.path.exists(engineConfigFile):
        raise Exception("The engine configuration file \"%s\" doesn't exist." % engineConfigFile)
    global engineConfig
    engineConfig = Config(engineConfigFile)

    # Get the id of the engine user:
    global engineUser
    global engineUid
    engineUser = engineConfig.getString("ENGINE_USER", "ovirt")
    try:
        engineUid = pwd.getpwnam(engineUser).pw_uid
    except:
        raise Exception("The engine user \"%s\" doesn't exist." % engineUser)

    # Get id of the engine group:
    global engineGroup
    global engineGid
    engineGroup = engineConfig.getString("ENGINE_GROUP", "ovirt")
    try:
        engineGid = grp.getgrnam(engineGroup).gr_gid
    except:
        raise Exception("The engine group \"%s\" doesn't exist." % engineGroup)

    # Java home directory:
    global javaHomeDir
    javaHomeDir = engineConfig.getString("JAVA_HOME", "/usr/lib/jvm/jre-1.7.0-openjdk.x86_64")

    # Java launcher:
    global javaLauncher
    javaLauncher = os.path.join(javaHomeDir, "bin/java")

    # JBoss directories:
    global jbossHomeDir
    jbossHomeDir = engineConfig.getString("JBOSS_HOME", "/usr/share/jboss-as")

    # JBoss files:
    global jbossModulesJar
    jbossModulesJar = os.path.join(jbossHomeDir, "jboss-modules.jar")

    # Engine directories:
    global engineEtcDir
    global engineLogDir
    global engineTmpDir
    global engineUsrDir
    global engineVarDir
    global engineLockDir
    global engineServiceDir
    global engineContentDir
    global engineDeploymentsDir
    engineEtcDir = engineConfig.getString("ENGINE_ETC", "/etc/ovirt-engine")
    engineLogDir = engineConfig.getString("ENGINE_LOG", "/var/log/ovirt-engine")
    engineTmpDir = engineConfig.getString("ENGINE_TMP", "/var/tmp/ovirt-engine")
    engineUsrDir = engineConfig.getString("ENGINE_USR", "/usr/share/ovirt-engine")
    engineVarDir = engineConfig.getString("ENGINE_VAR", "/var/lib/ovirt-engine")
    engineLockDir = engineConfig.getString("ENGINE_LOCK", "/var/lock/ovirt-engine")
    engineServiceDir = os.path.join(engineUsrDir, "service")
    engineContentDir = os.path.join(engineVarDir, "content")
    engineDeploymentsDir = os.path.join(engineVarDir, "deployments")

    # Engine files:
    global enginePidFile
    global engineLoggingFile
    global engineLogFile
    global jbossConfigTemplateFile
    global jbossConfigFile
    global engineBootLogFile
    global engineConsoleLogFile
    global engineServerLogFile
    enginePidFile = engineConfig.getString("ENGINE_PID", "/var/run/ovirt-engine.pid")
    engineLoggingFile = os.path.join(engineServiceDir, "engine-service-logging.properties")
    engineLogFile = os.path.join(engineLogDir, "engine.log")
    jbossConfigTemplateFile = os.path.join(engineServiceDir, "engine-service.xml.in")
    jbossConfigFile = os.path.join(engineTmpDir, "engine-service.xml")
    engineBootLogFile = os.path.join(engineLogDir, "boot.log")
    engineConsoleLogFile = os.path.join(engineLogDir, "console.log")
    engineServerLogFile = os.path.join(engineLogDir, "server.log")


def checkIdentity():
    if os.getuid() != 0:
        raise Exception("This script should run with the root user.")


def checkOwnership(name, uid=None, gid=None):
    # Get the metadata of the file:
    st = os.stat(name)

    # Check that the file is owned by the given user:
    if uid and st[stat.ST_UID] != uid:
        user = pwd.getpwuid(uid).pw_name
        owner = pwd.getpwuid(st[stat.ST_UID]).pw_name
        if os.path.isdir(name):
            raise Exception("The directory \"%s\" is not owned by user \"%s\", but by \"%s\"." % (name, user, owner))
        else:
            raise Exception("The file \"%s\" is not owned by user \"%s\", but by \"%s\"." % (name, user, owner))

    # Check that the file is owned by the given group:
    if gid and st[stat.ST_GID] != gid:
        group = grp.getgrgid(gid).gr_name
        owner = grp.getgrgid(st[stat.ST_GID]).gr_name
        if os.path.isdir(name):
            raise Exception("The directory \"%s\" is not owned by group \"%s\", but by \"%s\"." % (name, group, owner))
        else:
            raise Exception("The file \"%s\" is not owned by group \"%s\", but by \"%s\"." % (name, group, owner))


def checkDirectory(name, uid=None, gid=None):
    if not os.path.isdir(name):
        raise Exception("The directory \"%s\" doesn't exist." % name)
    checkOwnership(name, uid, gid)


def checkFile(name, uid=None, gid=None):
    if not os.path.isfile(name):
        raise Exception("The file \"%s\" doesn't exist." % name)
    checkOwnership(name, uid, gid)


def checkLog(name):
    log = os.path.join(engineLogDir, name)
    if os.path.exists(log):
        checkOwnership(log, engineUid, engineGid)


def checkInstallation():
    # Check that the Java home directory exists and that it contais at least
    # the java executable:
    checkDirectory(javaHomeDir)
    checkFile(javaLauncher)

    # Check the required JBoss directories and files:
    checkDirectory(jbossHomeDir)
    checkFile(jbossModulesJar)

    # Check the required engine directories and files:
    checkDirectory(engineEtcDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineLogDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineUsrDir, uid=0, gid=0)
    checkDirectory(engineVarDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineLockDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineServiceDir, uid=0, gid=0)
    checkDirectory(engineContentDir, uid=engineUid, gid=engineGid)
    checkDirectory(engineDeploymentsDir, uid=engineUid, gid=engineGid)
    checkFile(engineLoggingFile)
    checkFile(jbossConfigTemplateFile)

    # Check that log files are owned by the engine user, if they exist:
    checkLog(engineLogFile)
    checkLog(engineBootLogFile)
    checkLog(engineConsoleLogFile)
    checkLog(engineServerLogFile)

    # XXX: Add more checks here!


def loadEnginePid():
    if not os.path.exists(enginePidFile):
        return None
    with open(enginePidFile, "r") as enginePidFd:
        return int(enginePidFd.read())


def saveEnginePid(pid):
    with open(enginePidFile, "w") as enginePidFd:
        enginePidFd.write(str(pid) + "\n")


def removeEnginePid():
    if os.path.exists(enginePidFile):
        os.remove(enginePidFile)


def startEngine():
    # Get the PID:
    enginePid = loadEnginePid()

    # If the PID already exists then we need to check if the
    # process is running and tell the user that the service needs
    # to be restarted:
    if enginePid:
        if not os.path.exists("/proc/%d" % enginePid):
            raise Exception(
                "The engine PID file \"%s\" contains %d but "
                "that process doesn't exist. This means that "
                "the engine crashed or was killed. You need "
                "to explicitly run 'service ovirt-engine stop' "
                "and then 'service ovirt-engine start' to "
                "enable it again." %
                (enginePidFile, enginePid))
        else:
            syslog.syslog(syslog.LOG_WARNING,
                "The engine PID file \"%s\" exists and the "
                "process %d is running." %
                (enginePidFile, enginePid))
            return

    # The list of applications to be deployed:
    engineApps = engineConfig.getString("ENGINE_APPS", "engine.ear").split()

    for engineApp in engineApps:
        # Do nothing if the application is not available:
        engineAppDir = os.path.join(engineUsrDir, engineApp)
        if not os.path.exists(engineAppDir):
            syslog.syslog(syslog.LOG_WARNING, "The application \"%s\" doesn't exist, it will be ignored." % engineAppDir)
            continue

        # Make sure the application is linked in the deployments directory, if not
        # link it now:
        engineAppLink = os.path.join(engineDeploymentsDir, engineApp)
        if not os.path.islink(engineAppLink):
            syslog.syslog(syslog.LOG_INFO, "The symbolic link \"%s\" doesn't exist, will create it now." % engineAppLink)
            try:
                os.symlink(engineAppDir, engineAppLink)
{'fraction_non_alphanumeric': 0.10086414506304009, 'fraction_numerical': 0.0029277045851631486, 'mean_word_length': 3.3883133029423953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4421145', 'n_tokens_mistral': 6584, 'n_tokens_neox': 6379, 'n_words': 1849}