Beispiel #1
0
def getLogsArchive(bridgeNode, bridgePort, nodeSet=set(), outputdir='/tmp', timeout=30):
    
    if not nodeSet:
        log.info("Empty node set. Would query for just the bridge node.")
        nodeSet = set([bridgeNode.split('.')[0]])
    
    log.info("Node Set: %s" %(nodeSet))
        
    messaging = sendMessage(bridgeNode, bridgePort, list(nodeSet), 'daemon', 'getLogsArchive', {})

    result = recieveMessages(messaging, nodeSet, timeout)

    helpers.makeDir(outputdir)
    
    for node in result:
        nodeLogDir = os.path.join(outputdir, node)
        tardata = result[node]
        scratch = tempfile.TemporaryFile()
        sp = cStringIO.StringIO(tardata)
        base64.decode(sp, scratch)
        sp.close()

        # now untar that into the output directory
        scratch.seek(0)
        tar = tarfile.open(fileobj=scratch, mode="r:gz")
        for m in tar.getmembers():
            tar.extract(m, nodeLogDir)
        tar.close()
        
    failedNodes = nodeSet - set(result.keys())
    return ((len(failedNodes) == 0), result.keys())
def createConfigFiles(nodeName, topoGraph, externalAgentsCommPort=28809, isDBEnabled=True):
	
	config.setNodeDir("/tmp/%s" %(nodeName))
	
	expdl = dict()
	expdl['topoGraph'] = json_graph.node_link_data(topoGraph)
	
	dbdl = dict()
	dbdl['isDBEnabled'] = isDBEnabled
		
	expConf = dict()
	expConf['expdl'] = expdl
	expConf['dbdl'] = dbdl
	
	expConf = config.loadExperimentConfig(experimentConfig=expConf, 
										  distributionPath='/tmp/magi',
										  transportClass=helpers.TRANSPORT_TCP)
	
	localInfo = dict()
	localInfo['nodename'] = nodeName
	localInfo['processAgentsCommPort'] = externalAgentsCommPort
	
	nodeConf = dict()
	nodeConf['localInfo'] = localInfo
	
	nodeConf = config.loadNodeConfig(nodeConf, expConf)
	
	helpers.makeDir(config.getConfigDir())
	helpers.writeYaml(expConf, config.getExperimentConfFile())
	helpers.writeYaml(nodeConf, config.getNodeConfFile())
	
	return (config.getNodeConfFile(), config.getExperimentConfFile())
def createConfigFiles(nodeName,
                      topoGraph,
                      externalAgentsCommPort=28809,
                      isDBEnabled=True):

    config.setNodeDir("/tmp/%s" % (nodeName))

    expdl = dict()
    expdl['topoGraph'] = json_graph.node_link_data(topoGraph)

    dbdl = dict()
    dbdl['isDBEnabled'] = isDBEnabled

    expConf = dict()
    expConf['expdl'] = expdl
    expConf['dbdl'] = dbdl

    expConf = config.loadExperimentConfig(experimentConfig=expConf,
                                          distributionPath='/tmp/magi',
                                          transportClass=helpers.TRANSPORT_TCP)

    localInfo = dict()
    localInfo['nodename'] = nodeName
    localInfo['processAgentsCommPort'] = externalAgentsCommPort

    nodeConf = dict()
    nodeConf['localInfo'] = localInfo

    nodeConf = config.loadNodeConfig(nodeConf, expConf)

    helpers.makeDir(config.getConfigDir())
    helpers.writeYaml(expConf, config.getExperimentConfFile())
    helpers.writeYaml(nodeConf, config.getNodeConfFile())

    return (config.getNodeConfFile(), config.getExperimentConfFile())
Beispiel #4
0
def startDBServer(configfile=None, timeout=TIMEOUT):
    """
        Function to start a database server on the node
    """
    helpers.makeDir(config.getDbDir())
    return Server.startDBServer(configfile=configfile,
                                configDir=config.getConfigDir(),
                                dbPath=os.path.join(config.getDbDir(),
                                                    "mongodb"),
                                logPath=os.path.join(config.getLogDir(),
                                                     "mongodb.log"),
                                timeout=timeout)
Beispiel #5
0
def startConfigServer(port=CONFIG_SERVER_PORT,
                      dbPath=os.path.join(TEMP_DIR, "configdb"),
                      logPath=os.path.join(TEMP_DIR, "mongoc.log"),
                      block=True,
                      timeout=TIMEOUT):
    """
        Function to start a database config server on the node
    """
    if timeout <= 0:
        timeout = sys.maxint
    start = time.time()
    stop = start + timeout

    try:
        log.info(
            "Checking if an instance of mongo config server is already running"
        )
        if isDBRunning(port=port):
            return

        try:
            helpers.makeDir(
                dbPath)  # Make sure mongodb config data directory is around
        except OSError, e:
            log.exception("failed to create mondodb config data dir")
            raise

        log.info("Trying to start mongo config server")
        mongod = [
            'mongod', '--configsvr', '--dbpath', dbPath, '--port',
            str(port), '--logpath', logPath
        ]
        log.info("Running %s", mongod)

        while time.time() < stop:
            p = Popen(mongod)
            time.sleep(1)
            if p.poll() is None:
                log.info("Started mongod config server with pid %s", p.pid)
                return p
            log.debug("Failed to start mongod config server. Will retry.")
            if not block:
                break
            log.debug("Retrying to start mongo config server")

        log.error("Cannot start mongod config server")
        raise pymongo.errors.PyMongoError("Cannot start mongod config server")
Beispiel #6
0
def startConfigServer(port=CONFIG_SERVER_PORT, 
                      dbPath=os.path.join(TEMP_DIR, "configdb"), 
                      logPath=os.path.join(TEMP_DIR, "mongoc.log"),
                      block=True,
                      timeout=TIMEOUT):
    """
        Function to start a database config server on the node
    """
    if timeout <= 0:
        timeout = sys.maxint
    start = time.time()
    stop = start + timeout
    
    try:
        log.info("Checking if an instance of mongo config server is already running")
        if isDBRunning(port=port):
            return
        
        try:
            helpers.makeDir(dbPath)  # Make sure mongodb config data directory is around
        except OSError, e:
            log.exception("failed to create mondodb config data dir")
            raise

        log.info("Trying to start mongo config server")
        mongod = ['mongod', '--configsvr', 
                  '--dbpath', dbPath, 
                  '--port', str(port), 
                  '--logpath', logPath]
        log.info("Running %s", mongod)
        
        while time.time() < stop:
            p = Popen(mongod)
            time.sleep(1)
            if p.poll() is None:
                log.info("Started mongod config server with pid %s", p.pid)
                return p
            log.debug("Failed to start mongod config server. Will retry.")
            if not block:
                break
            log.debug("Retrying to start mongo config server")
            
        log.error("Cannot start mongod config server")
        raise pymongo.errors.PyMongoError("Cannot start mongod config server")
Beispiel #7
0
def getLogsArchive(bridgeNode,
                   bridgePort,
                   nodeSet=set(),
                   outputdir='/tmp',
                   timeout=30):

    if not nodeSet:
        log.info("Empty node set. Would query for just the bridge node.")
        nodeSet = set([bridgeNode.split('.')[0]])

    log.info("Node Set: %s" % (nodeSet))

    messaging = sendMessage(bridgeNode, bridgePort, list(nodeSet), 'daemon',
                            'getLogsArchive', {})

    result = recieveMessages(messaging, nodeSet, timeout)

    helpers.makeDir(outputdir)

    for node in result:
        nodeLogDir = os.path.join(outputdir, node)
        tardata = result[node]
        scratch = tempfile.TemporaryFile()
        sp = cStringIO.StringIO(tardata)
        base64.decode(sp, scratch)
        sp.close()

        # now untar that into the output directory
        scratch.seek(0)
        tar = tarfile.open(fileobj=scratch, mode="r:gz")
        for m in tar.getmembers():
            tar.extract(m, nodeLogDir)
        tar.close()

    failedNodes = nodeSet - set(result.keys())
    return ((len(failedNodes) == 0), result.keys())
Beispiel #8
0
def initializeProcessAgent(agent, argv):
    '''argv is assumed to have the following format. (This is usually set by the
    Magi daemon):

        agent_name agent_dock execute=[pipe|socket] (logfile=path)

    Where agent_name and agent_dock are strings and the key in the key=value
    pairs is literally the key given. The value may be restricted.
    '''
    if len(argv) < 3:
        log.critical('command line must start with name and dock')
        sys.exit(2)

    agent.name, dock, nodeConfigFile, experimentConfigFile = argv[1:5]
    args = argv_to_dict(argv[5:])

    config.loadNodeConfig(nodeConfigFile, experimentConfigFile)

    setAttributes(
        agent, {
            'hostname':
            config.getNodeName(),
            'execute':
            'socket',
            'logfile':
            os.path.join(config.getLogDir(), agent.name + '.log'),
            'loglevel':
            'DEBUG',
            'commHost':
            'localhost',
            'commPort':
            config.getConfig()['localInfo'].get('processAgentsCommPort',
                                                18809),
            'commGroup':
            None
        }, args)

    agent.docklist.add(dock)

    helpers.makeDir(os.path.dirname(agent.logfile))

    handler = logging.FileHandler(agent.logfile, 'w')
    handler.setFormatter(
        logging.Formatter(helpers.LOG_FORMAT_MSECS, helpers.LOG_DATEFMT))
    root = logging.getLogger()
    root.setLevel(helpers.logLevels.get(agent.loglevel.lower(), logging.INFO))
    root.handlers = []
    root.addHandler(handler)

    log.info('argv: %s', argv)
    log.info('agent attributes: %s', agent.__dict__)

    log.info("Setting up agent messaging interface")
    inTransport, outTransport = _getIOHandles(agent)
    agent.messenger = AgentMessenger(inTransport, outTransport, agent)
    agent.messenger.start()
    log.info("Agent messaging interface initialized and running")

    # Tell the daemon we want to listen on the dock.
    # GTL - why doesn't the Daemon just associate the dock
    # with this process?
    agent.messenger.listenDock(dock)

    if agent.commGroup:
        agent.messenger.joinGroup(agent.commGroup)
        #TODO: In ideal condition wait from the node to join group before proceeding further

    # now that we're connected, send an AgentLoaded message.
    agent.messenger.trigger(event='AgentLoadDone',
                            agent=agent.name,
                            nodes=[agent.hostname])

    return args
Beispiel #9
0
                    createNodeConfig = True

                if createNodeConfig:
                    log.info("Creating a new node configuration file")
                    # Use the experiment configuration to create node specific configuration
                    nodeConfig = config.createNodeConfig(
                        experimentConfig=experimentConfig)

            else:
                log.info("Using node configuration file at %s",
                         options.nodeconf)
                nodeConfig = config.loadNodeConfig(
                    nodeConfig=options.nodeconf,
                    experimentConfig=experimentConfig)

            helpers.makeDir(os.path.dirname(config.getNodeConfFile()))
            helpers.writeYaml(nodeConfig, config.getNodeConfFile())
            log.info("Created a node configuration file at %s",
                     config.getNodeConfFile())

            helpers.makeDir(os.path.dirname(config.getExperimentConfFile()))
            helpers.writeYaml(experimentConfig, config.getExperimentConfFile())
            log.info("Created a experiment configuration file at %s",
                     config.getExperimentConfFile())

        except:
            log.exception(
                "MAGI configuration failed, things probably aren't going to run"
            )

        if (config.getNodeName() == config.getServer()):
Beispiel #10
0
    optparser.add_option("-c", "--nodeconf", dest="nodeconf", 
                         help="Specify location of the node " +
                         "configuration file, ex: -c localnode.conf ")

    (options, args) = optparser.parse_args()
    
    expConfig = config.loadExperimentConfig(options.expconf)
    nodeConfig = config.loadNodeConfig(options.nodeconf)
    
    #import once the system is cofigured
    from magi.daemon.daemon import Daemon

    if not options.logfile:
        options.logfile = os.path.join(config.getLogDir(), "daemon.log")
    
    helpers.makeDir(os.path.dirname(options.logfile))
    helpers.makeDir(config.getTempDir())
            
    # Roll over the old log and create a new one
    # Note here that we will have at most 5 logs 
    # Need to check existence of file before creating the handler instance
    # This is because handler creation creates the file if not existent 
    if os.path.isfile(options.logfile):
        needroll = True
    else:
        needroll = False
    handler = logging.handlers.RotatingFileHandler(options.logfile, backupCount=5)
    if needroll:
        handler.doRollover()

    handler.setFormatter(logging.Formatter(helpers.LOG_FORMAT_MSECS, helpers.LOG_DATEFMT))
						help="Comma-separated list of the node names")
	#optparser.add_option("-f", "--file", dest="file", help="json topology FILE")
	optparser.add_option("-o", "--logfile", dest="logfile", action='store', 
						default='/tmp/magi_bootstrap.log',  
                        help="Log to specified file, ex: -f file.log. Default: %default")
	optparser.add_option("-l", "--loglevel", dest="loglevel", default="INFO", 
						help="set logger to level ALL, DEBUG, INFO, " + 
                        "WARNING, ERROR. Default: %default, ex: -l DEBUG")
	optparser.add_option("-D", "--nodataman", dest="nodataman", 
						action="store_true", default=False, 
						help="Do not install and setup data manager") 
        

	(options, args) = optparser.parse_args()

	helpers.makeDir(os.path.dirname(options.logfile))
	
	tempDir = '/tmp'
	helpers.makeDir(tempDir)
            
    # Roll over the old log and create a new one
    # Note here that we will have at most 5 logs 
    # Need to check existence of file before creating the handler instance
    # This is because handler creation creates the file if not existent 
	if os.path.isfile(options.logfile):
	    needroll = True
	else:
	    needroll = False
	handler = logging.handlers.RotatingFileHandler(options.logfile, backupCount=5)
	if needroll:
	    handler.doRollover()
Beispiel #12
0
                                nodeConfig = config.loadNodeConfig(nodeConfig=config.getNodeConfFile(), experimentConfig=experimentConfig) 
                        else:
                                # Node configuration file does not exist
                                log.info("No valid node configuration file found at %s. Need to create one.", config.getNodeConfFile())
                                createNodeConfig = True 

                        if createNodeConfig: 
                                log.info("Creating a new node configuration file")
                                # Use the experiment configuration to create node specific configuration
                                nodeConfig = config.createNodeConfig(experimentConfig=experimentConfig) 
                
                else:
                        log.info("Using node configuration file at %s", options.nodeconf)
                        nodeConfig = config.loadNodeConfig(nodeConfig=options.nodeconf, experimentConfig=experimentConfig) 
                        
                helpers.makeDir(os.path.dirname(config.getNodeConfFile()))
                helpers.writeYaml(nodeConfig, config.getNodeConfFile())
                log.info("Created a node configuration file at %s", config.getNodeConfFile())
                
                helpers.makeDir(os.path.dirname(config.getExperimentConfFile()))
                helpers.writeYaml(experimentConfig, config.getExperimentConfFile())
                log.info("Created a experiment configuration file at %s", config.getExperimentConfFile()) 
            
            except:
                    log.exception("MAGI configuration failed, things probably aren't going to run")
                                
            if (config.getNodeName() == config.getServer()):
                import shutil
                log.info("Copying experiment.conf to testbed experiment directory %s" %(config.getExperimentDir()))
                shutil.copy(config.getExperimentConfFile(), config.getExperimentDir())
                                        
    optparser.add_option("-l",
                         "--loglevel",
                         dest="loglevel",
                         default="INFO",
                         help="set logger to level ALL, DEBUG, INFO, " +
                         "WARNING, ERROR. Default: %default, ex: -l DEBUG")
    optparser.add_option("-D",
                         "--nodataman",
                         dest="nodataman",
                         action="store_true",
                         default=False,
                         help="Do not install and setup data manager")

    (options, args) = optparser.parse_args()

    helpers.makeDir(os.path.dirname(options.logfile))

    tempDir = '/tmp'
    helpers.makeDir(tempDir)

    # Roll over the old log and create a new one
    # Note here that we will have at most 5 logs
    # Need to check existence of file before creating the handler instance
    # This is because handler creation creates the file if not existent
    if os.path.isfile(options.logfile):
        needroll = True
    else:
        needroll = False
    handler = logging.handlers.RotatingFileHandler(options.logfile,
                                                   backupCount=5)
    if needroll:
Beispiel #14
0
	def __init__(self, topoGraph):
		Topo.__init__(self)
		interfacelist = defaultdict(list)
		
		inetSwitch = self.addSwitch('z1')
		
		nodes = topoGraph.nodes()
		nodes.sort()
		bridgeNode = nodes[0]
		for node in nodes:
			if 'control' == node.lower():
				bridgeNode = 'control'
				break

		#Creates mininet hosts that can route packets and generates list of interfaces necessary for each host
		for nodeName in topoGraph.nodes():
			
			node = self.addNode(name=nodeName, cls=LinuxRouter, ip='')
			#self.addLink(inetSwitch, node)
			
			neighbors = topoGraph.neighbors(nodeName)
			nodeIntfList = deque([])
			for i in range(len(neighbors)):
				intfname = '%s-eth%s'%(nodeName, i+1)				
				nodeIntfList.append(intfname)
			interfacelist[nodeName] = nodeIntfList
			
			# create config files for nodes
			directory = "/tmp/%s" % nodeName
			if not os.path.exists(directory):
				os.makedirs(directory)
				os.makedirs("%s/tmp" % directory)
			
			localInfo = dict()
			localInfo['configDir'] = "/tmp/%s/config" %(nodeName)
			localInfo['logDir'] = "/tmp/%s/logs" %(nodeName)
			localInfo['dbDir'] = "/tmp/%s/db" %(nodeName)
			localInfo['tempDir'] = "/tmp/%s/tmp" %(nodeName)
			localInfo['nodename'] = nodeName
			
			dbConfig = dict()
			dbConfig['isDBEnabled'] = True
			dbConfig['isDBSharded'] = False
			dbConfig['sensorToCollectorMap'] = {nodeName: nodeName}
			
			transportsConfig = []
			if nodeName == bridgeNode:
				transportsConfig.append({'class': 'TCPServer', 'address': '0.0.0.0', 'port': 18808})
				transportsConfig.append({'class': 'TCPServer', 'address': '0.0.0.0', 'port': 28808})
			else:
				transportsConfig.append({'class': 'TCPTransport', 'address': bridgeNode, 'port': 28808})
				
			nodeConfig = dict()
			nodeConfig['localInfo'] = localInfo
			nodeConfig['database'] = dbConfig
			nodeConfig['transports'] = transportsConfig
			
			nodeConfigDir = localInfo['configDir']
			helpers.makeDir(nodeConfigDir)
			nodeConfFile = "%s/node.conf" %(nodeConfigDir)
			helpers.writeYaml(nodeConfig, nodeConfFile)
			log.info("Created a node configuration file at %s", nodeConfFile)
			
		print "Interface List: %s" % interfacelist.items() 
			
		#adds edges in mininet topology and configures Ip addresses
		hostsConfigEntries = []
		for e in topoGraph.edges():
			#node1, node2 = self.get(e[0]), self.get(e[1]) 
			edgeNodes = []
			edgeNodes.append(e[0])
			edgeNodes.append(e[1])
			edgeNodes.sort()
			
			print "adding link between %s and %s" %(edgeNodes[0], edgeNodes[1])
			
			node1InterfaceList = interfacelist[edgeNodes[0]]
			node2InterfaceList = interfacelist[edgeNodes[1]]	
			
			node1Interface = node1InterfaceList.popleft()
			node2Interface = node2InterfaceList.popleft()
					
			print "node1Interface name = %s, node2Interface name = %s" %(node1Interface, node2Interface)
			
			ip1 = iplist.popleft()
			ip2 = iplist.popleft()
			
			print "ip1 = %s, ip2 = %s" %(ip1, ip2)
			
			#link1 = Link.__init__(e[0], e[1], intfName1=node1Interface, intfName2=node2Interface, addr1=iplist.popleft(), addr2=iplist.popleft())
			self.addLink( edgeNodes[0], edgeNodes[1], intfName1=node1Interface, intfName2=node2Interface, params1={'ip': ip1 }, params2={ 'ip' : ip2 } )
			
			hostsConfigEntries.append("%s	%s-%s %s" %(ip1.split('/')[0], edgeNodes[0], edgeNodes[1], edgeNodes[0]))
			hostsConfigEntries.append("%s	%s-%s %s" %(ip2.split('/')[0], edgeNodes[1], edgeNodes[0], edgeNodes[1]))
			print "\n\nadded link between %s and %s\n\n" %(edgeNodes[0], edgeNodes[1])
			#print self.linkInfo(a[0], a[1])
			
		itr = 1
		for nodeName in topoGraph.nodes():
			nodeIntf = '%s-eth0'%(nodeName)
			switchIntf = 's1-eth%s'%(nodeName)
			nodeIP = '10.1.%d.1/24'%(itr)
			switchIP = '10.1.%d.2/24'%(itr)
			
			print "nodeIntf name = %s, switchIntf name = %s" %(nodeIntf, switchIntf)
			print "nodeIP name = %s, switchIP name = %s" %(nodeIP, switchIP)
			
			self.addLink( nodeName, 'z1', intfName1=nodeIntf, params1={'ip': nodeIP }, params2={ 'ip' : switchIP } )
			itr += 1
			
		
		addEtcHostsMininetConfig(hostsConfigEntries)
Beispiel #15
0
                         dest="nodeconf",
                         help="Specify location of the node " +
                         "configuration file, ex: -c localnode.conf ")

    (options, args) = optparser.parse_args()

    expConfig = config.loadExperimentConfig(options.expconf)
    nodeConfig = config.loadNodeConfig(options.nodeconf)

    #import once the system is cofigured
    from magi.daemon.daemon import Daemon

    if not options.logfile:
        options.logfile = os.path.join(config.getLogDir(), "daemon.log")

    helpers.makeDir(os.path.dirname(options.logfile))
    helpers.makeDir(config.getTempDir())

    # Roll over the old log and create a new one
    # Note here that we will have at most 5 logs
    # Need to check existence of file before creating the handler instance
    # This is because handler creation creates the file if not existent
    if os.path.isfile(options.logfile):
        needroll = True
    else:
        needroll = False
    handler = logging.handlers.RotatingFileHandler(options.logfile,
                                                   backupCount=5)
    if needroll:
        handler.doRollover()
Beispiel #16
0
def startDBServer(port=DATABASE_SERVER_PORT,
                  configfile=None,
                  configDir=TEMP_DIR, 
                  dbPath=os.path.join(TEMP_DIR, "mongodb"), 
                  logPath=os.path.join(TEMP_DIR, "mongodb.log"),
                  block=True,
                  timeout=TIMEOUT):
    """
        Function to start a database server on the node
    """
    if timeout <= 0:
        timeout = sys.maxint
    start = time.time()
    stop = start + timeout
    
    try:
        log.info("Checking if an instance of mongod server is already running")
        if isDBRunning(port=port):
            return

        if configfile is None:
            configfile = createMongoDConfig(configDir=configDir,
                                            dbPath=dbPath, 
                                            logPath=logPath)
            
        mongo_conf = helpers.readPropertiesFile(configfile)

        try:
            helpers.makeDir(mongo_conf['dbpath'])  # Make sure mongodb data directory is around
        except:
            log.exception("failed to create mondodb data dir: %s", mongo_conf['dbpath'])
            raise

        try:
            logdir = os.path.dirname(mongo_conf['logpath'])
            helpers.makeDir(logdir)  # Make sure mongodb log directory is around
        except:
            log.exception("failed to create mondodb log dir: %s", logdir)
            raise

        log.info("Trying to start mongo database server")
        mongod = ['mongod', 
                  '--config', configfile, 
                  '--port', str(port), 
                  '--shardsvr', 
                  '--journal', 
                  '--smallfiles']
        log.info("Running %s", mongod)
        
        while time.time() < stop:
            p = Popen(mongod)
            time.sleep(1)
            if p.poll() is None:
                log.info("Started mongod with pid %s", p.pid)
                return p
            log.debug("Failed to start mongod server.")
            if not block:
                break
            log.debug("Retrying to start mongo database server")
            
        log.error("Cannot start database server")
        raise pymongo.errors.PyMongoError("Cannot start database server")
    
    except:
        log.exception("Exception while setting up mongo db database server")
        raise
Beispiel #17
0
    def __init__(self, topoGraph):
        Topo.__init__(self)
        interfacelist = defaultdict(list)

        inetSwitch = self.addSwitch('z1')

        nodes = topoGraph.nodes()
        nodes.sort()
        bridgeNode = nodes[0]
        for node in nodes:
            if 'control' == node.lower():
                bridgeNode = 'control'
                break

        #Creates mininet hosts that can route packets and generates list of interfaces necessary for each host
        for nodeName in topoGraph.nodes():

            node = self.addNode(name=nodeName, cls=LinuxRouter, ip='')
            #self.addLink(inetSwitch, node)

            neighbors = topoGraph.neighbors(nodeName)
            nodeIntfList = deque([])
            for i in range(len(neighbors)):
                intfname = '%s-eth%s' % (nodeName, i + 1)
                nodeIntfList.append(intfname)
            interfacelist[nodeName] = nodeIntfList

            # create config files for nodes
            directory = "/tmp/%s" % nodeName
            if not os.path.exists(directory):
                os.makedirs(directory)
                os.makedirs("%s/tmp" % directory)

            localInfo = dict()
            localInfo['configDir'] = "/tmp/%s/config" % (nodeName)
            localInfo['logDir'] = "/tmp/%s/logs" % (nodeName)
            localInfo['dbDir'] = "/tmp/%s/db" % (nodeName)
            localInfo['tempDir'] = "/tmp/%s/tmp" % (nodeName)
            localInfo['nodename'] = nodeName

            dbConfig = dict()
            dbConfig['isDBEnabled'] = True
            dbConfig['isDBSharded'] = False
            dbConfig['sensorToCollectorMap'] = {nodeName: nodeName}

            transportsConfig = []
            if nodeName == bridgeNode:
                transportsConfig.append({
                    'class': 'TCPServer',
                    'address': '0.0.0.0',
                    'port': 18808
                })
                transportsConfig.append({
                    'class': 'TCPServer',
                    'address': '0.0.0.0',
                    'port': 28808
                })
            else:
                transportsConfig.append({
                    'class': 'TCPTransport',
                    'address': bridgeNode,
                    'port': 28808
                })

            nodeConfig = dict()
            nodeConfig['localInfo'] = localInfo
            nodeConfig['database'] = dbConfig
            nodeConfig['transports'] = transportsConfig

            nodeConfigDir = localInfo['configDir']
            helpers.makeDir(nodeConfigDir)
            nodeConfFile = "%s/node.conf" % (nodeConfigDir)
            helpers.writeYaml(nodeConfig, nodeConfFile)
            log.info("Created a node configuration file at %s", nodeConfFile)

        print "Interface List: %s" % interfacelist.items()

        #adds edges in mininet topology and configures Ip addresses
        hostsConfigEntries = []
        for e in topoGraph.edges():
            #node1, node2 = self.get(e[0]), self.get(e[1])
            edgeNodes = []
            edgeNodes.append(e[0])
            edgeNodes.append(e[1])
            edgeNodes.sort()

            print "adding link between %s and %s" % (edgeNodes[0],
                                                     edgeNodes[1])

            node1InterfaceList = interfacelist[edgeNodes[0]]
            node2InterfaceList = interfacelist[edgeNodes[1]]

            node1Interface = node1InterfaceList.popleft()
            node2Interface = node2InterfaceList.popleft()

            print "node1Interface name = %s, node2Interface name = %s" % (
                node1Interface, node2Interface)

            ip1 = iplist.popleft()
            ip2 = iplist.popleft()

            print "ip1 = %s, ip2 = %s" % (ip1, ip2)

            #link1 = Link.__init__(e[0], e[1], intfName1=node1Interface, intfName2=node2Interface, addr1=iplist.popleft(), addr2=iplist.popleft())
            self.addLink(edgeNodes[0],
                         edgeNodes[1],
                         intfName1=node1Interface,
                         intfName2=node2Interface,
                         params1={'ip': ip1},
                         params2={'ip': ip2})

            hostsConfigEntries.append(
                "%s	%s-%s %s" %
                (ip1.split('/')[0], edgeNodes[0], edgeNodes[1], edgeNodes[0]))
            hostsConfigEntries.append(
                "%s	%s-%s %s" %
                (ip2.split('/')[0], edgeNodes[1], edgeNodes[0], edgeNodes[1]))
            print "\n\nadded link between %s and %s\n\n" % (edgeNodes[0],
                                                            edgeNodes[1])
            #print self.linkInfo(a[0], a[1])

        itr = 1
        for nodeName in topoGraph.nodes():
            nodeIntf = '%s-eth0' % (nodeName)
            switchIntf = 's1-eth%s' % (nodeName)
            nodeIP = '10.1.%d.1/24' % (itr)
            switchIP = '10.1.%d.2/24' % (itr)

            print "nodeIntf name = %s, switchIntf name = %s" % (nodeIntf,
                                                                switchIntf)
            print "nodeIP name = %s, switchIP name = %s" % (nodeIP, switchIP)

            self.addLink(nodeName,
                         'z1',
                         intfName1=nodeIntf,
                         params1={'ip': nodeIP},
                         params2={'ip': switchIP})
            itr += 1

        addEtcHostsMininetConfig(hostsConfigEntries)
Beispiel #18
0
def startDBServer(port=DATABASE_SERVER_PORT,
                  configfile=None,
                  configDir=TEMP_DIR,
                  dbPath=os.path.join(TEMP_DIR, "mongodb"),
                  logPath=os.path.join(TEMP_DIR, "mongodb.log"),
                  block=True,
                  timeout=TIMEOUT):
    """
        Function to start a database server on the node
    """
    if timeout <= 0:
        timeout = sys.maxint
    start = time.time()
    stop = start + timeout

    try:
        log.info("Checking if an instance of mongod server is already running")
        if isDBRunning(port=port):
            return

        if configfile is None:
            configfile = createMongoDConfig(configDir=configDir,
                                            dbPath=dbPath,
                                            logPath=logPath)

        mongo_conf = helpers.readPropertiesFile(configfile)

        try:
            helpers.makeDir(mongo_conf['dbpath']
                            )  # Make sure mongodb data directory is around
        except:
            log.exception("failed to create mondodb data dir: %s",
                          mongo_conf['dbpath'])
            raise

        try:
            logdir = os.path.dirname(mongo_conf['logpath'])
            helpers.makeDir(
                logdir)  # Make sure mongodb log directory is around
        except:
            log.exception("failed to create mondodb log dir: %s", logdir)
            raise

        log.info("Trying to start mongo database server")
        mongod = [
            'mongod', '--config', configfile, '--port',
            str(port), '--shardsvr', '--journal', '--smallfiles'
        ]
        log.info("Running %s", mongod)

        while time.time() < stop:
            p = Popen(mongod)
            time.sleep(1)
            if p.poll() is None:
                log.info("Started mongod with pid %s", p.pid)
                return p
            log.debug("Failed to start mongod server.")
            if not block:
                break
            log.debug("Retrying to start mongo database server")

        log.error("Cannot start database server")
        raise pymongo.errors.PyMongoError("Cannot start database server")

    except:
        log.exception("Exception while setting up mongo db database server")
        raise