Example #1
0
 def checkHostStates(self):
     """Check Host State deltas."""
     # Checking all active host states and set to cancel only ones which deltas are in:
     # for addition deltas - only removed state
     # for reduction deltas - remove and removed
     # ===========================================================================================
     states = self.getHostStates('active')
     for state in states:
         if state['state'] != 'active':
             continue
         deltaInfo = self.getDeltaInfo(state['deltaid'])
         if not deltaInfo:
             self.logger.debug('FE did not return anything for %s' %
                               state['deltaid'])
             continue
         if deltaInfo[0]['state'] in [
                 'remove', 'removed'
         ] and deltaInfo[0]['deltat'] in ['addition', 'modify']:
             self.cancelResources(
                 evaldict(deltaInfo[0]['addition'])[0], state['deltaid'])
             self.setHostState('cancel', state['deltaid'])
         elif deltaInfo[0]['state'] in [
                 'remove', 'removed'
         ] and deltaInfo[0]['deltat'] == 'reduction':
             self.cancelResources(
                 evaldict(deltaInfo[0]['reduction'])[0], state['deltaid'])
             self.setHostState('cancel', state['deltaid'])
         elif deltaInfo[0]['state'] in [
                 'activated'
         ] and deltaInfo[0]['deltat'] == 'reduction':
             self.setHostState('activated', state['deltaid'])
Example #2
0
 def _deltaAddition(self, dbObj, delta, mainGraphName, updateState=True):
     """Delta addition lookup."""
     delta['content'] = evaldict(delta['content'])
     self.logger.info('Working on %s delta addition in state' %
                      delta['uid'])
     mainGraph = Graph()
     mainGraph.parse(mainGraphName, format='turtle')
     addition = delta['content']['addition']
     tmpFile = tempfile.NamedTemporaryFile(delete=False, mode="w+")
     tmpFile.write(addition)
     tmpFile.close()
     tmpGraph = Graph()
     tmpGraph.parse(tmpFile.name, format='turtle')
     os.unlink(tmpFile.name)
     self.logger.info('Main Graph len: %s Addition Len: %s', len(mainGraph),
                      len(tmpGraph))
     mainGraph += tmpGraph
     # Add delta states for that specific delta
     mainGraph = self._addDeltaStatesInModel(mainGraph, delta['state'],
                                             evaldict(delta['addition']))
     if updateState:
         dbObj.update('deltasmod', [{
             'uid': delta['uid'],
             'updatedate': getUTCnow(),
             'modadd': 'added'
         }])
     return mainGraph
Example #3
0
 def getData(self, url):
     """ Get data from FE """
     self.logger.info('Query: %s%s' % (self.fullURL, url))
     out = getDataFromSiteFE({}, self.fullURL, url)
     if out[2] != 'OK':
         msg = 'Received a failure getting information from Site Frontend %s' % str(out)
         self.logger.critical(msg)
         return {}
     if self.debug:
         self.pretty.pprint(evaldict(out[0]))
     self.logger.info('End function checkdeltas')
     return evaldict(out[0])
Example #4
0
 def getData(self, url):
     """Get data from FE."""
     self.logger.info('Query: %s%s' % (self.fullURL, url))
     out = getDataFromSiteFE({}, self.fullURL, url)
     if out[2] != 'OK':
         msg = 'Received a failure getting information from Site Frontend %s' % str(
             out)
         self.logger.critical(msg)
         return {}
     if self.config.getboolean('general', "debug"):
         pretty = pprint.PrettyPrinter(indent=4)
         self.logger.debug(pretty.pprint(evaldict(out[0])))
     return evaldict(out[0])
Example #5
0
 def committed(self, dbObj):
     """Committing state Check."""
     for delta in dbObj.get('deltas', search=[['state', 'committed']]):
         if delta['deltat'] in ['addition', 'modify'] and delta['addition']:
             delta['addition'] = evaldict(delta['addition'])
             # Check the times...
             delayStart = False
             for connDelta in delta['addition']:
                 try:
                     if 'timestart' in list(connDelta.keys()):
                         timeleft = int(
                             connDelta['timestart']) - getUTCnow()
                         self.logger.info(
                             'CurrentTime %s TimeStart %s. Seconds Left %s'
                             % (getUTCnow(), connDelta['timestart'],
                                timeleft))
                         if connDelta['timestart'] < getUTCnow():
                             self.logger.info(
                                 'This delta already passed timestart mark. \
                                              Setting state to activating')
                             delayStart = not delayStart
                         else:
                             self.logger.info(
                                 'This delta %s did not passed timestart mark. \
                                              Leaving state as it is' %
                                 delta['uid'])
                             delayStart = True
                 except:
                     self.logger.info(
                         'This delta %s had an error checking starttime. \
                                      Leaving state as it is.' %
                         delta['uid'])
                 if not delayStart:
                     self._stateChangerDelta(dbObj, 'activating', **delta)
                     self.connMgr.activating(dbObj, delta)
         else:
             self.logger.info(
                 'This delta %s is in committed. Setting state to activating.'
                 % delta['uid'])
             self._stateChangerDelta(dbObj, 'activating', **delta)
             if delta['deltat'] in ['reduction']:
                 for connid in evaldict(delta['connectionid']):
                     for dConn in dbObj.get(
                             'delta_connections',
                             search=[['connectionid', connid]]):
                         self.logger.info(
                             'This connection %s belongs to this delta: %s. Cancel delta'
                             % (connid, dConn['deltaid']))
                         deltaRem = {'uid': dConn['deltaid']}
                         self._stateChangerDelta(dbObj, 'remove',
                                                 **deltaRem)
Example #6
0
def startwork(config=None, logger=None):
    """Main start """
    errors = []
    agents = getDataFromSiteFE({}, "http://localhost/",
                               "/sitefe/json/frontend/getdata")
    if agents[2] != 'OK':
        print 'Received a failure getting information from Site Frontend %s' % str(
            agents)
        return
    workDir = CONFIG.get('frontend', 'privatedir') + "/notificationService/"
    mailingSender = CONFIG.get('NotificationService', 'mailingSender')
    mailingList = CONFIG.get('NotificationService', 'mailingList').split(',')
    createDirs(workDir)
    jOut = {}
    try:
        jOut = evaldict(agents[0])
    except FailedToParseError as ex:
        print 'Server returned not a json loadable object. Raising error. Output %s. Errors: %s' % (
            str(agents), ex)
        return
    # We start with simple error messages
    for ipaddr, values in jOut.items():
        # Check if there is any error first
        checkPluginErrors(ipaddr, values, errors)
        checkCertLifeTime(ipaddr, values, errors)
        warningsFromMonComponent(ipaddr, values, errors)
    # Compare errors with previous run and send email only if there is something new...
    lastErrors = readFile(str(workDir + "lastRunErrors.json"))
    if lastErrors:
        try:
            lastErrors = evaldict(lastErrors[0])
        except FailedToParseError as ex:
            print 'Loaded object from the system is not evaluable. Raising error. \
                   Output %s. Errors: %s' % (str(lastErrors), ex)
            print 'Ignoring and continue as there was no errors before'
            lastErrors = []
    newErrors = []
    if lastErrors and errors:
        newErrors = compareErrors(lastErrors, errors)
    elif errors:
        # Means there is no previous errors.
        print errors
    elif lastErrors and not errors:
        print 'All errors were resolved...'
    print lastErrors, errors, newErrors
    if newErrors:
        prepareMailSend(newErrors, mailingSender, mailingList)
    writeNewFile(errors, workDir)
    return
Example #7
0
 def activating(self, dbObj):
     """Check on all deltas in state activating."""
     for delta in dbObj.get('deltas', search=[['state', 'activating']]):
         hostStates = {}
         delta['addition'] = evaldict(delta['addition'])
         delta['reduction'] = evaldict(delta['reduction'])
         for actionKey in ['reduction', 'addition']:
             if actionKey not in list(delta.keys()):
                 self.logger.info(
                     'This delta %s does not have yet actionKey %s defined.'
                     % (delta['uid'], actionKey))
                 continue
             if not isinstance(delta[actionKey], list):
                 self.logger.info(
                     'This delta %s does not have yet actionKey defined.' %
                     delta['uid'])
                 continue
             for connDelta in delta[actionKey]:
                 hostStates = {}
                 if 'hosts' not in list(connDelta.keys()):
                     self.logger.info(
                         'This delta %s does not have yet hosts defined.' %
                         delta['uid'])
                     continue
                 for hostname in list(connDelta['hosts'].keys()):
                     host = dbObj.get('hoststates',
                                      search=[['deltaid', delta['uid']],
                                              ['hostname', hostname]])
                     if host:
                         hostStates[host[0]['state']] = hostname
                     else:
                         self._newhoststate(
                             dbObj, **{
                                 'hostname': hostname,
                                 'state': 'activating',
                                 'deltaid': delta['uid']
                             })
                         hostStates['unset'] = hostname
             self.logger.info('Delta %s host states are: %s' %
                              (delta['uid'], hostStates))
             if timeendcheck(delta, self.logger):
                 self._stateChangerDelta(dbObj, 'cancel', **delta)
                 self.modelstatecancel(dbObj, **delta)
             if list(hostStates.keys()) == ['active']:
                 self._stateChangerDelta(dbObj, 'activated', **delta)
                 self.connMgr.activated(dbObj, delta)
             elif 'failed' in list(hostStates.keys()):
                 self._stateChangerDelta(dbObj, 'failed', **delta)
Example #8
0
 def start(self):
     """Start execution and get new requests from FE."""
     allWork = self.getAllAssignedtoHost()
     out, err, exitCode = "", "", 0
     for item in allWork:
         self.logger.debug("Work on: %s" % item)
         try:
             item['requestdict'] = evaldict(item['requestdict'])
             if item['requestdict']['type'] == 'rapidping':
                 out, err, exitCode = rapidping(item['requestdict'])
             elif item['requestdict']['type'] == 'tcpdump':
                 out, err, exitCode = tcpdump(item['requestdict'])
             elif item['requestdict']['type'] == 'arptable':
                 out, err, exitCode = arptable(item['requestdict'])
             elif item['requestdict']['type'] == 'iperf':
                 out, err, exitCode = iperf(item['requestdict'])
             elif item['requestdict']['type'] == 'iperfserver':
                 out, err, exitCode = iperfserver(item['requestdict'])
             else:
                 err = "Unknown Request"
                 exitCode = 500
         except:
             err = traceback.format_exc()
             exitCode = 501
         output = {'out': out, 'err': err, 'exitCode': exitCode}
         item['output'] = json.dumps(output)
         if exitCode != 0:
             item['state'] = 'failed'
         else:
             item['state'] = 'finished'
         self.logger.debug("Finish work on: %s" % item)
         self.publishToFE(item)
Example #9
0
    def modify(self, deltaInfo, state, informFE=True):
        """Modify of resources."""
        # TODO: For now - this is very basic modify. Tier down what reduction told us to do
        # And do addition for new resources. In future we should check if it is same vlan
        # or what are the changes. Possible changes I can think of now are:
        # Change routes;
        # Change IP;
        # Change VLAN - this will require to tier down and bring new interface;
        # Change QoS

        # First we try to identify which delta must be set into state remove
        # This means that with a new state modify - agents can also change delta states
        # while it was not possible before and was always done at FE Level.
        reduct = evaldict(deltaInfo[0]['reduction'])
        allProvisioned = self.vlansProvisioned()
        for item in allProvisioned:
            if item['connectionID'] == reduct[0]['connectionID']:
                # Means vlan is still present; do remove in FE
                # And return False, no further actions until previous delta removed
                # TODO: Later it will do smarter things depending on action:
                # Idea will be:
                #   if same vlan, remove file, set delta state remove and host state - removed and apply changes
                #   if diff vlan, set only delta state to remove, and wait for tier down of resources
                self.logger.info(
                    'Main delta based on conn ID is still active. Setting it to Remove state'
                )
                self.setHostState('remove', item['uid'])
                return
        self.addition(deltaInfo, state)
Example #10
0
def checkConflicts(dbObj, delta):
    """Check conflicting resources and not allow them"""
    delta['addition'] = evaldict(delta['addition'])
    for connDelta in delta['addition']:
        deltaIN = getdeltaInfo(connDelta, delta)
        checkNewDelta(deltaIN)
        for state in ['committing', 'committed', 'activating', 'activated']:
            for deltat in ['addition', 'modify']:
                for dbdelta in dbObj.get('deltas',
                                         search=[['state', state],
                                                 ['deltat', deltat]]):
                    print(dbdelta)
                    dbdelta['addition'] = evaldict(dbdelta['addition'])
                    for connDelta1 in dbdelta['addition']:
                        deltaInDB = getdeltaInfo(connDelta1, delta)
                        compareTwoDeltaTimes(deltaInDB, deltaIN)
Example #11
0
 def checkAllFiles(self):
     """Check All deltas active on the host."""
     self.logger.info('Started function checkAllFiles start')
     for fileName in glob.glob("%s/*.json" % self.workDir):
         inputDict = getFileContentAsJson(fileName)
         if 'uid' not in inputDict.keys():
             self.logger.info(
                 'Seems this dictionary is custom delta. Ignoring it.')
             continue
         deltaInfo = self.getDeltaInfo(inputDict['uid'])
         if not deltaInfo:
             self.logger.debug('FE did not return anything for %s' %
                               inputDict['uid'])
             continue
         if deltaInfo[0]['state'] in [
                 'remove', 'removing', 'removed', 'cancel', 'failed'
         ]:
             if os.path.isfile(fileName):
                 os.remove(fileName)
         elif deltaInfo[0]['state'] in [
                 'activating', 'activated'
         ] and deltaInfo[0]['deltat'] in ['addition', 'modify']:
             deltaInfo[0]['addition'] = evaldict(deltaInfo[0]['addition'])
             self.checkResources(deltaInfo[0]['addition'][0],
                                 inputDict['uid'])
         else:
             self.logger.warning("Delta is unknown: %s" % deltaInfo)
Example #12
0
def startwork(config=None, logger=None):
    """Main start """
    fullURL = getFullUrl(config)
    agents = getDataFromSiteFE({}, fullURL, "/sitefe/json/frontend/ips")
    if agents[2] != 'OK':
        print 'Received a failure getting information from Site Frontend %s' % str(
            agents)
        return
    workDir = config.get('frontend', 'privatedir') + "/forwardingService/"
    createDirs(workDir)
    copy2("/etc/httpd/conf.d/sitefe-httpd.conf",
          str(workDir + "httpd-copy.conf"))
    httpdCopy = readFile(str(workDir + "httpd-copy.conf"))
    try:
        newDict = evaldict(agents[0])
    except FailedToParseError as ex:
        print 'Server returned not a json loadable object. Raising error. Output %s. Errors: %s' % (
            str(agents), ex)
        return
    if not newDict:
        print 'Seems server returned empty dictionary. Exiting.'
        return
    newOut, changed = prepareNewHTTPDConfig(newDict, httpdCopy)
    if changed:
        writeNewConfig(newOut, workDir)
        copy2(str(workDir + "httpd-new.conf"),
              "/etc/httpd/conf.d/sitefe-httpd.conf")
        stdout = externalCommand("service httpd restart")
        print stdout
        # Restart apache...
    return
Example #13
0
 def reduction(self, deltaInfo, state, informFE=True):
     """Reduction of resouces."""
     deltaInfo[0]['reduction'] = evaldict(deltaInfo[0]['reduction'])
     for connDelta in deltaInfo[0]['reduction']:
         self.cancelResources(connDelta, state['deltaid'])
         if informFE:
             self.setHostState('active', state['deltaid'])
     return True
Example #14
0
def getdeltaAll(sitename):
    dbI = getDBConn('listalldeltas')
    dbobj = getVal(dbI, sitename=sitename)
    for delta in dbobj.get('deltas'):
        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
        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'] not in ['reduction', 'addition']:
            print 'SOMETHING WRONG WITH THIS DELTA. It does not have any type defined. Was not parsed properly'
            continue
        if not isinstance(delta[delta['deltat']], list):
            conns = [delta[delta['deltat']]]
        else:
            conns = delta[delta['deltat']]
        for conn in conns:
            if 'hosts' not in conn.keys():
                print 'SOMETHING WRONG WITH THIS DELTA. It does not have any hosts defined.'
                continue
            for hostname in conn['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'])
        print '-'*20
        print 'Connection details'
        for conn in conns:
            for dConn in dbobj.get('delta_connections', search=[['connectionid', conn['connectionID']]]):
                print dConn
Example #15
0
 def getData(self, fullURL, URLPath):
     """ Get data from FE """
     agents = getDataFromSiteFE({}, fullURL, URLPath)
     if agents[2] != 'OK':
         msg = 'Received a failure getting information from Site Frontend %s' % str(
             agents)
         self.logger.debug(msg)
         return {}
     return evaldict(agents[0])
Example #16
0
 def nodeinfo(self):
     """put  all node information from node reported stats."""
     for _, nodeDict in list(self.nodesInfo.items()):
         hostinfo = evaldict(nodeDict['hostinfo'])
         for intfKey, intfDict in list(
                 hostinfo['NetInfo']["interfaces"].items()):
             breakLoop = False
             for key in [
                     'switch_port', 'switch', 'vlan_range',
                     'available_bandwidth'
             ]:
                 if key not in list(intfDict.keys()):
                     breakLoop = True
             if breakLoop:
                 continue
             if intfDict['switch'] in list(self.output['switches'].keys()):
                 if intfDict['switch_port'] not in list(
                         self.output['switches'][
                             intfDict['switch']].keys()):
                     self.logger.debug(
                         'Frontend Config is not configured to use this Port %s',
                         intfDict['switch_port'])
                     continue
                 switch = intfDict['switch']
                 switchp = intfDict['switch_port']
                 self.output['switches'][switch][switchp] = nodeDict[
                     'hostname']
                 self.output['vlans'][switch][switchp] = {}
                 self.output['vlans'][switch][switchp]['destport'] = intfKey
                 self.output['vlans'][switch][switchp][
                     'hostname'] = nodeDict['hostname']
                 self.output['vlans'][switch][switchp][
                     'desttype'] = 'server'
                 self.output['vlans'][switch][switchp][
                     'vlan_range'] = intfDict['vlan_range']
                 self.output['vlans'][switch][switchp][
                     'capacity'] = intfDict['available_bandwidth']
                 if 'isAlias' in list(intfDict.keys()):
                     self.output['vlans'][switch][switchp][
                         'isAlias'] = intfDict['isAlias']
     if self.config.has_option(self.site, "l3_routing_map"):
         routingMap = self.config.get(self.site, "l3_routing_map")
         self.output['l3_routing'] = evaldict(routingMap)
Example #17
0
 def activating(dbObj, delta):
     """Change specific delta connection id state to commited."""
     if delta['deltat'] in ['addition', 'modify']:
         for connid in evaldict(delta['connectionid']):
             dbOut = {
                 'deltaid': delta['uid'],
                 'connectionid': connid,
                 'state': 'activating'
             }
             dbObj.update('delta_connections', [dbOut])
Example #18
0
 def activating(self, dbObj):
     """ Check on all deltas in state activating. """
     for delta in dbObj.get('deltas', search=[['state', 'activating']]):
         delta['addition'] = evaldict(delta['addition'])
         delta['reduction'] = evaldict(delta['reduction'])
         for actionKey in ['reduction', 'addition']:
             if delta[actionKey].keys() and delta['deltat'] == 'addition':
                 hostStates = {}
                 for hostname in delta[actionKey]['hosts'].keys():
                     host = dbObj.get('hoststates',
                                      search=[['deltaid', delta['uid']],
                                              ['hostname', hostname]])
                     if host:
                         hostStates[host[0]['state']] = hostname
                     else:
                         self._newhoststate(
                             dbObj, **{
                                 'hostname': hostname,
                                 'state': 'activating',
                                 'deltaid': delta['uid']
                             })
                         hostStates['unset'] = hostname
             if actionKey == 'reduction' and delta['deltat'] == 'reduction':
                 tmpID = delta['reductionid']
                 self.logger.info('Reduction for %s....' % tmpID)
                 for delta1 in dbObj.get('deltas',
                                         search=[['uid', tmpID]],
                                         limit=1):
                     currentState = delta1["state"]
                     if currentState not in [
                             'removing', 'remove', 'cancel'
                     ]:
                         self._stateChangerDelta(dbObj, 'removing',
                                                 **delta1)
                     self._stateChangerDelta(dbObj, 'activated', **delta)
             elif actionKey == 'addition' and delta['deltat'] == 'addition':
                 if timeendcheck(delta, self.logger):
                     self._stateChangerDelta(dbObj, 'cancel', **delta)
                     self.modelstatecancel(dbObj, **delta)
                 if hostStates.keys() == ['active']:
                     self._stateChangerDelta(dbObj, 'activated', **delta)
                 elif 'failed' in hostStates.keys():
                     self._stateChangerDelta(dbObj, 'failed', **delta)
Example #19
0
 def committed(dbObj, delta):
     """ Change specific delta connection id state to commited """
     if delta['deltat'] == 'addition':
         for connid in evaldict(delta['connectionid']):
             dbOut = {
                 'deltaid': delta['uid'],
                 'connectionid': connid,
                 'state': 'committed'
             }
             dbObj.update('delta_connections', [dbOut])
     return
Example #20
0
 def accepted(dbObj, delta):
     """ If delta addition and accepted - add connection entry in DB """
     if delta['deltat'] in ['addition', 'modify'] and delta['state'] in [
             'accepting', 'accepted'
     ]:
         for connid in evaldict(delta['connectionid']):
             dbOut = {
                 'deltaid': delta['uid'],
                 'connectionid': connid,
                 'state': 'accepted'
             }
             dbObj.insert('delta_connections', [dbOut])
Example #21
0
 def startwork(self):
     """Start Provisioning Service main worker."""
     fullURL = getFullUrl(self.config, sitename=self.sitename)
     jOut = self.getData(fullURL, "/sitefe/json/frontend/getdata")
     workDir = self.config.get('general',
                               'privatedir') + "/ProvisioningService/"
     createDirs(workDir)
     if not jOut:
         self.logger.info(
             'Seems server returned empty dictionary. Exiting.')
         return
     # Get switch information...
     switchPlugin = self.config.get(self.sitename, 'plugin')
     self.logger.info('Will load %s switch plugin' % switchPlugin)
     method = importlib.import_module(
         "SiteFE.ProvisioningService.Plugins.%s" % switchPlugin.lower())
     switchruler = method.mainCaller()
     topology = method.topology()
     switches = topology.getTopology()
     alliases = self.getAllAliases(switches)
     outputDict = {}
     allDeltas = self.getData(fullURL, "/sitefe/v1/deltas?oldview=true")
     for switchName in list(list(switches['switches'].keys()) + alliases):
         newDeltas = self.checkdeltas(switchName, allDeltas)
         for newDelta in newDeltas:
             outputDict.setdefault(newDelta['ID'])
             for actionKey in ['reduction', 'addition']:
                 try:
                     newvlan = self.getnewvlan(newDelta, newDelta['ID'],
                                               switchName, actionKey)
                     if actionKey == 'reduction' and newDelta[
                             'ParsedDelta'][actionKey]:
                         self.deltaRemoval(newDelta, newDelta['ID'],
                                           newvlan, switchName, switchruler,
                                           fullURL)
                     elif actionKey == 'addition' and newDelta[
                             'ParsedDelta'][actionKey]:
                         if newDelta['State'] in ['cancel']:
                             newDelta['ReductionID'] = newDelta['ID']
                             self.deltaRemoval(newDelta, newDelta['ID'],
                                               newvlan, switchName,
                                               switchruler, fullURL)
                         else:
                             self.deltaCommit(newDelta, newDelta['ID'],
                                              newvlan, switchName,
                                              switchruler, fullURL)
                     else:
                         self.logger.warning('Unknown delta state')
                         pretty = pprint.PrettyPrinter(indent=4)
                         pretty.pprint(evaldict(newDelta))
                 except IOError as ex:
                     print(ex)
                     raise Exception('Received IOError')
Example #22
0
def getdeltainfo(sitename, deltaUID):
    """Get all delta information.

    INPUT: sitename  - str mandatory
           deltaUID  - str mandatory
    """
    dbI = getDBConn('acceptdelta')
    dbobj = getVal(dbI, sitename=sitename)
    for delta in dbobj.get('deltas'):
        if delta['uid'] != deltaUID:
            continue
        delta['addition'] = evaldict(delta['addition'])
        delta['reduction'] = evaldict(delta['reduction'])
        LOGGER.info('=' * 80)
        LOGGER.info('Delta UID  :  %s', delta['uid'])
        LOGGER.info('Delta RedID:  %s', delta['reductionid'])
        LOGGER.info('Delta State:  %s', delta['state'])
        LOGGER.info('Delta ModAdd: %s', delta['modadd'])
        LOGGER.info('Delta InsDate: %s', delta['insertdate'])
        LOGGER.info('Delta Update:  %s', delta['updatedate'])
        LOGGER.info('Delta Model:  %s', delta['modelid'])
        LOGGER.info('Delta connID:  %s', delta['connectionid'])
        LOGGER.info('Delta Deltatype: %s', delta['deltat'])
        LOGGER.info('-' * 20)
        LOGGER.info('Delta times')
        for deltatimes in dbobj.get('states', search=[['deltaid', delta['uid']]]):
            LOGGER.info('State: %s Date: %s', deltatimes['state'], deltatimes['insertdate'])
        if delta['deltat'] in ['reduction', 'addition']:
            for hostname in list(delta[delta['deltat']]['hosts'].keys()):
                LOGGER.info('-' * 20)
                LOGGER.info('Host States %s', hostname)
                for hoststate in dbobj.get('hoststates', search=[['deltaid', delta['uid']], ['hostname', hostname]]):
                    LOGGER.info('Host %s State %s', hostname, hoststate['state'])
                    LOGGER.info('Insertdate %s UpdateDate %s', hoststate['insertdate'], hoststate['updatedate'])
                    LOGGER.info('-' * 20)
                    LOGGER.info('Host State History')
                    for hstatehistory in dbobj.get('hoststateshistory',
                                                   search=[['deltaid', delta['uid']], ['hostname', hostname]]):
                        LOGGER.info('State: %s, Date: %s', hstatehistory['state'], hstatehistory['insertdate'])
        return delta, dbobj
Example #23
0
 def activated(self, dbObj):
     """Check on all activated state deltas."""
     for delta in dbObj.get('deltas', search=[['state', 'activated']]):
         # Reduction
         if delta['deltat'] in ['reduction']:
             if delta['updatedate'] < int(getUTCnow() - 600):
                 self._stateChangerDelta(dbObj, 'removing', **delta)
             continue
         # Addition
         delta['addition'] = evaldict(delta['addition'])
         if timeendcheck(delta, self.logger):
             self._stateChangerDelta(dbObj, 'cancel', **delta)
             self.modelstatecancel(dbObj, **delta)
Example #24
0
 def activated(dbObj, delta):
     """ Change specific delta connection id state to activated. Reduction - cancelled """
     if delta['deltat'] == 'addition':
         for connid in evaldict(delta['connectionid']):
             dbOut = {
                 'deltaid': delta['uid'],
                 'connectionid': connid,
                 'state': 'activated'
             }
             dbObj.update('delta_connections', [dbOut])
     elif delta['deltat'] == 'reduction':
         for connid in evaldict(delta['connectionid']):
             for dConn in dbObj.get('delta_connections',
                                    search=[['connectionid', connid],
                                            ['state', 'activated']]):
                 dbOut = {
                     'deltaid': dConn['deltaid'],
                     'connectionid': dConn['connectionid'],
                     'state': 'cancelled'
                 }
                 dbObj.update('delta_connections', [dbOut])
     return
Example #25
0
 def pushInternalAction(self, url, state, deltaID, hostname):
     """ Push Internal action and return dict """
     newState = ""
     restOut = {}
     restOut = getDataFromSiteFE(
         {}, url, "/sitefe/v1/deltas/%s/internalaction/%s/%s" %
         (deltaID, hostname, state))
     if restOut[1] >= 400:
         msg = "Failed to set new state in database for %s delta and %s hostname. Error %s " \
               % (deltaID, hostname, restOut)
         self.logger.debug(msg)
         raise FailedInterfaceCommand(msg)
     restOutHIDs = getDataFromSiteFE({}, url,
                                     "/sitefe/v1/hostnameids/%s" % hostname)
     tmpOut = evaldict(restOutHIDs[0])
     newState = tmpOut[deltaID]
     msg = 'New State on the rest is %s and requested %s' % (newState,
                                                             state)
     self.logger.debug(msg)
     if newState != state:
         time.sleep(4)
     return evaldict(restOut)
Example #26
0
 def checkActivatingDeltas(self):
     """ Check all deltas in activating states """
     states = self.getHostStates('activating')
     for state in states:
         # Check delta State;
         deltaInfo = self.getDeltaInfo(state['deltaid'])
         if not deltaInfo:
             self.logger.debug('FE did not return anything for %s' %
                               state['deltaid'])
             continue
         if deltaInfo[0]['deltat'] == 'reduction':
             deltaInfo[0]['reduction'] = evaldict(deltaInfo[0]['reduction'])
             for connDelta in deltaInfo[0]['reduction']:
                 self.cancelResources(connDelta, state['deltaid'])
                 self.setHostState('active', state['deltaid'])
         elif deltaInfo[0]['deltat'] == 'addition':
             deltaInfo[0]['addition'] = evaldict(deltaInfo[0]['addition'])
             if deltaInfo[0]['state'] in ['activating', 'activated']:
                 self.logger.info('Activating delta %s' % state['deltaid'])
                 if not deltaInfo[0]['addition']:
                     self.logger.info(
                         'Failing delta %s. No addition parsed' %
                         state['deltaid'])
                     self.logger.info(deltaInfo[0])
                     # self.setHostState('failed', state['deltaid'])
                     continue
                 outExit = False
                 for connDelta in deltaInfo[0]['addition']:
                     outExit, message = self.activateResources(
                         connDelta, state['deltaid'])
                     self.logger.info("Exit: %s, Message: %s" %
                                      (outExit, message))
                 if outExit:
                     self.setHostState('active', state['deltaid'])
                 else:
                     # TODO. Have ability to save message in Frontend.
                     self.setHostState('failed', state['deltaid'])
def get_json_post_form(environ):
    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except ValueError:
        request_body_size = 0
    request_body = environ['wsgi.input'].read(request_body_size)
    try:
        params = json.loads(request_body)
    except:
        print 'Reached except in data load'
        params = evaldict(request_body)
        if not isinstance(params, dict):
            params = json.loads(params)
    environ.setdefault('params', {})
    for key in params.keys():
        environ['params'][key] = params[key]
    return environ['params']
Example #28
0
 def _deltaReduction(self, dbObj, delta, mainGraphName):
     """ Delta reduction """
     delta['content'] = evaldict(delta['content'])
     self.logger.info('Working on %s delta reduction in state' % delta['uid'])
     mainGraph = Graph()
     mainGraph.parse(mainGraphName, format='turtle')
     reduction = delta['content']['reduction']
     tmpFile = tempfile.NamedTemporaryFile(delete=False)
     tmpFile.write(reduction)
     tmpFile.close()
     tmpGraph = Graph()
     tmpGraph.parse(tmpFile.name, format='turtle')
     os.unlink(tmpFile.name)
     self.logger.info('Main Graph len: %s Reduction Len: %s', len(mainGraph), len(tmpGraph))
     mainGraph -= tmpGraph
     dbObj.update('deltasmod', [{'uid': delta['uid'], 'updatedate': getUTCnow(), 'modadd': 'removed'}])
     return mainGraph
Example #29
0
 def committed(self, dbObj):
     """ Committing state Check """
     for delta in dbObj.get('deltas', search=[['state', 'committed']]):
         if delta['deltat'] == 'addition' and delta['addition']:
             delta['addition'] = evaldict(delta['addition'])
             # Check the times...
             delayStart = False
             for connDelta in delta['addition']:
                 try:
                     if 'timestart' in connDelta.keys():
                         timeleft = int(
                             connDelta['timestart']) - getUTCnow()
                         self.logger.info(
                             'CurrentTime %s TimeStart %s. Seconds Left %s'
                             % (getUTCnow(), connDelta['timestart'],
                                timeleft))
                         if connDelta['timestart'] < getUTCnow():
                             self.logger.info(
                                 'This delta already passed timestart mark. \
                                              Setting state to activating')
                             delayStart = False if delayStart is not True else True
                         else:
                             self.logger.info(
                                 'This delta %s did not passed timestart mark. \
                                              Leaving state as it is' %
                                 delta['uid'])
                             delayStart = True
                 except:
                     self.logger.info(
                         'This delta %s had an error checking starttime. \
                                      Leaving state as it is.' %
                         delta['uid'])
                 if not delayStart:
                     self._stateChangerDelta(dbObj, 'activating', **delta)
                     self.connMgr.activating(dbObj, delta)
         else:
             self.logger.info(
                 'This delta %s is in committed. Setting state to activating.'
                 % delta['uid'])
             self._stateChangerDelta(dbObj, 'activating', **delta)
Example #30
0
 def addition(self, deltaInfo, state, informFE=True):
     """Addition of resources."""
     retState = True
     deltaInfo[0]['addition'] = evaldict(deltaInfo[0]['addition'])
     if deltaInfo[0]['state'] in ['activating', 'activated']:
         self.logger.info('Activating delta %s' % state['deltaid'])
         if not deltaInfo[0]['addition']:
             self.logger.info('Failing delta %s. No addition parsed' %
                              state['deltaid'])
             self.logger.info(deltaInfo[0])
             return retState
         outExit = False
         for connDelta in deltaInfo[0]['addition']:
             outExit, message = self.activateResources(
                 connDelta, state['deltaid'])
             self.logger.info("Exit: %s, Message: %s" % (outExit, message))
             saveState = 'active' if outExit else 'failed'
             if not outExit:
                 retState = False
             # TODO: Have ability to save message in the Frontend;
             if informFE:
                 self.setHostState(saveState, state['deltaid'])
     return retState