Ejemplo n.º 1
0
 def acceptDelta(self, deltapath):
     """Accept delta."""
     jOut = getAllHosts(self.sitename, self.logger)
     fileContent = self.siteDB.getFileContentAsJson(deltapath)
     os.unlink(deltapath)  # File is not needed anymore.
     toDict = dict(fileContent)
     toDict["State"] = "accepting"
     outputDict = {'addition': '', 'reduction': ''}
     try:
         self.logger.info(toDict["Content"])
         self.logger.info(type(toDict["Content"]))
         for key in ['reduction', 'addition']:
             if key in toDict["Content"] and toDict["Content"][key]:
                 self.logger.info('Got Content %s for key %s',
                                  toDict["Content"][key], key)
                 tmpFile = tempfile.NamedTemporaryFile(delete=False,
                                                       mode="w+")
                 try:
                     tmpFile.write(toDict["Content"][key])
                 except ValueError as ex:
                     self.logger.info(
                         'Received ValueError. More details %s. Try to write normally with decode',
                         ex)
                     tmpFile.write(decodebase64(toDict["Content"][key]))
                 tmpFile.close()
                 outputDict[key] = self.parseDeltaRequest(
                     tmpFile.name, jOut)
                 os.unlink(tmpFile.name)
     except (IOError, KeyError, AttributeError, IndentationError,
             ValueError, BadSyntax, HostNotFound,
             UnrecognizedDeltaOption) as ex:
         outputDict = getError(ex)
     dbobj = getVal(self.dbI, sitename=self.sitename)
     if 'errorType' in list(outputDict.keys()):
         toDict["State"] = "failed"
         toDict["Error"] = outputDict
         toDict['ParsedDelta'] = {'addition': '', 'reduction': ''}
         self.stateMachine.failed(dbobj, toDict)
     else:
         toDict["State"] = "accepted"
         connID = []
         toDict["ParsedDelta"] = outputDict
         toDict['modadd'] = 'idle'
         for key in outputDict:
             if not outputDict[key]:
                 continue
             toDict['Type'] = 'modify' if 'Type' in toDict.keys() else key
             # In case of modify, only addition connection IDs are stored;
             # otherwise, corresponding type connectionIDs
             if toDict['Type'] == 'modify':
                 connID = []
                 for item in outputDict['addition']:
                     connID.append(item['connectionID'])
             else:
                 for item in outputDict[key]:
                     connID.append(item['connectionID'])
         toDict['ConnID'] = connID
         self.stateMachine.accepted(dbobj, toDict)
         # =================================
     return toDict
Ejemplo n.º 2
0
def deltas(environ, **kwargs):
    """
    API Call associated with deltas
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/deltas/ # Will return info about all deltas
    Method: POST
    Output: application/json
    Examples: https://server-host/sitefe/v1/deltas/ # Will add new delta and returns it's ID
    """
    # ======================================================
    # GET
    if environ['REQUEST_METHOD'].upper() == 'GET':
        modTime = getModTime(kwargs['headers'])
        outdeltas = DELTABACKEND.getdelta(None, **kwargs)
        if kwargs['urlParams']['oldview']:
            print 'Return All deltas. 200 OK'
            kwargs['http_respond'].ret_200('application/json',
                                           kwargs['start_response'], None)
            return outdeltas
        outM = {"deltas": []}
        if not outdeltas:
            kwargs['http_respond'].ret_204(
                'application/json', kwargs['start_response'],
                [('Last-Modified', httpdate(getUTCnow()))])
            print 'Return empty list. There are no deltas on the system'
            return []
        updateTimestamp = 0
        for delta in outdeltas:
            if modTime > delta['updatedate']:
                continue
            updateTimestamp = updateTimestamp if updateTimestamp > delta[
                'updatedate'] else delta['updatedate']
            current = {
                "id": delta['uid'],
                "lastModified": convertTSToDatetime(delta['updatedate']),
                "state": delta['state'],
                "href": "%s/%s" % (environ['SCRIPT_URI'], delta['uid']),
                "modelId": delta['modelid']
            }
            if not kwargs['urlParams']['summary']:
                # Doing here not encode, because we are decoding. So it is opposite.
                current["addition"] = decodebase64(
                    delta['addition'], not kwargs['urlParams']['encode'])
                current["reduction"] = decodebase64(
                    delta['reduction'], not kwargs['urlParams']['encode'])
            outM["deltas"].append(current)
        if not outM["deltas"]:
            kwargs['http_respond'].ret_304(
                'application/json', kwargs['start_response'],
                ('Last-Modified', httpdate(modTime)))
            return []
        kwargs['http_respond'].ret_200(
            'application/json', kwargs['start_response'],
            [('Last-Modified', httpdate(updateTimestamp))])
        print 'Return Last Delta. 200 OK'
        return outM["deltas"]
    # ======================================================
    # POST
    out = {}
    postRequest = False
    if environ['REQUEST_METHOD'].upper() == 'POST':
        postRequest = is_post_request(environ)
    if not postRequest:
        if is_application_json(environ):
            out = get_json_post_form(environ)
        else:
            kwargs['http_respond'].ret_400('application/json',
                                           kwargs['start_response'], None)
            customErr = getCustomOutMsg(
                errMsg=
                'You did POST method, but provided CONTENT_TYPE is not correct',
                errCode=400)
            print 'Return 400. More details: %s' % customErr
            return customErr
    if not out:
        out = get_post_form(environ)
    newDelta = {}
    for key in out.keys():
        newDelta[key] = out.get(key, "")
    for key in ['modelId', 'id']:
        if not newDelta[key]:
            customErr = getCustomOutMsg(
                errMsg='You did POST method, %s is not specified' % key,
                errCode=400)
            print 'Wrong delta: %s. Parsed:%s Error: %s' % (out, newDelta,
                                                            customErr)
            kwargs['http_respond'].ret_400('application/json',
                                           kwargs['start_response'], None)
            return customErr
    if not newDelta['reduction'] and not newDelta['addition']:
        customErr = getCustomOutMsg(
            errMsg=
            'You did POST method, but nor reduction, nor addition is present',
            errCode=400)
        print 'Wrong delta: %s. Parsed:%s Error: %s' % (out, newDelta,
                                                        customErr)
        kwargs['http_respond'].ret_400('application/json',
                                       kwargs['start_response'], None)
        return customErr
    return DELTABACKEND.addNewDelta(newDelta, environ, **kwargs)
Ejemplo n.º 3
0
def getdeltaAll(sitename, deltaUID):
    dbI = getDBConn('analyzedelta')
    dbobj = getVal(dbI, sitename=sitename)
    policer = polS.PolicyService(CONFIG, LOGGER)
    for delta in dbobj.get('deltas'):
        if delta['uid'] != deltaUID:
            continue
        delta['addition'] = evaldict(delta['addition'])
        delta['reduction'] = evaldict(delta['reduction'])
        print('=' * 80)
        print('Delta UID  :  ', delta['uid'])
        print('Delta RedID:  ', delta['reductionid'])
        print('Delta State:  ', delta['state'])
        print('Delta ModAdd: ', delta['modadd'])
        print('Delta InsDate:', delta['insertdate'])
        print('Delta Update: ', delta['updatedate'])
        print('Delta Model:  ', delta['modelid'])
        print('Delta connID: ', delta['connectionid'])
        print('Delta Deltatype: ', delta['deltat'])
        print('-' * 20)
        import pprint
        pprint.pprint(delta)
        print('Delta times')
        for deltatimes in dbobj.get('states',
                                    search=[['deltaid', delta['uid']]]):
            print('State: %s Date: %s' %
                  (deltatimes['state'], deltatimes['insertdate']))
        if delta['deltat'] in ['reduction', 'addition']:
            for hostname in list(delta[delta['deltat']]['hosts'].keys()):
                print('-' * 20)
                print('Host States %s' % hostname)
                for hoststate in dbobj.get('hoststates',
                                           search=[['deltaid', delta['uid']],
                                                   ['hostname', hostname]]):
                    print('Host %s State %s' % (hostname, hoststate['state']))
                    print('Insertdate %s UpdateDate %s' %
                          (hoststate['insertdate'], hoststate['updatedate']))
                    print('-' * 20)
                    print('Host State History')
                    for hstatehistory in dbobj.get(
                            'hoststateshistory',
                            search=[['deltaid', delta['uid']],
                                    ['hostname', hostname]]):
                        print('State: %s, Date: %s' %
                              (hstatehistory['state'],
                               hstatehistory['insertdate']))
        toDict = ast.literal_eval(str(delta['content']))
        jOut = getAllHosts(sitename, LOGGER)
        for key in ['reduction', 'addition']:
            print(list(toDict.keys()))
            if key in toDict and toDict[key]:
                print('Got Content %s for key %s', toDict[key], key)
                tmpFile = tempfile.NamedTemporaryFile(delete=False, mode="w+")
                try:
                    tmpFile.write(toDict[key])
                except ValueError as ex:
                    print(
                        'Received ValueError. More details %s. Try to write normally with decode',
                        ex)
                    tmpFile.write(decodebase64(toDict["Content"][key]))
                tmpFile.close()
                # outputDict[key] = self.parseDeltaRequest(tmpFile.name, jOut)
                print("For %s this is delta location %s" % (key, tmpFile.name))
            out = policer.parseDeltaRequest(tmpFile.name, jOut)
            if not out:
                out = policer.parseDeltaRequest(tmpFile.name, jOut, sitename)
                print(out)
Ejemplo n.º 4
0
 def acceptDelta(self, deltapath, sitename):
     jOut = getAllHosts(sitename, self.logger)
     fileContent = self.siteDB.getFileContentAsJson(deltapath)
     os.unlink(deltapath)  # File is not needed anymore.
     toDict = dict(fileContent)
     toDict["State"] = "accepting"
     outputDict = {'addition': '', 'reduction': ''}
     try:
         self.logger.info(toDict["Content"])
         for key in ['reduction', 'addition']:
             if key in toDict["Content"] and toDict["Content"][key]:
                 self.logger.info('Got Content %s for key %s',
                                  toDict["Content"][key], key)
                 tmpFile = tempfile.NamedTemporaryFile(delete=False)
                 try:
                     tmpFile.write(toDict["Content"][key])
                 except ValueError as ex:
                     self.logger.info(
                         'Received ValueError. More details %s. Try to write normally with decode',
                         ex)
                     tmpFile.write(decodebase64(toDict["Content"][key]))
                 tmpFile.close()
                 outputDict[key] = self.parseDeltaRequest(
                     tmpFile.name, jOut, sitename)
                 self.logger.info("For %s this is delta location %s" %
                                  (key, tmpFile.name))
                 # os.unlink(tmpFile.name)
     except (IOError, KeyError, AttributeError, IndentationError,
             ValueError, BadSyntax, HostNotFound,
             UnrecognizedDeltaOption) as ex:
         outputDict = getError(ex)
     dbobj = getVal(self.dbI, sitename=sitename)
     if 'errorType' in outputDict.keys():
         toDict["State"] = "failed"
         toDict["Error"] = outputDict
         toDict['ParsedDelta'] = {'addition': '', 'reduction': ''}
         self.stateMachine.failed(dbobj, toDict)
     else:
         toDict["State"] = "accepted"
         toDict["ParsedDelta"] = outputDict
         dtype = None
         connID = None
         for key in outputDict:
             if not outputDict[key]:
                 continue
             # If key is reduction. Find out which one.
             # So this check will not be needed anymore.
             dtype = key
             connID = outputDict[key]['connectionID']
             if key == 'reduction':
                 if "ReductionID" not in outputDict.keys():
                     self.logger.info('Trying to identify which to delete')
                     reductionIDMap = self.reductionCompare(
                         sitename, outputDict[key]['connectionID'])
                     toDict["ReductionID"] = reductionIDMap
                 else:
                     self.logger.info('ReductionID is already defined.')
         toDict['Type'] = dtype
         toDict['ConnID'] = connID
         toDict['modadd'] = 'idle'
         self.stateMachine.accepted(dbobj, toDict)
         # =================================
     return toDict