def commandFailure(failure, cmdString):
     if fatalErrorOnFailure:
         errorutil.triggerFatalError('Execution of command "%s" failed to execute: "%s"' % (cmdString, failure))
     else:
         log.msg(
             'Execution of command "%s" failed to execute: "%s"' % (cmdString, failure), lvl="e", ss="ss_default"
         )
         return ("ERROR", "ERROR", 65535)
 def _checkForFile(result, numSecondsRemaining):
     if (waitForFileExists == True and os.path.exists(fileName)) or (
         waitForFileExists == False and not os.path.exists(fileName)
     ):
         # the file exists, ok we can quit
         return defer.succeed(True)
     else:
         numSecondsRemaining = numSecondsRemaining - pollPeriod
         if numSecondsRemaining <= 0:
             if fatalErrorOnFailure:
                 errorutil.triggerFatalError('File "%s" took too long to appear/disappear. Giving up...' % fileName)
             else:
                 raise Exception('File "%s" took too long to appear/disappear. Giving up...' % fileName)
         d = defer.Deferred()
         d.addCallback(_checkForFile, numSecondsRemaining)
         reactor.callLater(pollPeriod, d.callback, result)  # IGNORE:E1101
         return d
Exemplo n.º 3
0
 def _addToQueryBacklog(self, item):
     """
     Adds a query backlog item to the backlog queue
     
     @return: Return a deferred yielding true if the adding was successful, or false if not.
     """
     #signify last DB sync lost
     if not self._connectivityDownSince:
         self._connectivityDownSince = time.time()
     
     if len(self._queryBacklog) >= QUERY_BACKLOG_MAXSIZE:
         #connection has been dead for too long, die
         errorutil.triggerFatalError("Query backlog for pool \"%s\" would overflow (DB down for too long). Exiting!"
             % (self._poolName,))
         return None
     
     self._queryBacklog.append(item)
     return True
    def _checkForTCPConn():
        import errno

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # s.setblocking(0)
        s.settimeout(pollPeriod)
        numSecondsElapsed = 0
        t0 = 0
        t1 = 0

        while numSecondsElapsed < maxNumSeconds:
            if waitForCanMakeConn:
                try:
                    t0 = time.time()
                    s.connect((hostName, port))

                    # we made the connection, so we're all good...
                    s.close()
                    return
                except socket.error:
                    # couldn't make the connection, try again...
                    t1 = time.time()
                    if t1 - t0 <= pollPeriod:
                        time.sleep(pollPeriod - (t1 - t0))
                    numSecondsElapsed += pollPeriod
            else:
                try:
                    t0 = time.time()
                    s.connect((hostName, port))

                    # we made the connection still...
                    s.close()

                    # wait to try again...
                    t1 = time.time()
                    if t1 - t0 <= pollPeriod:
                        time.sleep(pollPeriod - (t1 - t0))
                    numSecondsElapsed += pollPeriod
                except socket.error:
                    # assume that we couldn't make the connection, so this is good in this case
                    return

        if waitForCanMakeConn:
            # if we're here, we were waiting to connect and the connect kept failing, so we gave up
            if fatalErrorOnFailure:
                errorutil.triggerFatalError(
                    'Couldn\'t connect to host "%s", port %s in alotted time. Giving up...' % (hostName, port)
                )
            else:
                raise Exception(
                    'Couldn\'t connect to host "%s", port %s in alotted time. Giving up...' % (hostName, port)
                )
        else:
            # if we're here, we were waiting to NOT be able to connect and the connect kept succeeding, so we gave up
            if fatalErrorOnFailure:
                errorutil.triggerFatalError(
                    'Couldn\'t NOT connect to host "%s", port %s in alotted time. Giving up...' % (hostName, port)
                )
            else:
                raise Exception(
                    'Couldn\'t NOT connect to host "%s", port %s in alotted time. Giving up...' % (hostName, port)
                )
    cmdString = "sudo touch %s; sudo chown %s:%s %s; sudo chmod %s %s;" % (
        filePath,
        user,
        group,
        filePath,
        mode,
        filePath,
    )
    d = runCmdInTwisted(cmdString)
    d.addCallback(_cbTouchAndSetPerms, filePath, cmdString)
    return d


def _cbTouchAndSetPerms((out, err, statusCode), filePath, cmdString):
    if statusCode:
        errorutil.triggerFatalError('Could touch file "%s".' ' Stdout: "%s"; Stderr: "%s"' % (filePath, out, err))
    return True


def replaceContentsOfPrivilegedFile(
    fileName, newContents, fatalErrorOnFailure=True, atomicFromDir=None, user="******", group="root", mode="600"
):
    """Replaces the contents of a file which requires root (or similar) access to manipulate with new data.
    @param newContents: A string containing the new contents of the file. The existing file will be
     overwritten with these contents (e.g. NOT appended)
    @param fileName: This file does not need to already exist on the filesystem.
    
    @param user: The username/UID that should own the file. If a file at fileName doesn't exist, then
    this value will be used to set the owner of the newly created file.
    @param group: The group name/GID that should own the file. If a file at fileName doesn't exist, then
    this value will be used to set the owning group of the newly created file.
def convertStrToInt(stringData, configDirectiveName='', fatalErrorOnFailure=True, isPortNum=False,
    minVal=-2147483648, maxVal=2147483647):
    """I convert a string variable into it's corresponding integer variable equivelent. 

    @param stringData: The string to interpret

    @param configDirectiveName: If this conversion is being done for the parsing of a configuration directive,
    this parameter should be set to the name of that directive. This will allow me to give more informative
    error output to the user in the event of conversion failure (when fatalErrorOnFailure is set to True as well)
    
    @param isPortNum: Set to true to enforce a numeric range consistent with a port number (1-65535). If
    set to True, the limits specified in the minVal and maxVal parameters will be ignored.
    
    @param minVal: The minimum allowable value for the integer as read in. 

    @param maxVal: The maximum allowable value for the integer as read in. 

    @param fatalErrorOnFailure: Set to True to cause a fatal error when I can't do the conversion. Otherwise,
    I will generate a ValueError exception.
    
    @return: The integer equivelent of the passed string
    
    >>> convertStrToInt("1", fatalErrorOnFailure=False)
    1
    >>> convertStrToInt("-1", fatalErrorOnFailure=False, isPortNum=False)
    -1
    >>> convertStrToInt("101012", fatalErrorOnFailure=False)
    101012
    >>> convertStrToInt("1", fatalErrorOnFailure=False, isPortNum=True)
    1
    >>> convertStrToInt("65534", fatalErrorOnFailure=False, isPortNum=True)
    65534
    >>> convertStrToInt("-1", fatalErrorOnFailure=False, isPortNum=True)
    Traceback (most recent call last):
        ...
    ValueError: "-1" is not a valid port number. It must be between 1 and 65534
    >>> convertStrToInt("65535", fatalErrorOnFailure=False, isPortNum=True)
    Traceback (most recent call last):
        ...
    ValueError: "65535" is not a valid port number. It must be between 1 and 65534
    """
    assert(isinstance(stringData, (str, unicode,)))
    
    try:
        result = int(stringData)
        
        if isPortNum and (result < 1 or result > 65534):
            if fatalErrorOnFailure:
                if configDirectiveName:
                    errorutil.triggerFatalError("Could not parse the configuration directive named \"%s\": "
                        "\"%s\" is not a valid port number (1-65534)" % (configDirectiveName, stringData))
                else:
                    errorutil.triggerFatalError(
                        "\"%s\" is not a valid port number. It must be between 1 and 65534"
                        % stringData)
            else:
                raise ValueError(
                    "\"%s\" is not a valid port number. It must be between 1 and 65534" % stringData)
        elif not isPortNum and result < minVal:
                if configDirectiveName:
                    errorutil.triggerFatalError("Could not parse the configuration directive named \"%s\": "
                        "\"%s\" is less than the allowable minimum (%i)" % (configDirectiveName, stringData, minVal))
                else:
                    errorutil.triggerFatalError(
                        "\"%s\" is not a valid port number. It is less than the allowable minimum (%i)"
                        % stringData, minVal)
        elif not isPortNum and result > maxVal:
                if configDirectiveName:
                    errorutil.triggerFatalError("Could not parse the configuration directive named \"%s\": "
                        "\"%s\" is greater than the allowable maximum (%i)" % (configDirectiveName, stringData, maxVal))
                else:
                    errorutil.triggerFatalError(
                        "\"%s\" is not a valid port number. It is greater than the allowable maximum (%i)"
                        % stringData, maxVal)
        return result
    except ValueError:
        if fatalErrorOnFailure:
            if configDirectiveName:
                errorutil.triggerFatalError("Could not parse the configuration directive named \"%s\": "
                    "Instead of it's current value, \"%s\", it must be a numerical value "
                    "(e.g. no non-digit characters allowed)" % (configDirectiveName, stringData))
            else:
                errorutil.triggerFatalError("Could not parse string \"%s\" into an integer value" % stringData)
        else:
            raise
def convertStrToBool(stringData, configDirectiveName='', fatalErrorOnFailure=True):
    """I convert a string variable into it's corresponding boolean variable equivelent. By default,
    everything EXCEPT 'true', 't', '1', or 'yes' (in upper or lower case) is interpreted as False

    @param stringData: The string to interpret

    @param configDirectiveName: If this conversion is being done for the parsing of a configuration directive,
    this parameter should be set to the name of that directive. This will allow me to give more informative
    error output to the user in the event of conversion failure (when fatalErrorOnFailure is set to True as well)

    @param fatalErrorOnFailure: Set to True to cause a fatal error when I can't do the conversion. Otherwise,
    I will generate a ValueError exception.
    
    @return: The boolean equivelent of the passed string
    
    >>> convertStrToBool("true", fatalErrorOnFailure=False)
    True
    >>> convertStrToBool("T", fatalErrorOnFailure=False)
    True
    >>> convertStrToBool("YeS", fatalErrorOnFailure=False)
    True
    >>> convertStrToBool("0", fatalErrorOnFailure=False)
    False
    >>> convertStrToBool("FALSE", fatalErrorOnFailure=False)
    False
    >>> convertStrToBool("f", fatalErrorOnFailure=False)
    False
    >>> convertStrToBool("nO", fatalErrorOnFailure=False)
    False
    
    >>> convertStrToBool("bla", fatalErrorOnFailure=False)
    Traceback (most recent call last):
        ...
    ValueError: Cannot convert string to boolean
    
    >>> convertStrToBool(2, fatalErrorOnFailure=False)
    Traceback (most recent call last):
        ...
    AssertionError

    >>> convertStrToBool(False, fatalErrorOnFailure=False)
    Traceback (most recent call last):
        ...
    AssertionError
    """
    assert(isinstance(stringData, basestring))
    
    stringData = stringData.lower()
    if stringData in ('true', 't', '1', 'yes'):
        return True
    elif stringData in ('false', 'f', '0', 'no'):
        return False
    elif fatalErrorOnFailure:
        if configDirectiveName:
            errorutil.triggerFatalError("Could not parse the configuration directive named \"%s\": "
                "Please set this to \"True\" or \"False\", instead of its current value, \"%s\""
                % (configDirectiveName, stringData))
        else:
            errorutil.triggerFatalError("Could not parse string into boolean value")
    else:
        raise ValueError("Cannot convert string to boolean")
Exemplo n.º 8
0
 def _periodicCheckForMaxConnectivityOutage(self):
     if     self._connectivityDownSince \
        and self._connectivityDownSince + QUERY_BACKLOG_MAX_CONN_DOWN_TIME < time.time():
         errorutil.triggerFatalError(
             "Loss of DB connectivity for pool \"%s\" has exceeded max allowable length of %i seconds. Exiting!"
             % (self._poolName, QUERY_BACKLOG_MAX_CONN_DOWN_TIME))