updated how distributed servers are added and connections are created to reduce duplicate connections

This commit is contained in:
Blake Harnden 2019-10-07 11:58:27 -07:00
parent 4eacd815d1
commit 212fec916b
3 changed files with 46 additions and 27 deletions

View file

@ -147,7 +147,7 @@ class Session(object):
self.sdt = Sdt(session=self) self.sdt = Sdt(session=self)
# distributed servers # distributed servers
self.servers = set() self.servers = {}
# initialize default node services # initialize default node services
self.services.default_services = { self.services.default_services = {
@ -158,10 +158,21 @@ class Session(object):
"host": ("DefaultRoute", "SSH"), "host": ("DefaultRoute", "SSH"),
} }
def add_distributed(self, server):
conn = Connection(server, user="root")
self.servers[server] = conn
def init_distributed(self): def init_distributed(self):
for server in self.servers: for server in self.servers:
conn = self.servers[server]
cmd = "mkdir -p %s" % self.session_dir cmd = "mkdir -p %s" % self.session_dir
Connection(server, user="root").run(cmd, hide=False) conn.run(cmd, hide=False)
def shutdown_distributed(self):
for server in self.servers:
conn = self.servers[server]
cmd = "rm -rf %s" % self.session_dir
conn.run(cmd, hide=False)
@classmethod @classmethod
def get_node_class(cls, _type): def get_node_class(cls, _type):
@ -676,6 +687,13 @@ class Session(object):
if not name: if not name:
name = "%s%s" % (node_class.__name__, _id) name = "%s%s" % (node_class.__name__, _id)
# verify distributed server
server = self.servers.get(node_options.emulation_server)
if node_options.emulation_server is not None and server is None:
raise CoreError(
"invalid distributed server: %s" % node_options.emulation_server
)
# create node # create node
logging.info( logging.info(
"creating node(%s) id(%s) name(%s) start(%s)", "creating node(%s) id(%s) name(%s) start(%s)",
@ -694,11 +712,7 @@ class Session(object):
) )
else: else:
node = self.create_node( node = self.create_node(
cls=node_class, cls=node_class, _id=_id, name=name, start=start, server=server
_id=_id,
name=name,
start=start,
server=node_options.emulation_server,
) )
# set node attributes # set node attributes
@ -972,6 +986,7 @@ class Session(object):
preserve = self.options.get_config("preservedir") == "1" preserve = self.options.get_config("preservedir") == "1"
if not preserve: if not preserve:
shutil.rmtree(self.session_dir, ignore_errors=True) shutil.rmtree(self.session_dir, ignore_errors=True)
self.shutdown_distributed()
# call session shutdown handlers # call session shutdown handlers
for handler in self.shutdown_handlers: for handler in self.shutdown_handlers:

View file

@ -13,8 +13,6 @@ from builtins import range
from socket import AF_INET, AF_INET6 from socket import AF_INET, AF_INET6
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from fabric import Connection
from core import constants, utils from core import constants, utils
from core.emulator.data import LinkData, NodeData from core.emulator.data import LinkData, NodeData
from core.emulator.enumerations import LinkTypes, NodeTypes from core.emulator.enumerations import LinkTypes, NodeTypes
@ -42,7 +40,8 @@ class NodeBase(object):
:param int _id: id :param int _id: id
:param str name: object name :param str name: object name
:param bool start: start value :param bool start: start value
:param str server: remote server node will run on, default is None for localhost :param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
""" """
self.session = session self.session = session
@ -53,8 +52,6 @@ class NodeBase(object):
name = "o%s" % self.id name = "o%s" % self.id
self.name = name self.name = name
self.server = server self.server = server
if self.server is not None:
self.server_conn = Connection(self.server, user="root")
self.type = None self.type = None
self.services = None self.services = None
@ -103,18 +100,23 @@ class NodeBase(object):
return utils.check_cmd(args, env=env) return utils.check_cmd(args, env=env)
else: else:
args = " ".join(args) args = " ".join(args)
return self.remote_cmd(args) return self.remote_cmd(args, env=env)
def remote_cmd(self, cmd): def remote_cmd(self, cmd, env=None):
""" """
Run command remotely using server connection. Run command remotely using server connection.
:param str cmd: command to run :param str cmd: command to run
:param dict env: environment for remote command, default is None
:return: stdout when success :return: stdout when success
:rtype: str :rtype: str
:raises CoreCommandError: when a non-zero exit status occurs :raises CoreCommandError: when a non-zero exit status occurs
""" """
result = self.server_conn.run(cmd, hide=False) if env is None:
result = self.server.run(cmd, hide=False)
else:
logging.info("command env: %s", env)
result = self.server.run(cmd, hide=False, env=env, replace_env=True)
if result.exited: if result.exited:
raise CoreCommandError( raise CoreCommandError(
result.exited, result.command, result.stdout, result.stderr result.exited, result.command, result.stdout, result.stderr
@ -969,7 +971,7 @@ class CoreNode(CoreNodeBase):
self.client.check_cmd(["sync"]) self.client.check_cmd(["sync"])
else: else:
self.net_cmd(["mkdir", "-p", directory]) self.net_cmd(["mkdir", "-p", directory])
self.server_conn.put(srcname, filename) self.server.put(srcname, filename)
def hostfilename(self, filename): def hostfilename(self, filename):
""" """
@ -992,7 +994,7 @@ class CoreNode(CoreNodeBase):
Create a node file with a given mode. Create a node file with a given mode.
:param str filename: name of file to create :param str filename: name of file to create
:param contents: contents of file :param str contents: contents of file
:param int mode: mode for file :param int mode: mode for file
:return: nothing :return: nothing
""" """
@ -1005,12 +1007,12 @@ class CoreNode(CoreNodeBase):
open_file.write(contents) open_file.write(contents)
os.chmod(open_file.name, mode) os.chmod(open_file.name, mode)
else: else:
temp = NamedTemporaryFile() temp = NamedTemporaryFile(delete=False)
temp.write(contents) temp.write(contents.encode("utf-8"))
temp.close() temp.close()
self.net_cmd(["mkdir", "-m", oct(0o755), "-p", dirname]) self.net_cmd(["mkdir", "-m", "%o" % 0o755, "-p", dirname])
self.server_conn.put(temp.name, hostfilename) self.server.put(temp.name, hostfilename)
self.net_cmd(["chmod", oct(mode), hostfilename]) self.net_cmd(["chmod", "%o" % mode, hostfilename])
logging.debug( logging.debug(
"node(%s) added file: %s; mode: 0%o", self.name, hostfilename, mode "node(%s) added file: %s; mode: 0%o", self.name, hostfilename, mode
) )
@ -1031,9 +1033,9 @@ class CoreNode(CoreNodeBase):
if mode is not None: if mode is not None:
os.chmod(hostfilename, mode) os.chmod(hostfilename, mode)
else: else:
self.server_conn.put(srcfilename, hostfilename) self.server.put(srcfilename, hostfilename)
if mode is not None: if mode is not None:
self.net_cmd(["chmod", oct(mode), hostfilename]) self.net_cmd(["chmod", "%o" % mode, hostfilename])
logging.info( logging.info(
"node(%s) copied file: %s; mode: %s", self.name, hostfilename, mode "node(%s) copied file: %s; mode: %s", self.name, hostfilename, mode
) )

View file

@ -1,4 +1,5 @@
import logging import logging
import pdb
from core.emulator.coreemu import CoreEmu from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import NodeOptions from core.emulator.emudata import NodeOptions
@ -14,7 +15,7 @@ def main():
session = coreemu.create_session() session = coreemu.create_session()
# initialize distributed # initialize distributed
session.servers.add("core2") session.add_distributed("core2")
session.init_distributed() session.init_distributed()
# must be in configuration state for nodes to start, when using "node_add" below # must be in configuration state for nodes to start, when using "node_add" below
@ -25,13 +26,12 @@ def main():
# create nodes # create nodes
options = NodeOptions() options = NodeOptions()
options.emulation_server = "10.10.4.38"
options.emulation_server = "core2" options.emulation_server = "core2"
session.add_node(node_options=options) session.add_node(node_options=options)
# interface = prefixes.create_interface(node_one) # interface = prefixes.create_interface(node_one)
# session.add_link(node_one.id, switch.id, interface_one=interface) # session.add_link(node_one.id, switch.id, interface_one=interface)
# node_two = session.add_node() session.add_node()
# interface = prefixes.create_interface(node_two) # interface = prefixes.create_interface(node_two)
# session.add_link(node_two.id, switch.id, interface_one=interface) # session.add_link(node_two.id, switch.id, interface_one=interface)
@ -46,6 +46,8 @@ def main():
# node_two.client.icmd(["iperf", "-t", "10", "-c", node_one_address]) # node_two.client.icmd(["iperf", "-t", "10", "-c", node_one_address])
# node_one.cmd(["killall", "-9", "iperf"]) # node_one.cmd(["killall", "-9", "iperf"])
pdb.set_trace()
# shutdown session # shutdown session
coreemu.shutdown() coreemu.shutdown()