Пример #1
0
def agentFunctionReportAgentConfig(functionParameterValues):
    comDavra.logInfo('Function: Reporting the agent config to server')
    comDavra.reportDeviceConfigurationToServer()
    comDavra.upsertJsonEntry(currentFunctionJson, 'response', comDavra.conf)
    comDavra.upsertJsonEntry(currentFunctionJson, 'status', 'completed')
    checkFunctionFinished()
    return
Пример #2
0
def runFunction(functionName, functionParameterValues):
    # Always assign a uuid to a function if not already
    if (functionParameterValues.has_key("functionUuid") == False):
        functionParameterValues["functionUuid"] = comDavra.generateUuid()
    # Put a file to indicate what is happening and start the function
    comDavra.provideFreshDirectory(currentFunctionDir)
    functionInfo = { 'functionName': functionName, \
        'functionParameterValues': functionParameterValues, \
        'status': 'running', \
        'startTime': comDavra.getMilliSecondsSinceEpoch() }
    with open(currentFunctionDir + '/currentFunction.json', 'w') as outfile:
        json.dump(functionInfo, outfile, indent=4)
    # Only run functions which are within capabilities
    if (comDavra.conf["capabilities"].has_key(functionName) is False):
        comDavra.logError(
            'Error: Attemping to to run a function which is not in capabilities: '
            + functionName)
        comDavra.logError('Error: Capabilities: ' +
                          str(comDavra.conf["capabilities"]))
        comDavra.upsertJsonEntry(currentFunctionJson, 'status', 'failed')
        return
    #
    comDavra.log('Running a function:' + json.dumps(functionInfo))
    flagIsFunctionRunning = True
    #
    # Is this capability something the agent knows how to do
    if (functionName in agentCapabilityFunctions):
        comDavra.log('Will run this function within the agent')
        agentCapabilityFunctions[functionName](functionParameterValues)
    else:
        # Send it onwards to the apps who may be able to do it
        comDavra.log(
            'Will run this function within an app rather than the agent')
        sendMessageFromAgentToApps(functionInfo)
    return
Пример #3
0
def updateFunctionStatusAsReportedByDeviceApp(functionInfo):
    comDavra.log('App reported it finished a function ' + str(functionInfo))
    comDavra.upsertJsonEntry(currentFunctionJson, 'response',
                             functionInfo["response"])
    comDavra.upsertJsonEntry(currentFunctionJson, 'status',
                             functionInfo["status"])
    checkFunctionFinished()
    return
Пример #4
0
def agentFunctionUpdateAgentConfig(functionParameterValues):
    comDavra.logInfo('Function: Updating the agent config to server ' +
                     str(functionParameterValues))
    comDavra.upsertConfigurationItem(functionParameterValues["key"],
                                     functionParameterValues["value"])
    comDavra.reportDeviceConfigurationToServer()
    comDavra.upsertJsonEntry(currentFunctionJson, 'response', comDavra.conf)
    comDavra.upsertJsonEntry(currentFunctionJson, 'status', 'completed')
    checkFunctionFinished()
    return
Пример #5
0
def agentFunctionRunScriptBash(functionParameterValues):
    if "script" not in functionParameterValues:
        comDavra.upsertJsonEntry(currentFunctionJson, 'response',
                                 'script missing')
        comDavra.upsertJsonEntry(currentFunctionJson, 'status', 'failed')
        comDavra.logError(
            'Could not run script as function because no script to run')
        checkFunctionFinished()
        return
    # Put the script into the function dir
    scriptFile = open(currentFunctionDir + "/script.sh", "a")
    scriptFile.write(str(functionParameterValues["script"]))
    scriptFile.close()
    comDavra.logInfo('Running script ' +
                     str(functionParameterValues["script"]))
    os.system("chmod 777 " + currentFunctionDir + "/script.sh")
    time.sleep(0.5)  # Time for file flush to disk
    # Run the script with -x flag so it prints each command before ruuning it.
    # This allows the UI to show it formatted better for user on jobs page
    scriptResponse = comDavra.runCommandWithTimeout('cd ' + currentFunctionDir + \
    ' && sudo bash -x ' + currentFunctionDir + "/script.sh", comDavra.conf["scriptMaxTime"])
    # scriptResponse is a tuple of (exitStatusCode, stdout) . For exitStatusCode: 0 = success, 1 = failed )
    comDavra.log("Script response: " + str(scriptResponse[1]))
    scriptStatus = 'completed' if (scriptResponse[0] == 0) else 'failed'
    comDavra.upsertJsonEntry(currentFunctionJson, 'response',
                             str(scriptResponse[1]))
    comDavra.upsertJsonEntry(currentFunctionJson, 'status', scriptStatus)
    checkFunctionFinished()
Пример #6
0
def checkIfJustBackAfterRebootTask():
    currentFunctionInfo = None
    if (os.path.isfile(currentFunctionJson) == True):
        with open(currentFunctionJson) as data_file:
            currentFunctionInfo = json.load(data_file)
        if (currentFunctionInfo["functionName"] == 'agent-action-rebootDevice'
            ):
            comDavra.log(
                'checkIfJustBackAfterRebootTask: True. Function completed')
            comDavra.upsertJsonEntry(currentFunctionJson, 'response',
                                     str(comDavra.getUptime()))
            comDavra.upsertJsonEntry(currentFunctionJson, 'status',
                                     'completed')
            checkFunctionFinished()
Пример #7
0
def agentFunctionPushAppWithInstaller(functionParameterValues):
    comDavra.logInfo(
        'Function: Pushing Application onto device to run as a service ' +
        str(functionParameterValues))
    if (functionParameterValues["Installation File"]):
        installationFile = functionParameterValues["Installation File"]
        # Download the app tarball
        try:
            tmpPath = '/tmp/' + str(comDavra.getMilliSecondsSinceEpoch())
            comDavra.ensureDirectoryExists(tmpPath)
            comDavra.runCommandWithTimeout(
                'cd ' + tmpPath + ' && /usr/bin/curl -LO ' + installationFile,
                300)
            comDavra.runCommandWithTimeout(
                'cd ' + tmpPath + ' && /bin/tar -xvf ./* ', 300)
            comDavra.runCommandWithTimeout(
                'cd ' + tmpPath + ' && chmod 777 ./* ', 300)
            # Ensure the install.sh is unix format
            comDavra.runCommandWithTimeout(
                "cd " + tmpPath + " &&  sed -i $'s/\r$//' install.sh ", 30)
            installedAppPath = comDavra.installationDir + '/apps/' + str(
                comDavra.getMilliSecondsSinceEpoch())
            comDavra.ensureDirectoryExists(installedAppPath)
            comDavra.runCommandWithTimeout(
                'cd ' + tmpPath + ' && cp -r * ' + installedAppPath, 300)
            installResponse = comDavra.runCommandWithTimeout(
                'cd ' + installedAppPath + ' && bash ./install.sh ',
                comDavra.conf["scriptMaxTime"])
            comDavra.log('Installation response: ' + str(installResponse[1]))
            scriptStatus = 'completed' if (installResponse[0]
                                           == 0) else 'failed'
            comDavra.upsertJsonEntry(currentFunctionJson, 'response',
                                     str(installResponse[1]))
            comDavra.upsertJsonEntry(currentFunctionJson, 'status',
                                     scriptStatus)
        except Exception as e:
            comDavra.logError('Failed to download application:' +
                              installationFile + " : Error: " + str(e))
            comDavra.upsertJsonEntry(currentFunctionJson, 'status', 'failed')
            checkFunctionFinished()
        comDavra.log('Finished agentFunctionPushAppWithInstaller')
        checkFunctionFinished()
    else:
        comDavra.logWarning('Action parameters missing, nothing to do')
    # TODO
    return